notification for new publications
[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         organization = super(OrganizationForm, self).save(commit=commit)
22         site = Site.objects.get_current()
23         send_notify_email(
24             'New organization in MIL/PEER',
25             '''New organization in MIL/PEER: %s. View their profile: https://%s%s.
26
27 --
28 MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url()))
29         return organization
30
31
32 class UserCardForm(forms.ModelForm):
33     cts = countries
34
35     first_name = forms.CharField(required=False)
36     last_name = forms.CharField(required=False)
37
38     class Meta:
39         model = UserCard
40         exclude = ['_html', 'user']
41
42     def __init__(self, *args, **kwargs):
43         if 'instance' in kwargs:
44             kwargs['initial'] = {
45                 'first_name': kwargs['instance'].user.first_name,
46                 'last_name': kwargs['instance'].user.last_name,
47             }
48         super(UserCardForm, self).__init__(*args, **kwargs)
49
50     def save(self, *args, **kwargs):
51         self.instance.user.first_name = self.cleaned_data.get('first_name', '')
52         self.instance.user.last_name = self.cleaned_data.get('last_name', '')
53         self.instance.user.save()
54         return super(UserCardForm, self).save(*args, **kwargs)