Commit 4ff4160a6fc67561d4d0e8810a3d7afc7d733716

Authored by Andrew Buss
1 parent 693e1afa5e
Exists in master

include can_enroll in sectionserializer

Showing 4 changed files with 24 additions and 17 deletions Side-by-side Diff

flashcards/admin.py View file @ 4ff4160
1 1 from django.contrib import admin
2 2 from django.contrib.auth.admin import UserAdmin
3 3 from flashcards.models import Flashcard, UserFlashcard, Section, \
4   - LecturePeriod, User, UserFlashcardQuiz
  4 + LecturePeriod, User, UserFlashcardQuiz, WhitelistedAddress, FlashcardHide
5 5  
6 6 admin.site.register([
7 7 Flashcard,
8 8 UserFlashcard,
9 9 UserFlashcardQuiz,
10 10 Section,
11   - LecturePeriod
  11 + LecturePeriod,
  12 + FlashcardHide,
  13 + WhitelistedAddress
12 14 ])
13 15 admin.site.register(User, UserAdmin)
flashcards/models.py View file @ 4ff4160
... ... @@ -10,7 +10,6 @@
10 10 from django.core.validators import MinLengthValidator
11 11 from django.db import IntegrityError
12 12 from django.db.models import *
13   -from django.utils.log import getLogger
14 13 from django.utils.timezone import now, make_aware
15 14 from flashy.settings import QUARTER_START
16 15 from simple_email_confirmation import SimpleEmailConfirmationUserMixin
... ... @@ -18,6 +17,7 @@
18 17 from cached_property import cached_property
19 18 from flashy.settings import IN_PRODUCTION
20 19  
  20 +
21 21 # Hack to fix AbstractUser before subclassing it
22 22  
23 23 AbstractUser._meta.get_field('email')._unique = True
... ... @@ -66,7 +66,6 @@
66 66 pass
67 67  
68 68  
69   -
70 69 class FlashcardNotInDeckException(Exception):
71 70 pass
72 71  
... ... @@ -106,6 +105,7 @@
106 105  
107 106 try:
108 107 import flashcards.notifications
  108 +
109 109 user_card = UserFlashcard.objects.get(user=self, flashcard=flashcard)
110 110 user_card.delete()
111 111 flashcards.notifications.notify_score_change(flashcard)
flashcards/serializers.py View file @ 4ff4160
1   -from json import dumps, loads
  1 +from json import loads
  2 +from collections import Iterable
2 3  
3 4 from django.utils.datetime_safe import datetime
4 5 from django.utils.timezone import now
5   -from flashcards.models import Section, LecturePeriod, User, Flashcard, UserFlashcard, UserFlashcardQuiz
  6 +from flashcards.models import Section, LecturePeriod, User, Flashcard, UserFlashcardQuiz
6 7 from flashcards.validators import FlashcardMask, OverlapIntervalException
7 8 from rest_framework import serializers
8 9 from rest_framework.fields import EmailField, BooleanField, CharField, IntegerField, DateTimeField, empty, \
... ... @@ -10,7 +11,6 @@
10 11 from rest_framework.serializers import ModelSerializer, Serializer, PrimaryKeyRelatedField, ListField
11 12 from rest_framework.validators import UniqueValidator
12 13 from flashy.settings import QUARTER_END, QUARTER_START
13   -from collections import Iterable
14 14  
15 15  
16 16 class EmailSerializer(Serializer):
... ... @@ -59,9 +59,9 @@
59 59 return data
60 60  
61 61  
62   -class Password(Serializer):
63   - email = EmailField(required=True)
64   - password = CharField(required=True)
  62 +# class Password(Serializer):
  63 +# email = EmailField(required=True)
  64 +# password = CharField(required=True)
65 65  
66 66  
67 67 class LecturePeriodSerializer(ModelSerializer):
68 68  
... ... @@ -74,9 +74,15 @@
74 74 lecture_times = CharField()
75 75 short_name = CharField()
76 76 long_name = CharField()
  77 + can_enroll = SerializerMethodField()
77 78  
78 79 class Meta:
79 80 model = Section
  81 +
  82 + def get_can_enroll(self, obj):
  83 + if 'user' not in self.context: return False
  84 + if not obj.is_whitelisted: return True
  85 + return obj.is_user_on_whitelist(self.context['user'])
80 86  
81 87  
82 88 class DeepSectionSerializer(SectionSerializer):
flashcards/views.py View file @ 4ff4160
  1 +from math import e
  2 +
1 3 import django
2 4 from django.contrib import auth
3 5 from django.shortcuts import get_object_or_404
4 6  
... ... @@ -19,13 +21,11 @@
19 21 from django.core.mail import send_mail
20 22 from django.contrib.auth import authenticate
21 23 from django.contrib.auth.tokens import default_token_generator
22   -from django.db.models import Count, Max, F, Value, When, Case, DateTimeField, FloatField
  24 +from django.db.models import Count, Max, F, Value, When, Case, FloatField
23 25 from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK
24 26 from rest_framework.response import Response
25 27 from rest_framework.exceptions import AuthenticationFailed, NotAuthenticated, ValidationError, PermissionDenied
26 28 from simple_email_confirmation import EmailAddress
27   -from math import e
28   -from django.utils.timezone import now
29 29  
30 30  
31 31 def log_event(request, event=''):
... ... @@ -105,7 +105,7 @@
105 105 query = request.GET.get('q', None)
106 106 if not query: return Response('[]')
107 107 qs = Section.search(query.split(' '))[:20]
108   - data = SectionSerializer(qs, many=True).data
  108 + data = SectionSerializer(qs, many=True, context={'user': request.user}).data
109 109 log_event(request, query)
110 110 return Response(data)
111 111  
... ... @@ -356,7 +356,6 @@
356 356 except FlashcardNotInDeckException:
357 357 raise ValidationError('Cannot unpull a card not in deck')
358 358  
359   -
360 359 def partial_update(self, request, *args, **kwargs):
361 360 """
362 361 Edit settings related to a card for the user.
... ... @@ -411,7 +410,7 @@
411 410 output_field=FloatField()
412 411 ),
413 412 retention_score=Case(
414   - default=Value(e, output_field=FloatField()) ** (F('days_since')*(-0.1/(F('study_count')+1))),
  413 + default=Value(e, output_field=FloatField()) ** (F('days_since') * (-0.1 / (F('study_count') + 1))),
415 414 output_field=FloatField()
416 415 )
417 416 ).order_by('retention_score')