newsletter checkboxes in suggestions and registration
authorJan Szejko <jan.szejko@gmail.com>
Thu, 28 Jul 2016 12:19:28 +0000 (14:19 +0200)
committerJan Szejko <janek37@gmail.com>
Wed, 14 Sep 2016 10:47:47 +0000 (12:47 +0200)
17 files changed:
src/ajaxable/templates/ajaxable/form.html
src/ajaxable/templatetags/ajaxable_tags.py
src/ajaxable/utils.py
src/catalogue/forms.py
src/catalogue/tasks.py
src/catalogue/templates/catalogue/custom_pdf_form.html [new file with mode: 0644]
src/catalogue/templatetags/catalogue_tags.py
src/catalogue/views.py
src/newsletter/forms.py
src/suggest/forms.py
src/suggest/templates/publishing_suggest.html
src/suggest/templates/suggest.html [new file with mode: 0644]
src/suggest/views.py
src/wolnelektury/forms.py [new file with mode: 0644]
src/wolnelektury/static/scss/main/dialogs.scss
src/wolnelektury/templates/auth/register.html
src/wolnelektury/views.py

index af98d51..3363bc8 100755 (executable)
@@ -3,7 +3,7 @@
 
 <h1>{{ title }}</h1>
 
-<form action="{{ request.get_full_path }}" method="post" accept-charset="utf-8"
+<form action="{{ action }}" method="post" accept-charset="utf-8"
       class="cuteform{% if placeholdize %} hidelabels{% endif %}">
   {% ssi_csrf_token %}
   {% if honeypot %}
@@ -12,7 +12,9 @@
   {% endif %}
   <ol>
     <div id="id_{% if form_prefix %}{{ form_prefix }}-{% endif %}__all__"></div>
-    {{ form.as_ul }}
+    {% block form_fields %}
+      {{ form.as_ul }}
+    {% endblock %}
     <li><input type="submit" value="{{ submit }}"/></li>
   </ol>
 </form>
index 351e9f1..c262a92 100644 (file)
@@ -3,6 +3,8 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django import template
+from django.utils.safestring import mark_safe
+
 from ajaxable.utils import placeholdized
 register = template.Library()
 
@@ -15,3 +17,23 @@ def placeholdize(form):
 @register.filter
 def placeholdized_ul(form):
     return placeholdized(form).as_ul()
+
+
+@register.filter
+def pretty_field(field, template=None):
+    if template is None:
+        template = '''
+            <li>
+              <span class="error">%(errors)s</span>
+              <label class="nohide"><span class="label">%(label)s: </span>%(input)s</label>
+            </li>'''
+    return mark_safe(template % {'errors': field.errors, 'input': field, 'label': field.label})
+
+
+@register.filter
+def pretty_checkbox(field):
+    return pretty_field(field, template='''
+        <li class="checkbox">
+          <span class="error">%(errors)s</span>
+          <label class="nohide">%(input)s<span class="label"> %(label)s</span></label>
+        </li>''')
index c2c3688..9fd0091 100755 (executable)
@@ -63,6 +63,7 @@ class AjaxableFormView(object):
     # override to customize form look
     template = "ajaxable/form.html"
     submit = _('Send')
+    action = ''
 
     title = ''
     success_message = ''
@@ -140,6 +141,7 @@ class AjaxableFormView(object):
                 "honeypot": self.honeypot,
                 "placeholdize": self.placeholdize,
                 "submit": self.submit,
+                "action": self.action,
                 "response_data": response_data,
                 "ajax_template": self.template,
                 "view_args": args,
