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
9b0c9d46
authored
Aug 10, 2014
by
Daniel Hahler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
flake8 fixes
parent
0591e1fa
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
47 additions
and
39 deletions
+47
-39
taggit/forms.py
+5
-2
taggit/managers.py
+21
-15
taggit/models.py
+2
-2
taggit/views.py
+0
-1
tests/forms.py
+1
-1
tests/tests.py
+18
-18
No files found.
taggit/forms.py
View file @
9b0c9d46
...
...
@@ -10,9 +10,11 @@ from taggit.utils import parse_tags, edit_string_for_tags
class
TagWidget
(
forms
.
TextInput
):
def
render
(
self
,
name
,
value
,
attrs
=
None
):
if
value
is
not
None
and
not
isinstance
(
value
,
six
.
string_types
):
value
=
edit_string_for_tags
([
o
.
tag
for
o
in
value
.
select_related
(
"tag"
)])
value
=
edit_string_for_tags
([
o
.
tag
for
o
in
value
.
select_related
(
"tag"
)])
return
super
(
TagWidget
,
self
)
.
render
(
name
,
value
,
attrs
)
class
TagField
(
forms
.
CharField
):
widget
=
TagWidget
...
...
@@ -21,4 +23,5 @@ class TagField(forms.CharField):
try
:
return
parse_tags
(
value
)
except
ValueError
:
raise
forms
.
ValidationError
(
_
(
"Please provide a comma-separated list of tags."
))
raise
forms
.
ValidationError
(
_
(
"Please provide a comma-separated list of tags."
))
taggit/managers.py
View file @
9b0c9d46
...
...
@@ -102,7 +102,7 @@ class _TaggableManager(models.Manager):
else
'content_object'
)
fk
=
self
.
through
.
_meta
.
get_field
(
fieldname
)
query
=
{
'
%
s__
%
s__in'
%
(
self
.
through
.
tag_relname
(),
fk
.
name
)
:
'
%
s__
%
s__in'
%
(
self
.
through
.
tag_relname
(),
fk
.
name
):
set
(
obj
.
_get_pk_val
()
for
obj
in
instances
)
}
join_table
=
self
.
through
.
_meta
.
db_table
...
...
@@ -110,8 +110,8 @@ class _TaggableManager(models.Manager):
connection
=
connections
[
db
]
qn
=
connection
.
ops
.
quote_name
qs
=
self
.
get_queryset
()
.
using
(
db
)
.
_next_is_sticky
()
.
filter
(
**
query
)
.
extra
(
select
=
{
'_prefetch_related_val'
:
'
%
s.
%
s'
%
(
qn
(
join_table
),
qn
(
source_col
))
select
=
{
'_prefetch_related_val'
:
'
%
s.
%
s'
%
(
qn
(
join_table
),
qn
(
source_col
))
}
)
return
(
qs
,
...
...
@@ -220,10 +220,12 @@ class _TaggableManager(models.Manager):
class
TaggableManager
(
RelatedField
,
Field
):
_related_name_counter
=
0
def
__init__
(
self
,
verbose_name
=
_
(
"Tags"
),
help_text
=
_
(
"A comma-separated list of tags."
),
through
=
None
,
blank
=
False
,
related_name
=
None
,
to
=
None
,
manager
=
_TaggableManager
):
Field
.
__init__
(
self
,
verbose_name
=
verbose_name
,
help_text
=
help_text
,
blank
=
blank
,
null
=
True
,
serialize
=
False
)
def
__init__
(
self
,
verbose_name
=
_
(
"Tags"
),
help_text
=
_
(
"A comma-separated list of tags."
),
through
=
None
,
blank
=
False
,
related_name
=
None
,
to
=
None
,
manager
=
_TaggableManager
):
Field
.
__init__
(
self
,
verbose_name
=
verbose_name
,
help_text
=
help_text
,
blank
=
blank
,
null
=
True
,
serialize
=
False
)
self
.
through
=
through
or
TaggedItem
self
.
rel
=
TaggableRel
(
self
,
related_name
,
self
.
through
,
to
=
to
)
self
.
swappable
=
False
...
...
@@ -233,12 +235,12 @@ class TaggableManager(RelatedField, Field):
def
__get__
(
self
,
instance
,
model
):
if
instance
is
not
None
and
instance
.
pk
is
None
:
raise
ValueError
(
"
%
s objects need to have a primary key value "
"before you can access their tags."
%
model
.
__name__
)
"before you can access their tags."
%
model
.
__name__
)
manager
=
self
.
manager
(
through
=
self
.
through
,
model
=
model
,
instance
=
instance
,
prefetch_cache_name
=
self
.
name
prefetch_cache_name
=
self
.
name
)
return
manager
...
...
@@ -287,7 +289,6 @@ class TaggableManager(RelatedField, Field):
else
:
self
.
post_through_setup
(
cls
)
def
__lt__
(
self
,
other
):
"""
Required contribute_to_class as Django uses bisect
...
...
@@ -364,7 +365,7 @@ class TaggableManager(RelatedField, Field):
def
extra_filters
(
self
,
pieces
,
pos
,
negate
):
if
negate
or
not
self
.
use_gfk
:
return
[]
prefix
=
"__"
.
join
([
"tagged_items"
]
+
pieces
[:
pos
-
2
])
prefix
=
"__"
.
join
([
"tagged_items"
]
+
pieces
[:
pos
-
2
])
get
=
ContentType
.
objects
.
get_for_model
cts
=
[
get
(
obj
)
for
obj
in
_get_subclasses
(
self
.
model
)]
if
len
(
cts
)
==
1
:
...
...
@@ -378,13 +379,18 @@ class TaggableManager(RelatedField, Field):
else
:
alias_to_join
=
lhs_alias
extra_col
=
self
.
through
.
_meta
.
get_field_by_name
(
'content_type'
)[
0
]
.
column
content_type_ids
=
[
ContentType
.
objects
.
get_for_model
(
subclass
)
.
pk
for
subclass
in
_get_subclasses
(
self
.
model
)]
content_type_ids
=
[
ContentType
.
objects
.
get_for_model
(
subclass
)
.
pk
for
subclass
in
_get_subclasses
(
self
.
model
)]
if
len
(
content_type_ids
)
==
1
:
content_type_id
=
content_type_ids
[
0
]
extra_where
=
" AND
%
s.
%
s =
%%
s"
%
(
qn
(
alias_to_join
),
qn
(
extra_col
))
extra_where
=
" AND
%
s.
%
s =
%%
s"
%
(
qn
(
alias_to_join
),
qn
(
extra_col
))
params
=
[
content_type_id
]
else
:
extra_where
=
" AND
%
s.
%
s IN (
%
s)"
%
(
qn
(
alias_to_join
),
qn
(
extra_col
),
','
.
join
([
'
%
s'
]
*
len
(
content_type_ids
)))
extra_where
=
" AND
%
s.
%
s IN (
%
s)"
%
(
qn
(
alias_to_join
),
qn
(
extra_col
),
','
.
join
([
'
%
s'
]
*
len
(
content_type_ids
)))
params
=
content_type_ids
return
extra_where
,
params
...
...
@@ -461,7 +467,7 @@ def _get_subclasses(model):
for
f
in
model
.
_meta
.
get_all_field_names
():
field
=
model
.
_meta
.
get_field_by_name
(
f
)[
0
]
if
(
isinstance
(
field
,
RelatedObject
)
and
getattr
(
field
.
field
.
rel
,
"parent_link"
,
None
)):
getattr
(
field
.
field
.
rel
,
"parent_link"
,
None
)):
subclasses
.
extend
(
_get_subclasses
(
field
.
model
))
return
subclasses
...
...
taggit/models.py
View file @
9b0c9d46
...
...
@@ -59,7 +59,7 @@ class TagBase(models.Model):
except
IntegrityError
:
pass
# Now try to find existing slugs with similar names
slugs
=
set
(
Tag
.
objects
.
filter
(
slug__startswith
=
self
.
slug
)
\
slugs
=
set
(
Tag
.
objects
.
filter
(
slug__startswith
=
self
.
slug
)
.
values_list
(
'slug'
,
flat
=
True
))
i
=
1
while
True
:
...
...
@@ -145,7 +145,7 @@ class GenericTaggedItemBase(ItemBase):
content_object
=
GenericForeignKey
()
class
Meta
:
abstract
=
True
abstract
=
True
@classmethod
def
lookup_kwargs
(
cls
,
instance
):
...
...
taggit/views.py
View file @
9b0c9d46
...
...
@@ -18,4 +18,3 @@ def tagged_object_list(request, slug, queryset, **kwargs):
kwargs
[
"extra_context"
]
=
{}
kwargs
[
"extra_context"
][
"tag"
]
=
tag
return
ListView
.
as_view
(
request
,
qs
,
**
kwargs
)
tests/forms.py
View file @
9b0c9d46
...
...
@@ -6,7 +6,7 @@ from .models import Food, DirectFood, CustomPKFood, OfficialFood
fields
=
None
if
VERSION
>=
(
1
,
6
):
if
VERSION
>=
(
1
,
6
):
fields
=
'__all__'
...
...
tests/tests.py
View file @
9b0c9d46
...
...
@@ -109,7 +109,7 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
def
test_add_tag
(
self
):
apple
=
self
.
food_model
.
objects
.
create
(
name
=
"apple"
)
self
.
assertEqual
(
list
(
apple
.
tags
.
all
()),
[])
self
.
assertEqual
(
list
(
self
.
food_model
.
tags
.
all
()),
[])
self
.
assertEqual
(
list
(
self
.
food_model
.
tags
.
all
()),
[])
apple
.
tags
.
add
(
'green'
)
self
.
assert_tags_equal
(
apple
.
tags
.
all
(),
[
'green'
])
...
...
@@ -150,7 +150,7 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
# make sure we don't double create.
# + 12 on Django 1.6 for save points.
queries
=
22
if
django
.
VERSION
<
(
1
,
6
):
if
django
.
VERSION
<
(
1
,
6
):
queries
-=
12
self
.
assertNumQueries
(
queries
,
apple
.
tags
.
add
,
"red"
,
"delicious"
,
"green"
)
...
...
@@ -160,7 +160,7 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
# make sure we dont't double create.
# + 4 on Django 1.6 for save points.
queries
=
9
if
django
.
VERSION
<
(
1
,
6
):
if
django
.
VERSION
<
(
1
,
6
):
queries
-=
4
self
.
assertNumQueries
(
queries
,
pear
.
tags
.
add
,
"green"
,
"delicious"
)
...
...
@@ -181,7 +181,7 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
def
test_delete_bulk
(
self
):
apple
=
self
.
food_model
.
objects
.
create
(
name
=
"apple"
)
kitty
=
self
.
pet_model
.
objects
.
create
(
pk
=
apple
.
pk
,
name
=
"kitty"
)
kitty
=
self
.
pet_model
.
objects
.
create
(
pk
=
apple
.
pk
,
name
=
"kitty"
)
apple
.
tags
.
add
(
"red"
,
"delicious"
,
"fruit"
)
kitty
.
tags
.
add
(
"feline"
)
...
...
@@ -222,9 +222,9 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
pks
=
self
.
pet_model
.
objects
.
filter
(
tags__name__in
=
[
"fuzzy"
])
model_name
=
self
.
pet_model
.
__name__
self
.
assertQuerysetEqual
(
pks
,
[
'<{0}: kitty>'
.
format
(
model_name
),
'<{0}: cat>'
.
format
(
model_name
)],
ordered
=
False
)
[
'<{0}: kitty>'
.
format
(
model_name
),
'<{0}: cat>'
.
format
(
model_name
)],
ordered
=
False
)
def
test_lookup_bulk
(
self
):
apple
=
self
.
food_model
.
objects
.
create
(
name
=
"apple"
)
...
...
@@ -254,14 +254,14 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
pear
=
self
.
food_model
.
objects
.
create
(
name
=
"pear"
)
pear
.
tags
.
add
(
"green"
,
"delicious"
)
guava
=
self
.
food_model
.
objects
.
create
(
name
=
"guava"
)
self
.
food_model
.
objects
.
create
(
name
=
"guava"
)
pks
=
self
.
food_model
.
objects
.
exclude
(
tags__name__in
=
[
"red"
])
model_name
=
self
.
food_model
.
__name__
self
.
assertQuerysetEqual
(
pks
,
[
'<{0}: pear>'
.
format
(
model_name
),
'<{0}: guava>'
.
format
(
model_name
)],
ordered
=
False
)
[
'<{0}: pear>'
.
format
(
model_name
),
'<{0}: guava>'
.
format
(
model_name
)],
ordered
=
False
)
def
test_similarity_by_tag
(
self
):
"""Test that pears are more similar to apples than watermelons"""
...
...
@@ -293,8 +293,8 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
'
%
s__name'
%
_model_name
(
self
.
pet_model
):
'Spot'
}
self
.
assert_tags_equal
(
self
.
tag_model
.
objects
.
filter
(
**
lookup_kwargs
),
[
'scary'
]
self
.
tag_model
.
objects
.
filter
(
**
lookup_kwargs
),
[
'scary'
]
)
def
test_taggeditem_unicode
(
self
):
...
...
@@ -492,7 +492,7 @@ class TagStringParseTestCase(UnitTestCase):
self
.
assertEqual
(
parse_tags
(
',one two'
),
[
'one two'
])
self
.
assertEqual
(
parse_tags
(
',one two three'
),
[
'one two three'
])
self
.
assertEqual
(
parse_tags
(
'a-one, a-two and a-three'
),
[
'a-one'
,
'a-two and a-three'
])
[
'a-one'
,
'a-two and a-three'
])
def
test_with_double_quoted_multiple_words
(
self
):
"""
...
...
@@ -504,7 +504,7 @@ class TagStringParseTestCase(UnitTestCase):
self
.
assertEqual
(
parse_tags
(
'"one two three'
),
[
'one'
,
'three'
,
'two'
])
self
.
assertEqual
(
parse_tags
(
'"one two"'
),
[
'one two'
])
self
.
assertEqual
(
parse_tags
(
'a-one "a-two and a-three"'
),
[
'a-one'
,
'a-two and a-three'
])
[
'a-one'
,
'a-two and a-three'
])
def
test_with_no_loose_commas
(
self
):
"""
...
...
@@ -523,9 +523,9 @@ class TagStringParseTestCase(UnitTestCase):
Double quotes can contain commas
"""
self
.
assertEqual
(
parse_tags
(
'a-one "a-two, and a-three"'
),
[
'a-one'
,
'a-two, and a-three'
])
[
'a-one'
,
'a-two, and a-three'
])
self
.
assertEqual
(
parse_tags
(
'"two", one, one, two, "one"'
),
[
'one'
,
'two'
])
[
'one'
,
'two'
])
def
test_with_naughty_input
(
self
):
"""
...
...
@@ -540,7 +540,7 @@ class TagStringParseTestCase(UnitTestCase):
self
.
assertEqual
(
parse_tags
(
',,,,,,'
),
[])
self
.
assertEqual
(
parse_tags
(
'",",",",",",","'
),
[
','
])
self
.
assertEqual
(
parse_tags
(
'a-one "a-two" and "a-three'
),
[
'a-one'
,
'a-three'
,
'a-two'
,
'and'
])
[
'a-one'
,
'a-three'
,
'a-two'
,
'and'
])
def
test_recreation_of_tag_list_string_representations
(
self
):
plain
=
Tag
.
objects
.
create
(
name
=
'plain'
)
...
...
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