1
|
|
|
|
2
|
|
|
import json |
3
|
|
|
from dateutil.rrule import rrule, MONTHLY |
4
|
|
|
from django.contrib.auth.models import User |
5
|
|
|
from django.db import models |
6
|
|
|
from django.db.models.signals import post_save |
7
|
|
|
from django.utils.functional import cached_property |
8
|
|
|
|
9
|
|
|
from slugify import slugify |
10
|
|
|
from wye.base.constants import WorkshopLevel, WorkshopStatus |
11
|
|
|
from wye.regions.models import Location, State |
12
|
|
|
from wye.workshops.models import Workshop, WorkshopSections |
13
|
|
|
from wye.organisations.models import Organisation |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class UserType(models.Model): |
17
|
|
|
''' |
18
|
|
|
USER_TYPE = ['Tutor', 'Regional Lead', 'College POC','admin'] |
19
|
|
|
''' |
20
|
|
|
slug = models.CharField(max_length=100, |
21
|
|
|
verbose_name="slug") |
22
|
|
|
display_name = models.CharField( |
23
|
|
|
max_length=300, verbose_name="Display Name") |
24
|
|
|
active = models.BooleanField(default=1) |
25
|
|
|
|
26
|
|
|
class Meta: |
27
|
|
|
db_table = 'users_type' |
28
|
|
|
verbose_name = 'UserType' |
29
|
|
|
verbose_name_plural = 'UserTypes' |
30
|
|
|
ordering = ('-id',) |
31
|
|
|
|
32
|
|
|
def __str__(self): |
33
|
|
|
return '{}'.format(self.display_name) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
class Profile(models.Model): |
37
|
|
|
user = models.OneToOneField(User, primary_key=True, related_name='profile') |
38
|
|
|
mobile = models.CharField(max_length=10, blank=False, null=True) |
39
|
|
|
is_mobile_visible = models.BooleanField(default=False) |
40
|
|
|
is_email_visible = models.BooleanField(default=False) |
41
|
|
|
usertype = models.ManyToManyField(UserType, null=True) |
42
|
|
|
enable_notifications = models.BooleanField( |
43
|
|
|
default=True, |
44
|
|
|
verbose_name=u"Email Notification") |
45
|
|
|
|
46
|
|
|
interested_sections = models.ManyToManyField(WorkshopSections) |
47
|
|
|
interested_level = models.PositiveSmallIntegerField( |
48
|
|
|
choices=WorkshopLevel.CHOICES, |
49
|
|
|
verbose_name="Interested Workshop Level", |
50
|
|
|
null=True, blank=True) |
51
|
|
|
interested_locations = models.ManyToManyField( |
52
|
|
|
Location, null=True, blank=True) |
53
|
|
|
interested_states = models.ManyToManyField( |
54
|
|
|
State, null=True, blank=True, |
55
|
|
|
verbose_name=u"Interested State *") |
56
|
|
|
location = models.ForeignKey( |
57
|
|
|
Location, related_name="user_location", null=True) |
58
|
|
|
work_location = models.TextField( |
59
|
|
|
null=True, blank=True, verbose_name=u"Present Company") |
60
|
|
|
work_experience = models.FloatField( |
61
|
|
|
null=True, blank=True, verbose_name=u"Work Experience") |
62
|
|
|
no_workshop = models.IntegerField( |
63
|
|
|
verbose_name=u"Workshop conducted(apart from pythonexpress)", |
64
|
|
|
default=0) |
65
|
|
|
github = models.URLField(null=True, blank=True) |
66
|
|
|
facebook = models.URLField(null=True, blank=True) |
67
|
|
|
googleplus = models.URLField(null=True, blank=True) |
68
|
|
|
linkedin = models.URLField(null=True, blank=True) |
69
|
|
|
twitter = models.URLField(null=True, blank=True) |
70
|
|
|
slideshare = models.URLField(null=True, blank=True) |
71
|
|
|
picture = models.ImageField( |
72
|
|
|
upload_to='images/', default='images/newuser.png') |
73
|
|
|
|
74
|
|
|
class Meta: |
75
|
|
|
db_table = 'user_profile' |
76
|
|
|
verbose_name = 'UserProfile' |
77
|
|
|
verbose_name_plural = 'UserProfiles' |
78
|
|
|
|
79
|
|
|
def __str__(self): |
80
|
|
|
return '{} {}'.format(self.user, self.slug) |
81
|
|
|
|
82
|
|
|
@property |
83
|
|
|
def is_profile_filled(self): |
84
|
|
|
if ('tutor' in [x.slug for x in self.usertype.all()]): |
85
|
|
|
if (self.location and |
86
|
|
|
self.interested_level and |
87
|
|
|
self.interested_states and |
88
|
|
|
self.interested_sections): |
89
|
|
|
return True |
90
|
|
|
return False |
91
|
|
|
if self.location: |
92
|
|
|
return True |
93
|
|
|
return False |
94
|
|
|
|
95
|
|
|
@cached_property |
96
|
|
|
def slug(self): |
97
|
|
|
return slugify( |
98
|
|
|
self.user.username, |
99
|
|
|
only_ascii=True) |
100
|
|
|
|
101
|
|
|
@property |
102
|
|
|
def get_workshop_details(self): |
103
|
|
|
return Workshop.objects.filter( |
104
|
|
|
presenter=self.user).order_by('-id') |
105
|
|
|
|
106
|
|
|
@property |
107
|
|
|
def can_create_organisation(self): |
108
|
|
|
org_count = Organisation.objects.filter( |
109
|
|
|
created_by=self.user).count() |
110
|
|
|
return False if org_count > 5 else True |
111
|
|
|
|
112
|
|
|
@property |
113
|
|
|
def get_workshop_completed_count(self): |
114
|
|
|
return len([x for x in |
115
|
|
|
self.get_workshop_details if ( |
116
|
|
|
x.status == WorkshopStatus.COMPLETED)]) |
117
|
|
|
|
118
|
|
|
@property |
119
|
|
|
def get_workshop_upcoming_count(self): |
120
|
|
|
return len([x for x in |
121
|
|
|
self.get_workshop_details if ( |
122
|
|
|
x.status == WorkshopStatus.ACCEPTED)]) |
123
|
|
|
|
124
|
|
|
@property |
125
|
|
|
def get_total_no_of_participants(self): |
126
|
|
|
return sum([x.no_of_participants for x in |
127
|
|
|
self.get_workshop_details if ( |
128
|
|
|
x.status == WorkshopStatus.COMPLETED)]) |
129
|
|
|
|
130
|
|
|
@property |
131
|
|
|
def get_last_workshop_date(self): |
132
|
|
|
pass |
133
|
|
|
|
134
|
|
|
@property |
135
|
|
|
def get_avg_workshop_rating(self): |
136
|
|
|
# TODO: Complete! |
137
|
|
|
return 0 |
138
|
|
|
|
139
|
|
|
@staticmethod |
140
|
|
|
def get_user_with_type(user_type=None): |
141
|
|
|
""" |
142
|
|
|
Would return user with user type list in argument. |
143
|
|
|
Eg Collage POC, admin etc |
144
|
|
|
""" |
145
|
|
|
return User.objects.filter( |
146
|
|
|
profile__usertype__display_name__in=user_type |
147
|
|
|
) |
148
|
|
|
|
149
|
|
|
@property |
150
|
|
|
def get_user_type(self): |
151
|
|
|
return [x.slug for x in self.usertype.all()] |
152
|
|
|
|
153
|
|
|
@property |
154
|
|
|
def get_interested_locations(self): |
155
|
|
|
return [x.name for x in self.interested_locations.all()] |
156
|
|
|
|
157
|
|
|
@property |
158
|
|
|
def get_graph_data(self): |
159
|
|
|
sections = WorkshopSections.objects.all() |
160
|
|
|
workshops = Workshop.objects.filter( |
161
|
|
|
presenter=self.user, |
162
|
|
|
status=WorkshopStatus.COMPLETED |
163
|
|
|
) |
164
|
|
|
if workshops: |
165
|
|
|
max_workshop_date = workshops.aggregate( |
166
|
|
|
models.Max('expected_date'))['expected_date__max'] |
167
|
|
|
min_workshop_date = workshops.aggregate( |
168
|
|
|
models.Min('expected_date'))['expected_date__min'] |
169
|
|
|
data = [] |
170
|
|
|
if max_workshop_date and min_workshop_date: |
171
|
|
|
dates = [dt for dt in rrule( |
172
|
|
|
MONTHLY, |
173
|
|
|
dtstart=min_workshop_date, |
174
|
|
|
until=max_workshop_date)] |
175
|
|
|
if dates: |
176
|
|
|
for section in sections: |
177
|
|
|
values = [] |
178
|
|
|
for d in dates: |
179
|
|
|
y = workshops.filter( |
180
|
|
|
expected_date__year=d.year, |
181
|
|
|
expected_date__month=d.month, |
182
|
|
|
workshop_section=section.pk).count() |
183
|
|
|
values.append( |
184
|
|
|
{'x': "{}-{}".format(d.year, d.month), 'y': y}) |
185
|
|
|
data.append({'key': section.name, 'values': values}) |
186
|
|
|
return json.dumps(data) |
187
|
|
|
else: |
188
|
|
|
return [] |
189
|
|
|
else: |
190
|
|
|
return [] |
191
|
|
|
else: |
192
|
|
|
return [] |
193
|
|
|
|
194
|
|
|
@classmethod |
195
|
|
|
def is_presenter(cls, user): |
196
|
|
|
return user.profile.usertype.filter(slug__iexact="tutor").exists() |
197
|
|
|
|
198
|
|
|
@classmethod |
199
|
|
|
def is_organiser(cls, user): |
200
|
|
|
return user.profile.usertype.filter(slug__icontains="poc").exists() |
201
|
|
|
|
202
|
|
|
@classmethod |
203
|
|
|
def is_regional_lead(cls, user): |
204
|
|
|
return user.profile.usertype.filter(slug__iexact="lead").exists() |
205
|
|
|
|
206
|
|
|
@classmethod |
207
|
|
|
def is_admin(cls, user): |
208
|
|
|
return user.profile.usertype.filter(slug__iexact="admin").exists() |
209
|
|
|
|
210
|
|
|
@classmethod |
211
|
|
|
def is_coordinator(cls, user): |
212
|
|
|
return user.profile.usertype.filter( |
213
|
|
|
slug__iexact="coordinator").exists() |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
def create_user_profile(sender, instance, created, **kwargs): |
217
|
|
|
if created: |
218
|
|
|
profile, created = Profile.objects.get_or_create(user=instance) |
219
|
|
|
profile.usertype.add(UserType.objects.get(slug='tutor')) |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
post_save.connect( |
223
|
|
|
create_user_profile, sender=User, dispatch_uid='create_user_profile') |
224
|
|
|
|