106 lines
3.6 KiB
Python
106 lines
3.6 KiB
Python
"""
|
|
Accounts serializers
|
|
"""
|
|
from django.contrib.auth import get_user_model
|
|
from rest_framework import serializers
|
|
|
|
from .models import Role
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
"""User serializer"""
|
|
roles = serializers.SerializerMethodField()
|
|
hospital_name = serializers.CharField(source='hospital.name', read_only=True)
|
|
department_name = serializers.CharField(source='department.name', read_only=True)
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = [
|
|
'id', 'username', 'email', 'first_name', 'last_name',
|
|
'phone', 'employee_id', 'hospital', 'hospital_name',
|
|
'department', 'department_name', 'avatar', 'bio',
|
|
'language', 'is_active', 'roles', 'date_joined',
|
|
'created_at', 'updated_at'
|
|
]
|
|
read_only_fields = ['id', 'date_joined', 'created_at', 'updated_at']
|
|
|
|
def get_roles(self, obj):
|
|
"""Get user roles"""
|
|
return obj.get_role_names()
|
|
|
|
|
|
class UserCreateSerializer(serializers.ModelSerializer):
|
|
"""User creation serializer with password"""
|
|
password = serializers.CharField(write_only=True, required=True, style={'input_type': 'password'})
|
|
password_confirm = serializers.CharField(write_only=True, required=True, style={'input_type': 'password'})
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = [
|
|
'username', 'email', 'password', 'password_confirm',
|
|
'first_name', 'last_name', 'phone', 'employee_id',
|
|
'hospital', 'department', 'language'
|
|
]
|
|
|
|
def validate(self, attrs):
|
|
"""Validate passwords match"""
|
|
if attrs['password'] != attrs['password_confirm']:
|
|
raise serializers.ValidationError({"password": "Passwords do not match."})
|
|
return attrs
|
|
|
|
def create(self, validated_data):
|
|
"""Create user with hashed password"""
|
|
validated_data.pop('password_confirm')
|
|
password = validated_data.pop('password')
|
|
user = User.objects.create(**validated_data)
|
|
user.set_password(password)
|
|
user.save()
|
|
return user
|
|
|
|
|
|
class UserUpdateSerializer(serializers.ModelSerializer):
|
|
"""User update serializer (without password)"""
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = [
|
|
'first_name', 'last_name', 'phone', 'employee_id',
|
|
'hospital', 'department', 'avatar', 'bio', 'language', 'is_active'
|
|
]
|
|
|
|
|
|
class ChangePasswordSerializer(serializers.Serializer):
|
|
"""Change password serializer"""
|
|
old_password = serializers.CharField(required=True, write_only=True)
|
|
new_password = serializers.CharField(required=True, write_only=True)
|
|
new_password_confirm = serializers.CharField(required=True, write_only=True)
|
|
|
|
def validate(self, attrs):
|
|
"""Validate passwords"""
|
|
if attrs['new_password'] != attrs['new_password_confirm']:
|
|
raise serializers.ValidationError({"new_password": "Passwords do not match."})
|
|
return attrs
|
|
|
|
def validate_old_password(self, value):
|
|
"""Validate old password"""
|
|
user = self.context['request'].user
|
|
if not user.check_password(value):
|
|
raise serializers.ValidationError("Old password is incorrect.")
|
|
return value
|
|
|
|
|
|
class RoleSerializer(serializers.ModelSerializer):
|
|
"""Role serializer"""
|
|
group_name = serializers.CharField(source='group.name', read_only=True)
|
|
|
|
class Meta:
|
|
model = Role
|
|
fields = [
|
|
'id', 'name', 'display_name', 'description',
|
|
'group', 'group_name', 'level', 'permissions',
|
|
'created_at', 'updated_at'
|
|
]
|
|
read_only_fields = ['id', 'created_at', 'updated_at']
|