summaryrefslogtreecommitdiff
path: root/postgresqleu/confreg/backendforms.py
blob: 9d146e163d4573c974be534558e74f948399e589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db.models import Q
from django.db.models.expressions import F
from django.contrib import messages
from django.contrib.auth.models import User
import django.forms
import django.forms.widgets
from django.utils import timezone
from django.utils.safestring import mark_safe
from django.utils.html import escape
from django.conf import settings

import datetime
from collections import OrderedDict
import os
import re
from urllib.parse import urlparse
from psycopg2.extras import DateTimeTZRange
import zoneinfo

from postgresqleu.util.db import exec_to_single_list, exec_to_scalar
from postgresqleu.util.crypto import generate_rsa_keypair
from postgresqleu.util.forms import SelectSetValueField
from postgresqleu.util.widgets import StaticTextWidget, EmailTextWidget, MonospaceTextarea
from postgresqleu.util.widgets import TagOptionsTextWidget
from postgresqleu.util.random import generate_random_token
from postgresqleu.util.backendforms import BackendForm, BackendBeforeNewForm
from postgresqleu.util.messaging import messaging_implementation_choices, get_messaging, get_messaging_class
from postgresqleu.util.messaging.short import get_shortened_post_length
from postgresqleu.util.time import datetime_string

import postgresqleu.accounting.models

from postgresqleu.confsponsor.models import SponsorshipBenefit
from postgresqleu.confsponsor.benefitclasses import get_benefit_id

from postgresqleu.confreg.models import Conference, ConferenceRegistration, ConferenceAdditionalOption
from postgresqleu.confreg.models import RegistrationClass, RegistrationType, RegistrationDay
from postgresqleu.confreg.models import ConferenceFeedbackQuestion, Speaker
from postgresqleu.confreg.models import ConferenceSession, Track, Room, ConferenceSessionTag
from postgresqleu.confreg.models import ConferenceSessionSlides
from postgresqleu.confreg.models import ConferenceSessionScheduleSlot, VolunteerSlot
from postgresqleu.confreg.models import DiscountCode, AccessToken, AccessTokenPermissions
from postgresqleu.confreg.models import ConferenceSeries
from postgresqleu.confreg.models import ConferenceNews
from postgresqleu.confreg.models import ConferenceTweetQueue, ConferenceHashtag
from postgresqleu.confreg.models import ConferenceTweetQueueErrorLog
from postgresqleu.confreg.models import ShirtSize
from postgresqleu.confreg.models import RefundPattern
from postgresqleu.confreg.models import ConferenceMessaging
from postgresqleu.confreg.models import MessagingProvider
from postgresqleu.newsevents.models import NewsPosterProfile

from postgresqleu.confreg.models import valid_status_transitions, get_status_string
from postgresqleu.confreg.models import STATUS_CHOICES, STATUS_CHOICES_LONG

from postgresqleu.util.backendlookups import GeneralAccountLookup, CountryLookup
from postgresqleu.confreg.backendlookups import RegisteredUsersLookup, SpeakerLookup, SessionTagLookup

from postgresqleu.confreg.campaigns import allcampaigns
from postgresqleu.confreg.regtypes import validate_special_reg_type_setup
from postgresqleu.confreg.contextutil import has_yaml


class BackendConferenceForm(BackendForm):
    helplink = 'configuring#conferenceform'
    markdown_fields = ['promotext', ]
    selectize_multiple_fields = {
        'testers': GeneralAccountLookup(),
        'talkvoters': GeneralAccountLookup(),
        'staff': GeneralAccountLookup(),
        'volunteers': RegisteredUsersLookup(None),
        'checkinprocessors': RegisteredUsersLookup(None),
        'initial_common_countries': CountryLookup(),
    }
    selectize_taglist_fields = ('dynafields', 'scannerfields', 'videoproviders', )
    vat_fields = {'transfer_cost': 'reg'}

    class Meta:
        model = Conference
        fields = ['registrationopen', 'registrationtimerange', 'callforpapersopen', 'callforpaperstimerange',
                  'callforsponsorsopen', 'callforsponsorstimerange', 'feedbackopen', 'checkinactive',
                  'conferencefeedbackopen', 'scheduleactive', 'tbdinschedule', 'sessionsactive', 'cardsactive', 'allowedit',
                  'promoactive', 'promotext', 'promopicurl',
                  'twitter_timewindow_start', 'twitter_timewindow_end', 'twitter_postpolicy',
                  'schedulewidth', 'pixelsperminute', 'notifyregs', 'notifysessionstatus', 'notifyvolunteerstatus',
                  'testers', 'talkvoters', 'staff', 'volunteers', 'checkinprocessors',
                  'askpronouns', 'asktshirt', 'askfood', 'asknick', 'asktwitter', 'askbadgescan', 'askshareemail', 'askphotoconsent',
                  'callforpapersmaxsubmissions', 'skill_levels', 'showvotes', 'scoring_method', 'callforpaperstags', 'callforpapersrecording', 'sendwelcomemail',
                  'tickets', 'confirmpolicy', 'queuepartitioning', 'invoice_autocancel_hours', 'attendees_before_waitlist',
                  'transfer_cost', 'initial_common_countries', 'jinjaenabled', 'dynafields', 'scannerfields',
                  'videoproviders', ]

    def fix_fields(self):
        self.selectize_multiple_fields['volunteers'] = RegisteredUsersLookup(self.conference)
        self.selectize_multiple_fields['checkinprocessors'] = RegisteredUsersLookup(self.conference)
        self.fields['notifyregs'].help_text = 'Notifications will be sent to {} whenever someone registers or cancels.'.format(self.conference.notifyaddr)
        self.fields['notifysessionstatus'].help_text = 'Notifications will be sent to {} whenever a speaker confirms a session.'.format(self.conference.notifyaddr)
        self.fields['notifyvolunteerstatus'].help_text = 'Notifications will be sent to {} whenever a volunteer makes changes to a slot.'.format(self.conference.notifyaddr)

    fieldsets = [
        {'id': 'base_info', 'legend': 'Basic information', 'fields': ['attendees_before_waitlist', 'invoice_autocancel_hours', 'transfer_cost', 'notifyregs', 'notifysessionstatus', 'notifyvolunteerstatus', ]},
        {'id': 'welcomeandreg', 'legend': 'Welcome and registration', 'fields': ['sendwelcomemail', 'tickets', 'confirmpolicy', 'queuepartitioning', 'initial_common_countries']},
        {'id': 'promo', 'legend': 'Website promotion', 'fields': ['promoactive', 'promotext', 'promopicurl']},
        {'id': 'twitter', 'legend': 'Twitter settings', 'fields': ['twitter_timewindow_start', 'twitter_timewindow_end', 'twitter_postpolicy', ]},
        {'id': 'fields', 'legend': 'Registration fields', 'fields': ['askpronouns', 'asktshirt', 'askfood', 'asknick', 'asktwitter', 'askbadgescan', 'askshareemail', 'askphotoconsent', 'dynafields', 'scannerfields', ]},
        {'id': 'steps', 'legend': 'Steps', 'fields': ['registrationopen', 'registrationtimerange', 'allowedit', 'callforpapersopen', 'callforpaperstimerange', 'callforsponsorsopen', 'callforsponsorstimerange', 'scheduleactive', 'tbdinschedule', 'sessionsactive', 'cardsactive', 'checkinactive', 'conferencefeedbackopen', 'feedbackopen']},
        {'id': 'callforpapers', 'legend': 'Call for papers', 'fields': ['callforpapersmaxsubmissions', 'skill_levels', 'callforpaperstags', 'callforpapersrecording', 'showvotes', 'scoring_method']},
        {'id': 'roles', 'legend': 'Roles', 'fields': ['testers', 'talkvoters', 'staff', 'volunteers', 'checkinprocessors', ]},
        {'id': 'display', 'legend': 'Display', 'fields': ['jinjaenabled', 'videoproviders', ]},
        {'id': 'legacy', 'legend': 'Legacy', 'fields': ['schedulewidth', 'pixelsperminute']},
    ]

    def clean(self):
        cleaned_data = super(BackendConferenceForm, self).clean()

        if cleaned_data.get('sendwelcomemail'):
            if not self.instance.jinjadir:
                self.add_error("sendwelcomemail", "Welcome emails cannot be enabled since there is no Jinja directory configured in superuser settings")
            elif not self.instance.jinjaenabled:
                self.add_error("sendwelcomemail", "Welcome emails cannot be enabled since Jinja templates are not enabled")
            elif not os.path.isfile(os.path.join(self.instance.jinjadir, 'templates/confreg/mail/welcomemail.txt')):
                self.add_error("sendwelcomemail", "Welcome emails cannot be enabled since the file confreg/mail/welcomemail.txt does not exist in the jinja template directory")

        if cleaned_data.get('tickets') and not cleaned_data.get('sendwelcomemail'):
            self.add_error('tickets', 'If tickets should be generated and sent, welcome emails must be sent!')

        if cleaned_data.get('confirmpolicy'):
            if not os.path.isfile(os.path.join(self.instance.jinjadir, 'templates/confreg/policy.html')):
                self.add_error('confirmpolicy', 'A policy has to be defined in the template confreg/policy.html in the jinja template directory')
            elif not self.instance.jinjaenabled:
                self.add_error('confirmpolicy', 'Jinja templates have to be enabled to use conference policy')

        if cleaned_data.get('checkinactive') and not cleaned_data.get('tickets'):
            self.add_error('checkinactive', 'Check-in cannot be activated if tickets are not used!')

        dynafields = set([x for x in cleaned_data.get('dynafields', '').split(',') if x])
        scannerfields = set([x for x in cleaned_data.get('scannerfields', '').split(',') if x])
        if scannerfields - dynafields:
            self.add_error('scannerfields', 'Only fields from the list of dynamic properties can be used. Incorrect fields: {}'.format(", ".join(scannerfields - dynafields)))

        return cleaned_data

    def clean_jinjaenabled(self):
        je = self.cleaned_data.get('jinjaenabled', False)
        if je:
            if not self.instance.jinjadir:
                raise ValidationError("Jinja templates cannot be enabled since there is no Jinja directory configured in superuser settings")
        return je

    def clean_videoproviders(self):
        val = self.cleaned_data['videoproviders']

        vals = [v.strip() for v in val.split(',') if v != '']
        # JS should've protected us against duplicate values, but we can't trust that, so validate
        if len(vals) != len(set(vals)):
            raise ValidationError("Duplicate video provider name found")

        for v in vals:
            if not re.match('^[a-z]+$', v, re.I):
                raise ValidationError("Video provider names can only contain letters a-z")

        # See if we tried to *remove* a property that's still in use
        used = exec_to_single_list("SELECT DISTINCT k FROM (SELECT jsonb_object_keys(videolinks) k FROM confreg_conferencesession s WHERE s.conference_id=%(confid)s) d WHERE NOT d.k=ANY(%(vals)s)", {
            'confid': self.instance.id,
            'vals': vals,
        })
        if used:
            raise ValidationError("The provider name(s) {} are still in use and cannot be removed.".format(sorted(used)))

        # Re-sort and save the stripped and lowercased version
        return ",".join(sorted(vals)).lower()

    def clean_dynafields(self):
        val = self.cleaned_data['dynafields']

        vals = [v.strip() for v in val.split(',') if v != '']
        # JS should've protected us against duplicate values, but we can't trust that, so validate
        if len(vals) != len(set(vals)):
            raise ValidationError("Duplicate dynamic property name found")

        for v in vals:
            if not re.match('^[a-z]+$', v, re.I):
                raise ValidationError("Property names can only contain letters a-z")

        # See if we tried to *remove* a property that's still in use
        used = exec_to_single_list("SELECT DISTINCT k FROM (SELECT jsonb_object_keys(dynaprops) k FROM confreg_conferenceregistration r WHERE r.conference_id=%(confid)s) d WHERE NOT d.k=ANY(%(vals)s)", {
            'confid': self.instance.id,
            'vals': vals,
        })
        if used:
            raise ValidationError("The key(s) {} are still in use and cannot be removed.".format(sorted(used)))

        # Re-sort and save the stripped version
        return ",".join(sorted(vals))

    def clean_scannerfields(self):
        val = self.cleaned_data['scannerfields']

        vals = [v.strip() for v in val.split(',') if v != '']
        # JS should've protected us against duplicate values, but we can't trust that, so validate
        if len(vals) != len(set(vals)):
            raise ValidationError("Duplicate scanner field name found")

        return ",".join(sorted(vals))


