Move AJAX person search to person/ with some support form code.
- Legacy-Id: 4168
This commit is contained in:
parent
c6f59d2c7c
commit
32659ed957
31
ietf/person/forms.py
Normal file
31
ietf/person/forms.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
from django.utils import simplejson
|
||||
from django.utils.html import escape
|
||||
from django.utils.functional import lazy
|
||||
from django import forms
|
||||
from django.core.urlresolvers import reverse as urlreverse
|
||||
|
||||
from ietf.person.models import *
|
||||
|
||||
def json_emails(emails):
|
||||
return simplejson.dumps([{"id": e.address + "", "name": escape(u"%s <%s>" % (e.person.name, e.address))} for e in emails])
|
||||
|
||||
class EmailsField(forms.CharField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs["max_length"] = 1000
|
||||
if not "help_text" in kwargs:
|
||||
kwargs["help_text"] = "Type in name to search for person"
|
||||
super(EmailsField, self).__init__(*args, **kwargs)
|
||||
self.widget.attrs["class"] = "emails-field"
|
||||
self.widget.attrs["data-ajax-url"] = lazy(urlreverse, str)("ajax_search_emails") # make this lazy to prevent initialization problem
|
||||
|
||||
def prepare_value(self, value):
|
||||
if not value:
|
||||
return ""
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
return json_emails(value)
|
||||
|
||||
def clean(self, value):
|
||||
value = super(EmailsField, self).clean(value)
|
||||
return Email.objects.filter(address__in=[x.strip() for x in value.split(",") if x.strip()]).select_related("person")
|
||||
|
5
ietf/person/urls.py
Normal file
5
ietf/person/urls.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.conf.urls.defaults import patterns, url
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^search/$', "ietf.person.views.ajax_search_emails", None, 'ajax_search_emails'),
|
||||
)
|
8
ietf/person/views.py
Normal file
8
ietf/person/views.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
from django.http import Http404, HttpResponse
|
||||
|
||||
from ietf.person.models import *
|
||||
from ietf.person.forms import json_emails
|
||||
|
||||
def ajax_search_emails(request):
|
||||
emails = Email.objects.filter(person__alias__name__istartswith=request.GET.get('q','')).order_by('person__name').distinct()
|
||||
return HttpResponse(json_emails(emails), mimetype='application/json')
|
|
@ -63,6 +63,7 @@ urlpatterns = patterns('',
|
|||
(r'^wg/', include('ietf.wginfo.urls')),
|
||||
(r'^wgcharter/', include('ietf.wgcharter.urls')),
|
||||
(r'^cookies/', include('ietf.cookies.urls')),
|
||||
(r'^person/', include('ietf.person.urls')),
|
||||
(r'^submit/', include('ietf.submit.urls')),
|
||||
(r'^streams/', include('ietf.ietfworkflows.urls')),
|
||||
|
||||
|
|
Loading…
Reference in a new issue