validate Projects field in organization
authorJan Szejko <janek37@gmail.com>
Thu, 26 Jan 2017 15:16:18 +0000 (16:16 +0100)
committerJan Szejko <janek37@gmail.com>
Thu, 26 Jan 2017 15:16:18 +0000 (16:16 +0100)
apps/organizations/forms.py
apps/organizations/models.py
apps/organizations/templates/organizations/edit.html
apps/organizations/templates/organizations/new.html
apps/organizations/templates/organizations/snippets/organization_form.html [new file with mode: 0644]

index 16a8207..6af8c6c 100644 (file)
@@ -5,6 +5,7 @@
 #
 from django import forms
 from django.contrib.sites.models import Site
 #
 from django import forms
 from django.contrib.sites.models import Site
+from django.utils.translation import ugettext as _
 
 from redakcja.utlis import send_notify_email
 from .models import Organization, UserCard, countries
 
 from redakcja.utlis import send_notify_email
 from .models import Organization, UserCard, countries
@@ -30,6 +31,29 @@ class OrganizationForm(forms.ModelForm):
 MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url()))
         return organization
 
 MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url()))
         return organization
 
+    def clean_projects(self):
+        projects = self.cleaned_data.get('projects', '')
+        lines = []
+        for line in projects.split('\n'):
+            line = line.strip()
+            if line:
+                try:
+                    url, lang, desc = line.split(None, 2)
+                except ValueError:
+                    raise forms.ValidationError(
+                        _('Each line has to consist of an Internet address, language and description, '
+                          'separated with spaces. Failed on: %s' % line))
+                # naive check
+                if '.' not in url or url.endswith('.'):
+                    raise forms.ValidationError(
+                        _('The first item in each line should be an Internet address. Failed on: %s') % url)
+                if not url.startswith('http'):
+                    url = 'http://' + url
+                lines.append(' '.join((url, lang, desc)))
+            else:
+                lines.append('')
+        return '\n'.join(lines)
+
 
 class UserCardForm(forms.ModelForm):
     cts = countries
 
 class UserCardForm(forms.ModelForm):
     cts = countries
index c236da4..8e04ddb 100644 (file)
@@ -71,7 +71,7 @@ class Card(models.Model):
 
     def get_projects(self):
         for project_line in self.projects.strip().split('\n'):
 
     def get_projects(self):
         for project_line in self.projects.strip().split('\n'):
-            parts = project_line.strip().split(' ', 2)
+            parts = project_line.strip().split(None, 2)
             if not parts or not parts[0]:
                 continue
             url, lang, desc = (parts + [''] * 2)[:3]
             if not parts or not parts[0]:
                 continue
             url, lang, desc = (parts + [''] * 2)[:3]
index 68abc04..8e40392 100644 (file)
@@ -7,34 +7,7 @@
 
 
     <form enctype="multipart/form-data" method="POST">
 
 
     <form enctype="multipart/form-data" method="POST">
-    {% csrf_token %}
-        {{ form.non_field_errors }}
-        <label for="title">{% trans "Name" %}</label>
-            {{ form.name.errors }}
-            <input class="form-control" name="name" type="text" value="{{ form.name.value|default:"" }}">
-        <label for="logo">{% trans "Logo" %}</label>
-            {{ form.logo.errors }}
-            <div>
-            {{ form.logo }}
-            </div>
-        <label for="country">{% trans "Country" %}</label>
-            {{ form.country.errors }}
-            <select class="form-control" name="country">
-                {% for c, t in form.cts %}
-                    <option value='{{ c }}' {% if form.country.value == c %}selected="selected"{% endif %}>{{ t }}</option>
-                {% endfor %}
-            </select>
-        <label for="www">{% trans "WWW" %}</label>
-            {{ form.www.errors }}
-            <input class="form-control" name="www" type="www" value="{{ form.www.value|default:"" }}">
-        <label for="title">{% trans "Description" %}</label>
-            {{ form.description.errors }}
-            <textarea class="form-control" name="description">{{ form.description.value|default:"" }}</textarea>
-        <label for="title">{% trans "Projects" %}</label>
-            {{ form.projects.errors }}
-            <textarea class="form-control" name="projects">{{ form.projects.value|default:"" }}</textarea>
-           {% trans "Enter each line as: URL language description" %}<br/>
-
+        {% include "organizations/snippets/organization_form.html" %}
         <button style="margin-top:1em;" class="btn btn-default" type="submit">{% trans "Change" %}</button></td></tr>
     </form>
 
         <button style="margin-top:1em;" class="btn btn-default" type="submit">{% trans "Change" %}</button></td></tr>
     </form>
 
