diff --git a/core/apps/dashboard/serializers/district.py b/core/apps/dashboard/serializers/district.py index 0136fab..19ab558 100644 --- a/core/apps/dashboard/serializers/district.py +++ b/core/apps/dashboard/serializers/district.py @@ -24,7 +24,6 @@ class DistrictListSerializer(serializers.ModelSerializer): "id": obj.user.id, "first_name": obj.user.first_name, "last_name": obj.user.last_name, - "telegram_id": obj.user.telegram_id, } diff --git a/core/apps/dashboard/urls.py b/core/apps/dashboard/urls.py index 096492a..30f5779 100644 --- a/core/apps/dashboard/urls.py +++ b/core/apps/dashboard/urls.py @@ -3,9 +3,12 @@ from django.urls import path, include ### dashboard ### # users from core.apps.dashboard.views import user as user_views +# district +from core.apps.dashboard.views import district as district_views urlpatterns = [ + # -------------- user -------------- path('user/', include( [ path('list/', user_views.UserListApiView.as_view(), name='user-list-api'), @@ -14,4 +17,14 @@ urlpatterns = [ path('/update/', user_views.UserUpdateApiView.as_view(), name='user-update-api'), ], )), + # -------------- district -------------- + path('district/', include( + [ + path('list/', district_views.DistrictListApiView.as_view(), name='district-list-api'), + path('create/', district_views.DistrictCreateApiView.as_view(), name='district-create-api'), + path('/update/', district_views.DistrictUpdateApiView.as_view(), name='district-update-api'), + path('/delete/', district_views.DistrictDeleteApiView.as_view(), name='district-delete-api'), + ] + )), + ] \ No newline at end of file diff --git a/core/apps/dashboard/views/district.py b/core/apps/dashboard/views/district.py new file mode 100644 index 0000000..9e525bf --- /dev/null +++ b/core/apps/dashboard/views/district.py @@ -0,0 +1,340 @@ +# django +from django.shortcuts import get_object_or_404 + +# rest framework +from rest_framework import generics, views +from rest_framework.permissions import IsAdminUser + +# drf yasg +from drf_yasg import openapi +from drf_yasg.utils import swagger_auto_schema + +# shared +from core.apps.shared.models import District +from core.apps.shared.utils.response_mixin import ResponseMixin + +# dashboard +from core.apps.dashboard.serializers import district as serializers + + +class DistrictListApiView(generics.GenericAPIView, ResponseMixin): + serializer_class = serializers.DistrictListSerializer + queryset = District.objects.all() + permission_classes = [IsAdminUser] + + @swagger_auto_schema( + tags=["Admin Districts"], + responses={ + 200: openapi.Response( + description="Success", + schema=None, + examples={ + "application/json": { + "status_code": 200, + "status": "success", + "message": "malumotlar fetch qilindi", + "data": { + "count": 0, + "next": "string", + "previous": "string", + "results": [ + { + "id": 0, + "name": "string", + "user": { + "id": 0, + "first_name": "string", + "last_name": "string", + }, + "created_at": "string" + } + ] + } + }, + "application/json": { + "status_code": 200, + "status": "success", + "message": "malumotlar fetch qilindi", + "data": [ + { + "id": 0, + "name": "string", + "user": { + "id": 0, + "first_name": "string", + "last_name": "string", + }, + "created_at": "string" + }, + ] + } + } + ), + 500: openapi.Response( + schema=None, + description="Server Error", + examples={ + "application/json": { + "status_code": 500, + "status": "error", + "message": "xatolik", + "data": "string" + } + } + ), + } + ) + def get(self, request): + try: + query = self.queryset.all() + + page = self.paginate_queryset(queryset=query) + if page is not None: + serializer = self.serializer_class(page, many=True) + return self.success_response( + data=self.get_paginated_response(serializer.data).data, + message="malumotlar fetch qilindi", + ) + serializer = self.serializer_class(query, 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' + ) + + +class DistrictCreateApiView(generics.GenericAPIView, ResponseMixin): + serializer_class = serializers.DistrictCreateSerializer + queryset = District.objects.all() + permission_classes = [IsAdminUser] + + @swagger_auto_schema( + tags=["Admin Districts"], + responses={ + 200: openapi.Response( + description="Success", + schema=None, + examples={ + "application/json": { + "status_code": 200, + "status": "success", + "message": "malumotlar qoshildi", + "data": { + "id": 0, + "name": "string", + "user": { + "id": 0, + "first_name": "string", + "last_name": "string", + }, + "created_at": "string" + } + }, + } + ), + 400: openapi.Response( + description="Failure", + schema=None, + examples={ + "application/json": { + "status_code": 400, + "status": "failure", + "message": "malumotlar qoshilmadi", + "data": "string" + }, + } + ), + 500: openapi.Response( + schema=None, + description="Server Error", + examples={ + "application/json": { + "status_code": 500, + "status": "error", + "message": "xatolik", + "data": "string" + } + } + ), + } + ) + def post(self, request): + try: + serializer = self.serializer_class(data=request.data) + if serializer.is_valid(): + obj = serializer.save() + return self.success_response( + data=serializers.DistrictListSerializer(obj).data, + message="malumot qoshildi", + status_code=201, + ) + else: + return self.failure_response( + data=serializer.errors, + message="malumot qoshilmadi", + ) + except Exception as e: + return self.error_response( + data=str(e), + message="xatolik" + ) + + +class DistrictUpdateApiView(generics.GenericAPIView, ResponseMixin): + serializer_class = serializers.DistrictUpdateSerializer + queryset = District.objects.all() + permission_classes = [IsAdminUser] + + @swagger_auto_schema( + tags=["Admin Districts"], + responses={ + 200: openapi.Response( + description="Success", + schema=None, + examples={ + "application/json": { + "status_code": 200, + "status": "success", + "message": "malumotlar tahrirlandi", + "data": { + "id": 0, + "name": "string", + "user": { + "id": 0, + "first_name": "string", + "last_name": "string", + }, + "created_at": "string" + } + }, + } + ), + 404: openapi.Response( + description="Failure", + schema=None, + examples={ + "application/json": { + "status_code": 404, + "status": "failure", + "message": "malumotlar topilmadi", + "data": {} + }, + } + ), + 400: openapi.Response( + description="Failure", + schema=None, + examples={ + "application/json": { + "status_code": 400, + "status": "failure", + "message": "malumotlar tahrirlanmadi", + "data": "string" + }, + } + ), + 500: openapi.Response( + schema=None, + description="Server Error", + examples={ + "application/json": { + "status_code": 500, + "status": "error", + "message": "xatolik", + "data": "string" + } + } + ), + } + ) + def patch(self, request, id): + try: + obj = District.objects.filter(id=id).first() + if not obj: + return self.failure_response( + data={}, message="malumot topilmadi", status_code=404 + ) + serializer = self.serializer_class(data=request.data, instance=obj) + if serializer.is_valid(): + updated_obj = serializer.save() + return self.success_response( + data=serializers.DistrictListSerializer(updated_obj).data, + message="malumot tahrirlandi", + ) + return self.failure_response( + data=serializer.errors, + message="malumot tahrirlanmadi" + ) + except Exception as e: + return self.error_response( + data=str(e), + message="xatolik" + ) + + +class DistrictDeleteApiView(views.APIView, ResponseMixin): + permission_classes = [IsAdminUser] + + @swagger_auto_schema( + tags=["Admin Districts"], + responses={ + 204: openapi.Response( + description="Success", + schema=None, + examples={ + "application/json": { + "status_code": 200, + "status": "success", + "message": "malumotlar ochirildi", + "data": {} + }, + } + ), + 404: openapi.Response( + description="Failure", + schema=None, + examples={ + "application/json": { + "status_code": 404, + "status": "failure", + "message": "malumotlar topilmadi", + "data": {} + }, + } + ), + 500: openapi.Response( + schema=None, + description="Server Error", + examples={ + "application/json": { + "status_code": 500, + "status": "error", + "message": "xatolik", + "data": "string" + } + } + ), + } + ) + def delete(self, request, id): + try: + obj = District.objects.filter(id=id).first() + if not obj: + return self.failure_response( + data={}, message="malumot topilmadi", status_code=404 + ) + obj.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' + ) diff --git a/core/apps/shared/models/tour_plan.py b/core/apps/shared/models/tour_plan.py index c3704e8..e6f5f8e 100644 --- a/core/apps/shared/models/tour_plan.py +++ b/core/apps/shared/models/tour_plan.py @@ -18,6 +18,6 @@ class TourPlan(BaseModel): return f"{self.user.first_name}'s tour plan to {self.place_name}" def save(self, *args, **kwargs): - if self.longitude and self.latitude: + if self.longitude is not None and self.latitude is not None: self.location_send = True return super().save(*args, **kwargs) \ No newline at end of file diff --git a/core/apps/shared/serializers/tour_plan.py b/core/apps/shared/serializers/tour_plan.py index c284f6d..ed3161f 100644 --- a/core/apps/shared/serializers/tour_plan.py +++ b/core/apps/shared/serializers/tour_plan.py @@ -10,4 +10,18 @@ class TourPlanSerializer(serializers.ModelSerializer): model = TourPlan fields = [ 'id', 'place_name', 'longitude', 'latitude', 'location_send', 'created_at' - ] \ No newline at end of file + ] + + +class TourPlanUpdateSerializer(serializers.ModelSerializer): + class Meta: + model = TourPlan + fields = [ + 'longitude', 'latitude' + ] + + def update(self, instance, validated_data): + instance.longitude = validated_data.get('longitude', instance.longitude) + instance.latitude = validated_data.get('latitude', instance.latitude) + instance.save() + return instance \ No newline at end of file diff --git a/core/apps/shared/urls.py b/core/apps/shared/urls.py index 61f236c..903ea06 100644 --- a/core/apps/shared/urls.py +++ b/core/apps/shared/urls.py @@ -75,6 +75,7 @@ urlpatterns = [ path('tour_plan/', include( [ path('list/', tp_view.TourPlanListApiView.as_view(), name='tour-plan-list-api'), + path('/update/', tp_view.TourPlanUpdateApiView.as_view()), ] )), path('factory/', include( diff --git a/core/apps/shared/views/tour_plan.py b/core/apps/shared/views/tour_plan.py index 456f6c7..c576e82 100644 --- a/core/apps/shared/views/tour_plan.py +++ b/core/apps/shared/views/tour_plan.py @@ -1,3 +1,6 @@ +# django +from django.shortcuts import get_object_or_404 + # rest framework from rest_framework import generics, permissions @@ -6,7 +9,7 @@ from drf_yasg.utils import swagger_auto_schema # shared from core.apps.shared.models import TourPlan -from core.apps.shared.serializers.tour_plan import TourPlanSerializer +from core.apps.shared.serializers.tour_plan import TourPlanSerializer, TourPlanUpdateSerializer from core.apps.shared.serializers.base import BaseResponseSerializer, SuccessResponseSerializer from core.apps.shared.utils.response_mixin import ResponseMixin @@ -30,4 +33,21 @@ class TourPlanListApiView(generics.GenericAPIView, ResponseMixin): serializer = self.serializer_class(queryset, many=True) return self.success_response(data=serializer.data, message='malumot fetch qilindi') except Exception as e: - return self.error_response(data=str(e), message='xatolik') \ No newline at end of file + return self.error_response(data=str(e), message='xatolik') + + +class TourPlanUpdateApiView(generics.GenericAPIView, ResponseMixin): + serializer_class = TourPlanUpdateSerializer + queryset = TourPlan.objects.all() + permission_classes = [permissions.IsAuthenticated] + + def patch(self, request, id): + obj = get_object_or_404(TourPlan, id=id, user=request.user) + serializer = self.serializer_class(data=request.data, instance=obj) + if serializer.is_valid(): + serializer.save() + return self.success_response( + data={}, + message="tahrirlandi" + ) + return self.failure_response(data=serializer.errors, message='xato') \ No newline at end of file