Skip to content

Commit 16b6b2d

Browse files
committed
Merge back of newforms-admin branch with some other minor cleanup for django 1.0 beta 2
git-svn-id: https://django-tagging.googlecode.com/svn/trunk@149 83e7428b-ec2a-0410-86f2-bf466d0e5e72
1 parent 9f74659 commit 16b6b2d

File tree

7 files changed

+41
-56
lines changed

7 files changed

+41
-56
lines changed

docs/overview.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ model::
699699

700700
This field will also validate that it has been given a valid list of
701701
tag names, separated by a single comma, a single space or a comma
702-
followed by a space, using the ``isTagList`` validator from
702+
followed by a space, using the ``is_tag_list`` validator from
703703
``tagging.validators``.
704704

705705

tagging/admin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.contrib import admin
2+
from tagging.models import Tag, TaggedItem
3+
4+
admin.site.register(TaggedItem)
5+
admin.site.register(Tag)

tagging/fields.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from tagging import settings
99
from tagging.models import Tag
1010
from tagging.utils import edit_string_for_tags
11-
from tagging.validators import isTagList
1211

1312
class TagField(CharField):
1413
"""
@@ -19,7 +18,6 @@ class TagField(CharField):
1918
def __init__(self, *args, **kwargs):
2019
kwargs['max_length'] = kwargs.get('max_length', 255)
2120
kwargs['blank'] = kwargs.get('blank', True)
22-
kwargs['validator_list'] = [isTagList] + kwargs.get('validator_list', [])
2321
super(TagField, self).__init__(*args, **kwargs)
2422

2523
def contribute_to_class(self, cls, name):

tagging/forms.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55
from django.utils.translation import ugettext as _
66

77
from tagging import settings
8+
from tagging.models import Tag
9+
from tagging.validators import is_tag, is_tag_list
810
from tagging.utils import parse_tag_input
911

12+
class AdminTagForm(forms.ModelForm):
13+
class Meta:
14+
model = Tag
15+
16+
def clean_name(self):
17+
value = self.cleaned_data["name"]
18+
return is_tag(value)
19+
20+
1021
class TagField(forms.CharField):
1122
"""
1223
A ``CharField`` which validates that its input is a valid list of
@@ -16,8 +27,4 @@ def clean(self, value):
1627
value = super(TagField, self).clean(value)
1728
if value == u'':
1829
return value
19-
for tag_name in parse_tag_input(value):
20-
if len(tag_name) > settings.MAX_TAG_LENGTH:
21-
raise forms.ValidationError(
22-
_('Each tag may be no more than %s characters long.') % settings.MAX_TAG_LENGTH)
23-
return value
30+
return is_tag_list(value)

tagging/models.py

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@
1616
from tagging import settings
1717
from tagging.utils import calculate_cloud, get_tag_list, get_queryset_and_model, parse_tag_input
1818
from tagging.utils import LOGARITHMIC
19-
from tagging.validators import isTag
2019

2120
qn = connection.ops.quote_name
2221

23-
try:
24-
from django.db.models.query import parse_lookup
25-
except ImportError:
26-
parse_lookup = None
27-
2822
############
2923
# Managers #
3024
############
@@ -147,23 +141,10 @@ def usage_for_model(self, model, counts=False, min_count=None, filters=None):
147141
"""
148142
if filters is None: filters = {}
149143

150-
if not parse_lookup:
151-
# post-queryset-refactor (hand off to usage_for_queryset)
152-
queryset = model._default_manager.filter()
153-
for f in filters.items():
154-
queryset.query.add_filter(f)
155-
usage = self.usage_for_queryset(queryset, counts, min_count)
156-
else:
157-
# pre-queryset-refactor
158-
extra_joins = ''
159-
extra_criteria = ''
160-
params = []
161-
if len(filters) > 0:
162-
joins, where, params = parse_lookup(filters.items(), model._meta)
163-
extra_joins = ' '.join(['%s %s AS %s ON %s' % (join_type, table, alias, condition)
164-
for (alias, (table, join_type, condition)) in joins.items()])
165-
extra_criteria = 'AND %s' % (' AND '.join(where))
166-
usage = self._get_usage(model, counts, min_count, extra_joins, extra_criteria, params)
144+
queryset = model._default_manager.filter()
145+
for f in filters.items():
146+
queryset.query.add_filter(f)
147+
usage = self.usage_for_queryset(queryset, counts, min_count)
167148

168149
return usage
169150

@@ -180,8 +161,6 @@ def usage_for_queryset(self, queryset, counts=False, min_count=None):
180161
greater than or equal to ``min_count`` will be returned.
181162
Passing a value for ``min_count`` implies ``counts=True``.
182163
"""
183-
if parse_lookup:
184-
raise AttributeError("'TagManager.usage_for_queryset' is not compatible with pre-queryset-refactor versions of Django.")
185164