index 101fa3b..531f5d4 100644 (file)
@@ -7,34 +7,7 @@
 
 
     <form enctype="multipart/form-data" method="POST">
 
 
     <form enctype="multipart/form-data" method="POST">
-    {% csrf_token %}
-        {{ form.non_field_errors }}
-        <label for="title">{% trans "Name" %}</label>
-            {{ form.name.errors }}
-            <input class="form-control" name="name" type="text" value="{{ form.name.value|default:"" }}">
-        <label for="logo">{% trans "Logo" %}</label>
-            {{ form.logo.errors }}
-            <div>
-            {{ form.logo }}
-            </div>
-        <label for="country">{% trans "Country" %}</label>
-            {{ form.country.errors }}
-            <select class="form-control" name="country">
-                {% for c, t in form.cts %}
-                    <option value='{{ c }}' {% if form.country.value == c %}selected="selected"{% endif %}>{{ t }}</option>
-                {% endfor %}
-            </select>
-        <label for="www">{% trans "WWW" %}</label>
-            {{ form.www.errors }}
-            <input class="form-control" name="www" type="www" value="{{ form.www.value|default:"" }}">
-        <label for="title">{% trans "Description" %}</label>
-            {{ form.description.errors }}
-            <textarea class="form-control" name="description">{{ form.description.value|default:"" }}</textarea>
-        <label for="title">{% trans "Projects" %}</label>
-            {{ form.projects.errors }}
-            <textarea class="form-control" name="projects">{{ form.projects.value|default:"" }}</textarea>
-           {% trans "Enter each line as: URL language description" %}<br/>
-
+        {% include "organizations/snippets/organization_form.html" %}
         <button style="margin-top:1em;" class="btn btn-default" type="submit">{% trans "Create organization" %}</button></td></tr>
     </form>
 
         <button style="margin-top:1em;" class="btn btn-default" type="submit">{% trans "Create organization" %}</button></td></tr>
     </form>
 
diff --git a/apps/organizations/templates/organizations/snippets/organization_form.html b/apps/organizations/templates/organizations/snippets/organization_form.html
new file mode 100644 (file)
index 0000000..8057114
--- /dev/null
@@ -0,0 +1,29 @@
+{% load i18n %}
+
+{% csrf_token %}
+{{ form.non_field_errors }}
+<label for="title">{% trans "Name" %}</label>
+    {{ form.name.errors }}
+    <input class="form-control" name="name" type="text" value="{{ form.name.value|default:"" }}">
+<label for="logo">{% trans "Logo" %}</label>
+    {{ form.logo.errors }}
+    <div>
+    {{ form.logo }}
+    </div>
+<label for="country">{% trans "Country" %}</label>
+    {{ form.country.errors }}
+    <select class="form-control" name="country">
+        {% for c, t in form.cts %}
+            <option value='{{ c }}' {% if form.country.value == c %}selected="selected"{% endif %}>{{ t }}</option>
+        {% endfor %}
+    </select>
+<label for="www">{% trans "WWW" %}</label>
+    {{ form.www.errors }}
+    <input class="form-control" name="www" type="www" value="{{ form.www.value|default:"" }}">
+<label for="title">{% trans "Description" %}</label>
+    {{ form.description.errors }}
+    <textarea class="form-control" name="description">{{ form.description.value|default:"" }}</textarea>
+<label for="title">{% trans "Projects" %}</label>
+    {{ form.projects.errors }}
+    <textarea class="form-control" name="projects">{{ form.projects.value|default:"" }}</textarea>
+{% trans "Enter each line as: Internet address, language, description, separated with spaces" %}<br/>
\ No newline at end of file