|
1 | 1 | import json |
2 | 2 |
|
3 | 3 | from collections import Counter |
| 4 | +from urllib import urlencode |
4 | 5 |
|
5 | 6 | from django.utils.html import escape |
6 | 7 | from django import forms |
@@ -47,12 +48,14 @@ class SearchablePersonsField(forms.CharField): |
47 | 48 | def __init__(self, |
48 | 49 | max_entries=None, # max number of selected objs |
49 | 50 | only_users=False, # only select persons who also have a user |
| 51 | + all_emails=False, # select only active email addresses |
50 | 52 | model=Person, # or Email |
51 | 53 | hint_text="Type in name to search for person.", |
52 | 54 | *args, **kwargs): |
53 | 55 | kwargs["max_length"] = 1000 |
54 | 56 | self.max_entries = max_entries |
55 | 57 | self.only_users = only_users |
| 58 | + self.all_emails = all_emails |
56 | 59 | assert model in [ Email, Person ] |
57 | 60 | self.model = model |
58 | 61 |
|
@@ -83,8 +86,13 @@ def prepare_value(self, value): |
83 | 86 | # doing this in the constructor is difficult because the URL |
84 | 87 | # patterns may not have been fully constructed there yet |
85 | 88 | self.widget.attrs["data-ajax-url"] = urlreverse("ajax_select2_search_person_email", kwargs={ "model_name": self.model.__name__.lower() }) |
| 89 | + query_args = {} |
86 | 90 | if self.only_users: |
87 | | - self.widget.attrs["data-ajax-url"] += "?user=1" # require a Datatracker account |
| 91 | + query_args["user"] = "1" |
| 92 | + if self.all_emails: |
| 93 | + query_args["a"] = "1" |
| 94 | + if query_args: |
| 95 | + self.widget.attrs["data-ajax-url"] += "?%s" % urlencode(query_args) |
88 | 96 |
|
89 | 97 | return u",".join(str(p.pk) for p in value) |
90 | 98 |
|
|
0 commit comments