Germano Gabbianelli avatar Germano Gabbianelli committed 4c8b5a4

added AutoCompleteField, renamed AutoComplete to AutoCompleteWidget.

Comments (0)

Files changed (4)

ac_example/forms.py

 from django import forms
 from django.contrib.auth.models import User, Message
-from autocomplete.widgets import AutoComplete
+from autocomplete import AutoCompleteWidget, AutoCompleteField
 
 class InsertMessage(forms.ModelForm):
     class Meta:
         model = Message
 
     user = forms.ModelChoiceField(User.objects.all(),
-            widget=AutoComplete('user'), help_text="you must select a valid choice")
-    username = forms.CharField(widget=AutoComplete('user', force_selection=False))
+            widget=AutoCompleteWidget('user'), help_text="you must select a valid choice")
+
+    other_user = AutoCompleteField('user')
+    username = forms.CharField(widget=AutoCompleteWidget('user', force_selection=False))

autocomplete/__init__.py

+from autocomplete.views import AutoComplete, autocomplete
+from autocomplete.widgets import AutoCompleteWidget
+from autocomplete.fields import AutoCompleteField

autocomplete/views.py

 from django.utils import simplejson
 from django.utils.encoding import smart_unicode
 
-AUTOCOMPLETE_NOTFOUND = HttpResponse(status=404)
-AUTOCOMPLETE_FORBIDDEN = HttpResponse(status=403)
 
 class AutoComplete(object):
 
 
     def __call__(self, request, ac_name, query_param='query'):
         if not self.settings.get(ac_name):
-            return AUTOCOMPLETE_NOTFOUND
+            return self.not_found(ac_name)
 
         qs, fields, limit, key, label, auth = self.settings[ac_name]
         if auth and not request.user.is_authenticated():
-            return AUTOCOMPLETE_FORBIDDEN
+            return self.forbidden(ac_name)
         query = request.GET.get(query_param, '')
         
         filter = Q()
     def register(self, id, queryset, fields, limit=None, key='pk', label=lambda obj: smart_unicode(obj), auth=False):
         self.settings[id] = (queryset, fields, limit, key, label, auth)
 
+    def not_found(self, ac_name):
+        return HttpResponse(status=404)
+
+    def forbidden(self, ac_name):
+        return HttpResponse(status=403)
+
 autocomplete = AutoComplete()

autocomplete/widgets.py

 </div>
 '''
 
-class AutoComplete(widgets.Widget):
+class AutoCompleteWidget(widgets.Widget):
 
     AC_TEMPLATE = AC_TEMPLATE
 
         }
         js = ('http://yui.yahooapis.com/combo'
               '?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js'
-              '&2.6.0/build/animation/animation-min.js'
+              # decomment to enable animation.
+              #'&2.6.0/build/animation/animation-min.js'
               '&2.6.0/build/connection/connection-min.js'
               '&2.6.0/build/datasource/datasource-min.js'
               '&2.6.0/build/autocomplete/autocomplete-min.js',
               'js/autocomplete.js')
 
     def __init__(self, ac_name, force_selection=True, view_name='autocomplete', attrs=None):
-        super(AutoComplete, self).__init__(attrs)
+        super(AutoCompleteWidget, self).__init__(attrs)
         self.ac_name = ac_name
         self.view_name = view_name
         self.force_selection = force_selection
         force_selection = str(self.force_selection).lower()
         return mark_safe(self.AC_TEMPLATE % locals())
 
-"""
-# TODO:
-class AdvancedChoiceField(forms.CharField):
-    
-    def __init__(self, ac_name=None, *args, **kwargs):
-        if not kwargs.pop('widget'):
-            kwargs['widget'] = AutoComplete(ac_name)
-        super(AdvancedChoiceField, self).__init__(*args, **kwargs)
-"""
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.