186165
extra_joins = ' '.join(queryset.query.get_from_clause()[0][1:])
187166
where, params = queryset.query.where.as_sql()
@@ -288,7 +267,7 @@ class TaggedItemManager(models.Manager):
288267
objects we're interested in, then use the ORM's ``__in``
289268
lookup to return a ``QuerySet``.
290269
291-
Once the queryset-refactor branch lands in trunk, this can be
270+
now that the queryset-refactor branch is in the trunk, this can be
292271
tidied up significantly.
293272
"""
294273
def get_by_model(self, queryset_or_model, tags):
@@ -463,7 +442,7 @@ class Tag(models.Model):
463442
"""
464443
A tag.
465444
"""
466-
name = models.CharField(_('name'), max_length=50, unique=True, db_index=True, validator_list=[isTag])
445+
name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
467446

468447
objects = TagManager()
469448

@@ -472,9 +451,6 @@ class Meta:
472451
verbose_name = _('tag')
473452
verbose_name_plural = _('tags')
474453

475-
class Admin:
476-
pass
477-
478454
def __unicode__(self):
479455
return self.name
480456

@@ -495,8 +471,5 @@ class Meta:
495471
verbose_name = _('tagged item')
496472
verbose_name_plural = _('tagged items')
497473

498-
class Admin:
499-
pass
500-
501474
def __unicode__(self):
502475
return u'%s [%s]' % (self.object, self.tag)

tagging/tests/tests.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
>>> from tagging.tests.models import Article, Link, Perch, Parrot, FormTest
99
>>> from tagging.utils import calculate_cloud, get_tag_list, get_tag, parse_tag_input
1010
>>> from tagging.utils import LINEAR
11-
>>> from tagging.validators import isTagList, isTag
11+
>>> from tagging.validators import is_tag_list, is_tag
1212
1313
#############
1414
# Utilities #
@@ -154,18 +154,18 @@
154154
155155
# Validators ##################################################################
156156
157-
>>> isTagList('foo qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbn bar', {})
157+
>>> is_tag_list('foo qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbn bar', {})
158158
Traceback (most recent call last):
159159
...
160160
ValidationError: [u'Each tag may be no more than 50 characters long.']
161161
162-
>>> isTag('"test"', {})
163-
>>> isTag(',test', {})
164-
>>> isTag('f o o', {})
162+
>>> is_tag('"test"', {})
163+
>>> is_tag(',test', {})
164+
>>> is_tag('f o o', {})
165165
Traceback (most recent call last):
166166
...
167167
ValidationError: [u'Multiple tags were given.']
168-
>>> isTagList('foo qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbn bar', {})
168+
>>> is_tag_list('foo qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbn bar', {})
169169
Traceback (most recent call last):
170170
...
171171
ValidationError: [u'Each tag may be no more than 50 characters long.']

tagging/validators.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@
33
required for basic ``django.contrib.admin`` application field validation
44
until the ``newforms-admin`` branch lands in trunk.
55
"""
6-
from django.core.validators import ValidationError
6+
from django import forms
77
from django.utils.translation import ugettext as _
88

99
from tagging import settings
1010
from tagging.utils import parse_tag_input
1111

12-
def isTagList(field_data, all_data):
12+
def is_tag_list(value):
1313
"""
14-
Validates that ``field_data`` is a valid list of tags.
14+
Validates that ``value`` is a valid list of tags.
1515
"""
16-
for tag_name in parse_tag_input(field_data):
16+
for tag_name in parse_tag_input(value):
1717
if len(tag_name) > settings.MAX_TAG_LENGTH:
18-
raise ValidationError(
18+
raise forms.ValidationError(
1919
_('Each tag may be no more than %s characters long.') % settings.MAX_TAG_LENGTH)
20+
return value
2021

21-
def isTag(field_data, all_data):
22+
def is_tag(value):
2223
"""
23-
Validates that ``field_data`` is a valid tag.
24+
Validates that ``value`` is a valid tag.
2425
"""
25-
tag_names = parse_tag_input(field_data)
26+
tag_names = parse_tag_input(value)
2627
if len(tag_names) > 1:
2728
raise ValidationError(_('Multiple tags were given.'))
2829
elif len(tag_names[0]) > settings.MAX_TAG_LENGTH:
29-
raise ValidationError(
30+
raise forms.ValidationError(
3031
_('A tag may be no more than %s characters long.') % settings.MAX_TAG_LENGTH)
32+
return value

0 commit comments

Comments
 (0)