16a8207ee1aec0fd8482e7a45eb3218e3040c6fe
[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
9 from redakcja.utlis import send_notify_email
10 from .models import Organization, UserCard, countries
11
12
13 class OrganizationForm(forms.ModelForm):
14     cts = countries
15
16     class Meta:
17         model = Organization
18         exclude = ['_html']
19
20     def save(self, commit=True):
21         new = self.instance.id is None
22         organization = super(OrganizationForm, self).save(commit=commit)
23         if new:
24             site = Site.objects.get_current()
25             send_notify_email(
26                 'New organization in MIL/PEER',
27                 '''New organization in MIL/PEER: %s. View their profile: https://%s%s.
28
29 --
30 MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url()))
31         return organization
32
33
34 class UserCardForm(forms.ModelForm):
35     cts = countries
36
37     first_name = forms.CharField(required=False)
38     last_name = forms.CharField(required=False)
39
40     class Meta:
41         model = UserCard
42         exclude = ['_html', 'user']
43
44     def __init__(self, *args, **kwargs):
45         if 'instance' in kwargs:
46             kwargs['initial'] = {
47                 'first_name': kwargs['instance'].user.first_name,
48                 'last_name': kwargs['instance'].user.last_name,
49             }
50         super(UserCardForm, self).__init__(*args, **kwargs)
51
52     def save(self, *args, **kwargs):
53         self.instance.user.first_name = self.cleaned_data.get('first_name', '')
54         self.instance.user.last_name = self.cleaned_data.get('last_name', '')
55         self.instance.user.save()
56         return super(UserCardForm, self).save(*args, **kwargs)