Skip to content

Commit

Permalink
changed the tagging widget to use ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
stephrdev committed May 8, 2009
1 parent 304f080 commit b82fc34
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cpc_project/apps/tagging_utils/urls.py
@@ -0,0 +1,5 @@
from django.conf.urls.defaults import *

urlpatterns = patterns('',
url(r'^autocomplete/(?P<app_label>\w+)/(?P<model>\w+)/$', 'tagging_utils.views.autocomplete', name='tagging_utils_autocomplete'),
)
23 changes: 23 additions & 0 deletions cpc_project/apps/tagging_utils/views.py
@@ -0,0 +1,23 @@
from django.http import HttpResponse, Http404
from django.db.models import get_model
from django.utils import simplejson
from tagging.models import Tag
from django.contrib.contenttypes.models import ContentType

def autocomplete(request, app_label, model):
try:
model = ContentType.objects.get(app_label=app_label, model=model)
except:
raise Http404

if not request.GET.has_key('q'):
raise Http404
else:
q = request.GET['q']

limit = request.GET.get('limit', None)

tags = Tag.objects.filter(items__content_type=model, name__istartswith=q).distinct().order_by('name')[:limit]
tag_list = '\n'.join([tag.name for tag in tags if tag])

return HttpResponse(tag_list)
24 changes: 16 additions & 8 deletions cpc_project/apps/tagging_utils/widgets.py
@@ -1,8 +1,6 @@
from django import forms
from django.db.models import get_model
from django.utils import simplejson
from django.utils.safestring import mark_safe
from tagging.models import Tag
from django.core.urlresolvers import reverse

class TagAutoCompleteInput(forms.TextInput):
class Media:
Expand All @@ -16,16 +14,15 @@ class Media:
'js/jquery.autocomplete.min.js'
)
def __init__(self, app_label, model, *args, **kwargs):
self.model = get_model(app_label, model)
self.app_label = app_label
self.model = model
super(TagAutoCompleteInput, self).__init__(*args, **kwargs)

def render(self, name, value, attrs=None):
output = super(TagAutoCompleteInput, self).render(name, value, attrs)
page_tags = Tag.objects.usage_for_model(self.model)
tag_list = simplejson.dumps([tag.name for tag in page_tags], ensure_ascii=False)

return output + mark_safe(u'''<script type="text/javascript">
jQuery("#id_%s").autocomplete(%s, {
jQuery("#id_%s").autocomplete('%s', {
max: 10,
highlight: false,
multiple: true,
Expand All @@ -35,4 +32,15 @@ def render(self, name, value, attrs=None):
matchContains: true,
autoFill: true,
});
</script>''' % (name, tag_list))
</script>''' % (
name,
reverse(
'tagging_utils_autocomplete',
args=[],
kwargs={
'app_label': self.app_label,
'model': self.model
}
)
)
)
1 change: 1 addition & 0 deletions cpc_project/urls.py
Expand Up @@ -27,6 +27,7 @@
(r'^comments/', include('threadedcomments.urls')),
(r'^paste/', include('dpaste.urls')),
(r'^wiki/', include('wiki.urls')),
(r'^tagging_utils/', include('tagging_utils.urls')),

(r'^admin/(.*)', admin.site.root),
)
Expand Down

0 comments on commit b82fc34

Please sign in to comment.