from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.utils.translation import gettext_lazy as _
from rest_framework_simplejwt.tokens import RefreshToken
from core.common import DateTimeStampMixin
from core.managers import UserManager
from utils.constants import (
CONTACT_ADMIN,
SUPER_ADMIN,
USER_ROLE,
)
from ministries.models import *
class User(AbstractBaseUser, PermissionsMixin):
"""User in the system."""
class PermissionLevel(models.TextChoices):
SUPER_ADMIN = SUPER_ADMIN, _("Super Admin")
CONTACT_ADMIN = SUPER_ADMIN, _("Contact Admin")
USER_ROLE = "USER", _("User")
organization_category = models.ForeignKey(
OrganizationCategory,
related_name="organizations",
on_delete=models.CASCADE,
null=True,
blank=True,
)
organization_name = models.ForeignKey(
OrganizationName,
related_name="organization_name",
on_delete=models.CASCADE,
null=True,
blank=True,
)
first_name = models.CharField(max_length=30, null=True, blank=True)
last_name = models.CharField(max_length=30, null=True, blank=True)
email = models.EmailField(max_length=30, unique=True, db_index=True)
password = models.CharField(max_length=30)
phone_number = models.CharField(max_length=12, null=True, blank=True)
username = models.CharField(max_length=30, null=True, blank=True)
identification = models.FileField(upload_to='ids/', null=True, blank=True)
dob = models.DateField(null=True, blank=True)
gender = models.CharField(max_length=30, null=True, blank=True)
country = models.CharField(max_length=30, null=True, blank=True)
district = models.CharField(max_length=30, null=True, blank=True)
permission_level = models.CharField(
max_length=30,
choices=PermissionLevel.choices,
default=PermissionLevel.USER_ROLE,
)
is_active = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_verified = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["first_name", "last_name", "password"]
def __str__(self):
return self.email
@property
def full_name(self):
return f"{self.first_name} {self.last_name}"
@property
def tokens(self):
refresh = RefreshToken.for_user(self)
return {
"refresh": str(refresh),
"access": str(refresh.access_token),
}
the dumps is
[{"model": "core.user", "pk": 1, "fields": {"last_login": null, "is_superuser": true, "organization_category": null, "organization_name": null, "first_name": "otim", "last_name": "admin", "email": "otim@mail.com", "password": "pbkdf2_sha256$390000$SicGy5s2uvKs2gPwNxhtSG$5WEqpa7tuCs7EIMVT+YKPPw/RT5ip/VS6Bq+mjOlINY=", "phone_number": null, "username": null, "identification": "", "dob": null, "gender": null, "country": null, "district": null, "permission_level": "SUPER_ADMIN", "is_active": false, "is_staff": true, "is_verified": true, "groups": [], "user_permissions": []}}, {"model": "core.user", "pk": 2, "fields": {"last_login": null, "is_superuser": true, "organization_category": null, "organization_name": null, "first_name": "William", "last_name": "Otim", "email": "william@mail.com", "password": "pbkdf2_sha256$390000$ceAzxxsSif0nBbCQV6m63B$hqfvZiR0u9LCg8X6Z8yIZTd2VMjsDmOVy43/Zheodik=", "phone_number": null, "username": null, "identification": "", "dob": null, "gender": null, "country": null, "district": null, "permission_level": "SUPER_ADMIN", "is_active": false, "is_staff": true, "is_verified": true, "groups": [], "user_permissions": []}}, {"model": "core.user", "pk": 3, "fields": {"last_login": null, "is_superuser": true, "organization_category": null, "organization_name": null, "first_name": "gerison", "last_name": "otim", "email": "gerison@mail.com", "password": "pbkdf2_sha256$390000$2hVEbbAWQQ6FOo0yqfdgS8$iAI3hbrW5XdQmQigtgF3to1IvnN/0KHc8CmJA4tY5Hk=", "phone_number": null, "username": null, "identification": "", "dob": null, "gender": null, "country": null, "district": null, "permission_level": "SUPER_ADMIN", "is_active": false, "is_staff": true, "is_verified": true, "groups": [], "user_permissions": []}}]```
and I created them using createsuperuser
Email: gerison@mail.com
First name: gerison
Last name: otim
Password: gerison@otim
Superuser created successfully.