Repository: dotkom/onlineweb4

Issue:
Mandatory phone number in profile
It has been requested from arrkom among others and decided in HS that phone numbers in the user profile should be mandatory for people attending events. So we need to implement functionality similar to the one used for "prikkeregler". 

If users hide this in their profile the info could behave as allergies and only show up when events are exported to pdf. 



Discussion:

Resolving patch:
diff --git a/apps/events/forms.py b/apps/events/forms.py
--- a/apps/events/forms.py
+++ b/apps/events/forms.py
@@ -1,12 +1,48 @@
+#-*- coding: utf-8 -*-
+
 from django import forms
 from captcha.fields import CaptchaField
+from django.utils.translation import ugettext as _
 
 class CaptchaForm(forms.Form):
+
+    phone_number = forms.CharField(label=_(u'Telefonnummer er påkrevd for å være påmeldt et arrangement.'),
+                                   error_messages={'required' : _(u'Telefonnummer er påkrevd!')})
+    mark_rules = forms.BooleanField(label=_(u'Jeg godtar <a href="/profile/#marks" target="_blank">prikkreglene</a>'),
+                                    error_messages={'required' : _(u'Du må godta prikkereglene!')})
+    captcha = CaptchaField(error_messages={'required' : _(u'Du klarte ikke captchaen! Er du en bot?')})
+
     def __init__(self, *args, **kwargs):
-        user = kwargs.pop('user', None)
+        self.user = kwargs.pop('user', None)
         super(CaptchaForm, self).__init__(*args, **kwargs)
+
         # Removing mark rules field if user has already accepted the rules
-        if user and user.is_authenticated() and user.mark_rules:
-            del self.fields['mark_rules']
-    mark_rules = forms.BooleanField(label=u'Jeg godtar <a href="/profile/#marks" target="_blank">prikkreglene</a>')
-    captcha = CaptchaField()
+        if self.user and self.user.is_authenticated():
+            if self.user.mark_rules:
+                del self.fields['mark_rules']
+
+            if self.user.phone_number:
+                del self.fields['phone_number']
+
+
+    def clean(self):
+        super(CaptchaForm, self).clean()
+        cleaned_data = self.cleaned_data
+
+        if 'mark_rules' in self.fields:
+            if 'mark_rules' in cleaned_data:
+                mark_rules = cleaned_data['mark_rules']
+
+                if mark_rules:
+                    self.user.mark_rules = True
+                    self.user.save()
+
+        if 'phone_number' in self.fields:
+            if 'phone_number' in cleaned_data:
+                phone_number = cleaned_data['phone_number']
+
+                if phone_number:
+                    self.user.phone_number = phone_number
+                    self.user.save()
+
+        return cleaned_data
\ No newline at end of file
diff --git a/apps/events/views.py b/apps/events/views.py
--- a/apps/events/views.py
+++ b/apps/events/views.py
@@ -89,14 +89,14 @@ def attendEvent(request, event_id):
     if not request.POST:
         messages.error(request, _(u'Vennligst fyll ut skjemaet.'))
         return redirect(event)
+
     form = CaptchaForm(request.POST, user=request.user)
 
     if not form.is_valid():
-        if not 'mark_rules' in request.POST and not request.user.mark_rules:
-            error_message = u'Du må godta prikkreglene for å melde deg på.'
-        else:
-            error_message = u'Du klarte ikke captcha-en. Er du en bot?'
-        messages.error(request, _(error_message))
+        for field,errors in form.errors.items():
+            for error in errors:
+                messages.error(request, error)
+
         return redirect(event)
 
     # Check if the user is eligible to attend this event.
@@ -106,10 +106,6 @@ def attendEvent(request, event_id):
     response = event.is_eligible_for_signup(request.user);
 
     if response['status']:   
-        # First time accepting mark rules
-        if 'mark_rules' in form.cleaned_data:
-            request.user.mark_rules = True
-            request.user.save()
         Attendee(event=attendance_event, user=request.user).save()
         messages.success(request, _(u"Du er nå påmeldt på arrangementet!"))
         return redirect(event)


Test patch: