-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Generated by Django 4.2.6 on 2023-10-12 07:07 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
('auth', '0012_alter_user_first_name_max_length'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='User', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('password', models.CharField(max_length=128, verbose_name='password')), | ||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), | ||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), | ||
('email', models.EmailField(max_length=255, unique=True)), | ||
('name', models.CharField(max_length=255)), | ||
('is_active', models.BooleanField(default=True)), | ||
('is_staff', models.BooleanField(default=False)), | ||
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), | ||
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,27 @@ | ||
from django.db import models | ||
from django.contrib.auth.models import ( | ||
AbstractBaseUser, | ||
BaseUserManager, | ||
PermissionsMixin | ||
) | ||
|
||
# Create your models here. | ||
|
||
class UserManager(BaseUserManager): | ||
"""Manager for users.""" | ||
def create_user(self, email, password=None, **extra_fields): | ||
"""create, save and return a new user.""" | ||
user = self.model(email=email, **extra_fields) | ||
user.set_password(password) | ||
user.save(using=self._db) | ||
|
||
return user | ||
|
||
class User(AbstractBaseUser, PermissionsMixin): | ||
"""User in you system.""" | ||
email = models.EmailField(max_length=255, unique=True) | ||
name = models.CharField(max_length=255) | ||
is_active = models.BooleanField(default=True) | ||
is_staff = models.BooleanField(default=False) | ||
|
||
USERNAME_FIELD = 'email' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" | ||
Tests for models | ||
""" | ||
|
||
from django.test import TestCase | ||
from django.contrib.auth import get_user_model | ||
|
||
class ModelTests(TestCase): | ||
"""Test Models.""" | ||
|
||
def test_create_user_with_email_successful(self): | ||
"""Test creating a user with an email is sucessful.""" | ||
email = '[email protected]' | ||
password = 'testpass123' | ||
user = get_user_model().objects.create_user( | ||
email=email, | ||
password=password, | ||
) | ||
|
||
self.assertEqual(user.email, email) | ||
self.assertTrue(user.check_password(password)) |