User model

This commit is contained in:
Kumi 2022-08-08 15:17:48 +00:00
parent bdfa5cab2a
commit 23a4a491b9
Signed by: kumi
GPG key ID: ECBCC9082395383F
7 changed files with 113 additions and 10 deletions

21
datastore/managers.py Normal file
View file

@ -0,0 +1,21 @@
from django.contrib.auth.base_user import BaseUserManager
class UserManager(BaseUserManager):
def create_user(self, email, **extra_fields):
if not email:
raise ValueError('The Email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.save()
return user
def create_superuser(self, email, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self.create_user(email, **extra_fields)

View file

@ -0,0 +1,34 @@
# Generated by Django 4.1 on 2022-08-08 12:59
from django.db import migrations, models
import django.utils.timezone
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=254, unique=True, verbose_name='email address')),
('is_staff', models.BooleanField(default=False)),
('is_active', models.BooleanField(default=True)),
('date_joined', models.DateTimeField(default=django.utils.timezone.now)),
('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,
},
),
]

View file

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1 @@
from .auth import User

20
datastore/models/auth.py Normal file
View file

@ -0,0 +1,20 @@
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.utils import timezone
from ..managers import UserManager
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField('email address', unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
def __str__(self):
return self.email

View file

@ -9,9 +9,9 @@ BASE_DIR = Path(__file__).resolve().parent.parent
CONFIG_PATH = BASE_DIR / "settings.ini" CONFIG_PATH = BASE_DIR / "settings.ini"
ASK = AutoSecretKey(CONFIG_PATH) ASK = AutoSecretKey(CONFIG_PATH)
MONSTERCONFIG = MonsterConfig(CONFIG_PATH)
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = ASK.config.getboolean("DUMUZID", "Debug", fallback=False)
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
@ -64,17 +64,32 @@ WSGI_APPLICATION = 'dumuzid.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases # https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = { if "MySQL" in ASK.config:
'default': { DATABASES = {
'ENGINE': 'django.db.backends.sqlite3', 'default': {
'NAME': BASE_DIR / 'db.sqlite3', 'ENGINE': 'django.db.backends.mysql',
'NAME': CONFIG_FILE.config.get("MySQL", "Database"),
'USER': CONFIG_FILE.config.get("MySQL", "Username"),
'PASSWORD': CONFIG_FILE.config.get("MySQL", "Password"),
'HOST': CONFIG_FILE.config.get("MySQL", "Host", fallback="localhost"),
'PORT': CONFIG_FILE.config.getint("MySQL", "Port", fallback=3306)
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
} }
}
# Password validation # Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_USER_MODEL = 'datastore.User'
AUTH_PASSWORD_VALIDATORS = [ AUTH_PASSWORD_VALIDATORS = [
{ {
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',

15
settings.dist.ini Normal file
View file

@ -0,0 +1,15 @@
[DUMUZID]
Debug = 0
# [MySQL]
# Database = academon
# Username = academon
# Password = secret123!
# Host = localhost
# Port = 3306
# [S3]
# AccessKey = academon
# SecretKey = !!!verysecret!!!
# Bucket = academon
# Endpoint = https://minio.acade.mon