This repository was archived by the owner on Apr 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathmodels.py
More file actions
1454 lines (1182 loc) · 45.9 KB
/
models.py
File metadata and controls
1454 lines (1182 loc) · 45.9 KB
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
import datetime
import os
import os.path
import uuid
import pytz
from collections import defaultdict
from urllib.parse import urlencode
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import exceptions
from django.core.cache import cache
from django.urls import reverse
from django.db import models, transaction
from django.template.defaultfilters import slugify
from django.utils import timezone
from django.utils.deconstruct import deconstructible
from django.utils.translation import ugettext as _
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import (
GenericForeignKey,
GenericRelation
)
from django_extensions.db.fields.json import JSONField
import shortuuid
from model_utils import Choices
from model_utils.models import TimeStampedModel
from taggit.managers import TaggableManager
from taggit.models import GenericTaggedItemBase, ItemBase, TagBase
CURRENT_CONFERENCE_CACHE_KEY = 'CONFERENCE_CURRENT'
# ConferenceTag and ConferenceTaggedItem are used to create a "namesapce"
# for the related tags to a conference.
class ConferenceTagQuerySet(models.QuerySet):
def annotate_with_usage(self):
return self.annotate(
usage=models.Count('conference_conferencetaggeditem_items')
)
def order_by_usage(self, asc=False):
key = 'usage' if asc else '-usage'
return self.annotate_with_usage().order_by(key)
class ConferenceTag(TagBase):
objects = ConferenceTagQuerySet.as_manager()
category = models.CharField(max_length=50, default='', blank=True)
class Meta:
ordering = ['name']
def save(self, **kw):
if not self.pk:
try:
c = ConferenceTag.objects.get(name__iexact=self.name)
except ConferenceTag.DoesNotExist:
pass
else:
self.pk = c.pk
return
return super(ConferenceTag, self).save(**kw)
class ConferenceTaggedItem(GenericTaggedItemBase, ItemBase):
tag = models.ForeignKey(
ConferenceTag,
related_name="%(app_label)s_%(class)s_items",
on_delete=models.CASCADE,
)
class Meta:
verbose_name = _("Tagged Item")
verbose_name_plural = _("Tagged Items")
class ConferenceManager(models.Manager):
def current(self):
data = cache.get(CURRENT_CONFERENCE_CACHE_KEY)
a_week = 60 * 60 * 24 * 7
if data is None:
data = self.get(code=settings.CONFERENCE_CONFERENCE)
cache.set(CURRENT_CONFERENCE_CACHE_KEY, data, a_week)
return data
@classmethod
def clear_cache(cls, sender, **kwargs):
cache.delete(CURRENT_CONFERENCE_CACHE_KEY)
class Conference(models.Model):
code = models.CharField(max_length=10, primary_key=True)
name = models.CharField(max_length=100)
cfp_start = models.DateField(null=True, blank=True)
cfp_end = models.DateField(null=True, blank=True)
conference_start = models.DateField(null=True, blank=True)
conference_end = models.DateField(null=True, blank=True)
voting_start = models.DateField(null=True, blank=True)
voting_end = models.DateField(null=True, blank=True)
objects = ConferenceManager()
def __str__(self):
return self.code
def save(self, *args, **kwargs):
"""
Every time we make a change to any Conference, we should clear the
CONFERENCE_CURRENT cache.
"""
cache.delete(CURRENT_CONFERENCE_CACHE_KEY)
super(Conference, self).save(*args, **kwargs)
def days(self):
output = []
if self.conference_start and self.conference_end:
d = self.conference_start
step = datetime.timedelta(days=1)
while d <= self.conference_end:
output.append(d)
d += step
return output
def clean(self):
if self.conference_start and self.conference_end:
if self.conference_start > self.conference_end:
raise exceptions.ValidationError('Conference end must be > of conference start')
if self.cfp_start and self.cfp_end:
if self.cfp_start > self.cfp_end:
raise exceptions.ValidationError('Cfp end must be > of cfp start')
if self.voting_start and self.voting_end:
if self.voting_start > self.voting_end:
raise exceptions.ValidationError('Voting end must be > of voting start')
def cfp(self):
today = timezone.localtime().date()
try:
return self.cfp_start <= today <= self.cfp_end
except TypeError:
# there is no date, return False
return False
def voting(self):
today = timezone.localtime().date()
try:
return self.voting_start <= today <= self.voting_end
except TypeError:
# there is no date, return False
return False
def conference(self):
today = timezone.localtime().date()
return self.conference_start <= today <= self.conference_end
@property
def has_finished(self):
today = timezone.localtime().date()
return today > self.conference_end
class MultilingualContentManager(models.Manager):
def setContent(self, object, content, language, body):
if language is None:
language = settings.LANGUAGE_CODE.split('-', 1)[0]
object_type = ContentType.objects.get_for_model(object)
try:
mc = self.get(content_type=object_type, object_id=object.pk, content=content, language=language)
except MultilingualContent.DoesNotExist:
mc = MultilingualContent(content_object=object)
mc.content = content
mc.language = language
mc.body = body
mc.save()
def getContent(self, object, content, language):
if language is None:
language = settings.LANGUAGE_CODE.split('-', 1)[0]
object_type = ContentType.objects.get_for_model(object)
records = dict(
(x.language, x)
for x in self.exclude(body='').filter(content_type=object_type, object_id=object.pk, content=content)
)
try:
return records[language]
except KeyError:
if not records:
return None
else:
return records.get(settings.LANGUAGE_CODE, list(records.values())[0])
class MultilingualContent(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField(db_index=True)
content_object = GenericForeignKey('content_type', 'object_id')
language = models.CharField(max_length = 3)
content = models.CharField(max_length = 20)
body = models.TextField()
objects = MultilingualContentManager()
@deconstructible
class _fs_upload_to(object):
"""Deconstructible class to avoid django migrations' limitations on
python2. See https://code.djangoproject.com/ticket/22999 """
def __init__(self, subdir, attr=None, package='conference'):
self.subdir = subdir
self.attr = attr if attr is not None else 'slug'
self.package = package
def __call__(self, instance, filename):
fpath = os.path.join(
self.package,
self.subdir,
'%s%s' % (getattr(instance, self.attr), os.path.splitext(filename)[1].lower())
)
ipath = os.path.join(settings.MEDIA_ROOT, fpath)
if os.path.exists(ipath):
os.unlink(ipath)
return fpath
class AttendeeProfileManager(models.Manager):
def findSlugForUser(self, user):
name = '%s %s' % (user.first_name, user.last_name)
slug = slugify(name)
rows = self.filter(models.Q(slug=slug) | models.Q(slug__startswith=slug + '-'))\
.values_list('slug', flat=True)
last = None
for r in rows:
try:
counter = int(r.rsplit('-', 1)[1])
except (ValueError, IndexError):
if last is None:
# The protection from slug like "str-str-str"
last = 0
continue
if last is None or counter > last:
last = counter
if last is not None:
slug = '%s-%d' % (slug, last + 1)
elif not slug:
# if there is no slug, because the firstname or the lastname are empties,
# we will return '-1'
slug = '-1'
return slug
def randomUUID(self, length=6):
import string
import random
return ''.join(random.sample(string.ascii_letters + string.digits, length))
# TODO: Use the savepoint.
# Remember that, at least up to 1.4 django, SQLite backend does not support
# savepoint. Then you must move from cursor.execute(); if you ever pass to PostgreSQL
# Rememeber to roll back the savepoint in the except (or set the autocommit)
def getOrCreateForUser(self, user):
"""
Returns or create the associated profile
"""
try:
p = AttendeeProfile.objects.get(user=user)
except AttendeeProfile.DoesNotExist:
p = AttendeeProfile(user=user)
else:
return p
from django.db import IntegrityError
slug = None
uuid = None
while True:
if slug is None:
slug = self.findSlugForUser(user)
if uuid is None:
uuid = self.randomUUID()
p.slug = slug
p.uuid = uuid
try:
p.save()
except IntegrityError as e:
msg = str(e)
if 'uuid' in msg:
uuid = None
elif 'slug' in msg:
slug = None
else:
raise
else:
break
return p
ATTENDEEPROFILE_VISIBILITY = Choices(
("p", "PUBLIC", "Publicly available"),
("m", "PARTICIPANTS_ONLY", "Visible to EuroPython attendees"),
("x", "PRIVATE", "Visible only to you"),
)
ATTENDEEPROFILE_GENDER = Choices(
("m", "MALE", "Male"),
("f", "FEMALE", "Female"),
("o", "OTHER", "Other"),
("x", "PREFER_NOT_TO_SAY", "Prefer not to say"),
)
class AttendeeProfile(models.Model):
"""
It's the profile of a participant (including the speaker) at the conference, there is a connection
to the auth.User via a ForeignKey.
"""
user = models.OneToOneField(get_user_model(), primary_key=True, on_delete=models.CASCADE)
slug = models.SlugField(unique=True)
uuid = models.CharField(max_length=6, unique=True)
image = models.ImageField(upload_to=_fs_upload_to('profile'), blank=True)
# NOTE(artcz): This is currently deprecated field, replaced with is_minor
# because that's what we basically used it for.
birthday = models.DateField(_('Birthday'), null=True, blank=True)
# minor == <18 years old.
is_minor = models.BooleanField(default=False)
phone = models.CharField(
_('Phone'),
max_length=30, blank=True,
help_text=_(
"We require a mobile phone number for all speakers "
"for last minute contacts and in case we need "
"timely clarification (if no reponse to previous emails). "
"Use the international format (e.g.: +44 123456789)."
),
)
gender = models.CharField(
max_length=1, choices=ATTENDEEPROFILE_GENDER,
help_text=_(
"We use this information for statistics related to conference "
"attendance diversity."
)
)
personal_homepage = models.URLField(_('Personal homepage'), blank=True)
company = models.CharField(_('Company'), max_length=50, blank=True)
company_homepage = models.URLField(_('Company homepage'), blank=True)
job_title = models.CharField(_('Job title'), max_length=50, blank=True)
location = models.CharField(_('Location'), max_length=100, blank=True)
bios = GenericRelation(MultilingualContent)
visibility = models.CharField(max_length=1, choices=ATTENDEEPROFILE_VISIBILITY, default='x')
objects = AttendeeProfileManager()
def __str__(self):
return self.slug
def clean(self):
from django.core.exceptions import ValidationError
if self.visibility != 'p':
if TalkSpeaker.objects.filter(speaker__user=self.user_id, talk__status='accepted').exists():
raise ValidationError('This profile must be public')
def setBio(self, body, language=None):
MultilingualContent.objects.setContent(self, 'bios', language, body)
def getBio(self, language=None):
return MultilingualContent.objects.getContent(self, 'bios', language)
class SpeakerManager(models.Manager):
def byConference(self, conf, only_accepted=True, talk_type=None):
"""
Return the speakers from a conference
"""
qs = TalkSpeaker.objects\
.filter(talk__conference=conf)\
.values('speaker')
if only_accepted:
qs = qs.filter(talk__status='accepted')
if talk_type:
if isinstance(talk_type, (list, tuple)):
qs = qs.filter(talk__type__in=talk_type)
else:
qs = qs.filter(talk__type=talk_type)
return Speaker.objects.filter(user__in=qs)
class Speaker(models.Model):
user = models.OneToOneField(get_user_model(), primary_key=True, on_delete=models.CASCADE)
objects = SpeakerManager()
def __str__(self):
return '%s %s' % (self.user.first_name, self.user.last_name)
def talks(self, conference=None, include_secondary=True, status=None):
"""
Build a QueryBuilder, try to fetch all the talks for the current speaker,
in function of the status and the selected conference.
"""
qs = TalkSpeaker.objects.filter(speaker=self)
if status in TALK_STATUS._db_values:
qs = qs.filter(talk__status=status)
elif status is not None:
raise ValueError('status unknown')
if not include_secondary:
qs = qs.filter(helper=False)
if conference is not None:
qs = qs.filter(talk__conference=conference)
return Talk.objects.filter(id__in=qs.values('talk'))
TALK_LANGUAGES = settings.LANGUAGES
TALK_STATUS = Choices(
('proposed', _('Proposed')),
('accepted', _('Accepted')),
('canceled', _('Canceled')),
('waitlist', _('Waitlist')),
('declined', _('Declined')),
)
VIDEO_TYPE = (
('viddler_oembed', 'oEmbed (Youtube, Vimeo, ...)'),
('download', 'Download'),
)
TALK_LEVEL = Choices(
('beginner', _('Beginner')),
('intermediate', _('Intermediate')),
('advanced', _('Advanced')),
)
class TalkQuerySet(models.QuerySet):
def _filter_by_status(self, status, conference=None):
assert status in TALK_STATUS._db_values
qs = self.filter(status=status)
if conference:
qs = qs.filter(conference=conference)
return qs
def proposed(self, conference=None):
return self._filter_by_status(TALK_STATUS.proposed, conference=conference)
def accepted(self, conference=None):
return self._filter_by_status(TALK_STATUS.accepted, conference=conference)
def canceled(self, conference=None):
return self._filter_by_status(TALK_STATUS.canceled, conference=conference)
def waitlist(self, conference=None):
return self._filter_by_status(TALK_STATUS.waitlist, conference=conference)
def createFromTitle(self, title, sub_title, conference, speaker,
prerequisites, abstract_short, abstract_extra,
status=TALK_STATUS.proposed, language='en',
level=TALK_LEVEL.beginner,
domain_level=TALK_LEVEL.beginner,
domain='',
training_available=False,
type='t_30'):
slug = slugify(title)
talk = Talk()
talk.title = title
talk.domain = domain
talk.domain_level = domain_level
talk.sub_title = sub_title
talk.prerequisites = prerequisites
talk.abstract_short = abstract_short
talk.conference = conference
talk.status = status
talk.language = language
talk.level = level
talk.abstract_extra = abstract_extra
talk.training_available = training_available
talk.type = type
with transaction.atomic():
count = 0
check = slug
while True:
if self.filter(slug=check).count() == 0:
break
count += 1
check = '%s-%d' % (slug, count)
talk.slug = check
talk.save()
# FIXME: This part should be done in one transaction.
TalkSpeaker(talk=talk, speaker=speaker).save()
return talk
# Previous definition of TALK_TYPE, kept around, since some of the
# code in the system uses the codes to checks.
#
# TALK_TYPE = (
# ('t', 'Talk'),
# ('i', 'Interactive'),
# ('r', 'Training'),
# ('p', 'Poster session'),
# ('n', 'Panel'),
# ('h', 'Help desk'),
# )
# Talk types combined with duration. Note that the system uses the
# first character to identify the generic talk type, so these should
# not be changed from the ones listed above.
TALK_TYPE = [
('t_30', 'Talk (30 mins)'),
('t_45', 'Talk (45 mins)'),
('t_60', 'Talk (60 mins)'),
('i_60', 'Interactive (60 mins)'),
('r_180', 'Training (180 mins)'),
('p_45', 'Poster session (45 mins)'),
('p_180', 'Poster session (180 mins)'),
('n_60', 'Panel (60 mins)'),
('n_90', 'Panel (90 mins)'),
('h_180', 'Help desk (180 mins)'),
]
TALK_TYPE_CHOICES = Choices(*TALK_TYPE)
# Copy of TALK_TYPE, which defines the types that can be chosen in the
# CFP. The other types remain available via the Django admin.
CFP_TALK_TYPE = [
('t_30', 'Talk (30 mins)'),
('t_45', 'Talk (45 mins)'),
#('t_60', 'Talk (60 mins)'),
('i_60', 'Interactive (60 mins)'),
('r_180', 'Training (180 mins)'),
('p_45', 'Poster session (45 mins)'),
#('p_180', 'Poster session (180 mins)'),
('n_60', 'Panel (60 mins)'),
#('n_90', 'Panel (90 mins)'),
('h_180', 'Help desk (180 mins)'),
]
# Mapping of TALK_TYPE to duration in minutes
TALK_DURATION = {
't_30': 30,
't_45': 45,
't_60': 60,
'i_60': 60,
'r_180': 180,
'p_45': 45,
'p_180': 180,
'n_60': 60,
'n_90': 90,
'h_180': 180,
}
# Admin talk entries
#
# These are usually not eligible for speaker coupons. See the
# create_speaker_coupons.py script for details.
#
TALK_ADMIN_TYPE = (
('o', 'Opening session'),
('c', 'Closing session'),
('l', 'Lightning talk'),
('k', 'Keynote'),
('r', 'Recruiting session'),
('m', 'EPS session'),
('p', 'Community session'),
('s', 'Open space'),
('e', 'Social event'),
('x', 'Reserved slot'),
('z', 'Sponsored session'),
)
def random_shortuuid():
return shortuuid.ShortUUID().random(length=7)
class Talk(models.Model):
# CharField because sqlite
uuid = models.CharField(
# FIXME(artcz)
# unique-False because we have al ot of old talks without uuid
# will update this later once we add some uuids on production
unique=False,
max_length=40,
default=random_shortuuid,
editable=False,
)
created_by = models.ForeignKey(get_user_model(), blank=True, null=True, on_delete=models.deletion.PROTECT)
title = models.CharField("Talk title", max_length=80)
sub_title = models.CharField(
"Sub title", max_length=1000, default="", blank=True
)
slug = models.SlugField(max_length=100, unique=True)
prerequisites = models.CharField(
"Prerequisites",
help_text="What should attendees know already",
default="",
blank=True,
max_length=150,
)
conference = models.CharField(
help_text="name of the conference", max_length=20
)
admin_type = models.CharField(
max_length=1, choices=TALK_ADMIN_TYPE, blank=True
)
speakers = models.ManyToManyField(Speaker, through="TalkSpeaker")
language = models.CharField(
"Language", max_length=3, choices=TALK_LANGUAGES, default="en"
)
abstracts = GenericRelation(
MultilingualContent,
verbose_name=_("Talk abstract"),
help_text=_(
"Please enter a short description of the talk you are submitting. "
"Be sure to includes the goals of your talk and any prerequisite "
"required to fully understand it.\n"
"Suggested size: two or three paragraphs."
),
)
abstract_short = models.TextField(
verbose_name="Talk abstract short",
help_text=(
"Please enter a short description of the talk you are submitting."
),
default="",
)
abstract_extra = models.TextField(
verbose_name=_("Talk abstract extra"),
help_text=_("<p>Please enter instructions for attendees.</p>"),
blank=True,
default="",
)
availability = models.TextField(
verbose_name=_('Timezone availability'),
help_text=_('<p>Please enter your time availability.</p>'),
blank=True,
default='',
)
slides = models.FileField(upload_to=_fs_upload_to("slides"), blank=True)
slides_url = models.URLField(blank=True)
repository_url = models.URLField(blank=True)
video_type = models.CharField(
max_length=30, choices=VIDEO_TYPE, blank=True
)
video_url = models.TextField(blank=True)
video_file = models.FileField(
upload_to=_fs_upload_to("videos"), blank=True
)
teaser_video = models.URLField(
_("Teaser video"),
blank=True,
help_text=_("Insert the url for your teaser video"),
)
status = models.CharField(
max_length=8, choices=TALK_STATUS, default=TALK_STATUS.proposed
)
# TODO: should be renamed to python_level,
# because we added also domain_level
level = models.CharField(
_("Audience Python level"),
default="beginner",
max_length=12,
choices=TALK_LEVEL,
)
training_available = models.BooleanField(default=False)
type = models.CharField(
max_length=5, choices=TALK_TYPE_CHOICES, default=TALK_TYPE_CHOICES.t_30
)
domain = models.CharField(
max_length=20,
choices=settings.CONFERENCE_TALK_DOMAIN,
default=settings.CONFERENCE_TALK_DOMAIN.other,
blank=True,
)
domain_level = models.CharField(
_("Audience Domain Level"),
default=TALK_LEVEL.beginner,
max_length=12,
choices=TALK_LEVEL, # using the same Choices as regular talk level
)
duration = models.IntegerField(
_("Duration"),
default=0,
help_text=_(
"This is the duration of the talk. "
"Set to 0 to use the default talk duration."
),
)
# Suggested Tags, normally, should use a submission model.
suggested_tags = models.CharField(max_length=100, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True, null=True)
tags = TaggableManager(through=ConferenceTaggedItem)
objects = TalkQuerySet.as_manager()
class Meta:
ordering = ["title"]
def save(self, *args, **kwargs):
# Handle duration customizations (if any)
if self.pk is not None:
# Use 0 duration if the type changed
old_object = Talk.objects.get(pk=self.pk)
if old_object.type != self.type:
self.duration = 0
if self.duration == 0:
# Use the talk type's default value in case the duration was
# set to 0
self.duration = TALK_DURATION[self.type]
super(Talk, self).save(*args, **kwargs)
def __str__(self):
return "%s [%s][%s][%s]" % (
self.title,
self.conference,
self.language,
self.duration,
)
def get_absolute_url(self):
return reverse("talks:talk", args=[self.slug])
def get_schedule_url(self):
event = self.get_event()
if not event:
return None
url = event.schedule.get_absolute_url()
slug = urlencode({'selected': self.slug})
time = event.get_utc_start_datetime().strftime('%H:%M-UTC')
return f"{url}?{slug}#{time}"
def get_slides_url(self):
""" Return the slides URL (relative to the website)
or None in case no slides are available.
For externally hosted slides, the URL refers to an absolute URL
(anything the speaker entered).
"""
if self.slides and self.slides.url:
return self.slides.url or None
else:
return self.slides_url or None
def get_admin_url(self):
return reverse("admin:conference_talk_change", args=[self.id])
get_url_path = get_absolute_url
def get_event(self):
try:
return self.event_set.all()[0]
except IndexError:
return None
def get_event_list(self):
try:
return self.event_set.all()
except IndexError:
return None
def get_all_speakers(self):
return self.speakers.all()
def setAbstract(self, body, language=None):
MultilingualContent.objects.setContent(
self, "abstracts", language, body
)
def getAbstract(self, language=None):
# TODO: this method should always return the contents body
# and the get_abstract method should be retired
return MultilingualContent.objects.getContent(
self, "abstracts", language
)
def get_abstract(self, language=None):
abstract = MultilingualContent.objects.getContent(
self, "abstracts", language
)
if abstract:
return abstract.body
else:
return self.abstract_short
def set_availability(self, values, language=None):
encoded_value = '|'.join(values)
self.availability = encoded_value
def get_availability(self):
return self.availability.split('|')
class TalkSpeaker(models.Model):
talk = models.ForeignKey(Talk, on_delete=models.CASCADE)
speaker = models.ForeignKey(Speaker, on_delete=models.CASCADE)
helper = models.BooleanField(default=False)
class Meta:
unique_together = (('talk', 'speaker'),)
def __str__(self):
return f'[{self.speaker.user}] for {self.talk.title}'
class FareQuerySet(models.QuerySet):
def available(self, conference=None):
today = timezone.localtime().date()
q1 = models.Q(start_validity=None, end_validity=None)
q2 = models.Q(start_validity__lte=today, end_validity__gte=today)
qs = self.filter(q1 | q2)
if conference:
qs = qs.filter(conference=conference)
return qs
# TODO(artcz) Convert those to Choices for easier enum-like interface
FARE_TICKET_TYPES = Choices(
('conference', 'Conference ticket'),
('partner', 'Partner Program'),
('event', 'Event'),
('other', 'Other'),
)
FARE_PAYMENT_TYPE = (
('p', 'Payment'),
('v', 'Voucher'),
('d', 'Deposit'),
)
FARE_TYPES = Choices(
('c', 'company', 'Company'),
('p', 'personal', 'Personal'),
('s', 'student', 'Student'),
)
class Fare(models.Model):
conference = models.CharField(help_text='Conference code', max_length=20)
code = models.CharField(max_length=10)
name = models.CharField(max_length=100)
description = models.TextField()
price = models.DecimalField(max_digits=6, decimal_places=2)
start_validity = models.DateField(null=True, blank=True)
end_validity = models.DateField(null=True, blank=True)
recipient_type = models.CharField(
max_length=1,
choices=FARE_TYPES,
default=FARE_TYPES.personal,
)
ticket_type = models.CharField(max_length=10, choices=FARE_TICKET_TYPES, default='conference', db_index=True)
payment_type = models.CharField(max_length=1, choices=FARE_PAYMENT_TYPE, default='p')
blob = models.TextField(blank=True)
objects = FareQuerySet.as_manager()
def __str__(self):
return '%s - %s' % (self.code, self.conference)
class Meta:
unique_together = (('conference', 'code'),)
def valid(self):
# numb = len(list(Ticket.objects.all()))
today = timezone.localtime().date()
try:
validity = self.start_validity <= today <= self.end_validity
except TypeError:
# if we have TypeError that probably means either start or end (or
# both) are set to None. That by default means fare is invalid
# right now.
validity = False
# validity = numb < settings.MAX_TICKETS
return validity
def fare_type(self):
""" Return the fare type based on the .recipient_type
"""
return dict(FARE_TYPES).get(self.recipient_type, 'Regular')
def calculated_price(self, qty=1, **kw):
params = dict(kw)
params['qty'] = qty
calc = {
'total': self.price * qty,
'params': params,
}
return calc['total']
def create_tickets(self, user):
""" Creates and returns the tickets associated with this rate.
Normally each fare involves just one ticket, but this
behavior can be modified by a listener attached to the
signal fare_tickets.
The instances returned by this method have an additional
attribute `fare_description` (volatile) and contains a
description of the fare specific for the single ticket.
"""
from conference.listeners import fare_tickets
params = {
'user': user,
'tickets': []
}
fare_tickets.send(sender=self, params=params)
if not params['tickets']:
t = Ticket(user=user, fare=self)
t.fare_description = self.name
t.save()
params['tickets'].append(t)
return params['tickets']
@property
def is_conference(self):
"""
Whether it's a conference ticket and is configurable.
"""
return self.ticket_type == FARE_TICKET_TYPES.conference
class TicketQuerySet(models.QuerySet):
def conference(self, conference):
return self.filter(fare__conference=conference)
TICKET_TYPE = (
('standard', 'standard'),
('staff', 'staff'),
)
class Ticket(models.Model):
user = models.ForeignKey(
get_user_model(),
help_text=_('Ticket assignee'),
on_delete=models.CASCADE
)
name = models.CharField(
max_length=60,
blank=True,
help_text=_('Attendee name, i.e. the person who will attend the conference.'))
fare = models.ForeignKey(Fare, on_delete=models.CASCADE)
frozen = models.BooleanField(
default=False,
verbose_name=_('ticket canceled / invalid / frozen'),
help_text=_(
'If a ticket was canceled or otherwise needs to be marked as '
'invalid, please check this checkbox to indicate this.'
),
)
ticket_type = models.CharField(max_length=8, choices=TICKET_TYPE, default='standard')
objects = TicketQuerySet.as_manager()
def __str__(self):
return 'Ticket "%s" (%s)' % (self.fare.name, self.fare.code)
@property
def assigned_email(self):
if getattr(self, 'p3_conference', None):
return self.p3_conference.assigned_to
return ''
@property
def buyer(self):