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