From b477b3cfa2ce28678eb6c861b286e86ed77046ee Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 26 Jan 2017 16:16:18 +0100 Subject: [PATCH] validate Projects field in organization --- apps/organizations/forms.py | 24 +++++++++++++++ apps/organizations/models.py | 2 +- .../templates/organizations/edit.html | 29 +------------------ .../templates/organizations/new.html | 29 +------------------ .../snippets/organization_form.html | 29 +++++++++++++++++++ 5 files changed, 56 insertions(+), 57 deletions(-) create mode 100644 apps/organizations/templates/organizations/snippets/organization_form.html diff --git a/apps/organizations/forms.py b/apps/organizations/forms.py index 16a8207e..6af8c6c3 100644 --- a/apps/organizations/forms.py +++ b/apps/organizations/forms.py @@ -5,6 +5,7 @@ # 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 @@ -30,6 +31,29 @@ class OrganizationForm(forms.ModelForm): 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 diff --git a/apps/organizations/models.py b/apps/organizations/models.py index c236da42..8e04ddb7 100644 --- a/apps/organizations/models.py +++ b/apps/organizations/models.py @@ -71,7 +71,7 @@ class Card(models.Model): 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] diff --git a/apps/organizations/templates/organizations/edit.html b/apps/organizations/templates/organizations/edit.html index 68abc047..8e403923 100644 --- a/apps/organizations/templates/organizations/edit.html +++ b/apps/organizations/templates/organizations/edit.html @@ -7,34 +7,7 @@
- {% csrf_token %} - {{ form.non_field_errors }} - - {{ form.name.errors }} - - - {{ form.logo.errors }} -
- {{ form.logo }} -
- - {{ form.country.errors }} - - - {{ form.www.errors }} - - - {{ form.description.errors }} - - - {{ form.projects.errors }} - - {% trans "Enter each line as: URL language description" %}
- + {% include "organizations/snippets/organization_form.html" %}
diff --git a/apps/organizations/templates/organizations/new.html b/apps/organizations/templates/organizations/new.html index 101fa3b7..531f5d4c 100644 --- a/apps/organizations/templates/organizations/new.html +++ b/apps/organizations/templates/organizations/new.html @@ -7,34 +7,7 @@
- {% csrf_token %} - {{ form.non_field_errors }} - - {{ form.name.errors }} - - - {{ form.logo.errors }} -
- {{ form.logo }} -
- - {{ form.country.errors }} - - - {{ form.www.errors }} - - - {{ form.description.errors }} - - - {{ form.projects.errors }} - - {% trans "Enter each line as: URL language description" %}
- + {% include "organizations/snippets/organization_form.html" %}
diff --git a/apps/organizations/templates/organizations/snippets/organization_form.html b/apps/organizations/templates/organizations/snippets/organization_form.html new file mode 100644 index 00000000..8057114f --- /dev/null +++ b/apps/organizations/templates/organizations/snippets/organization_form.html @@ -0,0 +1,29 @@ +{% load i18n %} + +{% csrf_token %} +{{ form.non_field_errors }} + + {{ form.name.errors }} + + + {{ form.logo.errors }} +
+ {{ form.logo }} +
+ + {{ form.country.errors }} + + + {{ form.www.errors }} + + + {{ form.description.errors }} + + + {{ form.projects.errors }} + +{% trans "Enter each line as: Internet address, language, description, separated with spaces" %}
\ No newline at end of file -- 2.20.1