mirror of
https://github.com/lemeow125/DocManagerBackend.git
synced 2025-06-29 00:45:46 +08:00
Move sex and age fields from questionnaire to user and add planning role restrictions
This commit is contained in:
parent
724132e396
commit
e0eba6ca21
25 changed files with 157 additions and 320 deletions
|
@ -10,7 +10,8 @@ class CustomUserAdmin(UserAdmin):
|
|||
readonly_fields = ("date_joined",)
|
||||
|
||||
# Add this line to include the role field in the admin form
|
||||
fieldsets = UserAdmin.fieldsets + ((None, {"fields": ("role",)}),)
|
||||
fieldsets = UserAdmin.fieldsets + \
|
||||
((None, {"fields": ("role", "sex", "birthday")}),)
|
||||
|
||||
|
||||
admin.site.register(CustomUser, CustomUserAdmin)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 5.1.3 on 2024-11-23 17:01
|
||||
# Generated by Django 5.1.3 on 2024-12-03 16:27
|
||||
|
||||
import django.contrib.auth.models
|
||||
import django.contrib.auth.validators
|
||||
|
@ -106,6 +106,13 @@ class Migration(migrations.Migration):
|
|||
default=django.utils.timezone.now, editable=False
|
||||
),
|
||||
),
|
||||
(
|
||||
"sex",
|
||||
models.CharField(
|
||||
choices=[("male", "Male"), ("female", "Female")], max_length=32
|
||||
),
|
||||
),
|
||||
("birthday", models.DateField()),
|
||||
(
|
||||
"groups",
|
||||
models.ManyToManyField(
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
from django.utils.timezone import now
|
||||
from django.utils.timezone import now, localdate
|
||||
|
||||
|
||||
class CustomUser(AbstractUser):
|
||||
|
@ -22,10 +22,33 @@ class CustomUser(AbstractUser):
|
|||
("staff", "Staff"),
|
||||
)
|
||||
|
||||
role = models.CharField(max_length=32, choices=ROLE_CHOICES, default="client")
|
||||
role = models.CharField(
|
||||
max_length=32, choices=ROLE_CHOICES, default="client")
|
||||
|
||||
date_joined = models.DateTimeField(default=now, editable=False)
|
||||
|
||||
SEX_CHOICES = (
|
||||
("male", "Male"),
|
||||
("female", "Female"),
|
||||
)
|
||||
|
||||
sex = models.CharField(
|
||||
max_length=32, choices=SEX_CHOICES, blank=False, null=False)
|
||||
birthday = models.DateField(blank=False, null=False)
|
||||
|
||||
@property
|
||||
def age(self):
|
||||
date_now = localdate(now())
|
||||
age = (
|
||||
date_now.year
|
||||
- self.birthday.year
|
||||
- (
|
||||
(date_now.month, date_now.day)
|
||||
< (self.birthday.month, self.birthday.day)
|
||||
)
|
||||
)
|
||||
return age
|
||||
|
||||
@property
|
||||
def full_name(self):
|
||||
return f"{self.first_name} {self.last_name}"
|
||||
|
|
|
@ -12,6 +12,15 @@ class IsStaff(BasePermission):
|
|||
)
|
||||
|
||||
|
||||
class IsPlanning(BasePermission):
|
||||
"""
|
||||
Allows access only to users with head, admin planning role
|
||||
"""
|
||||
|
||||
def has_permission(self, request, view):
|
||||
return bool(request.user and request.user.role in ("head", "admin", "planning"))
|
||||
|
||||
|
||||
class IsHead(BasePermission):
|
||||
"""
|
||||
Allows access only to users with staff role
|
||||
|
|
|
@ -6,6 +6,8 @@ from rest_framework.settings import api_settings
|
|||
|
||||
|
||||
class CustomUserSerializer(serializers.ModelSerializer):
|
||||
birthday = serializers.DateField(format="%m-%d-%Y")
|
||||
|
||||
class Meta:
|
||||
model = CustomUser
|
||||
fields = [
|
||||
|
@ -16,8 +18,22 @@ class CustomUserSerializer(serializers.ModelSerializer):
|
|||
"last_name",
|
||||
"full_name",
|
||||
"role",
|
||||
"birthday",
|
||||
"age",
|
||||
"sex",
|
||||
"date_joined",
|
||||
]
|
||||
read_only_fields = [
|
||||
"id",
|
||||
"username",
|
||||
"email",
|
||||
"full_name",
|
||||
"role",
|
||||
"birthday",
|
||||
"age",
|
||||
"sex",
|
||||
"date_joined",
|
||||
]
|
||||
read_only_fields = ["id", "username", "email", "full_name", "role"]
|
||||
|
||||
|
||||
class CustomUserRegistrationSerializer(serializers.ModelSerializer):
|
||||
|
@ -27,10 +43,12 @@ class CustomUserRegistrationSerializer(serializers.ModelSerializer):
|
|||
)
|
||||
first_name = serializers.CharField(required=True)
|
||||
last_name = serializers.CharField(required=True)
|
||||
birthday = serializers.DateField(format="%Y-%m-%d", required=True)
|
||||
|
||||
class Meta:
|
||||
model = CustomUser
|
||||
fields = ["email", "password", "first_name", "last_name"]
|
||||
fields = ["email", "password", "first_name",
|
||||
"last_name", "sex", "birthday"]
|
||||
|
||||
def validate(self, attrs):
|
||||
user_attrs = attrs.copy()
|
||||
|
@ -48,8 +66,7 @@ class CustomUserRegistrationSerializer(serializers.ModelSerializer):
|
|||
raise serializers.ValidationError({"password": errors})
|
||||
if self.Meta.model.objects.filter(username=attrs.get("email")).exists():
|
||||
raise serializers.ValidationError(
|
||||
"A user with that email already exists."
|
||||
)
|
||||
"A user with that email already exists.")
|
||||
return super().validate(attrs)
|
||||
|
||||
def create(self, validated_data):
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
from config.settings import get_secret
|
||||
from django.db.models.signals import post_migrate
|
||||
from django.dispatch import receiver
|
||||
from django.utils.timezone import now, localdate
|
||||
|
||||
from .models import CustomUser
|
||||
|
||||
|
||||
|
@ -8,12 +10,15 @@ from .models import CustomUser
|
|||
def create_admin_user(sender, **kwargs):
|
||||
# Programatically creates the administrator account
|
||||
if sender.name == "accounts":
|
||||
ADMIN_USER = CustomUser.objects.filter(email=get_secret("ADMIN_EMAIL")).first()
|
||||
ADMIN_USER = CustomUser.objects.filter(
|
||||
email=get_secret("ADMIN_EMAIL")).first()
|
||||
if not ADMIN_USER:
|
||||
ADMIN_USER = CustomUser.objects.create_superuser(
|
||||
username=get_secret("ADMIN_EMAIL"),
|
||||
email=get_secret("ADMIN_EMAIL"),
|
||||
password=get_secret("ADMIN_PASSWORD"),
|
||||
sex="male",
|
||||
birthday=localdate(now()),
|
||||
)
|
||||
|
||||
print("Created administrator account:", ADMIN_USER.email)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue