Skip to content
This repository

Autocomplete widget for admin UI #48

Closed
wants to merge 14 commits into from
This page is out of date. Refresh to see the latest.
3  taggit/admin.py
@@ -10,6 +10,9 @@ class TagAdmin(admin.ModelAdmin):
10 10 inlines = [
11 11 TaggedItemInline
12 12 ]
  13 + list_display = ('name', 'viewed', 'date_created', 'date_changed')
  14 + ordering = ('name',)
  15 + search_fields = ('name',)
13 16
14 17
15 18 admin.site.register(Tag, TagAdmin)
30 taggit/forms.py
... ... @@ -1,14 +1,36 @@
1 1 from django import forms
  2 +from django.utils.simplejson import dumps
  3 +from django.utils.safestring import mark_safe
2 4 from django.utils.translation import ugettext as _
3 5
4   -from taggit.utils import parse_tags, edit_string_for_tags
5   -
  6 +from taggit.models import Tag
  7 +from taggit.utils import parse_tags, edit_string_for_tags, LazyEncoder
6 8
7 9 class TagWidget(forms.TextInput):
8 10 def render(self, name, value, attrs=None):
9 11 if value is not None and not isinstance(value, basestring):
10 12 value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
11   - return super(TagWidget, self).render(name, value, attrs)
  13 + output = super(TagWidget, self).render(name, value, attrs)
  14 + tags = dumps([tag.name for tag in Tag.objects.all().order_by('name')], cls=LazyEncoder)
  15 +
  16 + return output + mark_safe(u'''<script type="text/javascript">
  17 + jQuery(document).ready(function() {
  18 + tagsExisting = %s;
  19 + jQuery("#id_%s").autocomplete(tagsExisting, {
  20 + width: 150,
  21 + max: 10,
  22 + min: 3,
  23 + highlight: false,
  24 + multiple: true,
  25 + multipleSeparator: ", ",
  26 + scroll: true,
  27 + scrollHeight: 300,
  28 + //matchContains: true,
  29 + autoFill: true,
  30 + selectFirst: false
  31 + });
  32 + });
  33 + </script>''' % (tags, name))
12 34
13 35 class TagField(forms.CharField):
14 36 widget = TagWidget
@@ -18,4 +40,4 @@ def clean(self, value):
18 40 try:
19 41 return parse_tags(value)
20 42 except ValueError:
21   - raise forms.ValidationError(_("Please provide a comma-separated list of tags."))
  43 + raise forms.ValidationError(_("Please provide a comma-separated list of tags."))
12 taggit/models.py
@@ -7,8 +7,13 @@
7 7
8 8
9 9 class TagBase(models.Model):
10   - name = models.CharField(verbose_name=_('Name'), max_length=100)
  10 + name = models.CharField(verbose_name=_('Name'), unique=True, max_length=100)
11 11 slug = models.SlugField(verbose_name=_('Slug'), unique=True, max_length=100)
  12 +
  13 + viewed = models.IntegerField(editable=False, blank=True, default=0, db_index=True)
  14 +
  15 + date_created = models.DateTimeField(auto_now_add=True)
  16 + date_changed = models.DateTimeField(auto_now=True, auto_now_add=True)
12 17
13 18 def __unicode__(self):
14 19 return self.name
@@ -43,6 +48,11 @@ def save(self, *args, **kwargs):
43 48 self.slug = "%s_%d" % (slug, i)
44 49 else:
45 50 return super(TagBase, self).save(*args, **kwargs)
  51 +
  52 + def update_views(self):
  53 + from django.db.models import F
  54 + self.viewed = F('viewed') + 1
  55 + self.save()
46 56
47 57 class Tag(TagBase):
48 58 class Meta:
17 taggit/utils.py
... ... @@ -1,6 +1,16 @@
  1 +from decimal import Decimal
  2 +from django.utils.functional import Promise
  3 +from django.utils.translation import force_unicode
  4 +from django.utils.simplejson import JSONEncoder
1 5 from django.utils.encoding import force_unicode
2 6 from django.utils.functional import wraps
3 7
  8 +class LazyEncoder(JSONEncoder):
  9 + def default(self, o):
  10 + if isinstance(o, Promise) or isinstance(o, Decimal):
  11 + return force_unicode(o)
  12 + else:
  13 + return super(LazyEncoder, self).default(o)
4 14
5 15 def parse_tags(tagstring):
6 16 """
@@ -22,8 +32,9 @@ def parse_tags(tagstring):
22 32 # input, we don't *do* a recall... I mean, we know we only need to
23 33 # split on spaces.
24 34 if u',' not in tagstring and u'"' not in tagstring:
25   - words = list(set(split_strip(tagstring, u' ')))
26   - words.sort()
  35 + # words = list(set(split_strip(tagstring, u' ')))
  36 + # words.sort()
  37 + words = [tagstring]
27 38 return words
28 39
29 40 words = []
@@ -71,7 +82,7 @@ def parse_tags(tagstring):
71 82 delimiter = u' '
72 83 for chunk in to_be_split:
73 84 words.extend(split_strip(chunk, delimiter))
74   - words = list(set(words))
  85 + words = list(set(map(unicode.lower, words)))
75 86 words.sort()
76 87 return words
77 88

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.