pharmacy serializer qoshildi va corsheaders.pyda yangi ozgarishlash
This commit is contained in:
@@ -9,4 +9,11 @@ CORS_ALLOW_CREDENTIALS = True
|
||||
|
||||
CSRF_TRUSTED_ORIGINS = [
|
||||
'https://api.meridynpharma.com/',
|
||||
]
|
||||
|
||||
CORS_ALLOW_METHODS = [
|
||||
"DELETE",
|
||||
"GET",
|
||||
"POST",
|
||||
"PUT",
|
||||
]
|
||||
71
core/apps/shared/serializers/pharmacy.py
Normal file
71
core/apps/shared/serializers/pharmacy.py
Normal file
@@ -0,0 +1,71 @@
|
||||
# django
|
||||
from django.db import transaction
|
||||
|
||||
# serializers
|
||||
from rest_framework import serializers
|
||||
|
||||
# shared
|
||||
from core.apps.shared.models import Pharmacy, District, Place
|
||||
|
||||
|
||||
|
||||
class PharmacySerializer(serializers.ModelSerializer):
|
||||
district = serializers.SerializerMethodField()
|
||||
place = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Pharmacy
|
||||
fields = [
|
||||
'id', 'name', 'inn', 'owner_phone', 'responsible_phone',
|
||||
'district', 'place', 'longitude', 'latitude', 'extra_location', 'created_at'
|
||||
]
|
||||
|
||||
def get_district(self, obj):
|
||||
return {
|
||||
'id': obj.distrcit.id,
|
||||
'name': obj.distrcit.name,
|
||||
}
|
||||
|
||||
def get_place(self, obj):
|
||||
return {
|
||||
'id': obj.place.id,
|
||||
'name': obj.place.name,
|
||||
}
|
||||
|
||||
|
||||
class PharmacyCreateSerializer(serializers.Serializer):
|
||||
name = serializers.CharField()
|
||||
inn = serializers.CharField()
|
||||
owner_phone = serializers.CharField()
|
||||
responsible_phone = serializers.CharField()
|
||||
district_id = serializers.IntegerField()
|
||||
place_id = serializers.IntegerField()
|
||||
longitude = serializers.FloatField()
|
||||
latitude = serializers.FloatField()
|
||||
extra_location = serializers.FloatField()
|
||||
|
||||
def validate(self, data):
|
||||
district = District.objects.filter(id=data['district_id']).first()
|
||||
if not district:
|
||||
raise serializers.ValidationError({"district_id": "District topilmadi"})
|
||||
place = Place.objects.filter(id=data['place_id']).first()
|
||||
if not place:
|
||||
raise serializers.ValidationError({'place_id': "Place topilmadi"})
|
||||
|
||||
data['distirct'] = district
|
||||
data['place'] = place
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
with transaction.atomic():
|
||||
return Pharmacy.objects.create(
|
||||
name=validated_data.get('name'),
|
||||
inn=validated_data.get('inn'),
|
||||
owner_phone=validated_data.get('owner_phone'),
|
||||
responsibele_phone=validated_data.get('responsible_phone'),
|
||||
district=validated_data.get('district'),
|
||||
place=validated_data.get('place'),
|
||||
longitude=validated_data.get('longitude'),
|
||||
latitude=validated_data.get('latitude'),
|
||||
extra_location=validated_data.get('extra_location'),
|
||||
)
|
||||
8
core/apps/shared/views/pharmacy.py
Normal file
8
core/apps/shared/views/pharmacy.py
Normal file
@@ -0,0 +1,8 @@
|
||||
# rest framework
|
||||
from rest_framework import generics, permissions
|
||||
|
||||
# drf yasg
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
|
||||
# shared
|
||||
from core.apps.shared.models import Pharmacy
|
||||
Reference in New Issue
Block a user