tour plan uchun crud apilar qoshildi
This commit is contained in:
72
core/apps/dashboard/serializers/tour_plan.py
Normal file
72
core/apps/dashboard/serializers/tour_plan.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
# django
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
|
# rest framework
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
# shared
|
||||||
|
from core.apps.shared.models import TourPlan
|
||||||
|
# accounts
|
||||||
|
from core.apps.accounts.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class TourPlanListSerializer(serializers.ModelSerializer):
|
||||||
|
user = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = TourPlan
|
||||||
|
fields = [
|
||||||
|
'id', 'place_name', 'user', 'latitude',
|
||||||
|
'longitude', 'location_send', 'date', 'created_at'
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_user(self, obj):
|
||||||
|
return {
|
||||||
|
"id": obj.user.id,
|
||||||
|
"first_name": obj.user.first_name,
|
||||||
|
"last_name": obj.user.last_name,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class TourPlanCreateSerializer(serializers.Serializer):
|
||||||
|
place_name = serializers.CharField()
|
||||||
|
user_id = serializers.IntegerField()
|
||||||
|
latitude = serializers.FloatField()
|
||||||
|
longitude = serializers.FloatField()
|
||||||
|
date = serializers.DateField()
|
||||||
|
|
||||||
|
def validate(self, data):
|
||||||
|
user = User.objects.filter(id=data['user_id']).first()
|
||||||
|
if not user:
|
||||||
|
raise serializers.ValidationError({"user_id": "Foydalanuvchi topilmadi"})
|
||||||
|
data['user'] = user
|
||||||
|
return data
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
with transaction.atomic():
|
||||||
|
return TourPlan.objects.create(
|
||||||
|
place_name=validated_data.get('place_name'),
|
||||||
|
user=validated_data.get('user'),
|
||||||
|
longitude=validated_data.get('longitude'),
|
||||||
|
latitude=validated_data.get('latitude'),
|
||||||
|
date=validated_data.get('date'),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TourPlanUpdateSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = TourPlan
|
||||||
|
fields = [
|
||||||
|
'place_name', 'user', 'latitude',
|
||||||
|
'longitude', 'date'
|
||||||
|
]
|
||||||
|
|
||||||
|
def update(self, instance, validated_data):
|
||||||
|
with transaction.atomic():
|
||||||
|
instance.place_name = validated_data.get('place_name', instance.place_name)
|
||||||
|
instance.user = validated_data.get('user', instance.user)
|
||||||
|
instance.latitude = validated_data.get('latitude', instance.latitude)
|
||||||
|
instance.longitude = validated_data.get('longitude', instance.longitude)
|
||||||
|
instance.date = validated_data.get('date', instance.date)
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
@@ -23,6 +23,9 @@ from core.apps.dashboard.views.pharmacy import PharmacyViewSet
|
|||||||
from core.apps.dashboard.views.product import ProductViewSet
|
from core.apps.dashboard.views.product import ProductViewSet
|
||||||
# factory
|
# factory
|
||||||
from core.apps.dashboard.views.factory import FactoryViewSet
|
from core.apps.dashboard.views.factory import FactoryViewSet
|
||||||
|
# tour plan
|
||||||
|
from core.apps.dashboard.views.tour_plan import TourPlanViewSet
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# -------------- user --------------
|
# -------------- user --------------
|
||||||
@@ -64,5 +67,7 @@ router.register("place", PlaceViewSet)
|
|||||||
router.register("pharmacy", PharmacyViewSet)
|
router.register("pharmacy", PharmacyViewSet)
|
||||||
router.register("product", ProductViewSet)
|
router.register("product", ProductViewSet)
|
||||||
router.register("factory", FactoryViewSet)
|
router.register("factory", FactoryViewSet)
|
||||||
|
router.register("tour_plan", TourPlanViewSet)
|
||||||
|
|
||||||
|
|
||||||
urlpatterns += router.urls
|
urlpatterns += router.urls
|
||||||
177
core/apps/dashboard/views/tour_plan.py
Normal file
177
core/apps/dashboard/views/tour_plan.py
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
# 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 tour_plan as serializers
|
||||||
|
|
||||||
|
# shared
|
||||||
|
from core.apps.shared.models import TourPlan
|
||||||
|
from core.apps.shared.utils.response_mixin import ResponseMixin
|
||||||
|
|
||||||
|
|
||||||
|
class TourPlanViewSet(viewsets.GenericViewSet, ResponseMixin):
|
||||||
|
permission_classes = [permissions.IsAdminUser]
|
||||||
|
queryset = TourPlan.objects.all()
|
||||||
|
|
||||||
|
def get_serializer_class(self):
|
||||||
|
if self.action == "post":
|
||||||
|
return serializers.TourPlanCreateSerializer
|
||||||
|
elif self.action in ["patch", "put"]:
|
||||||
|
return serializers.TourPlanUpdateSerializer
|
||||||
|
else:
|
||||||
|
return serializers.TourPlanListSerializer
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
tags=['Admin Tour Plans'],
|
||||||
|
manual_parameters=[
|
||||||
|
openapi.Parameter(
|
||||||
|
in_=openapi.IN_QUERY,
|
||||||
|
name="name",
|
||||||
|
description="place name bo'yicha filter",
|
||||||
|
required=False,
|
||||||
|
type=openapi.TYPE_STRING,
|
||||||
|
),
|
||||||
|
openapi.Parameter(
|
||||||
|
in_=openapi.IN_QUERY,
|
||||||
|
name="date",
|
||||||
|
description="date bo'yicha filter",
|
||||||
|
required=False,
|
||||||
|
type=openapi.FORMAT_DATE,
|
||||||
|
),
|
||||||
|
openapi.Parameter(
|
||||||
|
in_=openapi.IN_QUERY,
|
||||||
|
name="user",
|
||||||
|
description="tanlangan 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)
|
||||||
|
date = request.query_params.get('date', None)
|
||||||
|
user_full_name = request.query_params.get('user', None)
|
||||||
|
|
||||||
|
queryset = self.queryset.all()
|
||||||
|
|
||||||
|
# filters
|
||||||
|
if name is not None:
|
||||||
|
queryset = queryset.filter(place_name__istartswith=name)
|
||||||
|
|
||||||
|
if date is not None:
|
||||||
|
queryset = queryset.filter(date=date)
|
||||||
|
|
||||||
|
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 Tour Plans']
|
||||||
|
)
|
||||||
|
@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.TourPlanListSerializer(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 Tour Plans']
|
||||||
|
)
|
||||||
|
@action(detail=True, methods=['patch'], url_path='update')
|
||||||
|
def update_doctor(self, request, pk=None):
|
||||||
|
try:
|
||||||
|
tour_plan = TourPlan.objects.filter(id=pk).first()
|
||||||
|
if not tour_plan:
|
||||||
|
return self.failure_response(
|
||||||
|
data={},
|
||||||
|
message="tour_plan topilmadi",
|
||||||
|
status_code=404
|
||||||
|
)
|
||||||
|
serializer = self.get_serializer(data=request.data, instance=tour_plan)
|
||||||
|
if serializer.is_valid():
|
||||||
|
obj = serializer.save()
|
||||||
|
return self.success_response(
|
||||||
|
data=serializers.TourPlanListSerializer(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 Tour Plans']
|
||||||
|
)
|
||||||
|
@action(detail=True, methods=['delete'], url_path='delete')
|
||||||
|
def delete(self, request, pk=None):
|
||||||
|
try:
|
||||||
|
tour_plan = TourPlan.objects.filter(id=pk).first()
|
||||||
|
if not tour_plan:
|
||||||
|
return self.failure_response(
|
||||||
|
data={},
|
||||||
|
message="tour_plan topilmadi",
|
||||||
|
status_code=404
|
||||||
|
)
|
||||||
|
tour_plan.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