From a4d28c35907d5b4e4a3c4bd0ae289d00afdec69d Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 16 Sep 2019 15:00:42 +0200 Subject: [PATCH] Contact forms update. --- requirements/requirements.txt | 6 ++- src/contact/admin.py | 2 +- src/contact/fields.py | 11 +++- src/contact/forms.py | 11 ++-- src/contact/models.py | 2 +- src/contact/views.py | 2 +- src/wolnelektury/contact_forms.py | 50 ++++++++++--------- src/wolnelektury/settings/apps.py | 3 +- src/wolnelektury/settings/contrib.py | 11 ++++ .../templates/forms/form_detail.html | 8 +++ .../templates/forms/form_sent.html | 9 ++++ .../templates/forms/includes/built_form.html | 18 +++++++ src/wolnelektury/urls.py | 1 + 13 files changed, 99 insertions(+), 35 deletions(-) create mode 100644 src/wolnelektury/templates/forms/form_detail.html create mode 100644 src/wolnelektury/templates/forms/form_sent.html create mode 100644 src/wolnelektury/templates/forms/includes/built_form.html diff --git a/requirements/requirements.txt b/requirements/requirements.txt index ce0b5e912..3af144853 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -1,7 +1,7 @@ -i https://py.mdrn.pl/simple/ # django -Django==2.2.4 +Django==2.2.5 fnpdjango==0.4 #django-pipeline==1.6.13 @@ -22,6 +22,10 @@ djangorestframework-xml django-admin-ordering==0.10.0 django-machina==1.0.2 +# A version compatible with Django 2.2, with long help text and editable max_length. +-e git+https://github.com/rczajka/django-forms-builder@1e57b980ef5778895512218fbea015ed428dc89a#egg=django-forms-builder + + oauthlib>=3.0.1,<3.1 # contact diff --git a/src/contact/admin.py b/src/contact/admin.py index cf8d06cd0..98413f34c 100644 --- a/src/contact/admin.py +++ b/src/contact/admin.py @@ -56,7 +56,7 @@ class ContactAdmin(admin.ModelAdmin, metaclass=ContactAdminMeta): else: # Create readonly fields from the body JSON. attachments = list(instance.attachment_set.all()) - body_keys = body.keys() + [a.tag for a in attachments] + body_keys = list(body.keys()) + [a.tag for a in attachments] # Find the original form. try: diff --git a/src/contact/fields.py b/src/contact/fields.py index 578776aa7..ce85c2291 100644 --- a/src/contact/fields.py +++ b/src/contact/fields.py @@ -2,6 +2,8 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django import forms +from django.utils.html import conditional_escape +from django.utils.safestring import mark_safe from .widgets import HeaderWidget @@ -9,4 +11,11 @@ class HeaderField(forms.CharField): def __init__(self, required=False, widget=None, *args, **kwargs): if widget is None: widget = HeaderWidget - super(HeaderField, self).__init__(required=required, widget=widget, *args, **kwargs) + super(HeaderField, self).__init__(required=False, widget=widget, *args, **kwargs) + self.label = mark_safe('' + conditional_escape(self.label) + '') + + +class SeparatorField(HeaderField): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.label = '' diff --git a/src/contact/forms.py b/src/contact/forms.py index ecdc47d54..8fe00702e 100644 --- a/src/contact/forms.py +++ b/src/contact/forms.py @@ -76,15 +76,14 @@ class ContactForm(forms.Form, metaclass=ContactFormMeta): 'site_domain': getattr(self, 'site_domain', site.domain), 'contact': contact, } - context = RequestContext(request) mail_managers_subject = render_to_string([ 'contact/%s/mail_managers_subject.txt' % self.form_tag, 'contact/mail_managers_subject.txt', - ], dictionary, context).strip() + ], dictionary, request).strip() mail_managers_body = render_to_string([ 'contact/%s/mail_managers_body.txt' % self.form_tag, 'contact/mail_managers_body.txt', - ], dictionary, context) + ], dictionary, request) mail_managers(mail_managers_subject, mail_managers_body, fail_silently=True) try: @@ -95,19 +94,19 @@ class ContactForm(forms.Form, metaclass=ContactFormMeta): mail_subject = render_to_string([ 'contact/%s/mail_subject.txt' % self.form_tag, 'contact/mail_subject.txt', - ], dictionary, context).strip() + ], dictionary, request).strip() if self.result_page: mail_body = render_to_string( 'contact/%s/results_email.txt' % contact.form_tag, { 'contact': contact, 'results': self.results(contact), - }, context) + }, request) else: mail_body = render_to_string([ 'contact/%s/mail_body.txt' % self.form_tag, 'contact/mail_body.txt', - ], dictionary, context) + ], dictionary, request) send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [contact.contact], fail_silently=True) return contact diff --git a/src/contact/models.py b/src/contact/models.py index 929106eaa..e3659908a 100644 --- a/src/contact/models.py +++ b/src/contact/models.py @@ -23,7 +23,7 @@ class Contact(models.Model): if type(value) in (tuple, list, dict): value = yaml.safe_dump(value, allow_unicode=True, default_flow_style=False) if for_html: - value = smart_text(value).replace(" ", unichr(160)) + value = smart_text(value).replace(" ", chr(160)) return value class Meta: diff --git a/src/contact/views.py b/src/contact/views.py index 6f6205204..5f22f79d6 100644 --- a/src/contact/views.py +++ b/src/contact/views.py @@ -43,7 +43,7 @@ def form(request, form_tag, force_enabled=False): formsets = { prefix: formset_class(request.POST, request.FILES, prefix=prefix) for prefix, formset_class in formset_classes.items()} - if form.is_valid() and all(formset.is_valid() for formset in formsets.itervalues()): + if form.is_valid() and all(formset.is_valid() for formset in formsets.values()): contact = form.save(request, formsets.values()) if form.result_page: return redirect('contact_results', contact.id, contact.digest()) diff --git a/src/wolnelektury/contact_forms.py b/src/wolnelektury/contact_forms.py index 9eb87ffa7..89baea9e0 100644 --- a/src/wolnelektury/contact_forms.py +++ b/src/wolnelektury/contact_forms.py @@ -70,7 +70,7 @@ class KonkursForm(ContactForm): class CoJaCzytamForm(ContactForm): - form_tag = 'cojaczytam' + form_tag = 'cojaczytam2019' form_title = "#cojaczytam?" admin_list = ['opiekun_nazwisko', 'contact', 'nazwa_kampanii'] # ends_on = (2018, 11, 16) @@ -80,17 +80,22 @@ class CoJaCzytamForm(ContactForm): opiekun_nazwisko = forms.CharField(label='Imię i nazwisko Opiekuna/ki', max_length=128) contact = forms.EmailField(label='Adres e-mail Opiekuna/ki', max_length=128) opiekun_tel = forms.CharField(label='Numer telefonu Opiekuna/ki', max_length=32) - nazwa_dkk = forms.CharField(label='Nazwa szkoły/biblioteki publicznej', max_length=128) - adres_dkk = forms.CharField(label='Adres szkoły/biblioteki publicznej', max_length=128) + hadres = HeaderField(label=mark_safe('Placówka')) + nazwa_dkk = forms.CharField(label='Nazwa placówki', max_length=128) + adres1_dkk = forms.CharField(label='Ulica i numer', max_length=128) + adres2_dkk = forms.CharField(label='Miejscowość', max_length=128) + adres3_dkk = forms.CharField(label='Kod pocztowy', max_length=128) + + hkampania = HeaderField(label=mark_safe('Kampania')) nazwa_kampanii = forms.CharField(label='Nazwa kampanii', max_length=255) wiek = forms.ChoiceField(label='Grupa wiekowa', choices=( - ('9-14', 'uczniowie kl. IV-VIII szkół podstawowych w wieku 9-14 lat,'), - ('15-19', 'uczniowie gimnazjum oraz wszystkich typów szkół ponadpodstawowych w wieku 15-19.'), + ('9-14', 'uczestnicy w wieku 9-14 lat,'), + ('15-19', 'uczestnicy w wieku 15-19.'), ), widget=forms.RadioSelect) - uczestnik1_header = HeaderField(label='Dane\xa0Uczestników (3 do 5)') + uczestnik1_header = HeaderField(label=mark_safe('Dane\xa0Uczestników (3\xa0do\xa05)')) uczestnik1_imie = forms.CharField(label='Imię', max_length=128) uczestnik1_nazwisko = forms.CharField(label='Nazwisko', max_length=128) uczestnik1_email = forms.EmailField(label='Adres e-mail', max_length=128) @@ -111,33 +116,32 @@ class CoJaCzytamForm(ContactForm): uczestnik5_nazwisko = forms.CharField(label='Nazwisko', max_length=128, required=False) uczestnik5_email = forms.EmailField(label='Adres e-mail', max_length=128, required=False) - ankieta_header = HeaderField(label='') - opis_kampanii = forms.CharField( - label='Krótki opis realizacji oraz przebiegu kampanii', max_length=255, widget=forms.Textarea) - co_sie_udalo = forms.CharField(label='Co udało Wam się zrealizować?', max_length=1024, widget=forms.Textarea) - co_sie_nie_udalo = forms.CharField( - label='Czy jest coś, co chcieliście zrealizować, a się nie udało? Jeśli tak, to dlaczego?', max_length=1024, + ankieta_header = HeaderField(label=mark_safe('Efekty')) + opis_kampanii = forms.CharField(help_text='maks. 250 znaków', + label='Krótki opis realizacji oraz przebiegu kampanii', max_length=250, widget=forms.Textarea) + co_sie_udalo = forms.CharField(help_text='maks. 1000 znaków',label='Co udało Wam się zrealizować?', max_length=1000, widget=forms.Textarea) + co_sie_nie_udalo = forms.CharField(help_text='maks. 1000 znaków', + label='Czy jest coś, co chcieliście zrealizować, a się nie udało? Jeśli tak, to dlaczego?', max_length=1000, widget=forms.Textarea) - wnioski = forms.CharField( - label='Jakie wnioski na przyszłość wyciągnęliście z tego, czego się nie udało zrealizować?', max_length=1024, + wnioski = forms.CharField(help_text='maks. 1000 znaków', + label='Jakie wnioski na przyszłość wyciągnęliście z tego, czego się nie udało zrealizować?', max_length=1000, widget=forms.Textarea) - zasieg = forms.CharField( + zasieg = forms.CharField(help_text='maks. 1000 znaków', label='Do ilu odbiorców udało Wam się dotrzeć z Waszą kompanią? Podaj liczbę, może być szacunkowa.', - max_length=1024, widget=forms.Textarea) - grupy_odbiorcow = forms.CharField( + max_length=1000, widget=forms.Textarea) + grupy_odbiorcow = forms.CharField(help_text='maks. 1000 znaków', label='Do jakich grup odbiorców dotarliście (np. uczniowie, nauczyciele, rodzice, seniorzy, inni)?', - max_length=1024, widget=forms.Textarea) + max_length=1000, widget=forms.Textarea) plik = forms.FileField( label='Plik .zip ze stworzonymi materiałami (np. zdjęcia, dokumenty tekstowe)') - materialy = forms.CharField( - label='Adresy stworzonych materiałów online (jeśli dotyczy)', max_length=1024, widget=forms.Textarea, - required=False) + materialy = forms.CharField(help_text='maks. 1000 znaków', + label='Adresy stworzonych materiałów online', max_length=1000, widget=forms.Textarea) - agree_header = HeaderField(label='Oświadczenia') + agree_header = HeaderField(label=mark_safe('Oświadczenia')) agree_terms = forms.BooleanField( label='Regulamin', help_text=mark_safe_lazy( - 'Znam i akceptuję ' + 'Znam i akceptuję ' 'Regulamin Konkursu.'), ) agree_data = forms.BooleanField( diff --git a/src/wolnelektury/settings/apps.py b/src/wolnelektury/settings/apps.py index 7797d3a07..fa2f82f31 100644 --- a/src/wolnelektury/settings/apps.py +++ b/src/wolnelektury/settings/apps.py @@ -32,6 +32,7 @@ INSTALLED_APPS_OUR = [ 'isbn', 'paypal', 'push', + 'club.apps.ClubConfig', ] INSTALLED_APPS_CONTRIB = [ @@ -58,7 +59,7 @@ INSTALLED_APPS_CONTRIB = [ 'getpaid', 'getpaid.backends.payu', 'django_extensions', - 'club.apps.ClubConfig', + 'forms_builder.forms', 'debug_toolbar', diff --git a/src/wolnelektury/settings/contrib.py b/src/wolnelektury/settings/contrib.py index 66c876270..ce0882192 100644 --- a/src/wolnelektury/settings/contrib.py +++ b/src/wolnelektury/settings/contrib.py @@ -51,3 +51,14 @@ HAYSTACK_CONNECTIONS = { 'ENGINE': 'haystack.backends.simple_backend.SimpleEngine', }, } + + +FORMS_BUILDER_USE_SITES = False +FORMS_BUILDER_EDITABLE_FIELD_MAX_LENGTH = True +FORMS_BUILDER_EDITABLE_SLUGS = True +FORMS_BUILDER_EXTRA_FIELDS = [ + (100, 'contact.fields.HeaderField', 'Header'), + (101, 'contact.fields.SeparatorField', 'Separator'), +] +FORMS_BUILDER_HELPTEXT_MAX_LENGTH = 2048 +FORMS_BUILDER_REQUIRED_CSS_CLASS = 'required' diff --git a/src/wolnelektury/templates/forms/form_detail.html b/src/wolnelektury/templates/forms/form_detail.html new file mode 100644 index 000000000..519681bae --- /dev/null +++ b/src/wolnelektury/templates/forms/form_detail.html @@ -0,0 +1,8 @@ +{% extends form.base_template|default:"base/base.html" %} +{% load forms_builder_tags %} + +{% block title %}{{ form.title }}{% endblock %} + +{% block body %} + {% render_built_form form %} +{% endblock %} diff --git a/src/wolnelektury/templates/forms/form_sent.html b/src/wolnelektury/templates/forms/form_sent.html new file mode 100644 index 000000000..dbed3ce74 --- /dev/null +++ b/src/wolnelektury/templates/forms/form_sent.html @@ -0,0 +1,9 @@ +{% extends form.base_template|default:"base/base.html" %} +{% load forms_builder_tags %} + +{% block title %}{{ form.title }}{% endblock %} + +{% block body %} +

