validate projects on user card too
authorJan Szejko <janek37@gmail.com>
Thu, 26 Jan 2017 15:28:07 +0000 (16:28 +0100)
committerJan Szejko <janek37@gmail.com>
Thu, 26 Jan 2017 15:28:07 +0000 (16:28 +0100)
apps/organizations/forms.py
apps/organizations/templates/organizations/edit_user.html

index 6af8c6c..e7e3250 100644 (file)
@@ -11,6 +11,29 @@ 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 = 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
 
@@ -32,27 +55,7 @@ MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_u
         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)
+        return clean_projects(self.cleaned_data.get('projects', ''))
 
 
 class UserCardForm(forms.ModelForm):
@@ -78,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', ''))
index 97d413f..358035d 100644 (file)
@@ -38,7 +38,7 @@
         <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/>
+           {% trans "Enter each line as: Internet address, language, description, separated with spaces" %}<br/>
 
         <button style="margin-top:1em;" class="btn btn-default" type="submit">{% trans "Change" %}</button></td></tr>
     </form>