first base of project-changed apps: Herd-livestock-tag-log-elasticsearch-
This commit is contained in:
@@ -1,11 +1,179 @@
|
||||
from apps.authorization.api.v1.serializers import UserRelationSerializer
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import serializers
|
||||
from apps.authentication.models import User
|
||||
from apps.authentication.models import (
|
||||
User,
|
||||
City,
|
||||
Province,
|
||||
Organization,
|
||||
OrganizationType,
|
||||
BankAccountInformation
|
||||
)
|
||||
from apps.authorization import models as authorize_models
|
||||
import typing
|
||||
|
||||
|
||||
class CitySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = City
|
||||
fields = [
|
||||
'id',
|
||||
'name'
|
||||
]
|
||||
|
||||
|
||||
class ProvinceSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Province
|
||||
fields = [
|
||||
'id',
|
||||
'name'
|
||||
]
|
||||
|
||||
|
||||
class BankAccountSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = BankAccountInformation
|
||||
fields = [
|
||||
'id',
|
||||
'user',
|
||||
'account',
|
||||
'name',
|
||||
'card',
|
||||
'sheba'
|
||||
]
|
||||
extra_kwargs = {
|
||||
'user': {'required': False},
|
||||
'account': {'required': False},
|
||||
'card': {'required': False},
|
||||
'sheba': {'required': False}
|
||||
}
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" update user bank account information """
|
||||
instance.name = validated_data.get('name', instance.name)
|
||||
instance.account = validated_data.get('account', instance.account)
|
||||
instance.card = validated_data.get('card', instance.card)
|
||||
instance.sheba = validated_data.get('sheba', instance.sheba)
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = [
|
||||
'id',
|
||||
'username',
|
||||
'mobile'
|
||||
'password',
|
||||
'first_name',
|
||||
'last_name',
|
||||
'is_active',
|
||||
'mobile',
|
||||
'phone',
|
||||
'national_code',
|
||||
'birthdate',
|
||||
'nationality',
|
||||
'ownership',
|
||||
'address',
|
||||
'photo',
|
||||
'province',
|
||||
'city',
|
||||
'otp_status',
|
||||
]
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" update user instance """
|
||||
instance.username = validated_data.get('username', instance.username)
|
||||
instance.password = validated_data.get('password', instance.password)
|
||||
instance.first_name = validated_data.get('first_name')
|
||||
instance.last_name = validated_data.get('last_name')
|
||||
instance.is_active = validated_data.get('is_active')
|
||||
instance.mobile = validated_data.get('mobile')
|
||||
instance.phone = validated_data.get('phone')
|
||||
instance.national_code = validated_data.get('national_code')
|
||||
instance.birthdate = validated_data.get('birthdate')
|
||||
instance.nationality = validated_data.get('nationality')
|
||||
instance.ownership = validated_data.get('ownership')
|
||||
instance.address = validated_data.get('address')
|
||||
instance.photo = validated_data.get('photo')
|
||||
instance.province = Province.objects.get(id=validated_data.get('province'))
|
||||
instance.city = City.objects.get(id=validated_data.get('city'))
|
||||
instance.otp_status = validated_data.get('otp_status')
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
@staticmethod
|
||||
def update_relations(user: object, relation_data: dict, bank_data: dict) -> typing.Any:
|
||||
"""
|
||||
update user relations & bank account for user
|
||||
"""
|
||||
user_relation = UserRelationSerializer(data=relation_data) # Create user relation
|
||||
if user_relation.is_valid(raise_exception=True):
|
||||
user_relation_obj = user_relation.update(
|
||||
authorize_models.UserRelations.objects.get(user=user),
|
||||
validated_data=relation_data
|
||||
)
|
||||
|
||||
bank_info = BankAccountSerializer(data=bank_data) # Create user bank information
|
||||
if bank_info.is_valid(raise_exception=True):
|
||||
bank_obj = bank_info.update(
|
||||
BankAccountInformation.objects.get(id=bank_data['id']),
|
||||
validated_data=bank_data
|
||||
)
|
||||
|
||||
return user_relation_obj, bank_obj # noqa
|
||||
|
||||
|
||||
class OrganizationTypeSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = OrganizationType
|
||||
fields = [
|
||||
'id',
|
||||
'key',
|
||||
'name',
|
||||
]
|
||||
|
||||
|
||||
class OrganizationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Organization
|
||||
fields = [
|
||||
'id',
|
||||
'name',
|
||||
'type',
|
||||
'province',
|
||||
'city',
|
||||
'parent_organization',
|
||||
'national_unique_id'
|
||||
]
|
||||
extra_kwargs = {}
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, Organization):
|
||||
representation['province'] = ProvinceSerializer(instance.province).data
|
||||
representation['city'] = CitySerializer(instance.city).data
|
||||
representation['type'] = OrganizationTypeSerializer(instance.type).data
|
||||
if instance.parent_organization:
|
||||
representation['parent_organization'] = OrganizationSerializer(instance.parent_organization).data
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" update user organization information """ # noqa
|
||||
instance.name = validated_data.get('name', instance.name)
|
||||
if validated_data.get('type'):
|
||||
instance.type = OrganizationType.objects.get(id=validated_data.get('type', instance.type))
|
||||
if validated_data.get('province'):
|
||||
instance.province = Province.objects.get(id=validated_data.get('province', instance.province))
|
||||
if validated_data.get('city'):
|
||||
instance.city = City.objects.get(id=validated_data.get('city', instance.city))
|
||||
if validated_data.get('parent_organization'):
|
||||
instance.parent_organization = Organization.objects.get(
|
||||
id=validated_data.get('parent_organization', instance.parent_organization)
|
||||
)
|
||||
instance.national_unique_id = validated_data.get('national_unique_id', instance.national_unique_id)
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
Reference in New Issue
Block a user