{{ form.title }}

+

{{ form.response }}

+{% endblock %} diff --git a/src/wolnelektury/templates/forms/includes/built_form.html b/src/wolnelektury/templates/forms/includes/built_form.html new file mode 100644 index 000000000..4a8807f17 --- /dev/null +++ b/src/wolnelektury/templates/forms/includes/built_form.html @@ -0,0 +1,18 @@ +{% load honeypot %} + +

{{ form.title }}

+ +
+ {{ form.intro|safe }} +
+ + {{ form_for_form.media }} +
+ {% csrf_token %} + {% render_honeypot_field %} + + {{ form_for_form.as_table }} + +
+
+ diff --git a/src/wolnelektury/urls.py b/src/wolnelektury/urls.py index 97443fa77..bb2c54ead 100644 --- a/src/wolnelektury/urls.py +++ b/src/wolnelektury/urls.py @@ -42,6 +42,7 @@ urlpatterns += [ url(r'^ankieta/', include('polls.urls')), url(r'^biblioteki/', include('libraries.urls')), url(r'^newsletter/', include('newsletter.urls')), + url(r'^formularz-new/', include('forms_builder.forms.urls')), url(r'^formularz/', include('contact.urls')), url(r'^isbn/', include('isbn.urls')), -- 2.20.1