def _timezone_choices():
    return [(z, z) for z in zoneinfo.available_timezones() if z not in ('localtime', 'Factory')]


class BackendSuperConferenceForm(BackendForm):
    tzname = django.forms.ChoiceField(choices=_timezone_choices(), label='Time zone')

    helplink = 'super_conference#conferenceform'
    selectize_multiple_fields = {
        'administrators': GeneralAccountLookup(),
    }
    selectize_single_fields = {
        'tzname': None,
    }
    accounting_object = django.forms.ChoiceField(choices=[], required=False)
    exclude_date_validators = ['startdate', 'enddate']

    class Meta:
        model = Conference
        fields = ['conferencename', 'urlname', 'series', 'startdate', 'enddate', 'location',
                  'tzname', 'contactaddr', 'sponsoraddr', 'notifyaddr', 'confurl', 'administrators',
                  'jinjadir', 'accounting_object', 'vat_registrations', 'vat_sponsorship',
                  'paymentmethods', 'contractprovider', 'contractsendername', 'contractsenderemail',
                  'contractexpires', 'manualcontracts', 'autocontracts', 'web_origins']
        widgets = {
            'paymentmethods': django.forms.CheckboxSelectMultiple,
        }
        fieldsets = [
            {'id': 'base_info', 'legend': 'Basic information', 'fields': ['conferencename', 'urlname', 'series', 'location', 'confurl',
                                                                          'jinjadir', 'administrators']},
            {'id': 'time', 'legend': 'Timing information', 'fields': ['startdate', 'enddate', 'tzname']},
            {'id': 'contact', 'legend': 'Contact information', 'fields': ['contactaddr', 'sponsoraddr', 'notifyaddr']},
            {'id': 'financial', 'legend': 'Financial information', 'fields': ['accounting_object', 'vat_registrations',
                                                                              'vat_sponsorship', 'paymentmethods']},
            {'id': 'contracts', 'legend': 'Sponsorship contracts', 'fields': ['contractprovider', 'contractsendername', 'contractsenderemail', 'contractexpires', 'manualcontracts', 'autocontracts', ]},
            {'id': 'api', 'legend': 'API access', 'fields': ['web_origins', ]}
        ]

    def fix_fields(self):
        self.fields['paymentmethods'].label_from_instance = lambda x: "{0}{1}".format(x.internaldescription, x.active and " " or " (INACTIVE)")
        self.fields['accounting_object'].choices = [('', '----'), ] + [(o.name, o.name) for o in postgresqleu.accounting.models.Object.objects.filter(active=True)]
        if not self.instance.id:
            self.remove_field('accounting_object')

    def pre_create_item(self):
        # Create a new accounting object automatically if one does not exist already
        (obj, created) = postgresqleu.accounting.models.Object.objects.get_or_create(name=self.instance.urlname,
                                                                                     defaults={'active': True})
        self.instance.accounting_object = obj

    def post_save(self):
        # If we haven't got an RSA key for this conference yet, create it here
        if not self.instance.key_public:
            self.instance.key_private, self.instance.key_public = generate_rsa_keypair()
            self.instance.save(update_fields=['key_private', 'key_public'])

    def clean_tzname(self):
        # The entry for timezone is already validated against the zoneinfo database which should
        # normally be the same as the one in PostgreSQL, but we verify it against the
        # database side as well to be safe.
        if not exec_to_scalar("SELECT name FROM pg_timezone_names WHERE name=%(name)s", {
                'name': self.cleaned_data['tzname'],
        }):
            raise ValidationError("This timezone does not to exist in the database")

        return self.cleaned_data['tzname']

    def clean_web_origins(self):
        for o in self.cleaned_data['web_origins'].split(','):
            if o == '':
                continue
            try:
                p = urlparse(o.strip())
            except Exception:
                raise ValidationError("Could not parse url {}".format(o))
            if not p.scheme or not p.netloc:
                raise ValidationError("Incomplete url {}".format(o))

        # Re-join string without any spaces
        return ",".join(o.strip() for o in self.cleaned_data['web_origins'].split(','))

    def clean(self):
        cleaned_data = super().clean()
        if cleaned_data.get('autocontracts', False) and not cleaned_data['contractprovider']:
            self.add_error('autocontracts', 'Automatic contract workflow can only be enabled if a digital signature provider is configured')
        if not (cleaned_data.get('contractprovider', None) or cleaned_data.get('manualcontracts', False)):
            self.add_error('contractprovider', 'Either a digital signing provider or manual contracts must be enabled')
            self.add_error('manualcontracts', 'Either a digital signing provider or manual contracts must be enabled')

        if cleaned_data.get('contractprovider', False):
            if not self.cleaned_data.get('contractsendername', ''):
                self.add_error('contractsendername', 'This field is required when digital contracts are enabled')
            if not self.cleaned_data.get('contractsenderemail', ''):
                self.add_error('contractsenderemail', 'This field is required when digital contracts are enabled')

        return cleaned_data


class BackendConferenceSeriesForm(BackendForm):
    helplink = "series"
    list_fields = ['name', 'visible', 'sortkey', ]
    markdown_fields = ['intro', ]
    selectize_multiple_fields = {
        'administrators': GeneralAccountLookup(),
    }

    class Meta:
        model = ConferenceSeries
        fields = ['name', 'sortkey', 'visible', 'administrators', 'intro', ]


class BackendTshirtSizeForm(BackendForm):
    helplink = "meta"
    list_fields = ['shirtsize', 'sortkey', ]

    class Meta:
        model = ShirtSize
        fields = ['shirtsize', 'sortkey', ]


class BackendRegistrationForm(BackendForm):
    helplink = "registrations"

    class Meta:
        model = ConferenceRegistration
        fields = ['firstname', 'lastname', 'email', 'company', 'address', 'country', 'phone',
                  'shirtsize', 'dietary', 'pronouns', 'twittername', 'nick', 'badgescan', 'shareemail',
                  'regtype', 'additionaloptions']

    _all_dynamic_fields = set(['badgescan', 'shareemail', 'dietary', 'pronouns', 'shirtsize'])

    def _get_reginfo_fields(self):
        if self.conference.askbadgescan:
            yield 'badgescan'
        if self.conference.askshareemail:
            yield 'shareemail'

    def _get_attendeespec_fields(self):
        if self.conference.askpronouns:
            yield 'pronouns'
        if self.conference.askfood:
            yield 'dietary'
        if self.conference.asktshirt:
            yield 'shirtsize'

    @property
    def json_form_fields(self):
        if self.conference.dynafields:
            return {
                'dynaprops': self.conference.dynafields.split(','),
            }
        return None

    @property
    def fieldsets(self):
        fs = [
            {'id': 'personal_info', 'legend': 'Personal information', 'fields': ['firstname', 'lastname', 'email', 'company', 'address', 'country', 'phone', 'twittername', 'nick']},
            {'id': 'reg_info', 'legend': 'Registration information', 'fields': ['regtype', 'additionaloptions'] + list(self._get_reginfo_fields())},
        ]
        aspec = list(self._get_attendeespec_fields())
        if aspec:
            fs.append(
                {'id': 'attendee_specifics', 'legend': 'Attendee specifics', 'fields': aspec},
            )
        if self.conference.dynafields:
            fs.append(
                {'id': 'dynamic', 'legend': 'Dynamic reporting fields', 'fields': self.conference.dynafields.split(',')}
            )
        return fs

    def fix_fields(self):
        if self.instance.canceledat:
            self.warning_text = "WARNING! This registration has already been CANCELED! Edit with caution!"
        elif self.instance.payconfirmedat:
            self.warning_text = "WARNING! This registration has already been completed! Edit with caution!"

        if self.instance.partoftransfer:
            self.warning_text = "WARNING! This registration is part of a pending transfer. Should NOT be edited!"

        self.fields['additionaloptions'].queryset = ConferenceAdditionalOption.objects.filter(conference=self.conference)
        self.fields['regtype'].queryset = RegistrationType.objects.filter(conference=self.conference)

        for f in self._all_dynamic_fields.difference(list(self._get_reginfo_fields()) + list(self._get_attendeespec_fields())):
            self.remove_field(f)

        if self.conference.dynafields:
            for f in self.conference.dynafields.split(','):
                self.fields[f] = django.forms.CharField(label=f, required=False)
                self.fields[f].delete_on_empty = True

        self.update_protected_fields()


