Commit 8b6e41f08f72c206f8d38ab5bbdf5797aa03c7d7
1 parent
a33d629b29
Exists in
master
Added some helper model functions
Showing 3 changed files with 118 additions and 11 deletions Side-by-side Diff
flashy/flashcards/migrations/0001_initial.py
View file @
8b6e41f
1 | +# -*- coding: utf-8 -*- | |
2 | +from __future__ import unicode_literals | |
3 | + | |
4 | +from django.db import models, migrations | |
5 | +from django.conf import settings | |
6 | + | |
7 | + | |
8 | +class Migration(migrations.Migration): | |
9 | + | |
10 | + dependencies = [ | |
11 | + migrations.swappable_dependency(settings.AUTH_USER_MODEL), | |
12 | + ] | |
13 | + | |
14 | + operations = [ | |
15 | + migrations.CreateModel( | |
16 | + name='Class', | |
17 | + fields=[ | |
18 | + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | |
19 | + ('department', models.CharField(max_length=50)), | |
20 | + ('course_num', models.IntegerField()), | |
21 | + ('name', models.CharField(max_length=50)), | |
22 | + ('professor', models.CharField(max_length=50)), | |
23 | + ('quarter', models.CharField(max_length=4)), | |
24 | + ('members', models.ManyToManyField(to=settings.AUTH_USER_MODEL)), | |
25 | + ], | |
26 | + ), | |
27 | + migrations.CreateModel( | |
28 | + name='Flashcard', | |
29 | + fields=[ | |
30 | + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | |
31 | + ('text', models.CharField(max_length=255)), | |
32 | + ('pushed', models.DateTimeField()), | |
33 | + ('material_date', models.DateTimeField()), | |
34 | + ('hidden', models.CharField(max_length=255, null=True, blank=True)), | |
35 | + ('associated_class', models.ForeignKey(to='flashcards.Class')), | |
36 | + ('author', models.ForeignKey(to=settings.AUTH_USER_MODEL)), | |
37 | + ], | |
38 | + ), | |
39 | + migrations.CreateModel( | |
40 | + name='FlashcardMask', | |
41 | + fields=[ | |
42 | + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | |
43 | + ('ranges', models.CharField(max_length=255)), | |
44 | + ], | |
45 | + ), | |
46 | + migrations.CreateModel( | |
47 | + name='UserFlashcard', | |
48 | + fields=[ | |
49 | + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | |
50 | + ('pulled', models.DateTimeField(null=True)), | |
51 | + ('unpulled', models.DateTimeField(null=True)), | |
52 | + ('flashcard', models.ForeignKey(to='flashcards.Flashcard')), | |
53 | + ('mask', models.ForeignKey(to='flashcards.FlashcardMask')), | |
54 | + ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)), | |
55 | + ], | |
56 | + ), | |
57 | + migrations.CreateModel( | |
58 | + name='UserFlashCardReview', | |
59 | + fields=[ | |
60 | + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | |
61 | + ('when', models.DateTimeField()), | |
62 | + ('blanked_word', models.CharField(max_length=8)), | |
63 | + ('response', models.CharField(max_length=255, null=True, blank=True)), | |
64 | + ('correct', models.NullBooleanField()), | |
65 | + ('user_flashcard', models.ForeignKey(to='flashcards.UserFlashcard')), | |
66 | + ], | |
67 | + ), | |
68 | + migrations.AddField( | |
69 | + model_name='flashcard', | |
70 | + name='mask', | |
71 | + field=models.ForeignKey(to='flashcards.FlashcardMask', null=True), | |
72 | + ), | |
73 | + migrations.AddField( | |
74 | + model_name='flashcard', | |
75 | + name='previous', | |
76 | + field=models.ForeignKey(to='flashcards.Flashcard', null=True), | |
77 | + ), | |
78 | + ] |
flashy/flashcards/models.py
View file @
8b6e41f
1 | +from django.contrib.auth.models import User | |
1 | 2 | from django.db import models |
2 | 3 | |
4 | + | |
3 | 5 | class UserFlashcard(models.Model): |
4 | 6 | """ |
5 | 7 | Represents the relationship between a user and a flashcard by: |
6 | - 1. A user has a flashcard in their deck | |
7 | - 2. A user used to have a flashcard in their deck | |
8 | - 3. A user has a flashcard hidden from them | |
8 | + 1. A user has a flashcard in their deck | |
9 | + 2. A user used to have a flashcard in their deck | |
10 | + 3. A user has a flashcard hidden from them | |
9 | 11 | """ |
10 | 12 | user = models.ForeignKey(User) |
11 | 13 | mask = models.ForeignKey('FlashcardMask') |
12 | - pulled = models.DateTimeField() | |
13 | - flashcard = models.ForeignKey(Flashcard) | |
14 | - unpulled = models.DateTimeField() | |
14 | + pulled = models.DateTimeField(null=True) | |
15 | + flashcard = models.ForeignKey('Flashcard') | |
16 | + unpulled = models.DateTimeField(null=True) | |
15 | 17 | |
18 | + class Meta: | |
19 | + unique_together = (('user', 'flashcard'),) | |
20 | + index_together = ["user", "flashcard"] | |
21 | + ordering = ['-pulled'] | |
22 | + | |
23 | + def is_hidden(self): | |
24 | + return not self.pulled | |
25 | + | |
26 | + def is_active(self): | |
27 | + return self.pulled and not self.unpulled | |
28 | + | |
29 | + | |
16 | 30 | class FlashcardMask(models.Model): |
17 | 31 | ranges = models.CharField(max_length=255) |
18 | 32 | |
33 | + | |
19 | 34 | class Flashcard(models.Model): |
20 | 35 | text = models.CharField(max_length=255) |
21 | - associated_class = models.ForeignKey(Class) | |
36 | + associated_class = models.ForeignKey('Class') | |
22 | 37 | pushed = models.DateTimeField() |
23 | 38 | material_date = models.DateTimeField() |
24 | - previous = models.ForeignKey('Flashcard') | |
25 | - author = models.ForeignKey('User') | |
26 | - hidden = models.CharField(null=True, max_length=255) | |
27 | - mask = models.ForeignKey(FlashcardMask) | |
39 | + previous = models.ForeignKey('Flashcard', null=True) | |
40 | + author = models.ForeignKey(User) | |
41 | + hidden = models.CharField(null=True, blank=True, max_length=255) | |
42 | + mask = models.ForeignKey(FlashcardMask, null=True) | |
28 | 43 | |
44 | + def is_visible_to(self, user): | |
45 | + try: | |
46 | + result = self.userflashcard_set.get(user=user) | |
47 | + return not result.is_hidden() | |
48 | + except UserFlashcard.DoesNotExist: | |
49 | + return not self.hidden | |
50 | + | |
51 | + @classmethod | |
52 | + def cards_visible_to(cls, user): | |
53 | + return cls.objects.filter(hidden=False).exclude(userflashcard=user, userflashcard__pulled=None) | |
54 | + | |
55 | + | |
29 | 56 | class UserFlashCardReview(models.Model): |
30 | 57 | user_flashcard = models.ForeignKey(UserFlashcard) |
31 | 58 | when = models.DateTimeField() |
32 | 59 | blanked_word = models.CharField(max_length=8) |
33 | 60 | response = models.CharField(max_length=255, blank=True, null=True) |
34 | 61 | correct = models.NullBooleanField() |
62 | + | |
35 | 63 | |
36 | 64 | class Class(models.Model): |
37 | 65 | department = models.CharField(max_length=50) |