index c547279..c1348a1 100644 (file)
@@ -47,7 +47,7 @@ CUSTOMIZATION_FLAGS = (
     ('nofootnotes', _("Don't show footnotes")),
     ('nothemes', _("Don't disply themes")),
     ('nowlfont', _("Don't use our custom font")),
-    ('no-cover', _("Without cover")),
+    ('nocover', _("Without cover")),
     )
 CUSTOMIZATION_OPTIONS = (
     ('leading', _("Leading"), (
index aff0e6b..30bc55f 100644 (file)
@@ -48,9 +48,9 @@ def build_custom_pdf(book_id, customizations, file_name, waiter_id=None):
             kwargs = {
                 'cover': True,
             }
-            if 'no-cover' in customizations:
+            if 'nocover' in customizations:
                 kwargs['cover'] = False
-                customizations.remove('no-cover')
+                customizations.remove('nocover')
             wldoc = Book.objects.get(pk=book_id).wldocument()
             pdf = wldoc.as_pdf(
                 customizations=customizations,
diff --git a/src/catalogue/templates/catalogue/custom_pdf_form.html b/src/catalogue/templates/catalogue/custom_pdf_form.html
new file mode 100644 (file)
index 0000000..3abc22d
--- /dev/null
@@ -0,0 +1,21 @@
+{% load i18n %}
+{% load honeypot %}
+{% load ssi_csrf_token from ssify %}
+{% load ajaxable_tags %}
+
+<h1>{% trans "Download custom PDF" %}</h1>
+
+<form id='custom-pdf-form' action="" method="post" accept-charset="utf-8" class="cuteform">
+  {% ssi_csrf_token %}
+  {% render_honeypot_field %}
+  <ol>
+    {{ form.nofootnotes|pretty_checkbox }}
+    {{ form.nothemes|pretty_checkbox }}
+    {{ form.nowlfont|pretty_checkbox }}
+    {{ form.nocover|pretty_checkbox }}
+    {{ form.leading|pretty_field }}
+    {{ form.fontsize|pretty_field }}
+
+    <li><input type="submit" value="{% trans "Download" %}"/></li>
+  </ol>
+</form>
index c02a5d7..afb2e7e 100644 (file)
@@ -10,7 +10,6 @@ from django.conf import settings
 from django import template
 from django.template import Node, Variable, Template, Context
 from django.core.urlresolvers import reverse
-from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
 from django.utils.cache import add_never_cache_headers
 from django.utils.translation import ugettext as _
 
@@ -24,22 +23,6 @@ from picture.models import Picture
 register = template.Library()
 
 
-class RegistrationForm(UserCreationForm):
-    def as_ul(self):
-        """Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."""
-        return self._html_output(
-            u'<li>%(errors)s%(label)s %(field)s<span class="help-text">%(help_text)s</span></li>', u'<li>%s</li>',
-            '</li>', u' %s', False)
-
-
-class LoginForm(AuthenticationForm):
-    def as_ul(self):
-        """Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."""
-        return self._html_output(
-            u'<li>%(errors)s%(label)s %(field)s<span class="help-text">%(help_text)s</span></li>', u'<li>%s</li>',
-            '</li>', u' %s', False)
-
-
 def iterable(obj):
     try:
         iter(obj)
@@ -228,16 +211,6 @@ def all_editors(extra_info):
                      for person in sorted(set(editors)))
 
 
-@register.simple_tag
-def user_creation_form():
-    return RegistrationForm(prefix='registration').as_ul()
-
-
-@register.simple_tag
-def authentication_form():
-    return LoginForm(prefix='login').as_ul()
-
-
 @register.tag
 def catalogue_url(parser, token):
     bits = token.split_contents()
index 44a17b4..99e3c82 100644 (file)
@@ -628,6 +628,7 @@ class CustomPDFFormView(AjaxableFormView):
     form_class = forms.CustomPDFForm
     title = ugettext_lazy('Download custom PDF')
     submit = ugettext_lazy('Download')
+    template = 'catalogue/custom_pdf_form.html'
     honeypot = True
 
     def __call__(self, *args, **kwargs):
index f8927e5..d9f931b 100644 (file)
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
-
+from django.core.exceptions import ValidationError
+from django.core.validators import validate_email
 from django.forms import Form, BooleanField
 from django.utils.translation import ugettext_lazy as _
 
@@ -8,7 +9,8 @@ from newsletter.models import Subscription
 
 class NewsletterForm(Form):
     email_field = 'email'
-    agree_newsletter = BooleanField(required=False, label=_(u'I want to receive Wolne Lektury\'s newsletter.'))
+    agree_newsletter = BooleanField(
+        required=False, initial=True, label=_(u'I want to receive Wolne Lektury\'s newsletter.'))
 
     def save(self):
         try:
@@ -16,9 +18,16 @@ class NewsletterForm(Form):
             super(NewsletterForm, self).save()
         except AttributeError:
             pass
+        if not self.cleaned_data.get('agree_newsletter'):
+            return
         email = self.cleaned_data[self.email_field]
-        subscription, created = Subscription.objects.get_or_create(email=email)
-        if not created and not subscription.active:
-            subscription.active = True
-            subscription.save()
-        # Send some test email?
+        try:
+            validate_email(email)
+        except ValidationError:
+            pass
+        else:
+            subscription, created = Subscription.objects.get_or_create(email=email)
+            if not created and not subscription.active:
+                subscription.active = True
+                subscription.save()
+            # Send some test email?
index 57e5eee..4521805 100644 (file)
@@ -10,14 +10,18 @@ from django.core.urlresolvers import reverse
 from django.core.validators import validate_email
 from django.utils.translation import ugettext_lazy as _
 from django.utils.translation import ugettext
+
+from newsletter.forms import NewsletterForm
 from suggest.models import PublishingSuggestion, Suggestion
 
 
-class SuggestForm(forms.Form):
+class SuggestForm(NewsletterForm):
+    email_field = 'contact'
     contact = forms.CharField(label=_('Contact'), max_length=120, required=False)
     description = forms.CharField(label=_('Description'), widget=forms.Textarea, required=True)
 
     def save(self, request):
+        super(SuggestForm, self).save()
         contact = self.cleaned_data['contact']
         description = self.cleaned_data['description']
 
@@ -54,7 +58,8 @@ The suggestion has been referred to the project coordinator.""") +
                       'no-reply@wolnelektury.pl', [contact], fail_silently=True)
 
 
-class PublishingSuggestForm(forms.Form):
+class PublishingSuggestForm(NewsletterForm):
+    email_field = 'contact'
     contact = forms.CharField(label=_('Contact'), max_length=120, required=False)
     books = forms.CharField(label=_('books'), widget=forms.Textarea, required=True)
     ebook = forms.BooleanField(label=_('ebook'), required=False, initial=True)
@@ -68,6 +73,7 @@ class PublishingSuggestForm(forms.Form):
         return super(PublishingSuggestForm, self).clean()
 
     def save(self, request):
+        super(PublishingSuggestForm, self).save()
         contact = self.cleaned_data['contact']
         suggestion_text = self.cleaned_data['books'].strip(', \n\r')
 
index 16c70a4..66792c7 100755 (executable)
@@ -1,33 +1,16 @@
+{% extends "ajaxable/form.html" %}
 {% load i18n %}
-{% load honeypot %}
-{% load ssi_csrf_token from ssify %}
+{% load ajaxable_tags %}
 
-<h1>{% trans "Didn't find a book? Make a suggestion." %}</h1>
+{% block form_fields %}
+{{ form.contact|pretty_field }}
+  <li>{% trans "I'd like to find in WolneLektury.pl these…" %}</li>
+  {{ form.books|pretty_field }}
+  {{ form.ebook|pretty_checkbox }}
+  {{ form.audiobook|pretty_checkbox }}
+  {{ form.agree_newsletter|pretty_checkbox }}
+{% endblock %}
 
-<form id='suggest-publishing-form' action="{% url 'suggest_publishing' %}" method="post" accept-charset="utf-8" class="cuteform">
-  {% ssi_csrf_token %}
-  {% render_honeypot_field %}
-  <ol>
-    <li>
-      <span class="error">{{ form.contact.errors }}</span>
-      <label for="id_contact">{{ form.contact.label }}</label>
-      {{ form.contact }}
-    </li>
-
-    <li>{% trans "I'd like to find in WolneLektury.pl these…" %}</li>
-
-    <li><label for="id_books">{{ form.books.label }}:</label> {{ form.books }}</li>
-
-    <li class="checkbox">
-      <span class="error">{{ form.ebook.errors }}</span>
-      <label for="id_ebook">{{ form.ebook.label }}:</label>
-      {{ form.ebook }}
-    </li>
-    <li class="checkbox">
-      <span class="error">{{ form.audiobook.errors }}</span>
-      <label for="id_audiobook">{{ form.audiobook.label }}:</label>
-      {{ form.audiobook }}
-    </li>
 
     <li><input type="submit" value="{% trans "Send report" %}"/></li>
     <li>{% trans "Remember that we can only publish books in public domain, ie. 70 years after the death of the author!" %}</li>
diff --git a/src/suggest/templates/suggest.html b/src/suggest/templates/suggest.html
new file mode 100644 (file)
index 0000000..1166706
--- /dev/null
@@ -0,0 +1,8 @@
+{% extends "ajaxable/form.html" %}
+{% load ajaxable_tags %}
+
+{% block form_fields %}
+  {{ form.contact|pretty_field }}
+  {{ form.description|pretty_field }}
+  {{ form.agree_newsletter|pretty_checkbox }}
+{% endblock %}
\ No newline at end of file
index 035074d..706c549 100644 (file)
@@ -2,6 +2,7 @@
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
+from django.core.urlresolvers import reverse_lazy
 from django.utils.translation import ugettext_lazy as _
 
 from ajaxable.utils import AjaxableFormView
@@ -10,15 +11,19 @@ from suggest import forms
 
 class PublishingSuggestionFormView(AjaxableFormView):
     form_class = forms.PublishingSuggestForm
-    title = _('Report a bug or suggestion')
+    title = _("Didn't find a book? Make a suggestion.")
     template = "publishing_suggest.html"
+    submit = _('Send report')
     success_message = _('Report was sent successfully.')
     honeypot = True
+    action = reverse_lazy('suggest_publishing')
 
 
 class SuggestionFormView(AjaxableFormView):
     form_class = forms.SuggestForm
     title = _('Report a bug or suggestion')
+    template = "suggest.html"
     submit = _('Send report')
     success_message = _('Report was sent successfully.')
     honeypot = True
+    action = reverse_lazy('suggest')
diff --git a/src/wolnelektury/forms.py b/src/wolnelektury/forms.py
new file mode 100644 (file)
index 0000000..0973e7d
--- /dev/null
@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+from django.contrib.auth.forms import UserCreationForm
+from django.contrib.auth.models import User
+
+from newsletter.forms import NewsletterForm
+
+
+# has to be this order, because otherwise the form is lacking fields
+class RegistrationForm(UserCreationForm, NewsletterForm):
+    class Meta:
+        model = User
+        fields = ('username', 'email')
+
+    def save(self, commit=True):
+        super(RegistrationForm, self).save(commit=commit)
+        NewsletterForm.save(self)
index 5825979..6e5c90a 100755 (executable)
 
     .checkbox {
         label {
-            display: inline;
+            display: block;
+            padding-left: 15px;
+            text-indent: -15px;
         }
 
         input {
-            width: auto;
+            width: 13px;
+            height: 13px;
+            padding: 0;
+            margin:0;
+            vertical-align: bottom;
+            position: relative;
+            top: -1px;
+            *overflow: hidden;
         }
     }
 }
 
 
 
-.hidelabels label {
+.hidelabels label, .hidelabels .label {
     @include hidden-label;
 }
 
+.hidelabels label.nohide {
+    width: auto;
+    height: auto;
+}
+
+.hidelabels .checkbox .label {
+    display: inline;
+}
+
 
 @media screen and (min-width: 30em) {
     #login-window {
index 3252cae..fef1f19 100644 (file)
@@ -1,9 +1,20 @@
 {% extends "ajaxable/form.html" %}
 {% load i18n %}
+{% load ajaxable_tags %}
+
+
+{% block form_fields %}
+  {{ form.username|pretty_field }}
+  {{ form.email|pretty_field }}
+  {{ form.password1|pretty_field }}
+  {{ form.password2|pretty_field }}
+  {{ form.agree_newsletter|pretty_checkbox }}
+{% endblock %}
 
 
 {% block extra %}
 
+{% comment %}
 <h1>{% trans "Sign in using:" %}</h1>
 
 <ul class="socialaccount_providers">
@@ -11,5 +22,6 @@
 </ul>
 
 {% include "socialaccount/snippets/login_extra.html" %}
+{% endcomment %}
 
 {% endblock %}
index 265f766..7d6ed2b 100644 (file)
@@ -22,6 +22,7 @@ from catalogue.models import Book, Collection, Tag, Fragment
 from ssify import ssi_included
 
 from social.utils import get_or_choose_cite
+from wolnelektury.forms import RegistrationForm
 
 
 def main_page(request):
@@ -83,7 +84,7 @@ class LoginFormView(AjaxableFormView):
 
 
 class RegisterFormView(AjaxableFormView):
-    form_class = UserCreationForm
+    form_class = RegistrationForm
     template = "auth/register.html"
     placeholdize = True
     title = _('Register')