Commit ad724d791652285149b9c9a1dd6618f53794c3ee

Authored by Andrew Buss
1 parent fe44f16085
Exists in master

is_hidden specific to user

Showing 3 changed files with 20 additions and 20 deletions Side-by-side Diff

flashcards/models.py View file @ ad724d7
... ... @@ -16,11 +16,6 @@
16 16  
17 17  
18 18  
19   -
20   -
21   -
22   -
23   -
24 19 # Hack to fix AbstractUser before subclassing it
25 20  
26 21 AbstractUser._meta.get_field('email')._unique = True
... ... @@ -198,9 +193,7 @@
198 193 :param user:
199 194 :return: Whether the card is hidden from the user.
200 195 """
201   - if self.userflashcard_set.filter(user=user).exists(): return False
202   - if self.is_hidden or self.flashcardhide_set.filter(user=user).exists(): return True
203   - return False
  196 + return self.is_hidden or self.flashcardhide_set.filter(user=user).exists()
204 197  
205 198 def hide_from(self, user, reason=None):
206 199 if self.is_in_deck(user): user.unpull(self)
... ... @@ -264,6 +257,7 @@
264 257 def score(self):
265 258 def seconds_since_epoch(dt):
266 259 from datetime import datetime
  260 +
267 261 epoch = make_aware(datetime.utcfromtimestamp(0))
268 262 delta = dt - epoch
269 263 return delta.total_seconds()
... ... @@ -285,6 +279,10 @@
285 279 """
286 280 return cls.objects.filter(Q(author__confirmed_email=True) | Q(author=user)
287 281 ).exclude(Q(is_hidden=True) | Q(flashcardhide__user=user))
  282 +
  283 + @classmethod
  284 + def cards_hidden_by(cls, user):
  285 + return cls.objects.filter(flashcardhide__user=user)
288 286  
289 287  
290 288 class UserFlashcardQuiz(Model):
flashcards/serializers.py View file @ ad724d7
... ... @@ -5,7 +5,8 @@
5 5 from flashcards.models import Section, LecturePeriod, User, Flashcard, UserFlashcard, UserFlashcardQuiz
6 6 from flashcards.validators import FlashcardMask, OverlapIntervalException
7 7 from rest_framework import serializers
8   -from rest_framework.fields import EmailField, BooleanField, CharField, IntegerField, DateTimeField, empty
  8 +from rest_framework.fields import EmailField, BooleanField, CharField, IntegerField, DateTimeField, empty, \
  9 + SerializerMethodField
9 10 from rest_framework.serializers import ModelSerializer, Serializer, PrimaryKeyRelatedField, ListField
10 11 from rest_framework.validators import UniqueValidator
11 12 from flashy.settings import QUARTER_END, QUARTER_START
... ... @@ -121,7 +122,7 @@
121 122  
122 123  
123 124 class FlashcardSerializer(ModelSerializer):
124   - is_hidden = BooleanField(read_only=True)
  125 + is_hidden = SerializerMethodField()
125 126 # hide_reason = CharField(read_only=True)
126 127 material_date = DateTimeField(default=now)
127 128 mask = MaskFieldSerializer(allow_null=True)
... ... @@ -145,6 +146,10 @@
145 146 if len(self.initial_data['text']) < value.max_offset():
146 147 raise serializers.ValidationError("Mask out of bounds")
147 148 return value
  149 +
  150 + def get_is_hidden(self, obj):
  151 + if 'user' not in self.context: return False
  152 + return obj.is_hidden_from(self.context['user'])
148 153  
149 154 class Meta:
150 155 model = Flashcard
flashcards/views.py View file @ ad724d7
1   -from random import sample
2   -
3 1 import django
4 2 from django.contrib import auth
5 3 from django.shortcuts import get_object_or_404
6 4  
... ... @@ -18,12 +16,10 @@
18 16 from django.core.mail import send_mail
19 17 from django.contrib.auth import authenticate
20 18 from django.contrib.auth.tokens import default_token_generator
21   -from django.db.models import Count, F
22 19 from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK
23 20 from rest_framework.response import Response
24 21 from rest_framework.exceptions import AuthenticationFailed, NotAuthenticated, ValidationError, PermissionDenied
25 22 from simple_email_confirmation import EmailAddress
26   -from math import e
27 23  
28 24  
29 25 class SectionViewSet(ReadOnlyModelViewSet):
30 26  
31 27  
... ... @@ -38,12 +34,14 @@
38 34 Gets flashcards for a section, excluding hidden cards.
39 35 Returned in strictly chronological order (material date).
40 36 """
  37 + flashcards = Flashcard.cards_visible_to(request.user)
41 38 if 'hidden' in request.GET:
42   - flashcards = Flashcard.objects
43   - else:
44   - flashcards = Flashcard.cards_visible_to(request.user)
  39 + if request.get['hidden'] == 'only':
  40 + flashcards = Flashcard.cards_hidden_by(request.user)
  41 + else:
  42 + flashcards |= Flashcard.cards_hidden_by(request.user)
45 43 flashcards = flashcards.filter(section=self.get_object()).all()
46   - return Response(FlashcardSerializer(flashcards, many=True).data)
  44 + return Response(FlashcardSerializer(flashcards, context={"user": request.user}, many=True).data)
47 45  
48 46 @detail_route(methods=['POST'])
49 47 def enroll(self, request, pk):
... ... @@ -310,7 +308,6 @@
310 308 request.user.pull(self.get_object())
311 309 return Response(status=HTTP_204_NO_CONTENT)
312 310  
313   -
314 311 @detail_route(methods=['POST'])
315 312 def unpull(self, request, pk):
316 313 """
... ... @@ -335,7 +332,7 @@
335 332 data.is_valid(raise_exception=True)
336 333 new_flashcard = data.validated_data
337 334 new_flashcard = flashcard.edit(user, new_flashcard)
338   - return Response(FlashcardSerializer(new_flashcard).data, status=HTTP_200_OK)
  335 + return Response(FlashcardSerializer(new_flashcard, context={'user': request.user}).data, status=HTTP_200_OK)
339 336  
340 337  
341 338 class UserFlashcardQuizViewSet(GenericViewSet, CreateModelMixin, UpdateModelMixin):