annotate gpp/core/widgets.py @ 388:c3231af55778

For #191; r410 is wrong: it is returning the wrong posts. Rework. MySQL is not using an index on our query, and it is taking 10+ seconds. Replace this slow query with a loop that loops over the public forums, then sorts and returns the posts.
author Brian Neal <bgneal@gmail.com>
date Sat, 19 Mar 2011 05:03:51 +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