typo
[redakcja.git] / apps / organizations / forms.py
1 # -*- coding: utf-8 -*-
2 #
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.
5 #
6 from django import forms
7 from django.contrib.sites.models import Site
8 from django.utils.translation import ugettext as _
9
10 from redakcja.utils import send_notify_email
11 from .models import Organization, UserCard, countries
12
13
14 def clean_projects(projects):
15     lines = []
16     for line in projects.split('\n'):
17         line = line.strip()
18         if line:
19             try:
20                 url, lang, desc = [part.strip(',') for part in line.split(None, 2)]
21             except ValueError:
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))
25             # naive check
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'):
30                 url = 'http://' + url
31             lines.append(' '.join((url, lang, desc)))
32         else:
33             lines.append('')
34     return '\n'.join(lines)
35
36
37 class OrganizationForm(forms.ModelForm):
38     cts = countries
39
40     class Meta:
41         model = Organization
42         exclude = ['_html']
43
44     def save(self, commit=True):
45         new = self.instance.id is None
46         organization = super(OrganizationForm, self).save(commit=commit)
47         if new:
48             site = Site.objects.get_current()
49             send_notify_email(
50                 'New organization in MIL/PEER',
51                 '''New organization in MIL/PEER: %s. View their profile: https://%s%s.
52
53 --
54 MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url()))
55         return organization
56
57     def clean_projects(self):
58         return clean_projects(self.cleaned_data.get('projects', ''))
59
60
61 class UserCardForm(forms.ModelForm):
62     cts = countries
63
64     first_name = forms.CharField(required=False)
65     last_name = forms.CharField(required=False)
66
67     class Meta:
68         model = UserCard
69         exclude = ['_html', 'user']
70
71     def __init__(self, *args, **kwargs):
72         if 'instance' in kwargs:
73             kwargs['initial'] = {
74                 'first_name': kwargs['instance'].user.first_name,
75                 'last_name': kwargs['instance'].user.last_name,
76             }
77         super(UserCardForm, self).__init__(*args, **kwargs)
78
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)
84
85     def clean_projects(self):
86         return clean_projects(self.cleaned_data.get('projects', ''))