Add option (default set to true) to EmailsField to require that the
email addresses have associated Datatracker accounts - this takes effect immediately for the JS auto-complete thing, but the actual validation afterwards doesn't actually require it yet (check commented out for the time being), as it appears there are still a few people without accounts in active groups - Legacy-Id: 8277
This commit is contained in:
parent
f7ac066059
commit
a5e1586433
|
@ -20,15 +20,15 @@ class EmailsField(forms.CharField):
|
|||
representation on the way out and parse the ids coming back as a
|
||||
comma-separated list on the way in."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
def __init__(self, max_entries=None, hint_text="Type in name or email to search for person and email address", only_users=True,
|
||||
*args, **kwargs):
|
||||
kwargs["max_length"] = 1000
|
||||
self.max_entries = kwargs.pop("max_entries", None)
|
||||
hint_text = kwargs.pop("hint_text", "Type in name or email to search for person and email address")
|
||||
self.max_entries = max_entries
|
||||
self.only_users = only_users
|
||||
|
||||
super(EmailsField, self).__init__(*args, **kwargs)
|
||||
|
||||
self.widget.attrs["class"] = "tokenized-field"
|
||||
self.widget.attrs["data-ajax-url"] = lazy(urlreverse, str)("ajax_search_emails") # do this lazy to prevent form initialization problems
|
||||
self.widget.attrs["data-hint-text"] = hint_text
|
||||
if self.max_entries != None:
|
||||
self.widget.attrs["data-max-entries"] = self.max_entries
|
||||
|
@ -44,6 +44,11 @@ class EmailsField(forms.CharField):
|
|||
value = Email.objects.filter(address__in=addresses).select_related("person")
|
||||
|
||||
self.widget.attrs["data-pre"] = json_emails(value)
|
||||
# doing this in the constructor is difficult because the URL
|
||||
# patterns may not have been fully constructed there yet
|
||||
self.widget.attrs["data-ajax-url"] = urlreverse("ajax_search_emails")
|
||||
if self.only_users:
|
||||
self.widget.attrs["data-ajax-url"] += "?user=1" # require a Datatracker account
|
||||
|
||||
return ",".join(e.address for e in value)
|
||||
|
||||
|
@ -52,6 +57,9 @@ class EmailsField(forms.CharField):
|
|||
addresses = self.parse_tokenized_value(value)
|
||||
|
||||
emails = Email.objects.filter(address__in=addresses).exclude(person=None).select_related("person")
|
||||
# there are still a couple of active roles without accounts so don't disallow those yet
|
||||
#if self.only_users:
|
||||
# emails = emails.exclude(person__user=None)
|
||||
found_addresses = [e.address for e in emails]
|
||||
|
||||
failed_addresses = [x for x in addresses if x not in found_addresses]
|
||||
|
@ -59,7 +67,7 @@ class EmailsField(forms.CharField):
|
|||
raise forms.ValidationError(u"Could not recognize the following email addresses: %s. You can only input addresses already registered in the Datatracker." % ", ".join(failed_addresses))
|
||||
|
||||
if self.max_entries != None and len(emails) > self.max_entries:
|
||||
raise forms.ValidationError(u"You can only select at most %s entries." % self.max_entries)
|
||||
raise forms.ValidationError(u"You can select at most %s entries only." % self.max_entries)
|
||||
|
||||
return emails
|
||||
|
||||
|
|
|
@ -7,5 +7,8 @@ from ietf.person.fields import json_emails
|
|||
def ajax_search_emails(request):
|
||||
q = request.GET.get('q', '').strip()
|
||||
emails = Email.objects.filter(Q(person__alias__name__icontains=q) |
|
||||
Q(address__icontains=q)).filter(active='true').order_by('person__name').distinct()[:10]
|
||||
Q(address__icontains=q))
|
||||
if request.GET.get("user") == "1":
|
||||
emails = emails.exclude(person__user=None) # require an account at the Datatracker
|
||||
emails = emails.filter(active=True).order_by('person__name').distinct()[:10]
|
||||
return HttpResponse(json_emails(emails), content_type='application/json')
|
||||
|
|
Loading…
Reference in a new issue