dorixona uchun crud qoshildi
This commit is contained in:
130
core/apps/dashboard/serializers/pharmacy.py
Normal file
130
core/apps/dashboard/serializers/pharmacy.py
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
# django
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
|
# rest framework
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
# shared
|
||||||
|
from core.apps.shared.models import Pharmacy, District, Place
|
||||||
|
# accounts
|
||||||
|
from core.apps.accounts.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class PharmacyListSerializer(serializers.ModelSerializer):
|
||||||
|
district = serializers.SerializerMethodField(method_name='get_district')
|
||||||
|
place = serializers.SerializerMethodField(method_name='get_place')
|
||||||
|
user = serializers.SerializerMethodField(method_name='get_user')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Pharmacy
|
||||||
|
fields = [
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
'inn',
|
||||||
|
'owner_phone',
|
||||||
|
'responsible_phone',
|
||||||
|
'district',
|
||||||
|
'place',
|
||||||
|
'user',
|
||||||
|
'longitude',
|
||||||
|
'latitude',
|
||||||
|
'extra_location',
|
||||||
|
'created_at'
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_district(self, obj):
|
||||||
|
return {
|
||||||
|
'id': obj.district.id,
|
||||||
|
'name': obj.district.name
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_place(self, obj):
|
||||||
|
return {
|
||||||
|
'id': obj.place.id,
|
||||||
|
'name': obj.place.name
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_user(self, obj):
|
||||||
|
return {
|
||||||
|
'id': obj.user.id,
|
||||||
|
'first_name': obj.user.first_name,
|
||||||
|
'last_name': obj.user.last_name,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class AdminPharmacyCreateSerializer(serializers.Serializer):
|
||||||
|
name = serializers.CharField()
|
||||||
|
inn = serializers.CharField()
|
||||||
|
owner_phone = serializers.CharField()
|
||||||
|
responsible_phone = serializers.CharField()
|
||||||
|
district_id = serializers.IntegerField()
|
||||||
|
place_id = serializers.IntegerField()
|
||||||
|
user_id = serializers.IntegerField()
|
||||||
|
longitude = serializers.FloatField()
|
||||||
|
latitude = serializers.FloatField()
|
||||||
|
extra_location = serializers.JSONField()
|
||||||
|
|
||||||
|
def validate(self, data):
|
||||||
|
district = District.objects.filter(id=data['district_id']).first()
|
||||||
|
if not district:
|
||||||
|
raise serializers.ValidationError({"district_id": "Tuman topilmadi"})
|
||||||
|
|
||||||
|
place = Place.objects.filter(id=data['place_id']).first()
|
||||||
|
if not place:
|
||||||
|
raise serializers.ValidationError({'place_id': "Obyekt topilmadi"})
|
||||||
|
|
||||||
|
user = User.objects.filter(id=data['user_id']).first()
|
||||||
|
if not user:
|
||||||
|
raise serializers.ValidationError({"user_id": "Foydalanuvchi topilmadi"})
|
||||||
|
data['district'] = district
|
||||||
|
|
||||||
|
data['place'] = place
|
||||||
|
data['user'] = user
|
||||||
|
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'),
|
||||||
|
responsible_phone=validated_data.get('responsible_phone'),
|
||||||
|
district=validated_data.get('district'),
|
||||||
|
user=validated_data.get('user'),
|
||||||
|
place=validated_data.get('place'),
|
||||||
|
longitude=validated_data.get('longitude'),
|
||||||
|
latitude=validated_data.get('latitude'),
|
||||||
|
extra_location=validated_data.get('extra_location'),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class PharmacyUpdateSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Pharmacy
|
||||||
|
fields = [
|
||||||
|
'name',
|
||||||
|
'inn',
|
||||||
|
'owner_phone',
|
||||||
|
'responsible_phone',
|
||||||
|
'district',
|
||||||
|
'place',
|
||||||
|
'user',
|
||||||
|
'longitude',
|
||||||
|
'latitude',
|
||||||
|
'extra_location',
|
||||||
|
]
|
||||||
|
|
||||||
|
def update(self, instance, validated_data):
|
||||||
|
with transaction.atomic():
|
||||||
|
instance.name = validated_data.get('name', instance.name)
|
||||||
|
instance.inn = validated_data.get('inn', instance.inn)
|
||||||
|
instance.owner_phone = validated_data.get('owner_phone', instance.owner_phone)
|
||||||
|
instance.responsible_phone = validated_data.get('responsible_phone', instance.responsible_phone)
|
||||||
|
instance.user = validated_data.get('user', instance.user)
|
||||||
|
instance.district = validated_data.get('district', instance.district)
|
||||||
|
instance.place = validated_data.get('place', instance.place)
|
||||||
|
instance.longitude = validated_data.get('longitude', instance.longitude)
|
||||||
|
instance.latitude = validated_data.get('latitude', instance.latitude)
|
||||||
|
instance.extra_location = validated_data.get('extra_location', instance.extra_location)
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
@@ -17,6 +17,8 @@ from core.apps.dashboard.views import region as region_views
|
|||||||
from core.apps.dashboard.views.plan import PlanViewSet
|
from core.apps.dashboard.views.plan import PlanViewSet
|
||||||
# place
|
# place
|
||||||
from core.apps.dashboard.views.place import PlaceViewSet
|
from core.apps.dashboard.views.place import PlaceViewSet
|
||||||
|
# pharmacy
|
||||||
|
from core.apps.dashboard.views.pharmacy import PharmacyViewSet
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# -------------- user --------------
|
# -------------- user --------------
|
||||||
@@ -55,5 +57,6 @@ router = DefaultRouter()
|
|||||||
router.register("doctor", DoctorViewSet)
|
router.register("doctor", DoctorViewSet)
|
||||||
router.register("plan", PlanViewSet)
|
router.register("plan", PlanViewSet)
|
||||||
router.register("place", PlaceViewSet)
|
router.register("place", PlaceViewSet)
|
||||||
|
router.register("pharmacy", PharmacyViewSet)
|
||||||
|
|
||||||
urlpatterns += router.urls
|
urlpatterns += router.urls
|
||||||
188
core/apps/dashboard/views/pharmacy.py
Normal file
188
core/apps/dashboard/views/pharmacy.py
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
# django
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
|
# rest framework
|
||||||
|
from rest_framework import viewsets, permissions
|
||||||
|
from rest_framework.decorators import action
|
||||||
|
|
||||||
|
# drf yasg
|
||||||
|
from drf_yasg import openapi
|
||||||
|
from drf_yasg.utils import swagger_auto_schema
|
||||||
|
|
||||||
|
# dashboard
|
||||||
|
from core.apps.dashboard.serializers import pharmacy as serializers
|
||||||
|
|
||||||
|
# shared
|
||||||
|
from core.apps.shared.models import Pharmacy
|
||||||
|
from core.apps.shared.utils.response_mixin import ResponseMixin
|
||||||
|
|
||||||
|
|
||||||
|
class PharmacyViewSet(viewsets.GenericViewSet, ResponseMixin):
|
||||||
|
permission_classes = [permissions.IsAdminUser]
|
||||||
|
queryset = Pharmacy.objects.all()
|
||||||
|
|
||||||
|
def get_serializer_class(self):
|
||||||
|
if self.action == "post":
|
||||||
|
return serializers.AdminPharmacyCreateSerializer
|
||||||
|
elif self.action in ["patch", "put"]:
|
||||||
|
return serializers.PharmacyUpdateSerializer
|
||||||
|
else:
|
||||||
|
return serializers.PharmacyListSerializer
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
tags=['Admin Pharmacies'],
|
||||||
|
manual_parameters=[
|
||||||
|
openapi.Parameter(
|
||||||
|
in_=openapi.IN_QUERY,
|
||||||
|
name="name",
|
||||||
|
description="name bo'yicha search",
|
||||||
|
required=False,
|
||||||
|
type=openapi.TYPE_STRING,
|
||||||
|
),
|
||||||
|
openapi.Parameter(
|
||||||
|
in_=openapi.IN_QUERY,
|
||||||
|
name="place",
|
||||||
|
description="obyekt name bo'yicha search",
|
||||||
|
required=False,
|
||||||
|
type=openapi.TYPE_STRING,
|
||||||
|
),
|
||||||
|
openapi.Parameter(
|
||||||
|
in_=openapi.IN_QUERY,
|
||||||
|
name="district",
|
||||||
|
description="tuman name bo'yicha search",
|
||||||
|
required=False,
|
||||||
|
type=openapi.TYPE_STRING,
|
||||||
|
),
|
||||||
|
openapi.Parameter(
|
||||||
|
in_=openapi.IN_QUERY,
|
||||||
|
name="user",
|
||||||
|
description="qo'shgan foydalanuvchini ism va familiyasi bo'yicha qidirish",
|
||||||
|
required=False,
|
||||||
|
type=openapi.TYPE_STRING,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@action(detail=False, methods=['get'], url_path="list")
|
||||||
|
def get(self, request):
|
||||||
|
try:
|
||||||
|
# params
|
||||||
|
name = request.query_params.get('name', None)
|
||||||
|
place_name = request.query_params.get('place', None)
|
||||||
|
district_name = request.query_params.get('district', None)
|
||||||
|
user_full_name = request.query_params.get('user', None)
|
||||||
|
|
||||||
|
queryset = self.queryset.all()
|
||||||
|
|
||||||
|
# filters
|
||||||
|
if name is not None:
|
||||||
|
queryset = queryset.filter(name__istartswith=name)
|
||||||
|
|
||||||
|
if district_name is not None:
|
||||||
|
queryset = queryset.filter(district__name__istartswith=district_name)
|
||||||
|
|
||||||
|
if place_name is not None:
|
||||||
|
queryset = queryset.filter(place__name__istartswith=place_name)
|
||||||
|
|
||||||
|
if user_full_name is not None:
|
||||||
|
queryset = queryset.filter(
|
||||||
|
Q(user__first_name__istartswith=user_full_name) |
|
||||||
|
Q(user__last_name__istartswith=user_full_name)
|
||||||
|
)
|
||||||
|
|
||||||
|
page = self.paginate_queryset(queryset)
|
||||||
|
if page is not None:
|
||||||
|
serializer = self.get_serializer(page, many=True)
|
||||||
|
return self.success_response(
|
||||||
|
data=self.get_paginated_response(serializer.data).data,
|
||||||
|
message='malumotlar fetch qilindi'
|
||||||
|
)
|
||||||
|
serializer = self.get_serializer(queryset, many=True)
|
||||||
|
return self.success_response(
|
||||||
|
data=serializer.data,
|
||||||
|
message='malumotlar fetch qilindi'
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return self.error_response(
|
||||||
|
data=str(e),
|
||||||
|
message="xatolik"
|
||||||
|
)
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
tags=['Admin Pharmacies']
|
||||||
|
)
|
||||||
|
@action(detail=False, methods=['post'], url_path='create')
|
||||||
|
def post(self, request):
|
||||||
|
try:
|
||||||
|
serializer = self.get_serializer(data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
obj = serializer.save()
|
||||||
|
return self.success_response(
|
||||||
|
data=serializers.PharmacyListSerializer(obj).data,
|
||||||
|
message='malumot qoshildi'
|
||||||
|
)
|
||||||
|
return self.failure_response(
|
||||||
|
data=serializer.errors,
|
||||||
|
message='malumot qoshilmadi'
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return self.error_response(
|
||||||
|
data=str(e),
|
||||||
|
message="xatolik"
|
||||||
|
)
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
tags=['Admin Pharmacies']
|
||||||
|
)
|
||||||
|
@action(detail=True, methods=['patch'], url_path='update')
|
||||||
|
def update_pharmacy(self, request, pk=None):
|
||||||
|
try:
|
||||||
|
pharmacy = Pharmacy.objects.filter(id=pk).first()
|
||||||
|
if not pharmacy:
|
||||||
|
return self.failure_response(
|
||||||
|
data={},
|
||||||
|
message="pharmacy topilmadi",
|
||||||
|
status_code=404
|
||||||
|
)
|
||||||
|
serializer = self.get_serializer(data=request.data, instance=pharmacy)
|
||||||
|
if serializer.is_valid():
|
||||||
|
obj = serializer.save()
|
||||||
|
return self.success_response(
|
||||||
|
data=serializers.PharmacyListSerializer(obj).data,
|
||||||
|
message='malumot tahrirlandi'
|
||||||
|
)
|
||||||
|
return self.failure_response(
|
||||||
|
data=serializer.errors,
|
||||||
|
message='malumot tahrirlandi'
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return self.error_response(
|
||||||
|
data=str(e),
|
||||||
|
message="xatolik"
|
||||||
|
)
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
tags=['Admin Pharmacies']
|
||||||
|
)
|
||||||
|
@action(detail=True, methods=['delete'], url_path='delete')
|
||||||
|
def delete(self, request, pk=None):
|
||||||
|
try:
|
||||||
|
pharmacy = Pharmacy.objects.filter(id=pk).first()
|
||||||
|
if not pharmacy:
|
||||||
|
return self.failure_response(
|
||||||
|
data={},
|
||||||
|
message="pharmacy topilmadi",
|
||||||
|
status_code=404
|
||||||
|
)
|
||||||
|
pharmacy.delete()
|
||||||
|
return self.success_response(
|
||||||
|
data={},
|
||||||
|
message='malumot ochirildi',
|
||||||
|
status_code=204
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return self.error_response(
|
||||||
|
data=str(e),
|
||||||
|
message="xatolik"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user