class BackendRegistrationClassForm(BackendForm):
    helplink = 'registrations#regclasses'
    list_fields = ['regclass', 'badgecolor', 'badgeforegroundcolor']
    allow_copy_previous = True

    class Meta:
        model = RegistrationClass
        fields = ['regclass', 'badgecolor', 'badgeforegroundcolor']

    @classmethod
    def copy_from_conference(self, targetconf, sourceconf, idlist):
        # Registration classes are copied straight over, but we disallow duplicates
        for id in idlist:
            source = RegistrationClass.objects.get(conference=sourceconf, pk=id)
            if RegistrationClass.objects.filter(conference=targetconf, regclass=source.regclass).exists():
                yield 'A registration class with name {0} already exists.'.format(source.regclass)
            else:
                RegistrationClass(conference=targetconf,
                                  regclass=source.regclass,
                                  badgecolor=source.badgecolor,
                                  badgeforegroundcolor=source.badgeforegroundcolor).save()


class BackendRegistrationTypeForm(BackendForm):
    helplink = 'registrations#regtypes'
    list_fields = ['regtype', 'regclass', 'cost', 'active', 'sortkey']
    queryset_select_related = ['regclass', ]
    exclude_date_validators = ['activeuntil', ]
    vat_fields = {'cost': 'reg'}
    allow_copy_previous = True
    coltypes = {
        'Sortkey': ['nosearch', ],
    }
    defaultsort = [['sortkey', 'asc']]
    auto_cascade_delete_to = ['registrationtype_days', 'registrationtype_requires_option']

    class Meta:
        model = RegistrationType
        fields = ['regtype', 'regclass', 'cost', 'active', 'activeuntil', 'days', 'sortkey', 'specialtype', 'require_phone', 'alertmessage', 'invoice_autocancel_hours', 'requires_option', 'upsell_target']

    @classmethod
    def get_column_filters(cls, conference):
        return {
            'Registration class': RegistrationClass.objects.filter(conference=conference),
            'Active': [],
        }

    @classmethod
    def get_assignable_columns(cls, conference):
        return [
            {
                'name': 'regclass',
                'title': 'Registration class',
                'options': [(c.id, c.regclass) for c in RegistrationClass.objects.filter(conference=conference)],
                'canclear': False,
            },
        ]

    def fix_fields(self):
        self.fields['regclass'].queryset = RegistrationClass.objects.filter(conference=self.conference)
        self.fields['requires_option'].queryset = ConferenceAdditionalOption.objects.filter(conference=self.conference)
        if RegistrationDay.objects.filter(conference=self.conference).exists():
            self.fields['days'].queryset = RegistrationDay.objects.filter(conference=self.conference)
        else:
            self.remove_field('days')
            self.update_protected_fields()

        if not ConferenceAdditionalOption.objects.filter(conference=self.conference).exists():
            self.remove_field('requires_option')
            self.remove_field('upsell_target')
            self.update_protected_fields()

    def clean_cost(self):
        if self.cleaned_data['cost'] > 0 and not self.conference.paymentmethods.exists():
            raise ValidationError("Cannot assign a cost, this conference has no payment methods")

        if self.instance and self.instance.pk and self.instance.cost != self.cleaned_data['cost']:
            if self.instance.conferenceregistration_set.filter(Q(payconfirmedat__isnull=False) | Q(invoice__isnull=False) | Q(bulkpayment__isnull=False)).exists():
                raise ValidationError("This registration type has been used, so the cost can no longer be changed")

        return self.cleaned_data['cost']

    def clean_specialtype(self):
        if self.cleaned_data['specialtype']:
            validate_special_reg_type_setup(self.cleaned_data['specialtype'], self.cleaned_data)
        return self.cleaned_data['specialtype']

    def clean_regtype(self):
        newtype = self.cleaned_data['regtype']
        if self.instance and self.instance.regtype and self.instance.regtype != newtype:
            # Disallow changing the regtype if there is a sponsorship benefit that uses it,
            # since this link is done on name in a json object, and not a foreign key.
            if SponsorshipBenefit.objects.filter(
                    level__conference=self.instance.conference,
                    benefit_class=get_benefit_id('entryvouchers.EntryVouchers'),
                    class_parameters__type=self.instance.regtype,
            ).exists():
                raise ValidationError("A sponsorship benefit is using the registration type '{}', cannot rename.".format(self.instance.regtype))
        return newtype

    @classmethod
    def copy_from_conference(self, targetconf, sourceconf, idlist):
        # Registration types are copied straight over, but we disallow duplicates. We also
        # have to match the registration class.
        # NOTE! We do *not* attempt to adjust VAT rates!
        for id in idlist:
            source = RegistrationType.objects.get(conference=sourceconf, pk=id)
            if RegistrationType.objects.filter(conference=targetconf, regtype=source.regtype).exists():
                yield 'A registration type with name {0} already exists.'.format(source.regtype)
            else:
                try:
                    if source.regclass:
                        targetclass = RegistrationClass.objects.get(conference=targetconf,
                                                                    regclass=source.regclass.regclass)
                    else:
                        targetclass = None
                    RegistrationType(conference=targetconf,
                                     regtype=source.regtype,
                                     regclass=targetclass,
                                     active=source.active,
                                     # Not copying activeuntil
                                     inlist=source.inlist,
                                     sortkey=source.sortkey,
                                     specialtype=source.specialtype,
                                     # Not copying days
                                     alertmessage=source.alertmessage,
                                     upsell_target=source.upsell_target,
                                     # Not copying invoice_autocancel_hours
                                     # Not copying requires_option
                    ).save()
                except RegistrationClass.DoesNotExist:
                    yield 'Could not find registration class {0} for registration type {1}'.format(
                        source.regclass.regclass, source.regtype)


class BackendRegistrationDayForm(BackendForm):
    helplink = 'registrations#days'
    list_fields = ['day', ]

    class Meta:
        model = RegistrationDay
        fields = ['day', ]

    def clean_day(self):
        if self.instance.id:
            if RegistrationDay.objects.filter(conference=self.conference, day=self.cleaned_data['day']).exclude(id=self.instance.id).exists():
                raise ValidationError("This day already exists for this conference")
        else:
            if RegistrationDay.objects.filter(conference=self.conference, day=self.cleaned_data['day']).exists():
                raise ValidationError("This day already exists for this conference")

        return self.cleaned_data['day']


class AdditionalOptionUserManager(object):
    title = 'Users'
    singular = 'user'

    def get_list(self, instance):
        if instance.id:
            return [(r.id, r.fullname, "{} ({}{})".format(
                r.regtype.regtype if r.regtype else '',
                r.invoice_status,
                ', checked in at {}'.format(datetime_string(r.checkedinat)) if r.checkedinat else '',
            )) for r in instance.conferenceregistration_set.all()]

    def get_form(self):
        return None

    def get_object(self, masterobj, subid):
        try:
            return masterobj.conferenceregistration_set.get(pk=subid)
        except models.DoesNotExist:
            return None


class AdditionalOptionPendingManager(object):
    title = 'Pending additional users'
    singular = 'user'

    def get_list(self, instance):
        if instance.id:
            return [(None, p.reg.fullname, p.invoice_status) for p in instance.pendingadditionalorder_set.filter(payconfirmedat__isnull=True)]

    def get_form(self):
        return None


class BackendAdditionalOptionForm(BackendForm):
    helplink = 'registrations#additionaloptions'
    list_fields = ['name', 'cost', 'maxcount', 'invoice_autocancel_hours', 'public', 'sortkey']
    linked_objects = OrderedDict({
        '../../regdashboard/list': AdditionalOptionUserManager(),
        '../../addoptorders/': AdditionalOptionPendingManager(),
    })
    vat_fields = {'cost': 'reg'}
    auto_cascade_delete_to = ['registrationtype_requires_option', 'conferenceadditionaloption_requires_regtype',
                              'conferenceadditionaloption_mutually_exclusive', ]
    coltypes = {
        'Maxcount': ['nosearch', ],
    }

    class Meta:
        model = ConferenceAdditionalOption
        fields = ['name', 'cost', 'maxcount', 'sortkey', 'public', 'upsellable', 'invoice_autocancel_hours',
                  'requires_regtype', 'mutually_exclusive', 'additionaldays']

    def fix_fields(self):
        self.fields['requires_regtype'].queryset = RegistrationType.objects.filter(conference=self.conference)
        self.fields['mutually_exclusive'].queryset = ConferenceAdditionalOption.objects.filter(conference=self.conference).exclude(pk=self.instance.pk)
        self.fields['additionaldays'].queryset = RegistrationDay.objects.filter(conference=self.conference)


class BackendTrackForm(BackendForm):
    helplink = 'schedule#tracks'
    list_fields = ['trackname', 'incfp', 'insessionlist', 'sortkey']
    allow_copy_previous = True
    coltypes = {
        'Sortkey': ['nosearch', ],
    }
    defaultsort = [['sortkey', 'asc']]

    class Meta:
        model = Track
        fields = ['trackname', 'sortkey', 'color', 'fgcolor', 'incfp', 'insessionlist', 'showcompany', 'speakerreg']

    @classmethod
    def copy_from_conference(self, targetconf, sourceconf, idlist):
        # Tracks are copied straight over, but we disallow duplicates
        for id in idlist:
            source = Track.objects.get(conference=sourceconf, pk=id)
            if Track.objects.filter(conference=targetconf, trackname=source.trackname).exists():
                yield 'A track with name {0} already exists.'.format(source.trackname)
            else:
                Track(conference=targetconf,
                      trackname=source.trackname,
                      color=source.color,
                      fgcolor=source.fgcolor,
                      sortkey=source.sortkey,
                      incfp=source.incfp,
                ).save()


