Commit 52c37d174f9f637c30c6ac41106be5576cead790
1 parent
dc685f1923
Exists in
master
Strip letters from right side of course numbers when searching
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
flashcards/models.py
View file @
52c37d1
from django.contrib.auth.models import AbstractUser, UserManager | 1 | 1 | from django.contrib.auth.models import AbstractUser, UserManager | |
from django.db.models import * | 2 | 2 | from django.db.models import * | |
from django.utils.timezone import now | 3 | 3 | from django.utils.timezone import now | |
from simple_email_confirmation import SimpleEmailConfirmationUserMixin | 4 | 4 | from simple_email_confirmation import SimpleEmailConfirmationUserMixin | |
from fields import MaskField | 5 | 5 | from fields import MaskField | |
6 | 6 | |||
# Hack to fix AbstractUser before subclassing it | 7 | 7 | # Hack to fix AbstractUser before subclassing it | |
AbstractUser._meta.get_field('email')._unique = True | 8 | 8 | AbstractUser._meta.get_field('email')._unique = True | |
AbstractUser._meta.get_field('username')._unique = False | 9 | 9 | AbstractUser._meta.get_field('username')._unique = False | |
10 | 10 | |||
11 | 11 | |||
class EmailOnlyUserManager(UserManager): | 12 | 12 | class EmailOnlyUserManager(UserManager): | |
""" | 13 | 13 | """ | |
A tiny extension of Django's UserManager which correctly creates users | 14 | 14 | A tiny extension of Django's UserManager which correctly creates users | |
without usernames (using emails instead). | 15 | 15 | without usernames (using emails instead). | |
""" | 16 | 16 | """ | |
17 | 17 | |||
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields): | 18 | 18 | def _create_user(self, email, password, is_staff, is_superuser, **extra_fields): | |
""" | 19 | 19 | """ | |
Creates and saves a User with the given email and password. | 20 | 20 | Creates and saves a User with the given email and password. | |
""" | 21 | 21 | """ | |
email = self.normalize_email(email) | 22 | 22 | email = self.normalize_email(email) | |
user = self.model(email=email, | 23 | 23 | user = self.model(email=email, | |
is_staff=is_staff, is_active=True, | 24 | 24 | is_staff=is_staff, is_active=True, | |
is_superuser=is_superuser, | 25 | 25 | is_superuser=is_superuser, | |
date_joined=now(), **extra_fields) | 26 | 26 | date_joined=now(), **extra_fields) | |
user.set_password(password) | 27 | 27 | user.set_password(password) | |
user.save(using=self._db) | 28 | 28 | user.save(using=self._db) | |
return user | 29 | 29 | return user | |
30 | 30 | |||
def create_user(self, email, password=None, **extra_fields): | 31 | 31 | def create_user(self, email, password=None, **extra_fields): | |
return self._create_user(email, password, False, False, **extra_fields) | 32 | 32 | return self._create_user(email, password, False, False, **extra_fields) | |
33 | 33 | |||
def create_superuser(self, email, password, **extra_fields): | 34 | 34 | def create_superuser(self, email, password, **extra_fields): | |
return self._create_user(email, password, True, True, **extra_fields) | 35 | 35 | return self._create_user(email, password, True, True, **extra_fields) | |
36 | 36 | |||
37 | 37 | |||
class User(AbstractUser, SimpleEmailConfirmationUserMixin): | 38 | 38 | class User(AbstractUser, SimpleEmailConfirmationUserMixin): | |
""" | 39 | 39 | """ | |
An extension of Django's default user model. | 40 | 40 | An extension of Django's default user model. | |
We use email as the username field, and include enrolled sections here | 41 | 41 | We use email as the username field, and include enrolled sections here | |
""" | 42 | 42 | """ | |
objects = EmailOnlyUserManager() | 43 | 43 | objects = EmailOnlyUserManager() | |
USERNAME_FIELD = 'email' | 44 | 44 | USERNAME_FIELD = 'email' | |
REQUIRED_FIELDS = [] | 45 | 45 | REQUIRED_FIELDS = [] | |
sections = ManyToManyField('Section', help_text="The sections which the user is enrolled in") | 46 | 46 | sections = ManyToManyField('Section', help_text="The sections which the user is enrolled in") | |
47 | 47 | |||
48 | 48 | |||
class UserFlashcard(Model): | 49 | 49 | class UserFlashcard(Model): | |
""" | 50 | 50 | """ | |
Represents the relationship between a user and a flashcard by: | 51 | 51 | Represents the relationship between a user and a flashcard by: | |
1. A user has a flashcard in their deck | 52 | 52 | 1. A user has a flashcard in their deck | |
2. A user used to have a flashcard in their deck | 53 | 53 | 2. A user used to have a flashcard in their deck | |
3. A user has a flashcard hidden from them | 54 | 54 | 3. A user has a flashcard hidden from them | |
""" | 55 | 55 | """ | |
user = ForeignKey('User') | 56 | 56 | user = ForeignKey('User') | |
mask = MaskField(max_length=255, null=True, blank=True, help_text="The user-specific mask on the card") | 57 | 57 | mask = MaskField(max_length=255, null=True, blank=True, help_text="The user-specific mask on the card") | |
pulled = DateTimeField(blank=True, null=True, help_text="When the user pulled the card") | 58 | 58 | pulled = DateTimeField(blank=True, null=True, help_text="When the user pulled the card") | |
flashcard = ForeignKey('Flashcard') | 59 | 59 | flashcard = ForeignKey('Flashcard') | |
unpulled = DateTimeField(blank=True, null=True, help_text="When the user unpulled this card") | 60 | 60 | unpulled = DateTimeField(blank=True, null=True, help_text="When the user unpulled this card") | |
61 | 61 | |||
class Meta: | 62 | 62 | class Meta: | |
# There can be at most one UserFlashcard for each User and Flashcard | 63 | 63 | # There can be at most one UserFlashcard for each User and Flashcard | |
unique_together = (('user', 'flashcard'),) | 64 | 64 | unique_together = (('user', 'flashcard'),) | |
index_together = ["user", "flashcard"] | 65 | 65 | index_together = ["user", "flashcard"] | |
# By default, order by most recently pulled | 66 | 66 | # By default, order by most recently pulled | |
ordering = ['-pulled'] | 67 | 67 | ordering = ['-pulled'] | |
68 | 68 | |||
def is_hidden(self): | 69 | 69 | def is_hidden(self): | |
""" | 70 | 70 | """ | |
A card is hidden only if a user has not ever added it to their deck. | 71 | 71 | A card is hidden only if a user has not ever added it to their deck. | |
:return: Whether the flashcard is hidden from the user | 72 | 72 | :return: Whether the flashcard is hidden from the user | |
""" | 73 | 73 | """ | |
return not self.pulled | 74 | 74 | return not self.pulled | |
75 | 75 | |||
def is_in_deck(self): | 76 | 76 | def is_in_deck(self): | |
""" | 77 | 77 | """ | |
:return:Whether the flashcard is in the user's deck | 78 | 78 | :return:Whether the flashcard is in the user's deck | |
""" | 79 | 79 | """ | |
return self.pulled and not self.unpulled | 80 | 80 | return self.pulled and not self.unpulled | |
81 | 81 | |||
82 | 82 | |||
class Flashcard(Model): | 83 | 83 | class Flashcard(Model): | |
text = CharField(max_length=255, help_text='The text on the card') | 84 | 84 | text = CharField(max_length=255, help_text='The text on the card') | |
section = ForeignKey('Section', help_text='The section with which the card is associated') | 85 | 85 | section = ForeignKey('Section', help_text='The section with which the card is associated') | |
pushed = DateTimeField(auto_now_add=True, help_text="When the card was first pushed") | 86 | 86 | pushed = DateTimeField(auto_now_add=True, help_text="When the card was first pushed") | |
material_date = DateTimeField(default=now, help_text="The date with which the card is associated") | 87 | 87 | material_date = DateTimeField(default=now, help_text="The date with which the card is associated") | |
previous = ForeignKey('Flashcard', null=True, blank=True, | 88 | 88 | previous = ForeignKey('Flashcard', null=True, blank=True, | |
help_text="The previous version of this card, if one exists") | 89 | 89 | help_text="The previous version of this card, if one exists") | |
author = ForeignKey(User) | 90 | 90 | author = ForeignKey(User) | |
is_hidden = BooleanField(default=False) | 91 | 91 | is_hidden = BooleanField(default=False) | |
hide_reason = CharField(blank=True, max_length=255, help_text="Reason for hiding this card") | 92 | 92 | hide_reason = CharField(blank=True, max_length=255, help_text="Reason for hiding this card") | |
mask = MaskField(max_length=255, null=True, blank=True, help_text="The mask on the card") | 93 | 93 | mask = MaskField(max_length=255, null=True, blank=True, help_text="The mask on the card") | |
94 | 94 | |||
class Meta: | 95 | 95 | class Meta: | |
# By default, order by most recently pushed | 96 | 96 | # By default, order by most recently pushed | |
ordering = ['-pushed'] | 97 | 97 | ordering = ['-pushed'] | |
98 | 98 | |||
def is_hidden_from(self, user): | 99 | 99 | def is_hidden_from(self, user): | |
""" | 100 | 100 | """ | |
A card can be hidden globally, but if a user has the card in their deck, | 101 | 101 | A card can be hidden globally, but if a user has the card in their deck, | |
this visibility overrides a global hide. | 102 | 102 | this visibility overrides a global hide. | |
:param user: | 103 | 103 | :param user: | |
:return: Whether the card is hidden from the user. | 104 | 104 | :return: Whether the card is hidden from the user. | |
""" | 105 | 105 | """ | |
result = user.userflashcard_set.filter(flashcard=self) | 106 | 106 | result = user.userflashcard_set.filter(flashcard=self) | |
if not result.exists(): return self.is_hidden | 107 | 107 | if not result.exists(): return self.is_hidden | |
return result[0].is_hidden() | 108 | 108 | return result[0].is_hidden() | |
109 | 109 | |||
@classmethod | 110 | 110 | @classmethod | |
def cards_visible_to(cls, user): | 111 | 111 | def cards_visible_to(cls, user): | |
""" | 112 | 112 | """ | |
:param user: | 113 | 113 | :param user: | |
:return: A queryset with all cards that should be visible to a user. | 114 | 114 | :return: A queryset with all cards that should be visible to a user. | |
""" | 115 | 115 | """ | |
return cls.objects.filter(is_hidden=False).exclude(userflashcard__user=user, userflashcard__pulled=None) | 116 | 116 | return cls.objects.filter(is_hidden=False).exclude(userflashcard__user=user, userflashcard__pulled=None) | |
117 | 117 | |||
118 | 118 | |||
class UserFlashcardQuiz(Model): | 119 | 119 | class UserFlashcardQuiz(Model): | |
""" | 120 | 120 | """ | |
An event of a user being quizzed on a flashcard. | 121 | 121 | An event of a user being quizzed on a flashcard. | |
""" | 122 | 122 | """ | |
user_flashcard = ForeignKey(UserFlashcard) | 123 | 123 | user_flashcard = ForeignKey(UserFlashcard) | |
when = DateTimeField(auto_now=True) | 124 | 124 | when = DateTimeField(auto_now=True) | |
blanked_word = CharField(max_length=8, blank=True, help_text="The character range which was blanked") | 125 | 125 | blanked_word = CharField(max_length=8, blank=True, help_text="The character range which was blanked") | |
response = CharField(max_length=255, blank=True, null=True, help_text="The user's response") | 126 | 126 | response = CharField(max_length=255, blank=True, null=True, help_text="The user's response") | |
correct = NullBooleanField(help_text="The user's self-evaluation of their response") | 127 | 127 | correct = NullBooleanField(help_text="The user's self-evaluation of their response") | |
128 | 128 | |||
def status(self): | 129 | 129 | def status(self): | |
""" | 130 | 130 | """ | |
There are three stages of a quiz object: | 131 | 131 | There are three stages of a quiz object: | |
1. the user has been shown the card | 132 | 132 | 1. the user has been shown the card | |
2. the user has answered the card | 133 | 133 | 2. the user has answered the card | |
3. the user has self-evaluated their response's correctness | 134 | 134 | 3. the user has self-evaluated their response's correctness | |
135 | 135 | |||
:return: string (evaluated, answered, viewed) | 136 | 136 | :return: string (evaluated, answered, viewed) | |
""" | 137 | 137 | """ | |
if self.correct is not None: return "evaluated" | 138 | 138 | if self.correct is not None: return "evaluated" | |
if self.response: return "answered" | 139 | 139 | if self.response: return "answered" | |
return "viewed" | 140 | 140 | return "viewed" | |
141 | 141 | |||
142 | 142 | |||
class Section(Model): | 143 | 143 | class Section(Model): | |
""" | 144 | 144 | """ | |
A UCSD course taught by an instructor during a quarter. | 145 | 145 | A UCSD course taught by an instructor during a quarter. | |
We use the term "section" to avoid collision with the builtin keyword "class" | 146 | 146 | We use the term "section" to avoid collision with the builtin keyword "class" | |
We index gratuitously to support autofill and because this is primarily read-only | 147 | 147 | We index gratuitously to support autofill and because this is primarily read-only | |
""" | 148 | 148 | """ | |
department = CharField(db_index=True, max_length=50) | 149 | 149 | department = CharField(db_index=True, max_length=50) | |
department_abbreviation = CharField(db_index=True, max_length=10) | 150 | 150 | department_abbreviation = CharField(db_index=True, max_length=10) | |
course_num = CharField(db_index=True, max_length=6) | 151 | 151 | course_num = CharField(db_index=True, max_length=6) | |
course_title = CharField(db_index=True, max_length=50) | 152 | 152 | course_title = CharField(db_index=True, max_length=50) | |
instructor = CharField(db_index=True, max_length=100) | 153 | 153 | instructor = CharField(db_index=True, max_length=100) | |
quarter = CharField(db_index=True, max_length=4) | 154 | 154 | quarter = CharField(db_index=True, max_length=4) | |
155 | 155 | |||
@classmethod | 156 | 156 | @classmethod | |
def search(cls, terms): | 157 | 157 | def search(cls, terms): | |
""" | 158 | 158 | """ | |
Search all fields of all sections for a particular set of terms | 159 | 159 | Search all fields of all sections for a particular set of terms | |
A matching section must match at least one field on each term | 160 | 160 | A matching section must match at least one field on each term | |
:param terms:iterable | 161 | 161 | :param terms:iterable | |
:return: Matching QuerySet ordered by department and course number | 162 | 162 | :return: Matching QuerySet ordered by department and course number | |
""" | 163 | 163 | """ | |
final_q = Q() | 164 | 164 | final_q = Q() | |
for term in terms: | 165 | 165 | for term in terms: | |
q = Q(department__icontains=term) | 166 | 166 | q = Q(department__icontains=term) | |
q |= Q(department_abbreviation__icontains=term) | 167 | 167 | q |= Q(department_abbreviation__icontains=term) | |
q |= Q(course_title__icontains=term) | 168 | 168 | q |= Q(course_title__icontains=term) | |
q |= Q(course_num__icontains=term) | 169 | 169 | q |= Q(course_num__icontains=term) | |
q |= Q(instructor__icontains=term) | 170 | 170 | q |= Q(instructor__icontains=term) | |
final_q &= q | 171 | 171 | final_q &= q | |
qs = cls.objects.filter(final_q) | 172 | 172 | qs = cls.objects.filter(final_q) | |
# Have the database cast the course number to an integer so it will sort properly | 173 | 173 | # Have the database cast the course number to an integer so it will sort properly | |
# ECE 35 should rank before ECE 135 even though '135' < '35' lexicographically | 174 | 174 | # ECE 35 should rank before ECE 135 even though '135' < '35' lexicographically | |
qs = qs.extra(select={'course_num_int': 'CAST(course_num AS INTEGER)'}) | 175 | 175 | qs = qs.extra(select={'course_num_int': "CAST(rtrim(course_num, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') AS INTEGER)"}) | |
qs = qs.order_by('department_abbreviation', 'course_num_int') | 176 | 176 | qs = qs.order_by('department_abbreviation', 'course_num_int') | |
return qs | 177 | 177 | return qs | |
178 | 178 | |||
def is_whitelisted(self): | 179 | 179 | def is_whitelisted(self): | |
""" | 180 | 180 | """ | |
:return: whether a whitelist exists for this section | 181 | 181 | :return: whether a whitelist exists for this section | |
""" | 182 | 182 | """ | |
return self.whitelist.exists() | 183 | 183 | return self.whitelist.exists() | |
184 | 184 | |||
def is_user_on_whitelist(self, user): | 185 | 185 | def is_user_on_whitelist(self, user): | |
""" | 186 | 186 | """ | |
:return: whether the user is on the waitlist for this section | 187 | 187 | :return: whether the user is on the waitlist for this section | |
""" | 188 | 188 | """ | |
return self.whitelist.filter(email=user.email).exists() | 189 | 189 | return self.whitelist.filter(email=user.email).exists() | |
190 | 190 | |||
class Meta: | 191 | 191 | class Meta: | |
ordering = ['-course_title'] | 192 | 192 | ordering = ['-course_title'] | |
193 | 193 |