11import random
22import string
3+ from statistics import median , StatisticsError
34
5+ from django .conf import settings
6+ from django .core .validators import MaxValueValidator , MinValueValidator
47from django .db import models
5- from django import forms
6- from django .forms import ModelForm
8+ from django .db .models import CASCADE
79from markdownx .models import MarkdownxField
810from slugify import slugify
11+ from taggit .managers import TaggableManager
12+ from . import const
913
10- TALK = 1
11- WORKSHOP = 2
12- TYPE_CFP = (
13- (TALK , 'Talk' ),
14- (WORKSHOP , 'Workshop' ),
15- )
1614
1715class Cfp (models .Model ):
1816 name = models .CharField (max_length = 256 )
@@ -24,9 +22,16 @@ class Cfp(models.Model):
2422 description = MarkdownxField ()
2523 accepted = models .BooleanField (default = False )
2624 slug = models .CharField (unique = True , blank = True , max_length = 100 )
27- type = models .IntegerField (choices = TYPE_CFP , default = TALK )
25+ type = models .IntegerField (choices = const . TYPE_CFP , default = const . TALK )
2826 duration = models .CharField (max_length = 100 , null = True )
2927
28+ @property
29+ def rating (self ):
30+ try :
31+ return median (self .ratings .all ().values_list ('mark' ))
32+ except StatisticsError :
33+ return "N/A"
34+
3035 def __str__ (self ):
3136 return '{}: "{}" by {} - [{}]' .format (self .get_type_display (), self .title , self .name , 'Accepted' if self .accepted else 'Pending' )
3237
@@ -38,36 +43,12 @@ def save(self, *args, **kwargs):
3843 super (Cfp , self ).save (* args , ** kwargs )
3944
4045
41- class CfpForm (ModelForm ):
42- name = forms .CharField (widget = forms .TextInput (attrs = {'placeholder' : 'Name' , 'class' : 'form-control' }),
43- max_length = 256 , error_messages = {'required' : 'Please, enter your name.' }, label = '' )
44- company = forms .CharField (widget = forms .TextInput (attrs = {'placeholder' : 'Company' , 'class' : 'form-control' }),
45- max_length = 100 , required = False , label = '' )
46- email = forms .EmailField (widget = forms .TextInput (attrs = {'placeholder' : 'Email' , 'class' : 'form-control' }),
47- error_messages = {'required' : 'Please, enter a valid email address.' ,
48- 'invalid' : 'Please enter a valid email address.' }, label = '' )
49- personal_website = forms .CharField (widget = forms .TextInput (attrs = {'placeholder' : 'Personal Website (URL)' , 'class' : 'form-control' }),
50- max_length = 100 , required = False , label = '' )
51- linkedin = forms .CharField (widget = forms .TextInput (attrs = {'placeholder' : 'Linkedin (URL)' , 'class' : 'form-control' }),
52- max_length = 100 , required = False , label = '' )
53- title = forms .CharField (widget = forms .TextInput (attrs = {'placeholder' : 'Title of your proposal' , 'class' : 'form-control' }),
54- error_messages = {'required' : 'Please, enter the title.' }, label = '' )
55- duration = forms .CharField (widget = forms .TextInput (attrs = {'placeholder' : 'Duration (E.g. 2h, 30min, 1:30, etc.)' , 'class' : 'form-control' }),
56- error_messages = {'required' : 'Please, enter the duration.' }, label = '' )
57- description = forms .CharField (widget = forms .Textarea (attrs = {'placeholder' : 'Description of your proposal' , 'class' : 'form-control' }),
58- error_messages = {'required' : 'Please, enter the description of your proposal.' }, label = '' )
59- type = forms .ChoiceField (choices = TYPE_CFP , widget = forms .Select ())
46+ class CFPRating (models .Model ):
47+ mark = models .IntegerField (validators = [MaxValueValidator (10 , "Maximum rating is 10" ), MinValueValidator (0 , "Minimum Rating is 10" )])
48+ cfp = models .ForeignKey (Cfp , related_name = "ratings" , on_delete = CASCADE )
49+ user = models .ForeignKey (getattr (settings , "AUTH_USER_MODEL" ), on_delete = CASCADE )
50+ tags = TaggableManager ()
6051
6152 class Meta :
62- model = Cfp
63- fields = (
64- 'name' ,
65- 'company' ,
66- 'email' ,
67- 'personal_website' ,
68- 'linkedin' ,
69- 'title' ,
70- 'duration' ,
71- 'description' ,
72- 'type' ,
73- )
53+ unique_together = (("user" , "cfp" ,),)
54+
0 commit comments