Skip to content

Commit

Permalink
feat: use email as default username in custom user model
Browse files Browse the repository at this point in the history
  • Loading branch information
nkordis committed Apr 30, 2024
1 parent 15414d7 commit 0ab1c9b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: help lint test
.PHONY: help lint test makemigrations migrate

# Display callable targets to the user
help:
Expand All @@ -15,3 +15,17 @@ lint:
test:
@echo "Running unit tests..."
docker-compose run --rm app sh -c "python manage.py test && flake8"

# Create new migrations based on changes to Django models.
makemigrations:
@echo "Creating new migrations based on model changes..."
docker-compose run --rm app sh -c "python manage.py makemigrations"
@echo "Migrations created successfully."

# Ensures that the database is ready and then applies Django migrations to update the database schema according to the latest models.
migrate:
@echo "Checking database availability..."
docker-compose run --rm app sh -c "python manage.py wait_for_db"
@echo "Applying database migrations..."
docker-compose run --rm app sh -c "python manage.py migrate"
@echo "Database migration completed successfully."
2 changes: 2 additions & 0 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,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'
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.25 on 2024-04-30 15:18

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,
},
),
]
34 changes: 32 additions & 2 deletions app/core/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
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_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 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'

0 comments on commit 0ab1c9b

Please sign in to comment.