Commit 05b5a910c5e1a63a46a9d09e4b9173a0a823f6c6
1 parent
1890b5a76b
Exists in
master
include locked field in userserializer
Showing 3 changed files with 15 additions and 9 deletions Side-by-side Diff
flashcards/api.py
View file @
05b5a91
1 | -from django.utils.timezone import now | |
2 | 1 | from flashcards.models import Flashcard, UserFlashcardQuiz |
3 | 2 | from rest_framework.exceptions import PermissionDenied |
4 | 3 | from rest_framework.pagination import PageNumberPagination |
... | ... | @@ -48,7 +47,6 @@ |
48 | 47 | def has_permission(self, request, view): |
49 | 48 | if not request.user: return False |
50 | 49 | if not request.user.is_authenticated(): return False |
51 | - if request.user.confirmed_email: return True | |
52 | - if (now() - request.user.date_joined).days < 1: return True | |
53 | - raise PermissionDenied('Please verify your email before continuing') | |
50 | + if request.user.locked: raise PermissionDenied('Please verify your email before continuing') | |
51 | + return True |
flashcards/models.py
View file @
05b5a91
... | ... | @@ -22,6 +22,7 @@ |
22 | 22 | |
23 | 23 | |
24 | 24 | |
25 | + | |
25 | 26 | # Hack to fix AbstractUser before subclassing it |
26 | 27 | |
27 | 28 | AbstractUser._meta.get_field('email')._unique = True |
... | ... | @@ -76,6 +77,11 @@ |
76 | 77 | sections = ManyToManyField('Section', help_text="The sections which the user is enrolled in") |
77 | 78 | confirmed_email = BooleanField(default=False) |
78 | 79 | |
80 | + @property | |
81 | + def locked(self): | |
82 | + if self.confirmed_email: return False | |
83 | + return (now() - self.date_joined).days > 0 | |
84 | + | |
79 | 85 | def send_confirmation_email(self): |
80 | 86 | body = ''' |
81 | 87 | Visit the following link to confirm your email address: |
... | ... | @@ -457,7 +463,7 @@ |
457 | 463 | return '%s %s' % (self.department_abbreviation, self.course_num) |
458 | 464 | |
459 | 465 | def get_feed_for_user(self, user, page=1): |
460 | - cards = list(self.get_cards_for_user(user)[(page-1)*self.PAGE_SIZE:page*self.PAGE_SIZE]) | |
466 | + cards = list(self.get_cards_for_user(user)[(page - 1) * self.PAGE_SIZE:page * self.PAGE_SIZE]) | |
461 | 467 | cards.sort(key=lambda x: -x.score) |
462 | 468 | return cards |
463 | 469 |
flashcards/serializers.py
View file @
05b5a91
... | ... | @@ -46,19 +46,22 @@ |
46 | 46 | except User.DoesNotExist: |
47 | 47 | raise serializers.ValidationError('Could not verify reset token') |
48 | 48 | |
49 | + | |
49 | 50 | class EmailVerificationSerializer(Serializer): |
50 | 51 | confirmation_key = CharField() |
51 | 52 | |
53 | + | |
52 | 54 | class UserUpdateSerializer(Serializer): |
53 | 55 | old_password = CharField(required=False) |
54 | - new_password = CharField(required=False, allow_blank=False)\ | |
55 | - # reset_token = CharField(required=False) | |
56 | + new_password = CharField(required=False, allow_blank=False) \ | |
57 | + # reset_token = CharField(required=False) | |
56 | 58 | |
57 | 59 | def validate(self, data): |
58 | 60 | if 'new_password' in data and 'old_password' not in data: |
59 | 61 | raise serializers.ValidationError('old_password is required to set a new_password') |
60 | 62 | return data |
61 | 63 | |
64 | + | |
62 | 65 | class LecturePeriodSerializer(ModelSerializer): |
63 | 66 | class Meta: |
64 | 67 | model = LecturePeriod |
65 | 68 | |
... | ... | @@ -101,11 +104,10 @@ |
101 | 104 | class UserSerializer(ModelSerializer): |
102 | 105 | email = EmailField(required=False) |
103 | 106 | sections = SectionSerializer(many=True) |
104 | - is_confirmed = BooleanField() | |
105 | 107 | |
106 | 108 | class Meta: |
107 | 109 | model = User |
108 | - fields = ("sections", "email", "is_confirmed", "last_login", "date_joined") | |
110 | + fields = ("sections", "email", "is_confirmed", "last_login", "date_joined", 'locked') | |
109 | 111 | |
110 | 112 | |
111 | 113 | class MaskFieldSerializer(serializers.Field): |