Simplify forms
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Thu, 11 Oct 2012 11:06:30 +0000 (13:06 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Thu, 11 Oct 2012 11:06:30 +0000 (13:06 +0200)
chunks/templatetags/chunks.py
contact/forms.py
contact/templates/contact/form.html
prawokultury/contact_forms.py
prawokultury/locale/pl/LC_MESSAGES/django.mo
prawokultury/locale/pl/LC_MESSAGES/django.po
prawokultury/templates/contact/register/form.html [deleted file]

index ac762ad..fd24b02 100644 (file)
@@ -7,45 +7,23 @@ from ..models import Chunk, Attachment
 register = template.Library()
 
 
-def do_get_chunk(parser, token):
-    # split_contents() knows not to split quoted strings.
-    tokens = token.split_contents()
-    if len(tokens) < 2 or len(tokens) > 3:
-        raise template.TemplateSyntaxError, "%r tag should have either 2 or 3 arguments" % (tokens[0],)
-    if len(tokens) == 2:
-        tag_name, key = tokens
-        cache_time = 0
-    if len(tokens) == 3:
-        tag_name, key, cache_time = tokens
-    # Check to see if the key is properly double/single quoted
-    if not (key[0] == key[-1] and key[0] in ('"', "'")):
-        raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
-    # Send key without quotes and caching time
-    return ChunkNode(key[1:-1], cache_time)
-
-
-class ChunkNode(template.Node):
-    def __init__(self, key, cache_time=0):
-       self.key = key
-       self.cache_time = cache_time
-
-    def render(self, context):
-        try:
-            cache_key = 'chunk_' + self.key
-            c = cache.get(cache_key)
-            if c is None:
-                c = Chunk.objects.get(key=self.key)
-                cache.set(cache_key, c, int(self.cache_time))
-            content = c.content
-        except Chunk.DoesNotExist:
-            n = Chunk(key=self.key)
-            n.save()
-            return ''
-        return content
-
-register.tag('chunk', do_get_chunk)
+@register.simple_tag
+def chunk(key, cache_time=0):
+    try:
+        cache_key = 'chunk_' + key
+        c = cache.get(cache_key)
+        if c is None:
+            c = Chunk.objects.get(key=key)
+            cache.set(cache_key, c, int(cache_time))
+        content = c.content
+    except Chunk.DoesNotExist:
+        n = Chunk(key=key)
+        n.save()
+        return ''
+    return content
 
 
+@register.simple_tag
 def attachment(key, cache_time=0):
     try:
         cache_key = 'attachment_' + key
@@ -57,5 +35,4 @@ def attachment(key, cache_time=0):
     except Attachment.DoesNotExist:
         return ''
 
-register.simple_tag(attachment)
 
index 70a415b..4f5cf65 100644 (file)
@@ -5,6 +5,7 @@ from django.core.validators import email_re
 from django import forms
 from django.template.loader import render_to_string
 from django.template import RequestContext
+from django.utils.translation import ugettext_lazy as _
 from .models import Attachment, Contact
 
 
@@ -20,9 +21,13 @@ class ContactFormMeta(forms.Form.__metaclass__):
 class ContactForm(forms.Form):
     """Subclass and define some fields."""
     __metaclass__ = ContactFormMeta
+
+    form_tag = None
+    title = _('Contact form')
+    submit_label = _('Submit')
+
     required_css_class = 'required'
     contact = forms.CharField(max_length=128)
-    form_tag = None
 
     def save(self, request):
         body = {}
index 8c1a256..15ad117 100644 (file)
@@ -1,11 +1,12 @@
 {% extends "base.html" %}
-{% load i18n %}
+{% load i18n chunks %}
 {% block "body" %}
 
-    <h1>{% block contact_form_title %}{% trans "Contact form" %}{% endblock %}</h1>
+    <h1>{% block contact_form_title %}{{ form.title }}{% endblock %}</h1>
 
     <div class="form-info">
     {% block contact_form_description %}
+        {% chunk "contact_form "|add:form.form_tag %}
     {% endblock %}
     </div>
 
@@ -13,7 +14,7 @@
     {% csrf_token %}
     <table>
         {{ form.as_table }}
-        <tr><td></td><td><button>{% block contact_form_submit %}{% trans "Submit" %}{% endblock %}</button></td></tr>
+        <tr><td></td><td><button>{% block contact_form_submit %}{{ form.submit_label }}{% endblock %}</button></td></tr>
     </table>
     </form>
 
index 90fc529..7689269 100644 (file)
@@ -5,10 +5,14 @@ from django.utils.translation import ugettext_lazy as _
 
 class RegistrationForm(ContactForm):
     form_tag = 'register'
+    title = _('Registration form')
+
     name = forms.CharField(label=_('Name'), max_length=128)
     contact = forms.EmailField(label=_('E-mail'), max_length=128)
     organization = forms.CharField(label=_('Organization'), 
             max_length=256, required=False)
+    title = forms.CharField(label=_('Title of presentation'), 
+            max_length=256, required=False)
     presentation = forms.FileField(label=_('Presentation'),
             required=False)
     summary = forms.CharField(label=_('Summary'),
index 6a54584..13c910c 100644 (file)
Binary files a/prawokultury/locale/pl/LC_MESSAGES/django.mo and b/prawokultury/locale/pl/LC_MESSAGES/django.mo differ
index 193eb47..e86f7b0 100644 (file)
@@ -7,8 +7,8 @@ msgid ""
 msgstr ""
 "Project-Id-Version: prawokultury\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2012-10-10 12:41+0200\n"
-"PO-Revision-Date: 2012-10-10 12:42+0100\n"
+"POT-Creation-Date: 2012-10-11 12:45+0200\n"
+"PO-Revision-Date: 2012-10-11 13:03+0100\n"
 "Last-Translator: Radek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>\n"
 "Language-Team: FNP <fundacja@nowoczesnapolska.org.pl>\n"
 "Language: \n"
@@ -18,21 +18,33 @@ msgstr ""
 "Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n"
 
 #: contact_forms.py:8
+msgid "Registration form"
+msgstr "Formularz rejestracyjny"
+
+#: contact_forms.py:10
 msgid "Name"
 msgstr "Imię i nazwisko"
 
-#: contact_forms.py:9
+#: contact_forms.py:11
+msgid "E-mail"
+msgstr "E-mail"
+
+#: contact_forms.py:12
 msgid "Organization"
 msgstr "Organizacja"
 
-#: contact_forms.py:10
-msgid "Summary"
-msgstr "Podsumowanie prezentacji"
+#: contact_forms.py:14
+msgid "Title of presentation"
+msgstr "Tytuł prezentacji"
 
-#: contact_forms.py:11
+#: contact_forms.py:16
 msgid "Presentation"
 msgstr "Prezentacja"
 
+#: contact_forms.py:18
+msgid "Summary"
+msgstr "Podsumowanie prezentacji"
+
 #: menu_items.py:22
 msgid "Form"
 msgstr "Formularz"
@@ -50,18 +62,14 @@ msgstr "Strona, której szukasz, nie istnieje."
 msgid "CopyCamp"
 msgstr "CopyCamp"
 
-#: templates/base.html:41
+#: templates/base.html:42
 msgid "Search"
 msgstr "Szukaj"
 
-#: templates/base.html:84
+#: templates/base.html:85
 msgid "If not explicitly stated otherwise, all texts are licensed under the <a href='http://creativecommons.org/licenses/by-sa/3.0/'>Creative Commons Attribution-Share Alike</a> free license."
 msgstr "Jeśli nie oznaczono inaczej, wszystkie teksty są objęte wolną licencją <a href='http://creativecommons.org/licenses/by-sa/3.0/deed.pl'>Creative Commons Uznanie autorstwa – Na tych samych warunkach</a>."
 
-#: templates/contact/register/form.html:4
-msgid "Registration form"
-msgstr "Formularz rejestracyjny"
-
 #~ msgid "events"
 #~ msgstr "wydarzenia"
 
diff --git a/prawokultury/templates/contact/register/form.html b/prawokultury/templates/contact/register/form.html
deleted file mode 100755 (executable)
index 8e6a807..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-{% extends "contact/form.html" %}
-{% load i18n %}
-{% load chunks %}
-
-{% block contact_form_title %}{% trans "Registration form" %}{% endblock %}
-
-{% block contact_form_description %}
-    {% chunk "formularz rejestracyjny" %}
-{% endblock %}