annotate gpp/core/widgets.py @ 227:423c39ee44e0
Rework the who's online middleware and template tag for #87.
author |
Brian Neal <bgneal@gmail.com> |
date |
Tue, 06 Jul 2010 03:02:20 +0000 |
parents |
be3fff614b93 |
children |
5453aedf95fd |
rev |
line source |
gremmie@1
|
1 """
|
gremmie@1
|
2 Various useful widgets for the GPP application.
|
gremmie@1
|
3 """
|
gremmie@1
|
4
|
gremmie@1
|
5 from django import forms
|
gremmie@1
|
6 from django.utils.safestring import mark_safe
|
gremmie@1
|
7 from django.core.urlresolvers import reverse
|
bgneal@6
|
8 from django.conf import settings
|
gremmie@1
|
9
|
gremmie@1
|
10
|
gremmie@1
|
11 class AutoCompleteUserInput(forms.TextInput):
|
gremmie@1
|
12
|
gremmie@1
|
13 def render(self, name, value, attrs=None):
|
bgneal@149
|
14 url = reverse('core-ajax_users')
|
gremmie@1
|
15 output = super(AutoCompleteUserInput, self).render(name, value, attrs)
|
bgneal@186
|
16 return output + mark_safe(u"""\
|
gremmie@1
|
17 <script type="text/javascript">
|
bgneal@186
|
18 $(function() {
|
bgneal@186
|
19 var cache = {};
|
bgneal@186
|
20 var cacheSize = 0;
|
bgneal@186
|
21 $("#id_%s").autocomplete({
|
bgneal@186
|
22 delay: 400,
|
bgneal@186
|
23 minLength: 2,
|
bgneal@186
|
24 source: function(request, response) {
|
bgneal@186
|
25 if (cache[request.term]) {
|
bgneal@186
|
26 response(cache[request.term]);
|
bgneal@186
|
27 return;
|
bgneal@186
|
28 }
|
bgneal@186
|
29 $.ajax({
|
bgneal@186
|
30 url: "%s",
|
bgneal@186
|
31 type: "GET",
|
bgneal@186
|
32 data: {
|
bgneal@186
|
33 q: request.term,
|
bgneal@186
|
34 limit: 10
|
bgneal@186
|
35 },
|
bgneal@186
|
36 dataType: "json",
|
bgneal@186
|
37 success: function(data, textStatus) {
|
bgneal@186
|
38 if (cacheSize >= 16) {
|
bgneal@186
|
39 cache = {};
|
bgneal@186
|
40 cacheSize = 0;
|
bgneal@186
|
41 }
|
bgneal@186
|
42 cache[request.term] = data;
|
bgneal@186
|
43 ++cacheSize;
|
bgneal@186
|
44 response(data);
|
bgneal@186
|
45 },
|
bgneal@186
|
46 error: function(xhr, textStatus, ex) {
|
bgneal@186
|
47 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
|
bgneal@186
|
48 xhr.responseText);
|
bgneal@186
|
49 }
|
bgneal@186
|
50 });
|
bgneal@186
|
51 }
|
bgneal@186
|
52 });
|
gremmie@1
|
53 });
|
bgneal@186
|
54 </script>""" % (name, url))
|
gremmie@1
|
55
|