Commit 83e4896b059edd0c668a1a0cb7dc2eca023125c5
1 parent
1eebdbcc41
Exists in
master
include is_enrolled in sectionserializer
Showing 2 changed files with 12 additions and 3 deletions Side-by-side Diff
flashcards/models.py
View file @
83e4896
1 | 1 | from math import log1p |
2 | 2 | from math import exp |
3 | +from math import e | |
3 | 4 | |
4 | 5 | from django.contrib.auth.models import AbstractUser, UserManager |
5 | 6 | from django.contrib.auth.tokens import default_token_generator |
6 | 7 | |
7 | 8 | |
... | ... | @@ -11,14 +12,14 @@ |
11 | 12 | from django.db import IntegrityError |
12 | 13 | from django.db.models import * |
13 | 14 | from django.utils.timezone import now, make_aware |
14 | -from flashy.settings import QUARTER_START, QUARTER_END | |
15 | +from flashy.settings import QUARTER_START | |
15 | 16 | from simple_email_confirmation import SimpleEmailConfirmationUserMixin |
16 | 17 | from fields import MaskField |
17 | 18 | from cached_property import cached_property |
18 | 19 | from flashy.settings import IN_PRODUCTION |
19 | -from math import e | |
20 | 20 | |
21 | 21 | |
22 | + | |
22 | 23 | # Hack to fix AbstractUser before subclassing it |
23 | 24 | |
24 | 25 | AbstractUser._meta.get_field('email')._unique = True |
... | ... | @@ -158,7 +159,7 @@ |
158 | 159 | output_field=FloatField() |
159 | 160 | ), |
160 | 161 | retention_score=Case( |
161 | - default=Value(e, output_field=FloatField()) ** (F('days_since')*(-0.1/(F('study_count')+1))), | |
162 | + default=Value(e, output_field=FloatField()) ** (F('days_since') * (-0.1 / (F('study_count') + 1))), | |
162 | 163 | output_field=FloatField() |
163 | 164 | ) |
164 | 165 | ).order_by('retention_score') |
... | ... | @@ -412,6 +413,9 @@ |
412 | 413 | :return: whether the user is on the waitlist for this section |
413 | 414 | """ |
414 | 415 | return self.whitelist.filter(email=user.email).exists() |
416 | + | |
417 | + def is_user_enrolled(self, user): | |
418 | + return self.user_set.filter(pk=user.pk).exists() | |
415 | 419 | |
416 | 420 | def enroll(self, user): |
417 | 421 | if user.sections.filter(pk=self.pk).exists(): |
flashcards/serializers.py
View file @
83e4896
... | ... | @@ -75,6 +75,7 @@ |
75 | 75 | short_name = CharField() |
76 | 76 | long_name = CharField() |
77 | 77 | can_enroll = SerializerMethodField() |
78 | + is_enrolled = SerializerMethodField() | |
78 | 79 | |
79 | 80 | class Meta: |
80 | 81 | model = Section |
... | ... | @@ -83,6 +84,10 @@ |
83 | 84 | if 'user' not in self.context: return False |
84 | 85 | if not obj.is_whitelisted: return True |
85 | 86 | return obj.is_user_on_whitelist(self.context['user']) |
87 | + | |
88 | + def get_is_enrolled(self, obj): | |
89 | + if 'user' not in self.context: return False | |
90 | + return obj.is_user_enrolled(self.context['user']) | |
86 | 91 | |
87 | 92 | |
88 | 93 | class DeepSectionSerializer(SectionSerializer): |