fix
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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('<int:id>/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('<int:id>/update/', district_views.DistrictUpdateApiView.as_view(), name='district-update-api'),
|
||||
path('<int:id>/delete/', district_views.DistrictDeleteApiView.as_view(), name='district-delete-api'),
|
||||
]
|
||||
)),
|
||||
|
||||
]
|
||||
340
core/apps/dashboard/views/district.py
Normal file
340
core/apps/dashboard/views/district.py
Normal file
@@ -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'
|
||||
)
|
||||
@@ -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)
|
||||
@@ -11,3 +11,17 @@ class TourPlanSerializer(serializers.ModelSerializer):
|
||||
fields = [
|
||||
'id', 'place_name', 'longitude', 'latitude', 'location_send', 'created_at'
|
||||
]
|
||||
|
||||
|
||||
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
|
||||
@@ -75,6 +75,7 @@ urlpatterns = [
|
||||
path('tour_plan/', include(
|
||||
[
|
||||
path('list/', tp_view.TourPlanListApiView.as_view(), name='tour-plan-list-api'),
|
||||
path('<int:id>/update/', tp_view.TourPlanUpdateApiView.as_view()),
|
||||
]
|
||||
)),
|
||||
path('factory/', include(
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -31,3 +34,20 @@ class TourPlanListApiView(generics.GenericAPIView, ResponseMixin):
|
||||
return self.success_response(data=serializer.data, message='malumot fetch qilindi')
|
||||
except Exception as e:
|
||||
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')
|
||||
Reference in New Issue
Block a user