class BackendRoomForm(BackendForm):
    helplink = 'schedule#rooms'
    list_fields = ['roomname', 'capacity', 'comment', 'sortkey']
    allow_copy_previous = True
    coltypes = {
        'Sortkey': ['nosearch', ],
    }
    defaultsort = [['sortkey', 'asc']]

    class Meta:
        model = Room
        fields = ['roomname', 'sortkey', 'capacity', 'url', 'availabledays', 'comment']

    def fix_fields(self):
        if RegistrationDay.objects.filter(conference=self.conference).exists():
            self.fields['availabledays'].queryset = RegistrationDay.objects.filter(conference=self.conference)
        else:
            self.remove_field('availabledays')
            self.update_protected_fields()

    @classmethod
    def copy_from_conference(self, targetconf, sourceconf, idlist):
        # Rooms are copied straight over, but we disallow duplicates
        # Available days are not copied over.
        for id in idlist:
            source = Room.objects.get(conference=sourceconf, pk=id)
            if Room.objects.filter(conference=targetconf, roomname=source.roomname).exists():
                yield 'A room with name {0} already exists.'.format(source.roomname)
            else:
                Room(conference=targetconf,
                     roomname=source.roomname,
                     capacity=source.capacity,
                     sortkey=source.sortkey,
                     url=source.url,
                     comment=source.comment,
                     ).save()


class BackendTagForm(BackendForm):
    helplink = 'schedule#tags'
    list_fields = ['tag', ]

    class Meta:
        model = ConferenceSessionTag
        fields = ['tag', ]


class BackendTransformConferenceDateTimeForm(django.forms.Form):
    timeshift = django.forms.DurationField(required=True, help_text="Shift all times by this much")

    def __init__(self, source, target, *args, **kwargs):
        self.source = source
        self.target = target
        super(BackendTransformConferenceDateTimeForm, self).__init__(*args, **kwargs)
        self.fields['timeshift'].initial = self.source.startdate - self.target.startdate

    def confirm_value(self):
        return str(self.cleaned_data['timeshift'])


class BackendTransformConferenceDateForm(django.forms.Form):
    dayshift = django.forms.IntegerField(required=True, help_text="Shift all dates by this many days")

    def __init__(self, source, target, *args, **kwargs):
        self.source = source
        self.target = target
        super(BackendTransformConferenceDateForm, self).__init__(*args, **kwargs)
        self.fields['dayshift'].initial = (self.source.startdate - self.target.startdate).days

    def confirm_value(self):
        return str(self.cleaned_data['dayshift'])


class BackendRefundPatternForm(BackendForm):
    helplink = 'registrations'
    list_fields = ['fromdate', 'todate', 'percent', 'fees', ]
    list_order_by = (F('fromdate').asc(nulls_first=True), 'todate', 'percent')
    exclude_date_validators = ['fromdate', 'todate', ]
    allow_copy_previous = True
    copy_transform_form = BackendTransformConferenceDateForm
    vat_fields = {'fees': 'reg'}

    class Meta:
        model = RefundPattern
        fields = ['percent', 'fees', 'fromdate', 'todate', ]

    def clean(self):
        cleaned_data = super(BackendRefundPatternForm, self).clean()
        if cleaned_data['fromdate'] and cleaned_data['todate']:
            if cleaned_data['todate'] < cleaned_data['fromdate']:
                self.add_error('todate', 'To date must be after from date!')
        return cleaned_data

    @classmethod
    def copy_from_conference(self, targetconf, sourceconf, idlist, transformform):
        xform = datetime.timedelta(days=transformform.cleaned_data['dayshift'])
        for id in idlist:
            source = RefundPattern.objects.get(conference=sourceconf, pk=id)
            RefundPattern(conference=targetconf,
                          percent=source.percent,
                          fees=source.fees,
                          fromdate=source.fromdate and source.fromdate + xform or None,
                          todate=source.todate and source.todate + xform or None,
            ).save()
        return
        yield None  # Turn this into a generator

    @classmethod
    def get_transform_example(self, targetconf, sourceconf, idlist, transformform):
        xform = datetime.timedelta(days=transformform.cleaned_data['dayshift'])
        patterns = RefundPattern.objects.filter(conference=sourceconf, id__in=idlist)[:4]
        return ["{} - {} becomes {} - {}".format(
            p.fromdate or '<empty>',
            p.todate or '<empty>',
            p.fromdate + xform if p.fromdate else '<empty>',
            p.todate + xform if p.todate else '<empty>',
        ) for p in patterns]


