Commit 8e66c8186f7e31f52c7bec7207a641aba4a73a17

Authored by Andrew Buss
1 parent cec534fd3c
Exists in master

moved still more business logic into the model layer

Showing 2 changed files with 39 additions and 31 deletions Side-by-side Diff

flashcards/models.py View file @ 8e66c81
1 1 from django.contrib.auth.models import AbstractUser, UserManager
  2 +from django.contrib.auth.tokens import default_token_generator
2 3 from django.core.exceptions import ValidationError
3 4 from django.core.exceptions import PermissionDenied, SuspiciousOperation
  5 +from django.core.mail import send_mail
4 6 from django.db import IntegrityError
5 7 from django.db.models import *
6 8 from django.utils.timezone import now
7 9  
8 10  
... ... @@ -33,12 +35,26 @@
33 35 return user
34 36  
35 37 def create_user(self, email, password=None, **extra_fields):
36   - return self._create_user(email, password, False, False, **extra_fields)
  38 + user = self._create_user(email, password, False, False, **extra_fields)
  39 + body = '''
  40 + Visit the following link to confirm your email address:
  41 + https://flashy.cards/app/verifyemail/%s
37 42  
  43 + If you did not register for Flashy, no action is required.
  44 + '''
  45 +
  46 + assert send_mail("Flashy email verification",
  47 + body % user.confirmation_key,
  48 + "noreply@flashy.cards",
  49 + [user.email])
  50 + return user
  51 +
38 52 def create_superuser(self, email, password, **extra_fields):
39 53 return self._create_user(email, password, True, True, **extra_fields)
40 54  
41 55  
  56 +
  57 +
42 58 class User(AbstractUser, SimpleEmailConfirmationUserMixin):
43 59 """
44 60 An extension of Django's default user model.
45 61  
... ... @@ -75,7 +91,22 @@
75 91 raise ObjectDoesNotExist("User not enrolled in section")
76 92 return Flashcard.objects.all().filter(userflashcard__user=self).filter(section=section)
77 93  
  94 + def request_password_reset(self):
  95 + token = default_token_generator.make_token(self)
78 96  
  97 + body = '''
  98 + Visit the following link to reset your password:
  99 + https://flashy.cards/app/resetpassword/%d/%s
  100 +
  101 + If you did not request a password reset, no action is required.
  102 + '''
  103 +
  104 + send_mail("Flashy password reset",
  105 + body % (self.pk, token),
  106 + "noreply@flashy.cards",
  107 + [self.email])
  108 +
  109 +
79 110 class UserFlashcard(Model):
80 111 """
81 112 Represents the relationship between a user and a flashcard by:
... ... @@ -195,6 +226,11 @@
195 226 user_card.mask = new_data.get('mask', user_card.mask)
196 227 user_card.save()
197 228 return self
  229 +
  230 + def report(self, user, reason=None):
  231 + obj, created = FlashcardHide.objects.get_or_create(user=user, flashcard=self)
  232 + obj.reason = reason
  233 + obj.save()
198 234  
199 235 @classmethod
200 236 def cards_visible_to(cls, user):
flashcards/views.py View file @ 8e66c81
... ... @@ -183,18 +183,6 @@
183 183 user = authenticate(**data.validated_data)
184 184 auth.login(request, user)
185 185  
186   - body = '''
187   - Visit the following link to confirm your email address:
188   - https://flashy.cards/app/verifyemail/%s
189   -
190   - If you did not register for Flashy, no action is required.
191   - '''
192   -
193   - assert send_mail("Flashy email verification",
194   - body % user.confirmation_key,
195   - "noreply@flashy.cards",
196   - [user.email])
197   -
198 186 return Response(UserSerializer(request.user).data, status=HTTP_201_CREATED)
199 187  
200 188  
... ... @@ -238,21 +226,7 @@
238 226 """
239 227 data = PasswordResetRequestSerializer(data=request.data)
240 228 data.is_valid(raise_exception=True)
241   - user = User.objects.get(email=data['email'].value)
242   - token = default_token_generator.make_token(user)
243   -
244   - body = '''
245   - Visit the following link to reset your password:
246   - https://flashy.cards/app/resetpassword/%d/%s
247   -
248   - If you did not request a password reset, no action is required.
249   - '''
250   -
251   - send_mail("Flashy password reset",
252   - body % (user.pk, token),
253   - "noreply@flashy.cards",
254   - [user.email])
255   -
  229 + get_object_or_404(User, email=data['email'].value).request_password_reset()
256 230 return Response(status=HTTP_204_NO_CONTENT)
257 231  
258 232  
... ... @@ -315,9 +289,7 @@
315 289 ---
316 290 view_mocker: flashcards.api.mock_no_params
317 291 """
318   - obj, created = FlashcardHide.objects.get_or_create(user=request.user, flashcard=self.get_object())
319   - obj.reason = request.data.get('reason', None)
320   - obj.save()
  292 + self.get_object().report(request.user)
321 293 return Response(status=HTTP_204_NO_CONTENT)
322 294  
323 295 hide = report