comparison core/widgets.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/core/widgets.py@5453aedf95fd
children 678a1a2ef55a
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Various useful widgets for the GPP application.
3 """
4
5 from django import forms
6 from django.utils.safestring import mark_safe
7 from django.core.urlresolvers import reverse
8 from django.conf import settings
9
10
11 class AutoCompleteUserInput(forms.TextInput):
12
13 def render(self, name, value, attrs=None):
14 url = reverse('core-ajax_users')
15 output = super(AutoCompleteUserInput, self).render(name, value, attrs)
16 return output + mark_safe(u"""\
17 <script type="text/javascript">
18 $(function() {
19 var cache = {};
20 var cacheSize = 0;
21 $("#id_%s").autocomplete({
22 delay: 400,
23 minLength: 1,
24 source: function(request, response) {
25 if (cache[request.term]) {
26 response(cache[request.term]);
27 return;
28 }
29 $.ajax({
30 url: "%s",
31 type: "GET",
32 data: {
33 q: request.term,
34 limit: 15
35 },
36 dataType: "json",
37 success: function(data, textStatus) {
38 if (cacheSize >= 32) {
39 cache = {};
40 cacheSize = 0;
41 }
42 cache[request.term] = data;
43 ++cacheSize;
44 response(data);
45 },
46 error: function(xhr, textStatus, ex) {
47 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
48 xhr.responseText);
49 }
50 });
51 }
52 });
53 });
54 </script>""" % (name, url))
55