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', 'Volunteer'] |
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(is_active=True).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 in [ |
117
|
|
|
WorkshopStatus.COMPLETED, |
118
|
|
|
WorkshopStatus.FEEDBACK_PENDING])]) |
119
|
|
|
|
120
|
|
|
@property |
121
|
|
|
def get_workshop_upcoming_count(self): |
122
|
|
|
return len([x for x in |
123
|
|
|
self.get_workshop_details if ( |
124
|
|
|
x.status == WorkshopStatus.ACCEPTED)]) |
125
|
|
|
|
126
|
|
|
@property |
127
|
|
|
def get_total_no_of_participants(self): |
128
|
|
|
return sum([x.no_of_participants for x in |
129
|
|
|
self.get_workshop_details if ( |
130
|
|
|
x.status == WorkshopStatus.COMPLETED)]) |
131
|
|
|
|
132
|
|
|
@property |
133
|
|
|
def get_last_workshop_date(self): |
134
|
|
|
pass |
135
|
|
|
|
136
|
|
|
@property |
137
|
|
|
def get_avg_workshop_rating(self): |
138
|
|
|
# TODO: Complete! |
139
|
|
|
return 0 |
140
|
|
|
|
141
|
|
|
@staticmethod |
142
|
|
|
def get_user_with_type(user_type=None): |
143
|
|
|
""" |
144
|
|
|
Would return user with user type list in argument. |
145
|
|
|
Eg Collage POC, admin etc |
146
|
|
|
""" |
147
|
|
|
return User.objects.filter( |
148
|
|
|
profile__usertype__display_name__in=user_type |
149
|
|
|
) |
150
|
|
|
|
151
|
|
|
@property |
152
|
|
|
def get_user_type(self): |
153
|
|
|
return [x.slug for x in self.usertype.all()] |
154
|
|
|
|
155
|
|
|
@property |
156
|
|
|
def get_interested_locations(self): |
157
|
|
|
return [x.name for x in self.interested_locations.all()] |
158
|
|
|
|
159
|
|
|
@property |
160
|
|
|
def get_graph_data(self): |
161
|
|
|
sections = WorkshopSections.objects.all() |
162
|
|
|
workshops = Workshop.objects.filter( |
163
|
|
|
presenter=self.user, |
164
|
|
|
status=WorkshopStatus.COMPLETED, |
165
|
|
|
is_active=True |
166
|
|
|
) |
167
|
|
|
if workshops: |
168
|
|
|
max_workshop_date = workshops.aggregate( |
169
|
|
|
models.Max('expected_date'))['expected_date__max'] |
170
|
|
|
min_workshop_date = workshops.aggregate( |
171
|
|
|
models.Min('expected_date'))['expected_date__min'] |
172
|
|
|
data = [] |
173
|
|
|
if max_workshop_date and min_workshop_date: |
174
|
|
|
dates = [dt for dt in rrule( |
175
|
|
|
MONTHLY, |
176
|
|
|
dtstart=min_workshop_date, |
177
|
|
|
until=max_workshop_date)] |
178
|
|
|
if dates: |
179
|
|
|
for section in sections: |
180
|
|
|
values = [] |
181
|
|
|
for d in dates: |
182
|
|
|
y = workshops.filter( |
183
|
|
|
expected_date__year=d.year, |
184
|
|
|
expected_date__month=d.month, |
185
|
|
|
workshop_section=section.pk).count() |
186
|
|
|
values.append( |
187
|
|
|
{'x': "{}-{}".format(d.year, d.month), 'y': y}) |
188
|
|
|
data.append({'key': section.name, 'values': values}) |
189
|
|
|
return json.dumps(data) |
190
|
|
|
else: |
191
|
|
|
return [] |
192
|
|
|
else: |
193
|
|
|
return [] |
194
|
|
|
else: |
195
|
|
|
return [] |
196
|
|
|
|
197
|
|
|
@classmethod |
198
|
|
|
def is_presenter(cls, user): |
199
|
|
|
return user.profile.usertype.filter(slug__iexact="tutor").exists() |
200
|
|
|
|
201
|
|
|
@classmethod |
202
|
|
|
def is_organiser(cls, user): |
203
|
|
|
return user.profile.usertype.filter(slug__icontains="poc").exists() |
204
|
|
|
|
205
|
|
|
@classmethod |
206
|
|
|
def is_regional_lead(cls, user): |
207
|
|
|
return user.profile.usertype.filter(slug__iexact="lead").exists() |
208
|
|
|
|
209
|
|
|
@classmethod |
210
|
|
|
def is_admin(cls, user): |
211
|
|
|
return user.profile.usertype.filter(slug__iexact="admin").exists() |
212
|
|
|
|
213
|
|
|
@classmethod |
214
|
|
|
def is_coordinator(cls, user): |
215
|
|
|
return user.profile.usertype.filter( |
216
|
|
|
slug__iexact="coordinator").exists() |
217
|
|
|
|
218
|
|
|
@classmethod |
219
|
|
|
def is_volunteer(cls, user): |
220
|
|
|
return user.profile.usertype.filter(slug__iexact="volunteer").exists() |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
def create_user_profile(sender, instance, created, **kwargs): |
224
|
|
|
if created: |
225
|
|
|
profile, created = Profile.objects.get_or_create(user=instance) |
226
|
|
|
profile.usertype.add(UserType.objects.get(slug='tutor')) |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
post_save.connect( |
230
|
|
|
create_user_profile, sender=User, dispatch_uid='create_user_profile') |
231
|
|
|
|