class ConferenceSessionSlideForm(BackendForm):
    helplink = 'callforpapers#slides'
    exclude_fields_from_validation = ['content', ]
    formnote = 'Either enter an URL or upload a PDF file (not both)'
    maxnamelen = ConferenceSessionSlides._meta.get_field('name').max_length

    class Meta:
        model = ConferenceSessionSlides
        fields = ['url', 'content', ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.override_filename = 'unknown_pdf.pdf'
        if 'files' in kwargs:
            if 'content' in kwargs['files']:
                self.override_filename = kwargs['files']['content'].name

    def clean(self):
        d = super().clean()
        url = d.get('url', None)
        pdf = d.get('content', None)

        if url and pdf:
            self.add_error('url', 'Cannot both specify URL and upload PDF!')
            self.add_error('content', 'Cannot both specify URL and upload PDF!')
        elif not (url or pdf):
            self.add_error('url', 'Must either specify URL or upload PDF')
            self.add_error('content', 'Must either specify URL or upload PDF')

        if url:
            self.override_name = url[:self.maxnamelen]
        else:
            if len(self.override_filename) > self.maxnamelen:
                self.add_error('content', 'Filename cannot be longer than {} characters'.format(self.maxnamelen))
            self.override_name = self.override_filename

        return d

    def post_save(self):
        if self.instance.name != self.override_name:
            self.instance.name = self.override_name
            self.instance.save(update_fields=['name', ])


class ConferenceSessionSlideManager(object):
    title = 'Slides'
    singular = 'slide'
    can_add = True
    fieldset = {
        'id': 'slides',
        'legend': 'Slides',
    }

    def get_list(self, instance):
        if instance.id:
            return [(s.id, s.name, '') for s in instance.conferencesessionslides_set.all()]

    def get_form(self, obj, POST):
        return ConferenceSessionSlideForm

    def get_object(self, masterobj, subid):
        try:
            return ConferenceSessionSlides.objects.get(session=masterobj, pk=subid)
        except ConferenceSessionSlides.DoesNotExist:
            return None

    def get_instancemaker(self, masterobj):
        return lambda: ConferenceSessionSlides(session=masterobj)


class BackendConferenceSessionForm(BackendForm):
    helplink = 'schedule#sessions'
    list_fields = ['title', 'q_speaker_list', 'q_status_string', 'starttime', 'track', 'room', 'cross_schedule']
    verbose_field_names = {
        'q_speaker_list': 'Speakers',
        'q_status_string': 'Status',
        'cross_schedule': 'Cross sched',
    }
    queryset_extra_fields = {
        'q_status_string': "(SELECT statustext FROM confreg_status_strings css WHERE css.id=status)",
        'q_speaker_list': "(SELECT string_agg(spk.fullname, ', ') FROM confreg_speaker spk INNER JOIN confreg_conferencesession_speaker cs ON cs.speaker_id=spk.id WHERE cs.conferencesession_id=confreg_conferencesession.id)",
    }
    selectize_multiple_fields = {
        'speaker': SpeakerLookup(),
        'tags': SessionTagLookup(None),
    }
    linked_objects = OrderedDict({
        'slides': ConferenceSessionSlideManager(),
    })
    markdown_fields = ['abstract', ]
    allow_copy_previous = True
    copy_transform_form = BackendTransformConferenceDateTimeForm
    auto_cascade_delete_to = ['conferencesession_speaker', 'conferencesessionvote']
    allow_email = True

    class Meta:
        model = ConferenceSession
        fields = ['title', 'htmlicon', 'speaker', 'status', 'starttime', 'endtime', 'cross_schedule',
                  'track', 'room', 'can_feedback', 'skill_level', 'tags', 'recordingconsent', 'abstract', 'submissionnote',
                  'internalnote']

    @property
    def fieldsets(self):
        fs = [
            {
                'id': 'info',
                'legend': 'Session info',
                'fields': ['title', 'speaker', 'abstract'] +
                (['skill_level'] if self.conference.skill_levels else []) +
                (['tags'] if self.conference.callforpaperstags else []) +
                (['recordingconsent'] if self.conference.callforpapersrecording else [])
            },
            {'id': 'schedule', 'legend': 'Scheduling', 'fields': ['status', 'track', 'room', 'htmlicon', 'starttime', 'endtime', 'cross_schedule', 'can_feedback']},
            {'id': 'notes', 'legend': 'Notes', 'fields': ['submissionnote', 'internalnote']},
        ]
        if self.instance.conference.videoproviders:
            fs.append(
                {'id': 'videolinks', 'legend': 'Video links', 'fields': self.instance.conference.videoproviders.split(',')},
            )
        return fs

    @property
    def json_form_fields(self):
        if self.conference.videoproviders:
            return {
                'videolinks': self.instance.conference.videoproviders.split(','),
            }
        return None

    def fix_fields(self):
        self.fields['track'].queryset = Track.objects.filter(conference=self.conference)
        self.fields['room'].queryset = Room.objects.filter(conference=self.conference)

        if self.instance.status != self.instance.lastnotifiedstatus and self.instance.speaker.exists():
            self.fields['status'].help_text = '<b>Warning!</b> This session has <a href="/events/admin/{0}/sessionnotifyqueue/">pending notifications</a> that have not been sent. You probably want to make sure those are sent before editing the status!'.format(self.conference.urlname)

        if not self.conference.skill_levels:
            self.remove_field('skill_level')
            self.update_protected_fields()

        if not self.conference.callforpaperstags:
            self.remove_field('tags')
            if 'tags' in self.selectize_multiple_fields:
                del self.selectize_multiple_fields['tags']
            self.update_protected_fields()
        else:
            self.selectize_multiple_fields['tags'] = SessionTagLookup(self.conference)

        if not self.conference.callforpapersrecording:
            self.remove_field('recordingconsent')
            self.update_protected_fields()
        else:
            self.fields['recordingconsent'].help_text = 'Whether this speaker has consented to being recorded or not for this session. This should NOT be updated manually in the normal cases, as it requires explicit consent from the speaker.'

        if self.conference.videoproviders:
            for f in self.conference.videoproviders.split(','):
                self.fields[f] = django.forms.CharField(label=f.title(), required=False)
                self.fields[f].delete_on_empty = True

    @classmethod
    def get_column_filters(cls, conference):
        return {
            'Status': [v for k, v in STATUS_CHOICES],
            'Track': Track.objects.filter(conference=conference),
            'Room': Room.objects.filter(conference=conference),
            'Cross sched': ['true', 'false', ],
        }

    @classmethod
    def get_assignable_columns(cls, conference):
        return [
            {
                'name': 'track',
                'title': 'Track',
                'options': [(t.id, t.trackname) for t in Track.objects.filter(conference=conference)],
                'canclear': True,
            },
            {
                'name': 'room',
                'title': 'Room',
                'options': [(r.id, r.roomname) for r in Room.objects.filter(conference=conference)],
                'canclear': True,
            },
            {
                'name': 'cross_schedule',
                'title': 'Cross schedule',
                'options': [(1, 'Yes'), (0, 'No'), ],
                'canclear': False,
            },
            {
                'name': 'status',
                'title': 'Status',
                'options': STATUS_CHOICES_LONG,
                'canclear': False,
            },
        ]

    @classmethod
    def assign_assignable_column(cls, obj, what, setval):
        if what == 'status':
            if setval not in valid_status_transitions[obj.status]:
                raise ValidationError("Status change from {} to {} is not valid.".format(
                    get_status_string(obj.status),
                    get_status_string(setval),
                ))
        super().assign_assignable_column(obj, what, setval)

    def clean(self):
        cleaned_data = super(BackendConferenceSessionForm, self).clean()

        if cleaned_data.get('starttime') and not cleaned_data.get('endtime'):
            self.add_error('endtime', 'End time must be specified if start time is!')
        elif cleaned_data.get('endtime') and not cleaned_data.get('starttime'):
            self.add_error('starttime', 'Start time must be specified if end time is!')
        elif cleaned_data.get('starttime') and cleaned_data.get('endtime'):
            if cleaned_data.get('endtime') < cleaned_data.get('starttime'):
                self.add_error('endtime', 'End time must be later than start time!')

        if cleaned_data.get('cross_schedule') and cleaned_data.get('room'):
            self.add_error('room', 'Room cannot be specified for cross schedule sessions!')

        return cleaned_data

    def clean_status(self):
        newstatus = self.cleaned_data.get('status')
        if newstatus == self.instance.status:
            return newstatus

        # If there are speakers on the session, we lock it to the workflow. For sessions
        # with no speakers, anything goes
        if not self.cleaned_data.get('speaker').exists():
            return newstatus

        if newstatus not in valid_status_transitions[self.instance.status]:
            raise ValidationError("Sessions with speaker cannot change from {0} to {1}. Only one of {2} is allowed.".format(
                get_status_string(self.instance.status),
                get_status_string(newstatus),
                ", ".join(["{0} ({1})".format(get_status_string(s), v) for s, v in list(valid_status_transitions[self.instance.status].items())]),
            ))

        return newstatus

    @classmethod
    def copy_from_conference(self, targetconf, sourceconf, idlist, transformform):
        xform = transformform.cleaned_data['timeshift']
        for id in idlist:
            source = ConferenceSession.objects.get(conference=sourceconf, pk=id)
            try:
                if source.track:
                    targettrack = Track.objects.get(conference=targetconf,
                                                    trackname=source.track.trackname)
                else:
                    targettrack = None
                s = ConferenceSession(conference=targetconf,
                                      title=source.title,
                                      starttime=source.starttime and source.starttime + xform,
                                      endtime=source.starttime and source.endtime + xform,
                                      track=targettrack,
                                      cross_schedule=source.cross_schedule,
                                      can_feedback=source.can_feedback,
                                      abstract=source.abstract,
                                      skill_level=source.skill_level,
                                      status=0,
                                      submissionnote=source.submissionnote,
                                      internalnote=source.internalnote,
                                      initialsubmit=source.initialsubmit,
                                      htmlicon=source.htmlicon,
                )
                s.save()
                for spk in source.speaker.all():
                    s.speaker.add(spk)

            except Track.DoesNotExist:
                yield 'Could not find track {0}'.format(source.track.trackname)

    @classmethod
    def get_transform_example(self, targetconf, sourceconf, idlist, transformform):
        xform = transformform.cleaned_data['timeshift']
        slotlist = ConferenceSession.objects.filter(conference=sourceconf, id__in=idlist, starttime__isnull=False)[:4]
        if slotlist:
            return ["{} becomes {}".format(timezone.localtime(s.starttime, sourceconf.tzobj), timezone.localtime(s.starttime + xform, targetconf.tzobj)) for s in slotlist]

        # Do we have sessions without time?
        slotlist = ConferenceSession.objects.filter(conference=sourceconf, id__in=idlist)
        if slotlist:
            return "No scheduled sessions picked, so no transformation will happen"
        return None


class SpeakerSessionManager:
    title = 'Sessions'
    singular = 'Session'

    def __init__(self, conference):
        self.conference = conference

    def get_list(self, instance):
        if instance.id:
            qs = instance.conferencesession_set.filter(conference=self.conference) if self.conference else instance.conferencesession_set.all()
            return [(
                s.id if self.conference else None,
                s.title,
                s.status_string if self.conference else '{} ({})'.format(s.status_string, s.conference),
            ) for s in qs.select_related('conference').only('id', 'title', 'status', 'conference__conferencename')]

    def get_form(self):
        return None


# When creating a speaker locally to a conference, it has to immediately be
# attached to a session, otherwise it will simply disappear and only be accessible
# from the global view.
class BackendNewSpeakerForm(BackendBeforeNewForm):
    helplink = 'schedule#speakers'
    session = django.forms.ChoiceField(choices=(), help_text="Pick an initial session to assign this speaker to. Per-conferences speakers cannot exist without a session.")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['session'].choices = ((s.id, "{} ({})".format(s.title, s.status_string)) for s in ConferenceSession.objects.filter(conference=self.conference))

    def get_newform_data(self):
        return self.cleaned_data['session']


class BackendGlobalSpeakerForm(BackendForm):
    helplink = 'schedule#speakers'
    list_fields = ['fullname', 'user', 'company', ]
    markdown_fields = ['abstract', ]
    exclude_fields_from_validation = ['photo512', ]
    # We must save the photo field as well, since it's being updaed in the pre_save signal,
    # and we want to include that updating.
    extra_update_fields = ['photo', ]
    selectize_single_fields = {
        'user': None,
    }
    linked_objects = OrderedDict({
        '../../sessions': None,
    })
    extrabuttons = [
        ('Merge into other speaker profile', 'merge/'),
    ]

    class Meta:
        model = Speaker
        fields = ['fullname', 'user', 'company', 'abstract', 'photo512', 'attributes', ]

        @classmethod
        def conference_queryset(cls, conference):
            # Ugly because django can't properly do exists
            return Speaker.objects.extra(
                where=("EXISTS (SELECT 1 FROM confreg_conferencesession_speaker css INNER JOIN confreg_conferencesession s ON s.id=css.conferencesession_id WHERE css.speaker_id=confreg_speaker.id AND s.conference_id=%s)", ),
                params=(conference.id, ),
            )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['user'].queryset = User.objects.order_by('username')
        self.fields['user'].label_from_instance = lambda obj: "{0} - {1} {2} <{3}>".format(obj.username, obj.first_name, obj.last_name, obj.email)
        self.linked_objects['../../sessions'] = SpeakerSessionManager(self.conference)

    @classmethod
    def get_initial(self):
        return {
            'speakertoken': generate_random_token()
        }


class BackendConferenceSpeakerForm(BackendGlobalSpeakerForm):
    readonly_fields = ['user', ]
    exclude_fields_from_validation = ['user', 'photo512', ]
    form_before_new = BackendNewSpeakerForm
    extrabuttons = []

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['user'].widget = StaticTextWidget()

    def fix_fields(self):
        self.initial['user'] = escape(self.instance._display_user({}))

    def post_save(self):
        if self.newformdata:
            # First one, so add this session
            session = ConferenceSession.objects.get(pk=self.newformdata, conference=self.conference)
            session.speaker.add(self.instance.id)


class BackendConferenceSessionSlotForm(BackendForm):
    helplink = 'schedule#slots'
    list_fields = ['starttime', 'endtime', ]
    allow_copy_previous = True
    copy_transform_form = BackendTransformConferenceDateTimeForm

    class Meta:
        model = ConferenceSessionScheduleSlot
        fields = ['starttime', 'endtime', ]

    @classmethod
    def copy_from_conference(self, targetconf, sourceconf, idlist, transformform):
        xform = transformform.cleaned_data['timeshift']
        for id in idlist:
            source = ConferenceSessionScheduleSlot.objects.get(conference=sourceconf, pk=id)
            ConferenceSessionScheduleSlot(conference=targetconf,
                                          starttime=source.starttime + xform,
                                          endtime=source.endtime + xform,
                                          ).save()
        return
        yield None  # Turn this into a generator

    @classmethod
    def get_transform_example(self, targetconf, sourceconf, idlist, transformform):
        xform = transformform.cleaned_data['timeshift']
        slotlist = ConferenceSessionScheduleSlot.objects.filter(conference=sourceconf, id__in=idlist)[:4]
        return ["{} becomes {}".format(timezone.localtime(s.starttime, sourceconf.tzobj), timezone.localtime(s.starttime + xform, targetconf.tzobj)) for s in slotlist]


class BackendMergeSpeakerForm(django.forms.Form):
    sourcespeaker = django.forms.CharField(
        label='Source speaker profile',
        widget=StaticTextWidget,
        required=False,
    )
    targetspeaker = django.forms.ModelChoiceField(
        label='Target speaker profile',
        queryset=Speaker.objects.order_by('fullname'),
        help_text='Pick the speaker profile to merge into. That profile will be kept, and the source profile will be deleted',
    )
    confirm = django.forms.BooleanField(label="Confirm", required=False)

    def __init__(self, sourcespeaker, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields['sourcespeaker'].initial = "{} ({} - {})".format(sourcespeaker.fullname, sourcespeaker.user, sourcespeaker.user.email if sourcespeaker.user else '*no user/email*')
        self.fields['targetspeaker'].label_from_instance = lambda obj: "{} ({} - {})".format(obj.fullname, obj.user, obj.user.email if obj.user else '*no user/email*')

    def clean_confirm(self):
        if not self.cleaned_data['confirm']:
            raise ValidationError("Please check this box to confirm that you really want to merge the speaker profile into this target profile, deleting the source profile.")
        return self.cleaned_data['confirm']


class BackendVolunteerSlotForm(BackendForm):
    helplink = 'volunteers#slots'
    list_fields = ['timerange', 'title', 'min_staff', 'max_staff', ]
    allow_copy_previous = True
    copy_transform_form = BackendTransformConferenceDateTimeForm

    class Meta:
        model = VolunteerSlot
        fields = ['timerange', 'title', 'min_staff', 'max_staff', ]
    coltypes = {
        'Min staff': ['nosearch', ],
        'Max staff': ['nosearch', ],
    }

    def clean(self):
        cleaned_data = super(BackendVolunteerSlotForm, self).clean()
        if cleaned_data.get('min_staff') > cleaned_data.get('max_staff'):
            self.add_error('max_staff', 'Max staff must be at least as high as min_staff!')

        return cleaned_data

    @classmethod
    def copy_from_conference(self, targetconf, sourceconf, idlist, transformform):
        xform = transformform.cleaned_data['timeshift']
        for id in idlist:
            source = VolunteerSlot.objects.get(conference=sourceconf, pk=id)
            VolunteerSlot(conference=targetconf,
                          timerange=DateTimeTZRange(source.timerange.lower + xform,
                                                    source.timerange.upper + xform),
                          title=source.title,
                          min_staff=source.min_staff,
                          max_staff=source.max_staff,
            ).save()
        return
        yield None  # Turn this into a generator

    @classmethod
    def get_transform_example(self, targetconf, sourceconf, idlist, transformform):
        xform = transformform.cleaned_data['timeshift']
        slotlist = VolunteerSlot.objects.filter(conference=sourceconf, id__in=idlist)[:4]

        return ["{} - {} becomes {} - {}".format(
            timezone.localtime(s.timerange.lower, sourceconf.tzobj),
            timezone.localtime(s.timerange.upper, sourceconf.tzobj),
            timezone.localtime(s.timerange.lower + xform, targetconf.tzobj),
            timezone.localtime(s.timerange.upper + xform, targetconf.tzobj),
        ) for s in slotlist]


class BackendFeedbackQuestionForm(BackendForm):
    helplink = 'feedback#conference'
    list_fields = ['newfieldset', 'question', 'sortkey', ]
    allow_copy_previous = True

    class Meta:
        model = ConferenceFeedbackQuestion
        fields = ['question', 'isfreetext', 'textchoices', 'sortkey', 'newfieldset']

    coltypes = {
        'Sortkey': ['nosearch', ],
        'Newfieldset': ['nosort', ],
    }
    defaultsort = [['sortkey', 'asc']]

    def clean(self):
        cleaned_data = super(BackendFeedbackQuestionForm, self).clean()
        if not self.cleaned_data.get('isfreetext', 'False'):
            if self.cleaned_data.get('textchoices', ''):
                self.add_error('textchoices', 'Textchoices can only be specified for freetext fields')
        return cleaned_data

    def clean_textchoices(self):
        val = self.cleaned_data['textchoices']
        if val:
            if ';' not in val:
                if ':' in val:
                    raise ValidationError('When choices are used, more than one option must be specified! It looks like you may have used colon instead of semicolon to separate values.')
                else:
                    raise ValidationError('When choices are used, than one must be specified!')
        return val

    @classmethod
    def copy_from_conference(self, targetconf, sourceconf, idlist):
        # Conference feedback questions are copied straight over, but we disallow duplicates
        for id in idlist:
            source = ConferenceFeedbackQuestion.objects.get(conference=sourceconf, pk=id)
            if ConferenceFeedbackQuestion.objects.filter(conference=targetconf, question=source.question).exists():
                yield 'A question {0} already exists.'.format(source.question)
            else:
                ConferenceFeedbackQuestion(conference=targetconf,
                                           question=source.question,
                                           isfreetext=source.isfreetext,
                                           textchoices=source.textchoices,
                                           sortkey=source.sortkey,
                                           newfieldset=source.newfieldset,
                                           ).save()


class BackendNewDiscountCodeForm(BackendBeforeNewForm):
    helplink = 'vouchers#discountcodes'
    codetype = django.forms.ChoiceField(choices=((1, 'Fixed amount discount'), (2, 'Percentage discount')))

    def get_newform_data(self):
        return self.cleaned_data['codetype']


class DiscountCodeUserManager(object):
    title = 'Users'
    singular = 'user'

    def get_list(self, instance):
        if instance.code:
            return [(r.id, r.fullname, "{} ({})".format(r.regtype.regtype if r.regtype else '', r.invoice_status)) for r in ConferenceRegistration.objects.filter(conference=instance.conference, vouchercode=instance.code)]
        return []

    def get_form(self):
        return None

    def get_object(self, masterobj, subjid):
        try:
            return ConferenceRegistration.objects.get(discountcode=blah, pk=subjid)
        except ConferenceRegistration.DoesNotExist:
            return None


class BackendDiscountCodeForm(BackendForm):
    helplink = 'vouchers#discountcodes'
    list_fields = ['code', 'validuntil', 'maxuses', 'public']
    linked_objects = OrderedDict({
        '../../regdashboard/list': DiscountCodeUserManager(),
    })

    form_before_new = BackendNewDiscountCodeForm

    exclude_date_validators = ['validuntil', ]

    class Meta:
        model = DiscountCode
        fields = ['code', 'discountamount', 'discountpercentage', 'regonly', 'public', 'validuntil', 'maxuses',
                  'requiresregtype', 'requiresoption']

    def fix_fields(self):
        if self.newformdata == "1" and not self.instance.discountamount:
            self.instance.discountamount = 1
        elif self.newformdata == "2" and not self.instance.discountpercentage:
            self.instance.discountpercentage = 1

        if self.instance.discountamount:
            # Fixed amount discount
            self.remove_field('discountpercentage')
            self.remove_field('regonly')
            self.fields['discountamount'].validators.append(MinValueValidator(1))
        else:
            # Percentage discount
            self.remove_field('discountamount')
            self.fields['discountpercentage'].validators.extend([
                MinValueValidator(1),
                MaxValueValidator(99),
            ])
        self.fields['maxuses'].validators.append(MinValueValidator(0))

        self.fields['requiresregtype'].queryset = RegistrationType.objects.filter(conference=self.conference)
        self.fields['requiresoption'].queryset = ConferenceAdditionalOption.objects.filter(conference=self.conference).exclude(pk=self.instance.pk)

        self.update_protected_fields()

    def clean_code(self):
        if self.instance.id:
            if DiscountCode.objects.filter(conference=self.conference, code=self.cleaned_data['code']).exclude(id=self.instance.id).exists():
                raise ValidationError("This discount code already exists for this conference")
        else:
            if DiscountCode.objects.filter(conference=self.conference, code=self.cleaned_data['code']).exists():
                raise ValidationError("This discount code already exists for this conference")

        return self.cleaned_data['code']


class BackendAccessTokenForm(BackendForm):
    helplink = 'tokens'
    list_fields = ['token', 'description', 'permissions', ]
    readonly_fields = ['token', ]

    class Meta:
        model = AccessToken
        fields = ['token', 'description', 'permissions', ]

    def _transformed_accesstoken_permissions(self):
        yamllist = ['yaml'] if has_yaml else []
        for k, v in AccessTokenPermissions:
            baseurl = '/events/admin/{0}/tokendata/{1}/{2}'.format(self.conference.urlname, self.instance.token, k)
            if k == 'schedule':
                formats = ['json', ] + yamllist
            elif k == 'sponsorlevels':
                formats = ['json'] + yamllist
            else:
                formats = ['csv', 'tsv', 'json', ] + yamllist
            yield k, mark_safe('{0} ({1})'.format(v, ", ".join(['<a href="{0}.{1}">{1}</a>'.format(baseurl, f) for f in formats])))

    def fix_fields(self):
        self.fields['permissions'].widget = django.forms.CheckboxSelectMultiple(
            choices=self._transformed_accesstoken_permissions(),
        )

    @classmethod
    def get_initial(self):
        return {
            'token': generate_random_token()
        }


class BackendNewsForm(BackendForm):
    helplink = 'news'
    list_fields = ['title', 'datetime', 'author', ]
    queryset_select_related = ['author', ]
    markdown_fields = ['summary', ]
    exclude_date_validators = ['datetime', ]
    defaultsort = [['datetime', "desc"]]

    class Meta:
        model = ConferenceNews
        fields = ['author', 'datetime', 'title', 'inrss', 'summary', ]

    def fix_fields(self):
        # Must be administrator on current conference
        self.fields['author'].queryset = NewsPosterProfile.objects.filter(author__conference=self.conference)
        # Add help hint dynamically so we can include the conference name
        self.fields['title'].help_text = 'Note! Title will be prefixed with "{0} - " on shared frontpage and RSS!'.format(self.conference.conferencename)


class BackendMessagingForm(BackendForm):
    helplink = 'integrations#messaging'
    list_fields = ['providername', 'broadcast', 'privatebcast', 'notification', 'orgnotification', 'socialmediamanagement', ]
    verbose_field_names = {
        'providername': 'Provider name',
    }
    queryset_extra_fields = {
        'providername': '(SELECT internalname FROM confreg_messagingprovider WHERE id=confreg_conferencemessaging.provider_id)',
    }

    @property
    def fieldsets(self):
        fs = [
            {'id': 'provider', 'legend': 'Provider', 'fields': ['provider', ]},
            {'id': 'actions', 'legend': 'Actions', 'fields': ['broadcast', 'privatebcast', 'notification', 'orgnotification', 'socialmediamanagement', ]},
        ]
        cf = list(self._channel_fields())
        if cf:
            fs.append(
                {'id': 'channels', 'legend': 'Channels/Groups', 'fields': list(self._channel_fields())}
            )
        return fs

    class Meta:
        model = ConferenceMessaging
        fields = ['provider', 'broadcast', 'privatebcast', 'notification', 'orgnotification', 'socialmediamanagement', ]

    def _channel_fields(self):
        for fld in ('privatebcast', 'orgnotification', 'socialmediamanagement'):
            if getattr(self.impl, 'can_{}'.format(fld), False):
                yield '{}channel'.format(fld)

    @property
    def readonly_fields(self):
        yield 'provider'
        for fld in ('broadcast', 'privatebcast', 'notification', 'orgnotification'):
            if not getattr(self.impl, 'can_{}'.format(fld), False):
                yield fld
        yield from self._channel_fields()

    def __init__(self, *args, **kwargs):
        self.impl = get_messaging(kwargs['instance'].provider)
        if hasattr(self.impl, 'refresh_messaging_config'):
            if self.impl.refresh_messaging_config(kwargs['instance'].config):
                kwargs['instance'].save(update_fields=['config'])
        super().__init__(*args, **kwargs)

    _channel_fieldnames = {
        'privatebcast': 'Attendee only broadcast channel',
        'orgnotification': 'Organisation notification channel',
        'socialmediamanagement': 'Social media management',
    }

    def fix_fields(self):
        super().fix_fields()
        self.fields['provider'].widget.attrs['disabled'] = True
        self.fields['provider'].required = False

        # Update the different types of supported fields
        for fld in ('broadcast', 'privatebcast', 'notification', 'orgnotification', 'socialmediamanagement'):
            if not getattr(self.impl, 'can_{}'.format(fld), False):
                self.fields[fld].widget.attrs['disabled'] = True
                self.fields[fld].help_text = 'Action is not supported by this provider'

        for fld in ('privatebcast', 'orgnotification', 'socialmediamanagement'):
            if getattr(self.impl, 'can_{}'.format(fld), False):
                self.fields['{}channel'.format(fld)] = self.impl.get_channel_field(self.instance, fld)
                self.fields['{}channel'.format(fld)].label = self._channel_fieldnames[fld]
                self.fields['{}channel'.format(fld)].required = False


class BackendSeriesMessagingNewForm(BackendBeforeNewForm):
    helplink = 'integrations#provider'
    classname = SelectSetValueField(choices=messaging_implementation_choices(),
                                    setvaluefield='baseurl', label='Implementation class')
    baseurl = django.forms.URLField(label='Base URL')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def get_newform_data(self):
        return '{}:{}'.format(self.cleaned_data['classname'], self.cleaned_data['baseurl'])

    def clean_baseurl(self):
        return self.cleaned_data['baseurl'].rstrip('/')

    def clean(self):
        d = super().clean()
        if 'classname' in d and 'baseurl' in d:
            # Both fields specified, so verify they're an allowed combination
            r = get_messaging_class(d['classname']).validate_baseurl(d['baseurl'])
            if r:
                self.add_error('baseurl', r)
        return d


class BackendSeriesMessagingForm(BackendForm):
    helplink = 'integrations#provider'
    list_fields = ['internalname', 'publicname', 'active', 'route_incoming', 'classname_short', ]
    queryset_select_related = ['route_incoming', ]
    form_before_new = BackendSeriesMessagingNewForm
    verbose_field_names = {
        'classname_short': 'Implementation',
    }
    queryset_extra_fields = {
        'classname_short': r"substring(classname, '[^\.]+$')",
    }
    auto_cascade_delete_to = ['conferencemessaging', ]

    config_fields = []
    config_fieldsets = []
    config_readonly_fields = []

    process_incoming = False
    no_incoming_processing = False

    class Meta:
        model = MessagingProvider
        fields = ['internalname', 'publicname', 'active', 'classname', 'route_incoming']

    @property
    def readonly_fields(self):
        return ['classname', ] + self.config_readonly_fields

    @property
    def json_form_fields(self):
        return {
            'config': self.config_fields,
        }

    @property
    def fieldsets(self):
        fs = [
            {'id': 'common', 'legend': 'Common', 'fields': ['internalname', 'publicname', 'active', 'classname'], },
        ]
        if self.process_incoming:
            fs.append(
                {'id': 'incoming', 'legend': 'Incoming', 'fields': ['route_incoming', ], }
            )

        return fs + self.config_fieldsets

    def fix_fields(self):
        super().fix_fields()
        if self.newformdata:
            classname, baseurl = self.newformdata.split(':', 1)
            self.instance.classname = classname
            self.initial['classname'] = classname
            self.baseurl = baseurl

        impl = get_messaging_class(self.instance.classname)
        if getattr(impl, 'can_process_incoming', False) and not self.no_incoming_processing:
            self.process_incoming = True
            self.fields['route_incoming'].queryset = Conference.objects.filter(series=self.instance.series)
            self.fields['route_incoming'].help_text = 'Incoming messages from this provider will be added to the specified conference'
        else:
            del self.fields['route_incoming']
            self.update_protected_fields()


#
# Form to pick a conference to copy from
#
class BackendCopySelectConferenceForm(django.forms.Form):
    conference = django.forms.ModelChoiceField(Conference.objects.all())

    def __init__(self, request, conference, model, *args, **kwargs):
        super(BackendCopySelectConferenceForm, self).__init__(*args, **kwargs)
        self.fields['conference'].queryset = Conference.objects.filter(Q(administrators=request.user) | Q(series__administrators=request.user)).exclude(pk=conference.pk).extra(
            where=["EXISTS (SELECT 1 FROM {0} WHERE conference_id=confreg_conference.id)".format(model._meta.db_table), ]
        ).distinct()


#
# Form for twitter integration
#
class BackendTweetQueueErrorLogManager:
    title = "Errors"
    singular = "error"
    can_add = False

    def get_list(self, instance):
        for e in ConferenceTweetQueueErrorLog.objects.filter(tweet=instance):
            yield None, e.ts, e.message


class BackendTweetQueueForm(BackendForm):
    status = django.forms.CharField(required=False, label="Post status", widget=StaticTextWidget)
    helplink = 'integrations#broadcast'
    list_fields = ['datetime', 'contents', 'author', 'approved', 'approvedby', 'sent', 'hasimage', 'comment', ]
    verbose_field_names = {
        'hasimage': 'Has image',
        'approvedby': 'Approved by',
    }
    exclude_date_validators = ['datetime', ]
    defaultsort = [['sent', 'asc'], ['datetime', 'desc']]
    exclude_fields_from_validation = ['image', ]
    queryset_select_related = ['author', 'approvedby', ]
    queryset_extra_fields = {
        'hasimage': "image is not null and image != ''",
    }
    queryset_extra_columns = ['errorcount', ]
    auto_cascade_delete_to = ['conferencetweetqueue_remainingtosend', ]
    linked_objects = OrderedDict({
        'errorlogs': BackendTweetQueueErrorLogManager(),
    })
    auto_cascade_delete_to = ['conferencetweetqueueerrorlog', 'conferencetweetqueue_remainingtosend']

    class Meta:
        model = ConferenceTweetQueue
        fields = ['datetime', 'approved', 'image', 'comment']
        widgets = {
            'contents': MonospaceTextarea,
        }

    @property
    def nosave_fields(self):
        if isinstance(self.instance.contents, dict):
            return ['social_{}'.format(mp.id) for mp in MessagingProvider.objects.only('id').filter(active=True, conferencemessaging__conference=self.conference, conferencemessaging__broadcast=True)] + ['status', ]
        else:
            return ['textcontents', 'status', ]

    @property
    def readonly_fields(self):
        if self.instance.sent:
            # If it's already sent, nothing gets to be edited
            return ['datetime', 'approved', 'image', 'comment', ] + self.nosave_fields
        return []

    def fix_fields(self):
        if self.instance and isinstance(self.instance.contents, dict):
            socials = []
            postedstatus = []
            for mp in MessagingProvider.objects.only('internalname', 'classname').filter(active=True, conferencemessaging__conference=self.conference, conferencemessaging__broadcast=True):
                impl = get_messaging_class(mp.classname)
                fn = 'social_{}'.format(mp.id)
                self.fields[fn] = django.forms.CharField(widget=django.forms.Textarea)
                self.fields[fn].label = mp.internalname
                self.fields[fn].initial = self.instance.contents.get(str(mp.id), '')
                if 'class' in self.fields[fn].widget.attrs:
                    self.fields[fn].widget.attrs['class'] += " textarea-with-charcount"
                else:
                    self.fields[fn].widget.attrs['class'] = "textarea-with-charcount"
                self.fields[fn].widget.attrs['data-length-function'] = 'shortened_post_length'
                self.fields[fn].help_text = "Max length: {}".format(impl.max_post_length)

                socials.append(fn)
                postedstatus.append((mp.internalname, 'Posted' if mp.id in self.instance.postids.values() else 'Not posted'))
            self.fields['status'].initial = '<br/>'.join("{}: {}".format(*p) for p in postedstatus)
            self.order_fields(['datetime', 'approved', ] + socials + ['image', 'comment', 'status'])
        else:
            self.fields['textcontents'] = django.forms.CharField(label='Contents')
            self.fields['textcontents'].initial = self.instance.contents
            self.fields['textcontents'].widget = TagOptionsTextWidget([h.hashtag for h in ConferenceHashtag.objects.filter(conference=self.conference)])
            if 'class' in self.fields['textcontents'].widget.attrs:
                self.fields['textcontents'].widget.attrs['class'] += " textarea-with-charcount"
            else:
                self.fields['textcontents'].widget.attrs['class'] = "textarea-with-charcount"
            self.fields['textcontents'].widget.attrs['data-length-function'] = 'shortened_post_length'

            if self.conference:
                lengthstr = 'Maximum lengths are: {}'.format(', '.join(['{}: {}'.format(mess.provider.internalname, get_messaging(mess.provider).max_post_length) for mess in self.conference.conferencemessaging_set.select_related('provider').filter(broadcast=True, provider__active=True)]))
            else:
                lengthstr = 'Maximum lengths are: {}'.format(', '.join(['{}: {}'.format(provider.internalname, get_messaging(provider).max_post_length) for provider in MessagingProvider.objects.filter(series__isnull=True, active=True)]))

            self.fields['textcontents'].help_text = lengthstr
            postedstatus = []
            for mp in MessagingProvider.objects.only('internalname', 'classname').filter(active=True, conferencemessaging__conference=self.conference, conferencemessaging__broadcast=True):
                postedstatus.append((mp.internalname, 'Posted' if mp.id in self.instance.postids.values() else 'Not posted'))
            self.fields['status'].initial = '<br/>'.join("{}: {}".format(*p) for p in postedstatus)
            self.order_fields(['datetime', 'approved', 'textcontents', 'image', 'comment', 'status'])

        if self.instance and self.instance.approved and not self.instance.sent and self.instance.errorcount > 0:
            self.fields['status'].initial += '<br/><br/>Next retry to post at {}'.format(
                # NOTE! The pow() formula should be kept in sync with util/messaging/sender.py
                self.instance.datetime + datetime.timedelta(seconds=pow(2.25, self.instance.errorcount + 4)),
            )

        self.update_protected_fields()

    def pre_create_item(self):
        # When creating a new item, we need to set contents to something not-null. For now,
        # new items are always single-provider, so set it to a string.
        self.instance.contents = ""

    def post_save(self):
        if isinstance(self.instance.contents, dict):
            updated = False
            for mp in MessagingProvider.objects.only('id').filter(active=True, conferencemessaging__conference=self.conference, conferencemessaging__broadcast=True):
                if self.instance.contents[str(mp.id)] != self.cleaned_data['social_{}'.format(mp.id)]:
                    self.instance.contents[str(mp.id)] = self.cleaned_data['social_{}'.format(mp.id)]
                    updated = True
            if updated:
                self.instance.save(update_fields=['contents'])
        else:
            if self.instance.contents != self.cleaned_data['textcontents']:
                self.instance.contents = self.cleaned_data['textcontents']
                self.instance.save(update_fields=['contents'])

    def clean_datetime(self):
        if self.instance:
            t = self.cleaned_data['datetime'].time()
            if self.conference and self.conference.twitter_timewindow_start and self.conference.twitter_timewindow_start != datetime.time(0, 0, 0):
                if t < self.conference.twitter_timewindow_start:
                    raise ValidationError("Tweets for this conference cannot be scheduled before {}".format(self.conference.twitter_timewindow_start))
            if self.conference and self.conference.twitter_timewindow_end:
                if t > self.conference.twitter_timewindow_end and self.conference.twitter_timewindow_end != datetime.time(0, 0, 0):
                    raise ValidationError("Tweets for this conference cannot be scheduled after {}".format(self.conference.twitter_timewindow_end))
            return self.cleaned_data['datetime']

    def clean_contents(self):
        d = self.cleaned_data['contents']
        shortlen = get_shortened_post_length(d)

        if self.conference:
            providers = [mess.provider for mess in self.conference.conferencemessaging_set.select_related('provider').filter(broadcast=True, provider__active=True)]
        else:
            providers = MessagingProvider.objects.filter(series__isnull=True, active=True)

        for provider in providers:
            impl = get_messaging(provider)
            if shortlen > impl.max_post_length:
                messages.warning(self.request, "Post will be truncated to {} characters on {}".format(impl.max_post_length, provider.internalname))
        return d

    @classmethod
    def get_assignable_columns(cls, conference):
        return [
            {
                'name': 'approved',
                'title': 'Approval',
                'options': [(1, 'Yes'), (0, 'No'), ],
                'canclear': False,
            },
        ]

    @classmethod
    def get_rowclass_and_title(self, obj, cache):
        if obj.sent:
            if obj.errorcount > 0:
                return "warning", "Sent, but had errors"
            else:
                return "info", "Sent"
        else:
            if obj.errorcount > 0:
                return "danger", "Errors present"

        ol = obj.is_overlength(cache)
        if ol:
            return "warning", ol

        return None, None

    @classmethod
    def get_column_filters(cls, conference):
        if conference:
            return {
                'Author': exec_to_single_list('SELECT DISTINCT username FROM confreg_conferencetweetqueue q INNER JOIN auth_user u ON u.id=q.author_id WHERE q.conference_id=%(confid)s', {'confid': conference.id, }),
                'Approved': ['true', 'false'],
                'Approved by': exec_to_single_list('SELECT DISTINCT username FROM confreg_conferencetweetqueue q INNER JOIN auth_user u ON u.id=q.approvedby_id WHERE q.conference_id=%(confid)s', {'confid': conference.id, }),
                'Sent': ['true', 'false'],
            }
        else:
            return {
                'Author': exec_to_single_list('SELECT DISTINCT username FROM confreg_conferencetweetqueue q INNER JOIN auth_user u ON u.id=q.author_id WHERE q.conference_id IS NULL'),
                'Approved': ['true', 'false'],
                'Approved by': exec_to_single_list('SELECT DISTINCT username FROM confreg_conferencetweetqueue q INNER JOIN auth_user u ON u.id=q.approvedby_id WHERE q.conference_id IS NULL'),
                'Sent': ['true', 'false'],
            }


class BackendHashtagForm(BackendForm):
    helplink = 'integrations#hashtags'
    list_fields = ['hashtag', 'sortkey', 'autoadd']

    class Meta:
        model = ConferenceHashtag
        fields = ['hashtag', 'sortkey', 'autoadd']


class TweetCampaignSelectForm(django.forms.Form):
    campaigntype = django.forms.ChoiceField(
        label='Campaign type',
        choices=[(id, c.name) for id, c in allcampaigns],
    )


#
# Form for confirming a registration
#
class ConfirmRegistrationForm(django.forms.Form):
    confirm = django.forms.BooleanField(help_text="Confirm that you want to confirm this registration!<br/>Normaly this is handled by the automated system, and registrations should only be manually confirmed in special cases!")


#
# Form for re-sending welcome email
#
class ResendWelcomeMailForm(django.forms.Form):
    confirm = django.forms.BooleanField(help_text="Confirm that you want to re-send the welcome email for this registration!")


#
# Form for re-sending attachment email
#
class ResendAttachMailForm(django.forms.Form):
    confirm = django.forms.BooleanField(help_text="Confirm that you want to re-send the attachment email for this registration!")


#
# Form for canceling a registration
#
class CancelRegistrationForm(django.forms.Form):
    refund = django.forms.ChoiceField(required=True, label="Method of refund")
    reason = django.forms.CharField(required=True, max_length=100, label="Reason for cancel",
                                    help_text="Copied directly into confirmation emails and refund notices!")
    confirm = django.forms.BooleanField(help_text="Confirm that you want to cancel this registration!")

    class Methods:
        NO_REFUND = -1

    def __init__(self, totalnovat, totalvat, refundchoices, *args, **kwargs):
        self.totalnovat = totalnovat
        self.totalvat = totalvat
        super(CancelRegistrationForm, self).__init__(*args, **kwargs)
        self.fields['refund'].choices = [(None, '-- Select method'), ] + refundchoices

        if 'refund' not in self.data:
            del self.fields['confirm']


#
# Form for canceling a conference invoice
#
class ConferenceInvoiceCancelForm(django.forms.Form):
    reason = django.forms.CharField(max_length=400, min_length=10, required=True, label="Reason for cancel",
                                    help_text="Specify the reason for canceling the invoice. Note that this reason is sent by email to the invoice recipient.")
    confirm = django.forms.BooleanField(help_text="Confirm that you really want to cancel this invoice!")

    def __init__(self, *args, **kwargs):
        super(ConferenceInvoiceCancelForm, self).__init__(*args, **kwargs)

        if 'reason' not in self.data:
            del self.fields['confirm']


#
# Form for refunding a purchased order
#
class PurchasedVoucherRefundForm(django.forms.Form):
    confirm = django.forms.BooleanField(label="Confirm", required=True)


#
# Form for refunding a multi registration
#
class BulkPaymentRefundForm(django.forms.Form):
    amount = django.forms.DecimalField(decimal_places=2, required=False, label="Refund amount (ex VAT)")
    vatamount = django.forms.DecimalField(decimal_places=2, required=False, label="Refund VAT amount")
    confirm = django.forms.BooleanField(label="Confirm", required=True)

    def __init__(self, invoice, *args, **kwargs):
        self.invoice = invoice
        super().__init__(*args, **kwargs)

        total = invoice.total_refunds

        self.fields['amount'].validators = [MinValueValidator(0), MaxValueValidator(total['remaining']['amount'])]
        if total['amount'] > 0:
            self.fields['amount'].help_text = '{}{} of {}{} remains to be refunded on this invoice.'.format(settings.CURRENCY_SYMBOL, total['remaining']['amount'], settings.CURRENCY_SYMBOL, invoice.total_amount - invoice.total_vat)
        else:
            self.fields['amount'].help_text = 'Invoice total value is {}{}'.format(settings.CURRENCY_SYMBOL, invoice.total_amount - invoice.total_vat)

        if not invoice.total_vat:
            del self.fields['vatamount']
        else:
            self.fields['vatamount'].validators = [MinValueValidator(0), MaxValueValidator(total['remaining']['vatamount'])]
            if total['vatamount'] > 0:
                self.fields['vatamount'].help_text = '{}{} of {}{} remains to be refunded on this invoice.'.format(settings.CURRENCY_SYMBOL, total['remaining']['vatamount'], settings.CURRENCY_SYMBOL, invoice.total_vat)
            else:
                self.fields['vatamount'].help_text = 'Invoice total value is {}{}'.format(settings.CURRENCY_SYMBOL, invoice.total_vat)


#
# Form for sending email
#
class BackendSendEmailForm(django.forms.Form):
    _from = django.forms.CharField(max_length=100, disabled=True, label="From")
    sendat = django.forms.DateTimeField(required=True, initial=timezone.now, label='Send at')
    subject = django.forms.CharField(max_length=128, required=True)
    recipients = django.forms.Field(widget=StaticTextWidget, required=False)
    message = django.forms.CharField(widget=EmailTextWidget, required=True)
    idlist = django.forms.CharField(widget=django.forms.HiddenInput, required=True)
    confirm = django.forms.BooleanField(label="Confirm", required=False)

    def __init__(self, conference, *args, **kwargs):
        super(BackendSendEmailForm, self).__init__(*args, **kwargs)
        self.conference = conference
        if not (self.data.get('subject') and self.data.get('message')):
            del self.fields['confirm']

        self.fields['subject'].help_text = 'Subject will be prefixed with <strong>[{}]</strong>'.format(conference)

    def clean_confirm(self):
        if not self.cleaned_data['confirm']:
            raise ValidationError("Please check this box to confirm that you are really sending this email! There is no going back!")

    def clean_subject(self):
        if not self.cleaned_data['subject']:
            raise ValidationError("Please enter a subject")

        # Max length of subject is 100, but we prefix with [] and a space
        maxlen = 100 - len(str(self.conference)) - 3

        if len(self.cleaned_data['subject']) > maxlen:
            raise ValidationError("Maximum length of subject is {}, to leave room for prefix. You entered {} characters.".format(maxlen, len(self.cleaned_data['subject'])))

        return self.cleaned_data['subject']


class BackendRegistrationDmForm(django.forms.Form):
    message = django.forms.CharField(max_length=500, required=True)

    def __init__(self, maxlength, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if maxlength:
            self.fields['message'].max_length = maxlength
            self.fields['message'].help_text = 'Maximum message length for this provider is {} characters.'.format(maxlength)