annotate gpp/core/widgets.py @ 479:32cec6cd8808

Refactor RateLimiter so that if Redis is not running, everything still runs normally (minus the rate limiting protection). My assumption that creating a Redis connection would throw an exception if Redis wasn't running was wrong. The exceptions actually occur when you issue a command. This is for #224.
author Brian Neal <bgneal@gmail.com>
date Sun, 25 Sep 2011 00:49:05 +0000
parents 5453aedf95fd
children
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@326 23 minLength: 1,
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@326 34 limit: 15
bgneal@186 35 },
bgneal@186 36 dataType: "json",
bgneal@186 37 success: function(data, textStatus) {
bgneal@326 38 if (cacheSize >= 32) {
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