Skip to content

Commit

Permalink
created custom user model and user manager
Browse files Browse the repository at this point in the history
  • Loading branch information
nishu-saini committed Nov 1, 2023
1 parent 444b345 commit 8759f51
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 32 deletions.
11 changes: 0 additions & 11 deletions app/app/calc.py

This file was deleted.

2 changes: 2 additions & 0 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

AUTH_USER_MODEL = 'core.User'
19 changes: 0 additions & 19 deletions app/app/test.py

This file was deleted.

33 changes: 33 additions & 0 deletions app/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.2.22 on 2023-11-01 12:14

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,
},
),
]
35 changes: 33 additions & 2 deletions app/core/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
from django.db import models # noqa
"""
Database models
"""
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_field):
"""Create, save and return a new user"""
user = self.model(email, **extra_field)
user.set_password(password)

user.save(using=self._db)

return user


class User(AbstractBaseUser, PermissionsMixin):
"""User in the 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)

objects = UserManager()

USERNAME_FIELD = 'email'
37 changes: 37 additions & 0 deletions app/core/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
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 successful.."""
email = 'test@example.com'
password = 'testpass123'

user = get_user_model().objects.create(
email=email,
password=password
)

self.assertEqual(user.email, email)
self.assertTrue(user.check_password(password))


def test_new_user_email_normalized(self):
"""Test email is normalised for new users"""
sample_emails = [
['test1@EXAMPLE.com', 'test1@example.com'],
['Test2@Example.com', 'Test2@example.com'],
['TEST3@EXAMPLE.com', 'TEST3@example.com'],
['test4@example.COM', 'test4@example.com']
]

for email, expected in sample_emails:
user = get_user_model().objects.create(email=email, password='sample123')
self.assertEqual(user.email, expected)

0 comments on commit 8759f51

Please sign in to comment.