298da4893d724422f2fafe5c06bfa35eb622fc98
[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 constance import config
8 from django.contrib.sites.models import Site
9 from django.core.mail import send_mail
10
11 from .models import Organization, UserCard, countries
12
13
14 class OrganizationForm(forms.ModelForm):
15     cts = countries
16
17     class Meta:
18         model = Organization
19         exclude = ['_html']
20
21     def save(self, commit=True):
22         organization = super(OrganizationForm, self).save(commit=commit)
23         site = Site.objects.get_current()
24         send_mail(
25             'New organization in MIL/PEER',
26             '''New organization in MIL/PEER: %s. View their profile: https://%s%s.
27
28 --
29 MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url()),
30             'milpeer@mdrn.pl', [config.NOTIFY_EMAIL])
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)