add update forms + update deadline
[edumed.git] / edumed / contact_forms.py
index 01f3276..7fe13c9 100644 (file)
@@ -6,6 +6,8 @@ from django.utils.safestring import mark_safe
 from contact.forms import ContactForm
 from django.utils.translation import ugettext_lazy as _
 
+from wtem.models import TeacherConfirmation, Confirmation
+
 WOJEWODZTWA = (
     u'dolnośląskie',
     u'kujawsko-pomorskie',
@@ -39,7 +41,7 @@ polityce prywatności</a>.''' % middle_text)
 class ReminderForm(ContactForm):
     form_tag = 'nie-przegap-2018'
     form_title = u'Rejestracja. Nie przegap terminu!'
-    email = forms.EmailField(label=u'Adres e-mail', max_length=128)
+    contact = forms.EmailField(label=u'Adres e-mail', max_length=128)
     data_processing = make_data_processing(
         u'Dane są przetwarzane w zakresie niezbędnym do wysłania powiadomienia odbiorcom.')
     submit_label = u'Wyślij'
@@ -51,6 +53,17 @@ class WTEMStudentForm(forms.Form):
     email = forms.EmailField(label=u'Adres e-mail', max_length=128)
     form_tag = "student"
 
+    def clean_email(self):
+        email = self.cleaned_data['email']
+        if Confirmation.objects.filter(email=email):
+            raise forms.ValidationError(u'Uczeń z tym adresem już został zgłoszony.')
+        return email
+
+
+class WTEMStudentUpdateForm(WTEMStudentForm):
+    def clean_email(self):
+        return self.cleaned_data['email']
+
 
 class NonEmptyBaseFormSet(BaseFormSet):
     """
@@ -63,27 +76,69 @@ class NonEmptyBaseFormSet(BaseFormSet):
         forms.ValidationError(u"Proszę podać dane przynajmniej jednej osoby.")
 
 
+class StudentBaseFormSet(NonEmptyBaseFormSet):
+    def check_unique_emails(self):
+        from django.forms.util import ErrorList
+
+        emails = set()
+        for form in self.forms:
+            if not form.is_valid():
+                continue
+            if form.cleaned_data:
+                email = form.cleaned_data['email']
+                instance = getattr(self, 'instance', None)
+                if email in emails:
+                    errors = form._errors.setdefault('email', ErrorList())
+                    errors.append(u'Każdy zgłoszony uczeń powinien mieć własny adres email')
+                elif instance and Confirmation.objects.exclude(contact=instance).filter(email=email).exists():
+                    errors = form._errors.setdefault('email', ErrorList())
+                    errors.append(u'Uczeń z tym adresem już został zgłoszony w innym formularzu.')
+                else:
+                    emails.add(email)
+
+    def clean(self):
+        super(StudentBaseFormSet, self).clean()
+        self.check_unique_emails()
+
+
+class StudentUpdateFormSet(StudentBaseFormSet):
+    takes_instance = True
+
+    def __init__(self, *args, **kwargs):
+        instance = kwargs.pop('instance', None)
+        super(StudentUpdateFormSet, self).__init__(*args, **kwargs)
+        self.instance = instance
+
+    def clean(self):
+        super(StudentUpdateFormSet, self).clean()
+        self.check_unique_emails()
+
+
 class CommissionForm(forms.Form):
     name = forms.CharField(label=u'Imię i nazwisko Członka Komisji', max_length=128)
     form_tag = "commission"
 
 
 class OlimpiadaForm(ContactForm):
-    ends_on = (2017, 11, 17, 0, 5)
+    ends_on = (2018, 11, 1, 0, 5)
     disabled_template = 'wtem/disabled_contact_form.html'
     form_tag = "olimpiada"
-    old_form_tags = ["olimpiada-2016"]
+    old_form_tags = ["olimpiada-2016", "olimpiada-2017"]
     form_title = u"Olimpiada Cyfrowa - Elektroniczny System Zgłoszeń"
     submit_label = u"Wyślij zgłoszenie"
     admin_list = ['nazwisko', 'school']
     form_formsets = {
-        'student': forms.formsets.formset_factory(WTEMStudentForm, formset=NonEmptyBaseFormSet),
+        'student': forms.formsets.formset_factory(WTEMStudentForm, formset=StudentBaseFormSet),
         'commission': forms.formsets.formset_factory(CommissionForm),
     }
     mailing_field = 'zgoda_newsletter'
+    confirmation_class = TeacherConfirmation
 
     contact = forms.EmailField(label=u'Adres e-mail Przewodniczącego/Przewodniczącej', max_length=128)
     przewodniczacy = forms.CharField(label=u'Imię i nazwisko Przewodniczącego/Przewodniczącej', max_length=128)
+    przewodniczacy_phone = forms.CharField(
+        label=u'Numer telefonu Przewodniczącego/Przewodniczącej', max_length=128,
+        help_text=u'Zadzwonimy tylko w przypadku problemów ze zgłoszeniem.')
     school = forms.CharField(label=u'Nazwa szkoły', max_length=255)
     school_address = forms.CharField(label=u'Adres szkoły', widget=forms.Textarea, max_length=1000)
     school_wojewodztwo = forms.ChoiceField(label=u'Województwo', choices=WOJEWODZTWO_CHOICES)
@@ -138,6 +193,16 @@ class OlimpiadaForm(ContactForm):
                 current = {}
         return toret
 
+    def get_dictionary(self, contact):
+        dictionary = super(OlimpiadaForm, self).get_dictionary(contact)
+        conf = TeacherConfirmation.objects.filter(contact=contact)
+        if conf:
+            confirmation = conf.get()
+        else:
+            confirmation = TeacherConfirmation.create(contact=contact)
+        dictionary['confirmation'] = confirmation
+        return dictionary
+
     def save(self, request, formsets=None):
         from wtem.models import Confirmation
         contact = super(OlimpiadaForm, self).save(request, formsets)
@@ -148,7 +213,7 @@ class OlimpiadaForm(ContactForm):
                     email = f.cleaned_data.get('email', None)
                     if email:
                         try:
-                            Confirmation.objects.get(email=email)
+                            confirmation = Confirmation.objects.get(email=email)
                         except Confirmation.DoesNotExist:
                             first_name = f.cleaned_data.get('first_name', None)
                             last_name = f.cleaned_data.get('last_name', None)
@@ -156,4 +221,16 @@ class OlimpiadaForm(ContactForm):
                                 confirmation = Confirmation.create(
                                     first_name=first_name, last_name=last_name, email=email, contact=contact)
                                 confirmation.send_mail()
+                        else:
+                            confirmation.first_name = f.cleaned_data.get('first_name', None)
+                            confirmation.last_name = f.cleaned_data.get('last_name', None)
+                            confirmation.save()
         return contact
+
+
+class OlimpiadaUpdateForm(OlimpiadaForm):
+    form_type = 'update'
+    form_formsets = {
+        'student': forms.formsets.formset_factory(WTEMStudentUpdateForm, formset=StudentUpdateFormSet),
+        'commission': forms.formsets.formset_factory(CommissionForm),
+    }