diff --git a/flashcards/api.py b/flashcards/api.py index f0e6578..7710bce 100644 --- a/flashcards/api.py +++ b/flashcards/api.py @@ -1,4 +1,3 @@ -from django.utils.timezone import now from flashcards.models import Flashcard, UserFlashcardQuiz from rest_framework.exceptions import PermissionDenied from rest_framework.pagination import PageNumberPagination @@ -48,6 +47,5 @@ class IsAuthenticatedAndConfirmed(BasePermission): def has_permission(self, request, view): if not request.user: return False if not request.user.is_authenticated(): return False - if request.user.confirmed_email: return True - if (now() - request.user.date_joined).days < 1: return True - raise PermissionDenied('Please verify your email before continuing') + if request.user.locked: raise PermissionDenied('Please verify your email before continuing') + return True diff --git a/flashcards/models.py b/flashcards/models.py index 826eda5..7c9a481 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -22,6 +22,7 @@ from flashy.settings import IN_PRODUCTION + # Hack to fix AbstractUser before subclassing it AbstractUser._meta.get_field('email')._unique = True @@ -76,6 +77,11 @@ class User(AbstractUser, SimpleEmailConfirmationUserMixin): sections = ManyToManyField('Section', help_text="The sections which the user is enrolled in") confirmed_email = BooleanField(default=False) + @property + def locked(self): + if self.confirmed_email: return False + return (now() - self.date_joined).days > 0 + def send_confirmation_email(self): body = ''' Visit the following link to confirm your email address: @@ -457,7 +463,7 @@ class Section(Model): return '%s %s' % (self.department_abbreviation, self.course_num) def get_feed_for_user(self, user, page=1): - cards = list(self.get_cards_for_user(user)[(page-1)*self.PAGE_SIZE:page*self.PAGE_SIZE]) + cards = list(self.get_cards_for_user(user)[(page - 1) * self.PAGE_SIZE:page * self.PAGE_SIZE]) cards.sort(key=lambda x: -x.score) return cards diff --git a/flashcards/serializers.py b/flashcards/serializers.py index c0c440b..5473ae1 100644 --- a/flashcards/serializers.py +++ b/flashcards/serializers.py @@ -46,19 +46,22 @@ class PasswordResetSerializer(Serializer): except User.DoesNotExist: raise serializers.ValidationError('Could not verify reset token') + class EmailVerificationSerializer(Serializer): confirmation_key = CharField() + class UserUpdateSerializer(Serializer): old_password = CharField(required=False) - new_password = CharField(required=False, allow_blank=False)\ - # reset_token = CharField(required=False) + new_password = CharField(required=False, allow_blank=False) \ + # reset_token = CharField(required=False) def validate(self, data): if 'new_password' in data and 'old_password' not in data: raise serializers.ValidationError('old_password is required to set a new_password') return data + class LecturePeriodSerializer(ModelSerializer): class Meta: model = LecturePeriod @@ -101,11 +104,10 @@ class FeedRequestSerializer(Serializer): class UserSerializer(ModelSerializer): email = EmailField(required=False) sections = SectionSerializer(many=True) - is_confirmed = BooleanField() class Meta: model = User - fields = ("sections", "email", "is_confirmed", "last_login", "date_joined") + fields = ("sections", "email", "is_confirmed", "last_login", "date_joined", 'locked') class MaskFieldSerializer(serializers.Field):