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
cd097f7f
authored
Dec 08, 2009
by
Alex Gaynor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Forms are better
parent
daaf93d3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
20 additions
and
20 deletions
+20
-20
taggit/forms.py
+6
-13
taggit/managers.py
+11
-2
taggit/tests/forms.py
+2
-2
taggit/tests/tests.py
+1
-3
No files found.
taggit/forms.py
View file @
cd097f7f
...
...
@@ -3,16 +3,9 @@ from django import forms
from
taggit.utils
import
parse_tags
class
TaggableForm
(
forms
.
ModelForm
):
tags
=
forms
.
CharField
(
help_text
=
"A comma seperated list of tags."
)
def
save
(
self
,
commit
=
True
):
obj
=
super
(
TaggableForm
,
self
)
.
save
(
commit
=
commit
)
def
save_tags
():
# TODO: Remove the assumption that the manager is named 'tags'
obj
.
tags
.
set
(
*
parse_tags
(
self
.
cleaned_data
[
'tags'
]))
if
commit
:
save_tags
()
else
:
obj
.
save_tags
=
save_tags
return
obj
class
TagField
(
forms
.
CharField
):
def
clean
(
self
,
value
):
try
:
return
parse_tags
(
value
)
except
ValueError
:
raise
forms
.
ValidationError
(
"Please provide a comma seperate list of tags."
)
taggit/managers.py
View file @
cd097f7f
...
...
@@ -3,6 +3,7 @@ from django.db import models
from
django.db.models.fields.related
import
ManyToManyRel
from
django.db.models.query_utils
import
QueryWrapper
from
taggit.forms
import
TagField
from
taggit.models
import
Tag
,
TaggedItem
from
taggit.utils
import
require_instance_manager
...
...
@@ -20,7 +21,7 @@ class TaggableRel(ManyToManyRel):
class
TaggableManager
(
object
):
def
__init__
(
self
):
self
.
rel
=
TaggableRel
()
self
.
editable
=
Fals
e
self
.
editable
=
Tru
e
self
.
unique
=
False
self
.
creates_table
=
False
self
.
db_column
=
None
...
...
@@ -41,7 +42,7 @@ class TaggableManager(object):
setattr
(
cls
,
name
,
self
)
def
save_form_data
(
self
,
instance
,
value
):
pass
getattr
(
instance
,
self
.
name
)
.
set
(
*
value
)
def
get_db_prep_lookup
(
self
,
lookup_type
,
value
):
if
lookup_type
not
in
(
"in"
,
"exact"
):
...
...
@@ -62,6 +63,14 @@ class TaggableManager(object):
sql
,
params
=
qs
.
values_list
(
"pk"
,
flat
=
True
)
.
query
.
as_sql
()
return
QueryWrapper
((
"(
%
s)"
%
sql
),
params
)
def
formfield
(
self
,
form_class
=
TagField
,
**
kwargs
):
defaults
=
{
"label"
:
"Tags"
,
"help_text"
:
"A comma seperated list of tags."
}
defaults
.
update
(
kwargs
)
return
form_class
(
**
kwargs
)
def
related_query_name
(
self
):
return
None
...
...
taggit/tests/forms.py
View file @
cd097f7f
from
taggit.forms
import
TaggableForm
from
django
import
forms
from
taggit.tests.models
import
Food
class
FoodForm
(
Taggable
Form
):
class
FoodForm
(
forms
.
Model
Form
):
class
Meta
:
model
=
Food
taggit/tests/tests.py
View file @
cd097f7f
...
...
@@ -7,8 +7,7 @@ from taggit.tests.models import Food, Pet
class
BaseTaggingTest
(
TestCase
):
def
assert_tags_equal
(
self
,
qs
,
tags
):
tags
=
Tag
.
objects
.
filter
(
name__in
=
tags
)
self
.
assertEqual
(
list
(
qs
),
list
(
tags
))
self
.
assertEqual
(
map
(
lambda
tag
:
tag
.
name
,
qs
),
list
(
tags
))
class
AddTagTestCase
(
BaseTaggingTest
):
...
...
@@ -65,6 +64,5 @@ class TaggableFormTestCase(BaseTaggingTest):
f
=
FoodForm
({
'name'
:
'apple'
,
'tags'
:
'green, red, yummy'
})
f
.
save
()
apple
=
Food
.
objects
.
get
(
name
=
'apple'
)
self
.
assert_tags_equal
(
apple
.
tags
.
all
(),
[
'green'
,
'red'
,
'yummy'
])
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