Form fix
[prawokultury.git] / prawokultury / contact_forms.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 from django.conf import settings
5 from django import forms
6 from django.core.mail import send_mail
7
8 from contact.forms import ContactForm
9 from contact.models import Contact
10 from contact.fields import HeaderField
11 from django.utils.functional import lazy
12 from django.utils.translation import ugettext_lazy as _
13 from django.utils.safestring import mark_safe
14 from migdal.models import Entry
15
16 from prawokultury.countries import COUNTRIES, TRAVEL_GRANT_COUNTRIES
17
18 mark_safe_lazy = lazy(mark_safe, unicode)
19
20
21 class RegistrationForm(ContactForm):
22     form_tag = 'register'
23
24     save_as_tag = '2019'
25     conference_name = u'CopyCamp 2019'
26     notify_on_register = False
27     
28     form_title = _('Registration')
29     admin_list = ['first_name', 'last_name', 'organization']
30
31     first_name = forms.CharField(label=_('First name'), max_length=128)
32     last_name = forms.CharField(label=_('Last name'), max_length=128)
33     contact = forms.EmailField(label=_('E-mail'), max_length=128)
34     organization = forms.CharField(label=_('Organization'), max_length=256, required=False)
35     agree_license = forms.BooleanField(
36         label=_('Permission for publication'),
37         help_text=mark_safe_lazy(_(
38             u'I agree to having materials, recorded during the conference, released under the terms of '
39             u'<a href="http://creativecommons.org/licenses/by-sa/3.0/deed">CC\u00a0BY-SA</a> license and '
40             u'to publishing my image.')),
41         required=False
42     )
43     agree_terms = forms.BooleanField(
44         label=mark_safe_lazy(
45             _(u'I accept <a href="/en/info/terms-and-conditions/">CopyCamp Terms and Conditions</a>.'))
46     )
47
48     def __init__(self, *args, **kwargs):
49         super(RegistrationForm, self).__init__(*args, **kwargs)
50         self.started = getattr(settings, 'REGISTRATION_STARTED', False)
51         self.limit_reached = Contact.objects.filter(form_tag=self.save_as_tag).count() >= settings.REGISTRATION_LIMIT
52
53     def main_fields(self):
54         return [self[name] for name in (
55             'first_name', 'last_name', 'contact', 'organization')]
56
57     def survey_fields(self):
58         return []
59
60     def agreement_fields(self):
61         return [self[name] for name in ('agree_license', 'agree_toc')]
62
63
64 class RegisterSpeaker(RegistrationForm):
65     form_tag = 'register-speaker'
66     save_as_tag = '2019-speaker'
67     form_title = _('Open call for presentations')
68     notify_on_register = False
69     mailing_field = 'agree_mailing'
70
71     bio = forms.CharField(label=mark_safe_lazy(
72         _('Short biographical note in Polish (max. 500 characters)')),
73                           widget=forms.Textarea, max_length=500, required=True)
74     bio_en = forms.CharField(label=_('Short biographical note in English (max. 500 characters, not required)'), widget=forms.Textarea,
75                              max_length=500, required=False)
76     photo = forms.FileField(label=_('Photo'), required=False)
77     phone = forms.CharField(label=_('Phone number'), max_length=64,
78                             required=False,
79                             help_text=_('(used only for organizational purposes)'))
80
81     presentation_title = forms.CharField(
82         label=mark_safe_lazy(_('Presentation title in Polish')),
83         max_length=256)
84     presentation_title_en = forms.CharField(
85         label=_('Presentation title in English (not required)'), max_length=256, required=False)
86     presentation_summary = forms.CharField(label=_('Presentation summary (max. 1800 characters)'),
87                                            widget=forms.Textarea, max_length=1800)
88
89     # presentation_post_conference_publication = forms.BooleanField(
90     #     label=_('I am interested in including my paper in the post-conference publication'),
91     #     required=False
92     # )
93
94     agree_data = None
95
96     def __init__(self, *args, **kw):
97         super(RegisterSpeaker, self).__init__(*args, **kw)
98         self.started = getattr(settings, 'REGISTRATION_SPEAKER_STARTED', False)
99         self.closed = getattr(settings, 'REGISTRATION_SPEAKER_CLOSED', False)
100         self.fields.keyOrder = [
101             'first_name',
102             'last_name',
103             'contact',
104             'phone',
105             'organization',
106             'bio',
107             'bio_en',
108             'photo',
109             'presentation_title',
110             'presentation_title_en',
111             'presentation_summary',
112
113             'agree_license',
114             'agree_terms',
115         ]
116
117
118 class RemindForm(ContactForm):
119     form_tag = 'remind-me'
120     save_as_tag = 'remind-me-2019'
121     form_title = u'CopyCamp 2019'
122     notify_on_register = False
123     notify_user = False
124
125
126 class NextForm(ContactForm):
127     form_tag = '/next'
128     form_title = _('Next CopyCamp')
129
130     name = forms.CharField(label=_('Name'), max_length=128)
131     contact = forms.EmailField(label=_('E-mail'), max_length=128)
132     organization = forms.CharField(label=_('Organization'),
133                                    max_length=256, required=False)
134
135
136 def workshop_field(label, help=None):
137     return forms.BooleanField(label=_(label), required=False, help_text=help)
138
139
140 class WorkshopForm(ContactForm):
141     form_tag = 'workshops'
142     save_as_tag = 'workshops-2018'
143     conference_name = u'CopyCamp 2018'
144     form_title = _('Workshop')
145     notify_on_register = False
146     mailing_field = 'agree_mailing'
147
148     first_name = forms.CharField(label=_('First name'), max_length=128)
149     last_name = forms.CharField(label=_('Last name'), max_length=128)
150     contact = forms.EmailField(label=_('E-mail'), max_length=128)
151     organization = forms.CharField(label=_('Organization'), max_length=256, required=False)
152     country = forms.ChoiceField(
153         label=_('Country of residence'), choices=[('', '--------')] + zip(COUNTRIES, COUNTRIES), required=False)
154
155     _header = HeaderField(
156         label=mark_safe_lazy(_("<h3>I'll take a part in workshops</h3>")),
157         help_text=_('Only workshops with any spots left are visible here.'))
158
159     _h1 = HeaderField(label=mark_safe_lazy(_("<strong>Friday, October 5th, 11 a.m.–1 p.m.</strong>")))
160
161     w_dobosz = workshop_field(
162         u'Elżbieta Dobosz, Urząd Patentowy RP: Ochrona wzornictwa, co można chronić, co warto chronić i w jaki sposób',
163         u'Uczestnicy mogą przedstawić na warsztatach swoje wzory – '
164         u'rozwiązania wizualne ze wszystkich kategorii produktów.')
165     w_kozak = workshop_field(
166         u'Łukasz Kozak i Krzysztof Siewicz: Projekt : Upiór – wprowadzenie i warsztaty dla twórców gier')
167     w_secker = workshop_field(
168         u'Jane Secker and Chris Morrison: Embedding Copyright literacy using games-based learning',
169         _(u'The workshop will be conducted in English.'))
170
171     _h2 = HeaderField(label=mark_safe_lazy(_("<strong>Saturday, October 6th, 11 a.m.–1 p.m.</strong>")))
172
173     w_kakareko = workshop_field(
174         u'Ksenia Kakareko: Regulacje prawne dotyczące wykorzystania materiałów zdigitalizowanych')
175     w_kakareko_question = forms.CharField(
176         label=u'Możesz opisać sprawy, z którymi najczęściej spotykasz się jako pracownik instytucji posiadającej '
177               u'zdigitalizowane zbiory lub jako użytkownik tych zbiorów '
178               u'(max 800 znaków)',
179         max_length=800, widget=forms.Textarea, required=False)
180     w_sikorska = workshop_field(
181         u'Krzysztof Siewicz: Autor: projektant / prawo autorskie dla projektantów')
182     w_sikorska_question = forms.CharField(
183         label=u'Jeżeli chcesz, możesz przesłać prowadzącemu swoje pytanie dotyczące prawa autorskiego, '
184               u'co pomoże mu lepiej przygotować warsztaty '
185               u'(max 800 znaków)',
186         max_length=800, widget=forms.Textarea, required=False)
187     w_sztoldman = workshop_field(
188         u'dr Agnieszka Sztoldman, Aleksandra Burda, SMM Legal: Spory o pieniądze w branżach IP-driven')
189
190     _header_1 = HeaderField(label='')
191     _header_2 = HeaderField(label='')
192
193     start_workshops = ('dobosz', 'kozak', 'secker', 'kakareko', 'sikorska', 'sztoldman')
194
195     slots = (
196         ('_h1', 'dobosz', 'kozak', 'secker'),
197         ('_h2', 'kakareko', 'sikorska', 'sztoldman'),
198     )
199
200     limits = {
201         'dobosz': 30,
202         'kozak': 30,
203         'secker': 30,
204         'kakareko': 30,
205         'sikorska': 30,
206         'sztoldman': 30,
207     }
208
209     agree_mailing = forms.BooleanField(
210         label=_('I am interested in receiving information about the Modern Poland Foundation\'s activities by e-mail'),
211         required=False)
212     agree_license = forms.BooleanField(
213         label=_('Permission for publication'),
214         help_text=mark_safe_lazy(_(
215             u'I agree to having materials, recorded during the conference, released under the terms of '
216             u'<a href="http://creativecommons.org/licenses/by-sa/3.0/deed">CC\u00a0BY-SA</a> '
217             u'license and to publishing my image.')),
218         required=False)
219
220     def __init__(self, *args, **kwargs):
221         super(WorkshopForm, self).__init__(*args, **kwargs)
222         try:
223             url = Entry.objects.get(slug_pl='regulamin').get_absolute_url()
224             self.fields['agree_toc'] = forms.BooleanField(
225                 required=True,
226                 label=mark_safe(_('I accept <a href="%s">Terms and Conditions of CopyCamp</a>') % url)
227             )
228         except Entry.DoesNotExist:
229             pass
230         counts = {k: 0 for k in self.start_workshops}
231         for contact in Contact.objects.filter(form_tag=self.save_as_tag):
232             for workshop in self.start_workshops:
233                 if contact.body.get('w_%s' % workshop, False):
234                     counts[workshop] += 1
235                     # if workshop == 'youtube' and counts[workshop] == 30:
236                     #     send_mail(u'Warsztaty YouTube', u'Przekroczono limit 30 osób na warsztaty YouTube',
237                     #               'no-reply@copycamp.pl',
238                     #               ['krzysztof.siewicz@nowoczesnapolska.org.pl'],
239                     #               fail_silently=True)
240
241         some_full = False
242         for k, v in counts.items():
243             if v >= self.limits[k]:
244                 some_full = True
245                 if 'w_%s' % k in self.fields:
246                     del self.fields['w_%s' % k]
247         if not some_full:
248             self.fields['_header'].help_text = None
249
250     def clean(self):
251         for slot in self.slots:
252             if sum(1 for w in slot if self.cleaned_data.get('w_%s' % w)) > 1:
253                 self._errors[slot[0]] = [_("You can't choose more than one workshop during the same period")]
254         if not any(self.cleaned_data.get('w_%s' % w) for w in self.start_workshops):
255             self._errors['_header'] = [_("Please choose at least one workshop.")]
256         return self.cleaned_data