From 8e66c8186f7e31f52c7bec7207a641aba4a73a17 Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Mon, 18 May 2015 02:20:18 -0700 Subject: [PATCH] moved still more business logic into the model layer --- flashcards/models.py | 38 +++++++++++++++++++++++++++++++++++++- flashcards/views.py | 32 ++------------------------------ 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/flashcards/models.py b/flashcards/models.py index 0e9676f..017c770 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -1,6 +1,8 @@ from django.contrib.auth.models import AbstractUser, UserManager +from django.contrib.auth.tokens import default_token_generator from django.core.exceptions import ValidationError from django.core.exceptions import PermissionDenied, SuspiciousOperation +from django.core.mail import send_mail from django.db import IntegrityError from django.db.models import * from django.utils.timezone import now @@ -33,12 +35,26 @@ class EmailOnlyUserManager(UserManager): return user def create_user(self, email, password=None, **extra_fields): - return self._create_user(email, password, False, False, **extra_fields) + user = self._create_user(email, password, False, False, **extra_fields) + body = ''' + Visit the following link to confirm your email address: + https://flashy.cards/app/verifyemail/%s + + If you did not register for Flashy, no action is required. + ''' + + assert send_mail("Flashy email verification", + body % user.confirmation_key, + "noreply@flashy.cards", + [user.email]) + return user def create_superuser(self, email, password, **extra_fields): return self._create_user(email, password, True, True, **extra_fields) + + class User(AbstractUser, SimpleEmailConfirmationUserMixin): """ An extension of Django's default user model. @@ -75,6 +91,21 @@ class User(AbstractUser, SimpleEmailConfirmationUserMixin): raise ObjectDoesNotExist("User not enrolled in section") return Flashcard.objects.all().filter(userflashcard__user=self).filter(section=section) + def request_password_reset(self): + token = default_token_generator.make_token(self) + + body = ''' + Visit the following link to reset your password: + https://flashy.cards/app/resetpassword/%d/%s + + If you did not request a password reset, no action is required. + ''' + + send_mail("Flashy password reset", + body % (self.pk, token), + "noreply@flashy.cards", + [self.email]) + class UserFlashcard(Model): """ @@ -196,6 +227,11 @@ class Flashcard(Model): user_card.save() return self + def report(self, user, reason=None): + obj, created = FlashcardHide.objects.get_or_create(user=user, flashcard=self) + obj.reason = reason + obj.save() + @classmethod def cards_visible_to(cls, user): """ diff --git a/flashcards/views.py b/flashcards/views.py index 715ca6a..bca6be4 100644 --- a/flashcards/views.py +++ b/flashcards/views.py @@ -183,18 +183,6 @@ def register(request, format=None): user = authenticate(**data.validated_data) auth.login(request, user) - body = ''' - Visit the following link to confirm your email address: - https://flashy.cards/app/verifyemail/%s - - If you did not register for Flashy, no action is required. - ''' - - assert send_mail("Flashy email verification", - body % user.confirmation_key, - "noreply@flashy.cards", - [user.email]) - return Response(UserSerializer(request.user).data, status=HTTP_201_CREATED) @@ -238,21 +226,7 @@ def request_password_reset(request, format=None): """ data = PasswordResetRequestSerializer(data=request.data) data.is_valid(raise_exception=True) - user = User.objects.get(email=data['email'].value) - token = default_token_generator.make_token(user) - - body = ''' - Visit the following link to reset your password: - https://flashy.cards/app/resetpassword/%d/%s - - If you did not request a password reset, no action is required. - ''' - - send_mail("Flashy password reset", - body % (user.pk, token), - "noreply@flashy.cards", - [user.email]) - + get_object_or_404(User, email=data['email'].value).request_password_reset() return Response(status=HTTP_204_NO_CONTENT) @@ -315,9 +289,7 @@ class FlashcardViewSet(GenericViewSet, CreateModelMixin, RetrieveModelMixin): --- view_mocker: flashcards.api.mock_no_params """ - obj, created = FlashcardHide.objects.get_or_create(user=request.user, flashcard=self.get_object()) - obj.reason = request.data.get('reason', None) - obj.save() + self.get_object().report(request.user) return Response(status=HTTP_204_NO_CONTENT) hide = report -- 1.9.1