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
f759697f
authored
Oct 17, 2010
by
Alex Gaynor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for a custom slugify.
parent
dccc744b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
9 deletions
+48
-9
taggit/managers.py
+0
-0
taggit/models.py
+10
-5
taggit/tests/models.py
+26
-1
taggit/tests/tests.py
+12
-3
No files found.
taggit/managers.py
View file @
f759697f
taggit/models.py
View file @
f759697f
...
@@ -2,7 +2,7 @@ import django
...
@@ -2,7 +2,7 @@ import django
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.contenttypes.generic
import
GenericForeignKey
from
django.contrib.contenttypes.generic
import
GenericForeignKey
from
django.db
import
models
,
IntegrityError
,
transaction
from
django.db
import
models
,
IntegrityError
,
transaction
from
django.template.defaultfilters
import
slugify
from
django.template.defaultfilters
import
slugify
as
default_slugify
from
django.utils.translation
import
ugettext_lazy
as
_
,
ugettext
from
django.utils.translation
import
ugettext_lazy
as
_
,
ugettext
...
@@ -18,7 +18,7 @@ class TagBase(models.Model):
...
@@ -18,7 +18,7 @@ class TagBase(models.Model):
def
save
(
self
,
*
args
,
**
kwargs
):
def
save
(
self
,
*
args
,
**
kwargs
):
if
not
self
.
pk
and
not
self
.
slug
:
if
not
self
.
pk
and
not
self
.
slug
:
self
.
slug
=
s
lug
=
slugify
(
self
.
name
)
self
.
slug
=
s
elf
.
slugify
(
self
.
name
)
if
django
.
VERSION
>=
(
1
,
2
):
if
django
.
VERSION
>=
(
1
,
2
):
from
django.db
import
router
from
django.db
import
router
using
=
kwargs
.
get
(
"using"
)
or
router
.
db_for_write
(
using
=
kwargs
.
get
(
"using"
)
or
router
.
db_for_write
(
...
@@ -39,11 +39,17 @@ class TagBase(models.Model):
...
@@ -39,11 +39,17 @@ class TagBase(models.Model):
return
res
return
res
except
IntegrityError
:
except
IntegrityError
:
transaction
.
savepoint_rollback
(
sid
,
**
trans_kwargs
)
transaction
.
savepoint_rollback
(
sid
,
**
trans_kwargs
)
i
+=
1
self
.
slug
=
self
.
slugify
(
self
.
name
,
i
)
self
.
slug
=
"
%
s_
%
d"
%
(
slug
,
i
)
else
:
else
:
return
super
(
TagBase
,
self
)
.
save
(
*
args
,
**
kwargs
)
return
super
(
TagBase
,
self
)
.
save
(
*
args
,
**
kwargs
)
def
slugify
(
self
,
tag
,
i
=
None
):
slug
=
default_slugify
(
tag
)
if
i
is
not
None
:
slug
+=
"_
%
d"
%
i
return
slug
class
Tag
(
TagBase
):
class
Tag
(
TagBase
):
class
Meta
:
class
Meta
:
verbose_name
=
_
(
"Tag"
)
verbose_name
=
_
(
"Tag"
)
...
@@ -137,4 +143,3 @@ class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
...
@@ -137,4 +143,3 @@ class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
class
Meta
:
class
Meta
:
verbose_name
=
_
(
"Tagged Item"
)
verbose_name
=
_
(
"Tagged Item"
)
verbose_name_plural
=
_
(
"Tagged Items"
)
verbose_name_plural
=
_
(
"Tagged Items"
)
taggit/tests/models.py
View file @
f759697f
from
django.db
import
models
from
django.db
import
models
from
taggit.managers
import
TaggableManager
from
taggit.managers
import
TaggableManager
from
taggit.models
import
TaggedItemBase
,
GenericTaggedItemBase
,
TagBase
from
taggit.models
import
(
TaggedItemBase
,
GenericTaggedItemBase
,
TaggedItem
,
TagBase
,
Tag
)
class
Food
(
models
.
Model
):
class
Food
(
models
.
Model
):
...
@@ -115,3 +116,27 @@ class Photo(Media):
...
@@ -115,3 +116,27 @@ class Photo(Media):
class
Movie
(
Media
):
class
Movie
(
Media
):
pass
pass
class
ArticleTag
(
Tag
):
class
Meta
:
proxy
=
True
def
slugify
(
self
,
tag
,
i
=
None
):
slug
=
"category-
%
s"
%
tag
if
i
is
not
None
:
slug
+=
"-
%
d"
%
i
return
slug
class
ArticleTaggedItem
(
TaggedItem
):
class
Meta
:
proxy
=
True
# Basically we want to overide the tag ForeignKey to point at the proxy
# inherited ArticleTag so we can get the right slugify, unfortunately I
# can't seem to figure out how to do this, so we're on hold ATM.
class
Article
(
models
.
Model
):
title
=
models
.
CharField
(
max_length
=
100
)
tags
=
TaggableManager
(
through
=
ArticleTaggedItem
)
taggit/tests/tests.py
View file @
f759697f
...
@@ -12,13 +12,13 @@ from taggit.tests.forms import (FoodForm, DirectFoodForm, CustomPKFoodForm,
...
@@ -12,13 +12,13 @@ from taggit.tests.forms import (FoodForm, DirectFoodForm, CustomPKFoodForm,
from
taggit.tests.models
import
(
Food
,
Pet
,
HousePet
,
DirectFood
,
DirectPet
,
from
taggit.tests.models
import
(
Food
,
Pet
,
HousePet
,
DirectFood
,
DirectPet
,
DirectHousePet
,
TaggedPet
,
CustomPKFood
,
CustomPKPet
,
CustomPKHousePet
,
DirectHousePet
,
TaggedPet
,
CustomPKFood
,
CustomPKPet
,
CustomPKHousePet
,
TaggedCustomPKPet
,
OfficialFood
,
OfficialPet
,
OfficialHousePet
,
TaggedCustomPKPet
,
OfficialFood
,
OfficialPet
,
OfficialHousePet
,
OfficialThroughModel
,
OfficialTag
,
Photo
,
Movie
)
OfficialThroughModel
,
OfficialTag
,
Photo
,
Movie
,
Article
)
from
taggit.utils
import
parse_tags
,
edit_string_for_tags
from
taggit.utils
import
parse_tags
,
edit_string_for_tags
class
BaseTaggingTest
(
object
):
class
BaseTaggingTest
(
object
):
def
assert_tags_equal
(
self
,
qs
,
tags
,
sort
=
True
):
def
assert_tags_equal
(
self
,
qs
,
tags
,
sort
=
True
,
attr
=
"name"
):
got
=
map
(
lambda
tag
:
tag
.
name
,
qs
)
got
=
map
(
lambda
tag
:
getattr
(
tag
,
attr
)
,
qs
)
if
sort
:
if
sort
:
got
.
sort
()
got
.
sort
()
tags
.
sort
()
tags
.
sort
()
...
@@ -61,6 +61,15 @@ class TagModelTestCase(BaseTaggingTransactionTestCase):
...
@@ -61,6 +61,15 @@ class TagModelTestCase(BaseTaggingTransactionTestCase):
yummy
=
self
.
tag_model
.
objects
.
create
(
name
=
"yummy"
)
yummy
=
self
.
tag_model
.
objects
.
create
(
name
=
"yummy"
)
apple
.
tags
.
add
(
yummy
)
apple
.
tags
.
add
(
yummy
)
def
test_slugify
(
self
):
a
=
Article
.
objects
.
create
(
title
=
"django-taggit 1.0 Released"
)
a
.
tags
.
add
(
"awesome"
,
"release"
,
"AWESOME"
)
self
.
assert_tags_equal
(
a
.
tags
.
all
(),
[
"category-awesome"
,
"category-release"
,
"category-awesome-1"
],
attr
=
"slug"
)
class
TagModelDirectTestCase
(
TagModelTestCase
):
class
TagModelDirectTestCase
(
TagModelTestCase
):
food_model
=
DirectFood
food_model
=
DirectFood
tag_model
=
Tag
tag_model
=
Tag
...
...
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