Skip to content

Commit 058d08e

Browse files
committed
add custom PersonIdentifier field and widget
1 parent b1c94e2 commit 058d08e

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

Diff for: ynr/apps/bulk_adding/fields.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from django import forms
2+
from people.models import PersonIdentifier
3+
4+
5+
class PersonIdentifierWidget(forms.MultiWidget):
6+
template_name = "bulk_add/includes/person_identifier_multiwidget.html"
7+
8+
def __init__(self, *args, **kwargs):
9+
super().__init__(
10+
widgets=[
11+
forms.TextInput(),
12+
forms.Select(
13+
choices=[("", "Select an option")]
14+
+ PersonIdentifier.objects.select_choices()[1:],
15+
),
16+
],
17+
**kwargs,
18+
)
19+
20+
def decompress(self, value):
21+
# TODO: Turn single value back into list of two values
22+
return [None, None]
23+
24+
25+
class PersonIdentifierField(forms.MultiValueField):
26+
def __init__(self, **kwargs):
27+
fields = (
28+
forms.CharField(
29+
required=True,
30+
error_messages={
31+
"incomplete": "Please enter a social media link",
32+
},
33+
),
34+
forms.ChoiceField(
35+
required=True,
36+
error_messages={
37+
"incomplete": "Please select a link type",
38+
},
39+
choices=[("", "Select an option")]
40+
+ PersonIdentifier.objects.select_choices()[1:],
41+
),
42+
)
43+
widget = PersonIdentifierWidget()
44+
45+
super().__init__(
46+
fields=fields,
47+
require_all_fields=False,
48+
widget=widget,
49+
**kwargs,
50+
)
51+
52+
def compress(self, data_list):
53+
# TODO:
54+
# Validations
55+
# if valid, create single value
56+
return " ".join(data_list)

Diff for: ynr/apps/bulk_adding/forms.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from bulk_adding.fields import PersonIdentifierField
12
from django import forms
23
from django.core.exceptions import ValidationError
34
from django.db.models import Prefetch
@@ -262,6 +263,10 @@ class BulkAddByPartyForm(NameOnlyPersonForm):
262263
required=False,
263264
widget=forms.NumberInput,
264265
)
266+
person_identifier = PersonIdentifierField(
267+
label="Links and social media",
268+
required=False,
269+
)
265270

266271

267272
class QuickAddSinglePersonForm(PopulatePartiesMixin, NameOnlyPersonForm):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<tr>
2+
<td>
3+
<div class="large-12 columns">
4+
<div class="row collapse">
5+
<div class="small-9 columns">
6+
{% include widget.subwidgets.0.template_name with widget=widget.subwidgets.0 %}
7+
</div>
8+
<div class="small-3 columns">
9+
<span class="postfix">
10+
{% include widget.subwidgets.1.template_name with widget=widget.subwidgets.1 %}
11+
</span>
12+
</div>
13+
</div>
14+
</div>
15+
</td>
16+
</tr>

Diff for: ynr/apps/bulk_adding/tests/test_bulk_add_by_party.py

+32
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,35 @@ def test_submit_name_and_demographic_details_for_area(self):
170170
# We should have created 2 logged actions, one for person-create
171171
# and one for person-update (adding the membership)
172172
self.assertEqual(LoggedAction.objects.count(), 2)
173+
174+
def test_submit_social_media_link_without_link_type(self):
175+
ballot = self.election.ballot_set.first()
176+
form = self.app.get(
177+
"/bulk_adding/party/parl.2015-05-07/PP52/",
178+
user=self.user_who_can_upload_documents,
179+
).forms[1]
180+
181+
form["source"] = "https://example.com/candidates/"
182+
183+
# Fill in the link field but don't select the link type
184+
form[f"{ballot.pk}-0-name"] = "Pemphero Pasternak"
185+
form[f"{ballot.pk}-0-person_identifier_0"] = "https://example.com"
186+
187+
response = form.submit()
188+
self.assertContains(response, "Please select a link type")
189+
190+
def test_submit_social_media_link_type_without_link(self):
191+
ballot = self.election.ballot_set.first()
192+
form = self.app.get(
193+
"/bulk_adding/party/parl.2015-05-07/PP52/",
194+
user=self.user_who_can_upload_documents,
195+
).forms[1]
196+
197+
form["source"] = "https://example.com/candidates/"
198+
199+
# Select a link type but leave link field blank
200+
form[f"{ballot.pk}-0-name"] = "Pemphero Pasternak"
201+
form[f"{ballot.pk}-0-person_identifier_1"] = "mastodon_username"
202+
203+
response = form.submit()
204+
self.assertContains(response, "Please enter a social media link")

0 commit comments

Comments
 (0)