1 # -*- coding: utf-8 -*-
3 # This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from django import forms
7 from django.contrib.sites.models import Site
8 from django.utils.translation import ugettext as _
10 from redakcja.utlis import send_notify_email
11 from .models import Organization, UserCard, countries
14 def clean_projects(projects):
16 for line in projects.split('\n'):
20 url, lang, desc = [part.strip(',') for part in line.split(None, 2)]
22 raise forms.ValidationError(
23 _('Each line has to consist of an Internet address, language and description, '
24 'separated with spaces. Failed on: %s' % line))
26 if '.' not in url or url.endswith('.'):
27 raise forms.ValidationError(
28 _('The first item in each line should be an Internet address. Failed on: %s') % url)
29 if not url.startswith('http'):
31 lines.append(' '.join((url, lang, desc)))
34 return '\n'.join(lines)
37 class OrganizationForm(forms.ModelForm):
44 def save(self, commit=True):
45 new = self.instance.id is None
46 organization = super(OrganizationForm, self).save(commit=commit)
48 site = Site.objects.get_current()
50 'New organization in MIL/PEER',
51 '''New organization in MIL/PEER: %s. View their profile: https://%s%s.
54 MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url()))
57 def clean_projects(self):
58 return clean_projects(self.cleaned_data.get('projects', ''))
61 class UserCardForm(forms.ModelForm):
64 first_name = forms.CharField(required=False)
65 last_name = forms.CharField(required=False)
69 exclude = ['_html', 'user']
71 def __init__(self, *args, **kwargs):
72 if 'instance' in kwargs:
74 'first_name': kwargs['instance'].user.first_name,
75 'last_name': kwargs['instance'].user.last_name,
77 super(UserCardForm, self).__init__(*args, **kwargs)
79 def save(self, *args, **kwargs):
80 self.instance.user.first_name = self.cleaned_data.get('first_name', '')
81 self.instance.user.last_name = self.cleaned_data.get('last_name', '')
82 self.instance.user.save()
83 return super(UserCardForm, self).save(*args, **kwargs)
85 def clean_projects(self):
86 return clean_projects(self.cleaned_data.get('projects', ''))