Skip to content

Commit f8b3e8b

Browse files
committed
validate user has first/last name in user profile when self nominating
1 parent d3247b9 commit f8b3e8b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

nominations/forms.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django import forms
2+
from django.utils.safestring import mark_safe
23

34
from markupfield.widgets import MarkupTextarea
45

@@ -30,7 +31,23 @@ class Meta:
3031

3132

3233
class NominationCreateForm(NominationForm):
34+
def __init__(self, *args, **kwargs):
35+
self.request = kwargs.pop("request", None)
36+
super(NominationCreateForm, self).__init__(*args, **kwargs)
37+
3338
self_nomination = forms.BooleanField(
3439
required=False,
3540
help_text="If you are nominating yourself, we will automatically associate the nomination with your python.org user.",
3641
)
42+
43+
def clean_self_nomination(self):
44+
data = self.cleaned_data["self_nomination"]
45+
if data:
46+
if not self.request.user.first_name or not self.request.user.last_name:
47+
raise forms.ValidationError(
48+
mark_safe(
49+
'You must set your First and Last name in your <a href="/users/edit/">User Profile</a> to self nominate.'
50+
)
51+
)
52+
53+
return data

nominations/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class NominationCreate(LoginRequiredMixin, NominationMixin, CreateView):
5959

6060
login_message = "Please login to make a nomination."
6161

62+
def get_form_kwargs(self):
63+
kwargs = super(NominationCreate, self).get_form_kwargs()
64+
kwargs.update({"request": self.request})
65+
return kwargs
66+
6267
def get_form_class(self):
6368
election = Election.objects.get(slug=self.kwargs["election"])
6469
if election.nominations_complete:

0 commit comments

Comments
 (0)