2021-11-08 17:27:57 +08:00
|
|
|
from django.contrib.auth.hashers import make_password
|
2021-11-04 23:49:56 +08:00
|
|
|
from extensions.serializers import *
|
|
|
|
from extensions.exceptions import *
|
2021-11-08 17:27:57 +08:00
|
|
|
from apps.system.models import *
|
2021-11-04 23:49:56 +08:00
|
|
|
|
|
|
|
|
2021-11-08 17:27:57 +08:00
|
|
|
class PermissionSerializer(BaseSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Permission
|
|
|
|
fields = ['id', 'name', 'code']
|
|
|
|
|
|
|
|
|
|
|
|
class PermissionTypeSerializer(BaseSerializer):
|
|
|
|
permission_items = PermissionSerializer(source='permissions', many=True, label='权限')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PermissionType
|
|
|
|
fields = ['id', 'name', 'permission_items']
|
|
|
|
|
|
|
|
|
|
|
|
class RoleSerializer(BaseSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Role
|
|
|
|
read_only_fields = ['id']
|
|
|
|
fields = ['name', 'remark', 'permissions', *read_only_fields]
|
2021-11-04 23:49:56 +08:00
|
|
|
|
2021-11-08 17:27:57 +08:00
|
|
|
|
|
|
|
class UserSerializer(BaseSerializer):
|
2021-11-11 00:23:25 +08:00
|
|
|
sex_display = CharField(source='get_sex_display', read_only=True, label='性别')
|
2021-11-08 17:27:57 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2021-11-11 00:23:25 +08:00
|
|
|
read_only_fields = ['id', 'sex_display', 'is_manager', 'create_time']
|
2021-11-08 17:27:57 +08:00
|
|
|
fields = ['username', 'name', 'phone', 'email', 'sex', 'roles', 'is_active', *read_only_fields]
|
|
|
|
|
|
|
|
def validate_username(self, value):
|
|
|
|
self.validate_unique({'username': value}, message=f'用户名[{value}]已存在')
|
|
|
|
return value
|
|
|
|
|
|
|
|
def validate_roles(self, instances):
|
|
|
|
instances = self.validate_foreign_key_set(Role, instances)
|
|
|
|
return instances
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
validated_data['password'] = make_password(self.team.number)
|
|
|
|
return super().create(validated_data)
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'PermissionSerializer', 'PermissionTypeSerializer', 'RoleSerializer', 'UserSerializer',
|
2021-11-04 23:49:56 +08:00
|
|
|
]
|