Commit ddd9b214550605969ecaded850aec381dfee1673

Authored by Andrew Buss
1 parent 56c04ca5b8
Exists in master

Fixed usermanager more completely

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

flashcards/api.py View file @ ddd9b21
... ... @@ -55,7 +55,7 @@
55 55 existing_users = User.objects.filter(email=email)
56 56 if existing_users.exists():
57 57 raise ValidationError("An account with this email already exists")
58   - user = User.objects.create_user(email, email=email, password=request.data['password'])
  58 + user = User.objects.create_user(email=email, password=request.data['password'])
59 59  
60 60 body = '''
61 61 Visit the following link to confirm your email address:
flashcards/models.py View file @ ddd9b21
1 1 from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, AbstractUser, UserManager
2 2 from django.db.models import *
  3 +from django.utils.timezone import now
3 4 from simple_email_confirmation import SimpleEmailConfirmationUserMixin
4 5  
5 6 # Hack to fix AbstractUser before subclassing it
6 7 AbstractUser._meta.get_field('email')._unique = True
7 8  
8 9  
9   -class UserManager(UserManager):
  10 +class EmailOnlyUserManager(UserManager):
  11 + """
  12 + A tiny extension of Django's UserManager which correctly creates users
  13 + without usernames (using emails instead).
  14 + """
  15 +
  16 + def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
  17 + """
  18 + Creates and saves a User with the given username, email and password.
  19 + """
  20 + email = self.normalize_email(email)
  21 + user = self.model(email=email,
  22 + is_staff=is_staff, is_active=True,
  23 + is_superuser=is_superuser,
  24 + date_joined=now(), **extra_fields)
  25 + user.set_password(password)
  26 + user.save(using=self._db)
  27 + return user
  28 +
  29 + def create_user(self, email, password=None, **extra_fields):
  30 + return self._create_user(email, password, False, False, **extra_fields)
  31 +
10 32 def create_superuser(self, email, password, **extra_fields):
11   - return self._create_user(email, email, password, True, True, **extra_fields)
  33 + return self._create_user(email, password, True, True, **extra_fields)
12 34  
13 35  
14 36 class User(AbstractUser, SimpleEmailConfirmationUserMixin):
15   - objects = UserManager()
  37 + """
  38 + An extension of Django's default user model.
  39 + We use email as the username field, and include enrolled sections here
  40 + """
  41 + objects = EmailOnlyUserManager()
16 42 USERNAME_FIELD = 'email'
17 43 REQUIRED_FIELDS = []
18   - sections = ManyToManyField('Section')
  44 + sections = ManyToManyField('Section', help_text="The sections which the user is enrolled in")
19 45  
20 46  
21 47 class UserFlashcard(Model):
... ... @@ -137,7 +163,6 @@
137 163  
138 164 class Meta:
139 165 unique_together = (('department', 'course_num', 'quarter', 'instructor'),)
140   - ordering = ['-quarter']
141 166  
142 167  
143 168 class LecturePeriod(Model):
... ... @@ -145,7 +170,7 @@
145 170 A lecture period for a section
146 171 """
147 172 section = ForeignKey(Section)
148   - week_day = IntegerField(help_text="0-indexed day of week, starting at Monday")
  173 + week_day = IntegerField(help_text="1-indexed day of week, starting at Sunday")
149 174 start_time = TimeField()
150 175 end_time = TimeField()
151 176  
152 177  
153 178  
... ... @@ -154,14 +179,20 @@
154 179  
155 180  
156 181 class WhitelistedAddress(Model):
  182 + """
  183 + An email address that has been whitelisted for a section at an instructor's request
  184 + """
157 185 email = EmailField()
158 186 section = ForeignKey(Section)
159 187  
160 188  
161 189 class FlashcardReport(Model):
  190 + """
  191 + A report by a user that a flashcard is unsuitable. A reason is optional
  192 + """
162 193 user = ForeignKey(User)
163 194 flashcard = ForeignKey(Flashcard)
164   - reason = CharField(max_length=255)
  195 + reason = CharField(max_length=255, blank=True)
165 196  
166 197 class Meta:
167 198 unique_together = (('user', 'flashcard'),)
flashcards/tests/test_api.py View file @ ddd9b21
... ... @@ -7,7 +7,7 @@
7 7 class LoginTests(APITestCase):
8 8 def setUp(self):
9 9 email = "test@flashy.cards"
10   - User.objects.create_user(email, email=email, password="1234")
  10 + User.objects.create_user(email=email, password="1234")
11 11  
12 12 def test_login(self):
13 13 url = '/api/login'
... ... @@ -51,7 +51,7 @@
51 51 class ProfileViewTest(APITestCase):
52 52 def setUp(self):
53 53 email = "profileviewtest@flashy.cards"
54   - User.objects.create_user(email, email=email, password="1234")
  54 + User.objects.create_user(email=email, password="1234")
55 55  
56 56 def test_get_me(self):
57 57 url = '/api/users/me'