Skip to content

Commit

Permalink
[SC-16] Added account model (#14)
Browse files Browse the repository at this point in the history
* Added account model

* Override user model to not require username
  • Loading branch information
Fungucide authored Feb 5, 2025
1 parent 6736959 commit 23f8ac5
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.sqlite3
__pycache__/
Empty file added backend/accounts/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions backend/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin
from .models import User

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
pass
6 changes: 6 additions & 0 deletions backend/accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'
40 changes: 40 additions & 0 deletions backend/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 5.1.5 on 2025-02-03 21:07

import accounts.user_manager
import django.utils.timezone
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, verbose_name='email address')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('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,
},
managers=[
('objects', accounts.user_manager.UserManager()),
],
),
]
Empty file.
38 changes: 38 additions & 0 deletions backend/accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.db import models

from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin

from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from .user_manager import UserManager

class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
_("email address"),
unique=True,
max_length=255,
blank=False
)
first_name = models.CharField(_("first name"), max_length=150, blank=True)
last_name = models.CharField(_("last name"), max_length=150, blank=True)
is_staff = models.BooleanField(
_("staff status"),
default=False,
help_text=_("Designates whether the user can log into this admin site."),
)
is_active = models.BooleanField(
_("active"),
default=True,
help_text=_(
"Designates whether this user should be treated as active. "
"Unselect this instead of deleting accounts."
),
)

date_joined = models.DateTimeField(_("date joined"), default=timezone.now)

objects = UserManager()

USERNAME_FIELD = 'email'
3 changes: 3 additions & 0 deletions backend/accounts/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
31 changes: 31 additions & 0 deletions backend/accounts/user_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.contrib.auth.base_user import BaseUserManager


class UserManager(BaseUserManager):

use_in_migrations = True

def _create_user(self, email, password, **extra_fields):
'''Create and save a user with the given email, and
password.
'''
if not email:
raise ValueError("No email provided")

email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(email, password, **extra_fields)

def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
assert(extra_fields.get("is_staff"))
assert(extra_fields.get("is_superuser"))
return self._create_user(email, password, **extra_fields)
3 changes: 3 additions & 0 deletions backend/accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
3 changes: 3 additions & 0 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -69,6 +70,8 @@
},
]

AUTH_USER_MODEL = 'accounts.User'

WSGI_APPLICATION = 'backend.wsgi.application'


Expand Down

0 comments on commit 23f8ac5

Please sign in to comment.