From: Radek Czajka Date: Mon, 7 Oct 2013 13:37:24 +0000 (+0200) Subject: Add formsets to contact forms, add WTEM form. X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/5fd8cacf1c73a8670d45406b7214a59eedaff07f Add formsets to contact forms, add WTEM form. --- diff --git a/contact/forms.py b/contact/forms.py index 0279e12..e369e95 100644 --- a/contact/forms.py +++ b/contact/forms.py @@ -33,12 +33,22 @@ class ContactForm(forms.Form): required_css_class = 'required' contact = forms.CharField(max_length=128) - def save(self, request): + def save(self, request, formsets=None): from .models import Attachment, Contact body = {} for name, value in self.cleaned_data.items(): if not isinstance(value, UploadedFile) and name != 'contact': - body[name] = value + body[name] = value + + for formset in formsets or []: + for f in formset.forms: + sub_body = {} + for name, value in f.cleaned_data.items(): + if not isinstance(value, UploadedFile): + sub_body[name] = value + if sub_body: + body.setdefault(f.form_tag, []).append(sub_body) + contact = Contact.objects.create(body=body, ip=request.META['REMOTE_ADDR'], contact=self.cleaned_data['contact'], diff --git a/contact/views.py b/contact/views.py index d18598c..26ccf60 100644 --- a/contact/views.py +++ b/contact/views.py @@ -14,14 +14,24 @@ def form(request, form_tag): raise Http404 if request.method == 'POST': form = form_class(request.POST, request.FILES) - if form.is_valid(): - form.save(request) + formsets = [] + valid = form.is_valid() + for formset in getattr(form, 'form_formsets', ()): + fset = formset(request.POST, request.FILES) + if not fset.is_valid(): + valid = False + formsets.append(fset) + if valid: + form.save(request, formsets) return redirect('contact_thanks', form_tag) else: form = form_class(initial=request.GET) + formsets = [] + for formset in getattr(form, 'form_formsets', ()): + formsets.append(formset()) return render(request, ['contact/%s/form.html' % form_tag, 'contact/form.html'], - {'form': form} + {'form': form, 'formsets': formsets} ) diff --git a/edumed/contact_forms.py b/edumed/contact_forms.py index 89e5121..f688e5b 100644 --- a/edumed/contact_forms.py +++ b/edumed/contact_forms.py @@ -81,3 +81,50 @@ class UdzialForm(ContactForm): contact = forms.EmailField(label=u'Adres e-mail', max_length=128) telefon = forms.CharField(label=u'Telefon', max_length=32) uczestnicy = forms.IntegerField(label=u'Przewidywana liczba uczestników zajęć') + + +class WTEMStudentForm(forms.Form): + first_name = forms.CharField(label=u'Imię', max_length=128) + last_name = forms.CharField(label=u'Nazwisko', max_length=128) + email = forms.EmailField(label=u'Adres e-mail', max_length=128) + form_tag = "student" + +class NoEmptyFormsAllowedBaseFormSet(forms.formsets.BaseFormSet): + """ + Won't allow formset_factory to be submitted with no forms + """ + def clean(self): + for form in self.forms: + if form.cleaned_data: + return + raise forms.ValidationError(u"Proszę podać dane przynajmniej jednego ucznia.") + +class WTEMForm(ContactForm): + form_tag = "wtem" + form_title = u"WTEM - rejestracja uczestników" + submit_label = u"Wyślij zgłoszenie" + form_formsets = (forms.formsets.formset_factory(WTEMStudentForm, formset=NoEmptyFormsAllowedBaseFormSet),) + + contact = forms.EmailField(label=u'Adres e-mail opiekuna/opiekunki', max_length=128) + imie = forms.CharField(label=u'Imię', max_length=128) + nazwisko = forms.CharField(label=u'Nazwisko', max_length=128) + function = forms.CharField(label=u'Pełniona funkcja', max_length=255) + institution = forms.CharField(label=u'Nazwa instytucji', max_length=255) + institution_address = forms.CharField(label=u'Adres instytucji', widget=forms.Textarea, max_length=1000) + institution_email = forms.EmailField(label=u'Adres e-mail instytucji', max_length=128) + institution_phone = forms.CharField(label=u'Telefon do instytucji', max_length=32) + institution_www = forms.URLField(label=u'Strona WWW instytucji', max_length=255, required=False) + + zgoda_regulamin = forms.BooleanField( + label=u'Znam i akceptuję regulamin Wielkiego Turnieju Edukacji Medialnej.', + help_text=u'Zobacz regulamin Wielkiego Turnieju Edukacji Medialnej.' + ) + potw_uczniowie = forms.BooleanField( + label=u'Potwierdzam, że zgłoszeni Uczestnicy/Uczestniczki w chwili rejestracji są uczniami/uczennicami szkoły ponadgimnazjalnej.', + #help_text=u'Zobacz regulamin Wielkiego Turnieju Edukacji Medialnej.' + ) + zgoda_informacje = forms.BooleanField( + label=u'Wyrażam zgodę na otrzymywanie informacji od Fundacji Nowoczesna Polska związanych z edukacją medialną.', + required=False + ) + diff --git a/edumed/settings.d/50-static.py b/edumed/settings.d/50-static.py index 4013a66..b6d9591 100644 --- a/edumed/settings.d/50-static.py +++ b/edumed/settings.d/50-static.py @@ -40,6 +40,7 @@ PIPELINE_JS = { 'catalogue/js/lesson.js', 'sponsors/js/sponsors.js', 'curriculum/curriculum.js', + 'js/formset.js', ), 'output_filename': 'compressed/base.js', }, diff --git a/edumed/static/js/formset.js b/edumed/static/js/formset.js new file mode 100755 index 0000000..7a4657f --- /dev/null +++ b/edumed/static/js/formset.js @@ -0,0 +1,30 @@ +(function($) { + $(function() { + + +function cloneMore(selector, type) { + var newElement = $(selector).clone(true); + var total = $('#id_' + type + '-TOTAL_FORMS').val(); + newElement.find(':input').each(function() { + var name = $(this).attr('name').replace('__prefix__', total); + var id = 'id_' + name; + $(this).attr({'name': name, 'id': id}); + }); + newElement.find('label').each(function() { + var newFor = $(this).attr('for').replace('__prefix__', total); + $(this).attr('for', newFor); + }); + newElement.attr({'style': '', 'id': ''}); + total++; + $('#id_' + type + '-TOTAL_FORMS').val(total); + $(selector).before(newElement); +} + + + + $('.add_more').click(function() { + cloneMore($(this).data('selector'), $(this).data('prefix')); + }); + + }); +})(jQuery); diff --git a/edumed/templates/contact/wtem/form.html b/edumed/templates/contact/wtem/form.html new file mode 100755 index 0000000..ca3b930 --- /dev/null +++ b/edumed/templates/contact/wtem/form.html @@ -0,0 +1,66 @@ +{% extends "base.html" %} +{% load chunks %} + +{% block title %}{{ form.form_title }}{% endblock %} + +{% block body %} + + + + + + +

{% block contact_form_title %}{{ form.form_title }}{% endblock %}

+ +
+ {% block contact_form_description %} + {% chunk "contact_form__"|add:form.form_tag %} + {% endblock %} +
+ +
+ {% csrf_token %} +

Dane Opiekuna/Opiekunki i instytucji zgłaszającej Uczestnika:

+ + {{ form.as_table }} +
+ + {% for formset in formsets %} + + {{ formset.management_form }} + + + + {% for form in formset.forms %} +

Dane Uczestnika/Uczestniczki:

+ + + {{ form.as_table }} +
+ {% endfor %} + + + + + + + {% endfor %} + +

+ +

+
+ + +{% endblock %} diff --git a/edumed/templates/contact/wtem/mail_body.txt b/edumed/templates/contact/wtem/mail_body.txt new file mode 100755 index 0000000..0c20f5f --- /dev/null +++ b/edumed/templates/contact/wtem/mail_body.txt @@ -0,0 +1,18 @@ +Dziękujemy za rejestrację w Wielkim Turnieju Edukacji Medialnej. +Do udziału zostały zgłoszone następujące osoby: +{% for student in contact.body.student %} +* {{ student.first_name }} {{ student.last_name }}{% endfor %} + +Pierwszy etap Turnieju odbędzie się 21 listopada 2013 r. o godz. 15.30. +Po zakończeniu rejestracji (4 listopada) każdy Uczestnik otrzyma maila +z indywidualną instrukcją dotyczącą udziału w I etapie Turnieju. + +Wszystkie ogłoszenia związane z Turniejem będą publikowane na stronie +http://edukacjamedialna.edu.pl/WTEM. W razie pytań lub wątpliwości +możesz kontaktować się z nami pisząc na adres +edukacjamedialna@nowoczesnapolska.org.pl. + +Z pozdrowieniami + +Zespół Edukacji Medialnej +Fundacja Nowoczesna Polska diff --git a/edumed/templates/contact/wtem/mail_subject.txt b/edumed/templates/contact/wtem/mail_subject.txt new file mode 100755 index 0000000..15f682a --- /dev/null +++ b/edumed/templates/contact/wtem/mail_subject.txt @@ -0,0 +1 @@ +Dziękujemy za rejestrację w Wielkim Turnieju Edukacji Medialnej. diff --git a/edumed/templates/contact/wtem/thanks.html b/edumed/templates/contact/wtem/thanks.html new file mode 100755 index 0000000..9ad56ff --- /dev/null +++ b/edumed/templates/contact/wtem/thanks.html @@ -0,0 +1,14 @@ +{% extends "contact/thanks.html" %} + +{% block contact_form_description %} +

Dziękujemy za rejestrację w Wielkim Turnieju Edukacji Medialnej.

+ +

Na adres adres e-mail Opiekuna została wysłana wiadomość potwierdzająca +rejestrację.

+ +

Pierwszy etap Turnieju odbędzie się 21 listopada 2013 r. o godz. 15.30.
+Serdecznie zapraszamy do udziału.

+ +

Zespół Edukacji Medialnej
+Fundacja Nowoczesna Polska

+{% endblock %}