Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
django-taggit
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2b00049c
authored
Mar 21, 2010
by
Alex Gaynor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed #17. On django 1.2 support deleting objects with Tags. Also a lot of other fixers.
parent
c5659072
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
7 deletions
+36
-7
taggit/forms.py
+8
-0
taggit/managers.py
+9
-5
taggit/tests/__init__.py
+3
-1
taggit/tests/tests.py
+16
-1
No files found.
taggit/forms.py
View file @
2b00049c
...
...
@@ -3,7 +3,15 @@ from django import forms
from
taggit.utils
import
parse_tags
class
TagWidget
(
forms
.
TextInput
):
def
render
(
self
,
name
,
value
,
attrs
=
None
):
if
not
isinstance
(
value
,
basestring
):
value
=
", "
.
join
(
o
.
tag
.
name
for
o
in
value
.
select_related
(
"tag"
))
return
super
(
TagWidget
,
self
)
.
render
(
name
,
value
,
attrs
)
class
TagField
(
forms
.
CharField
):
widget
=
TagWidget
def
clean
(
self
,
value
):
try
:
return
parse_tags
(
value
)
...
...
taggit/managers.py
View file @
2b00049c
...
...
@@ -70,8 +70,9 @@ class TaggableManager(object):
else
:
# Fucking flip-floppers.
raise
ValueError
(
"You can't combine Tag objects and strings. '
%
s' was provided."
%
value
)
sql
,
params
=
qs
.
values_list
(
"pk"
,
flat
=
True
)
.
query
.
as_sql
()
return
QueryWrapper
((
"(
%
s)"
%
sql
),
params
)
if
hasattr
(
models
.
Field
,
"get_prep_lookup"
):
return
models
.
Field
()
.
get_prep_lookup
(
lookup_type
,
qs
)
return
models
.
Field
()
.
get_db_prep_lookup
(
lookup_type
,
qs
)
if
django
.
VERSION
<
(
1
,
2
):
get_db_prep_lookup
=
get_prep_lookup
...
...
@@ -79,7 +80,7 @@ class TaggableManager(object):
def
get_db_prep_lookup
(
self
,
lookup_type
,
value
,
connection
,
prepared
=
False
):
if
not
prepared
:
return
self
.
get_prep_lookup
(
lookup_type
,
value
)
return
value
return
models
.
Field
()
.
get_db_prep_lookup
(
lookup_type
,
value
,
connection
=
connection
,
prepared
=
True
)
def
formfield
(
self
,
form_class
=
TagField
,
**
kwargs
):
defaults
=
{
...
...
@@ -91,8 +92,11 @@ class TaggableManager(object):
def
value_from_object
(
self
,
instance
):
if
instance
.
pk
:
return
", "
.
join
(
map
(
unicode
,
getattr
(
instance
,
self
.
name
)
.
all
()))
return
""
return
TaggedItem
.
objects
.
filter
(
object_id
=
instance
.
pk
,
content_type
=
ContentType
.
objects
.
get_for_model
(
instance
)
)
return
TaggedItem
.
objects
.
none
()
def
related_query_name
(
self
):
return
None
...
...
taggit/tests/__init__.py
View file @
2b00049c
from
taggit.tests.tests
import
AddTagTestCase
,
LookupByTagTestCase
,
TaggableFormTestCase
,
SimilarityByTagTestCase
,
TagReuseTestCase
from
taggit.tests.tests
import
(
AddTagTestCase
,
DeleteObjecTestCase
,
LookupByTagTestCase
,
TaggableFormTestCase
,
SimilarityByTagTestCase
,
TagReuseTestCase
)
taggit/tests/tests.py
View file @
2b00049c
...
...
@@ -67,13 +67,24 @@ class AddTagTestCase(BaseTaggingTest):
apple
.
tags
.
add
(
"Red"
,
"red"
)
class
DeleteObjecTestCase
(
BaseTaggingTest
):
def
test_delete_obj
(
self
):
apple
=
Food
.
objects
.
create
(
name
=
"apple"
)
apple
.
tags
.
add
(
"red"
)
self
.
assert_tags_equal
(
apple
.
tags
.
all
(),
[
"red"
])
strawberry
=
Food
.
objects
.
create
(
name
=
"strawberry"
)
strawberry
.
tags
.
add
(
"red"
)
apple
.
delete
()
self
.
assert_tags_equal
(
strawberry
.
tags
.
all
(),
[
"red"
])
class
LookupByTagTestCase
(
BaseTaggingTest
):
def
test_lookup_by_tag
(
self
):
apple
=
Food
.
objects
.
create
(
name
=
"apple"
)
apple
.
tags
.
add
(
"red"
,
"green"
)
pear
=
Food
.
objects
.
create
(
name
=
"pear"
)
pear
.
tags
.
add
(
"green"
)
self
.
assertEqual
(
list
(
Food
.
objects
.
filter
(
tags__in
=
[
"red"
])),
[
apple
])
self
.
assertEqual
(
list
(
Food
.
objects
.
filter
(
tags__in
=
[
"green"
])),
[
apple
,
pear
])
...
...
@@ -100,6 +111,7 @@ class TaggableFormTestCase(BaseTaggingTest):
self
.
assertEqual
(
FoodForm
.
base_fields
.
keys
(),
[
'name'
,
'tags'
])
f
=
FoodForm
({
'name'
:
'apple'
,
'tags'
:
'green, red, yummy'
})
self
.
assertEqual
(
str
(
f
),
"""<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" value="apple" maxlength="50" /></td></tr>
\n
<tr><th><label for="id_tags">Tags:</label></th><td><input type="text" name="tags" value="green, red, yummy" id="id_tags" /></td></tr>"""
)
f
.
save
()
apple
=
Food
.
objects
.
get
(
name
=
'apple'
)
self
.
assert_tags_equal
(
apple
.
tags
.
all
(),
[
'green'
,
'red'
,
'yummy'
])
...
...
@@ -113,6 +125,9 @@ class TaggableFormTestCase(BaseTaggingTest):
f
=
FoodForm
({
"name"
:
"raspberry"
})
raspberry
=
f
.
save
()
self
.
assert_tags_equal
(
raspberry
.
tags
.
all
(),
[])
f
=
FoodForm
(
instance
=
apple
)
self
.
assertEqual
(
str
(
f
),
"""<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" value="apple" maxlength="50" /></td></tr>
\n
<tr><th><label for="id_tags">Tags:</label></th><td><input type="text" name="tags" value="green, red, yummy, delicious" id="id_tags" /></td></tr>"""
)
class
SimilarityByTagTestCase
(
BaseTaggingTest
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment