X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/02d8ce2efb84683b087b102087d125d5b3d293ca..2c85a46e149380dccf5a69694402bd2519e80203:/apps/organizations/forms.py diff --git a/apps/organizations/forms.py b/apps/organizations/forms.py index 16a8207e..98efeab0 100644 --- a/apps/organizations/forms.py +++ b/apps/organizations/forms.py @@ -5,11 +5,35 @@ # 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 +def clean_projects(projects): + lines = [] + for line in projects.split('\n'): + line = line.strip() + if line: + try: + url, lang, desc = [part.strip(',') for part in 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 OrganizationForm(forms.ModelForm): cts = countries @@ -30,6 +54,9 @@ class OrganizationForm(forms.ModelForm): MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url())) return organization + def clean_projects(self): + return clean_projects(self.cleaned_data.get('projects', '')) + class UserCardForm(forms.ModelForm): cts = countries @@ -54,3 +81,6 @@ class UserCardForm(forms.ModelForm): self.instance.user.last_name = self.cleaned_data.get('last_name', '') self.instance.user.save() return super(UserCardForm, self).save(*args, **kwargs) + + def clean_projects(self): + return clean_projects(self.cleaned_data.get('projects', ''))