plan ochirish va tahrirlash uchun apilar qoshildi

This commit is contained in:
behruz-dev
2025-11-26 16:53:40 +05:00
parent 6965fed7f5
commit 1623d4792f
3 changed files with 161 additions and 4 deletions

View File

@@ -22,4 +22,20 @@ class PlanSerializer(serializers.ModelSerializer):
description=validated_data.get('description'),
date=validated_data.get('date'),
user=self.context.get('user'),
)
)
class PlanUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = Plan
fields = [
'title', 'description', 'date', 'is_done',
]
def update(self, instance, validated_data):
instance.title = validated_data.get('title', instance.title)
instance.description = validated_data.get('description', instance.description)
instance.date = validated_data.get('date', instance.date)
instance.is_done = validated_data.get('is_done')
instance.save()
return instance

View File

@@ -59,6 +59,8 @@ urlpatterns = [
[
path('', plan_view.PlanApiView.as_view(), name='plan-create-list-api'),
path('<int:id>/complite/', plan_view.ComplitePlanApiView.as_view(), name='complite-plan-api'),
path('<int:id>/delete/', plan_view.PlanDeleteApiView.as_view(), name='plan-delete-api'),
path('<int:id>/update/', plan_view.PlanUpdateApiView.as_view(), name='plan-update-api'),
]
)),
path('location/', include(

View File

@@ -1,15 +1,16 @@
# django
from django.shortcuts import get_object_or_404
# rest framework
from rest_framework import generics, permissions
from rest_framework import generics, permissions, views
# drf yasg
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
# shared
from core.apps.shared.models import Plan
from core.apps.shared.serializers.base import BaseResponseSerializer, SuccessResponseSerializer
from core.apps.shared.serializers.plan import PlanSerializer
from core.apps.shared.serializers.plan import PlanSerializer, PlanUpdateSerializer
from core.apps.shared.utils.response_mixin import ResponseMixin
@@ -86,4 +87,142 @@ class ComplitePlanApiView(generics.GenericAPIView, ResponseMixin):
message='malumot yangilandi'
)
except Exception as e:
return self.error_response(data=str(e), message='xatolik')
return self.error_response(data=str(e), message='xatolik')
class PlanUpdateApiView(generics.GenericAPIView, ResponseMixin):
serializer_class = PlanUpdateSerializer
queryset = Plan.objects.all()
permission_classes = [permissions.IsAuthenticated]
@swagger_auto_schema(
responses={
200: openapi.Response(
schema=None,
description="Success",
examples={
"application/json": {
"status_code": 200,
"status": "success",
"message": "malumot tahrirlandi",
"data": {
"id": 1,
"title": "string",
"description": "string",
"date": "string",
"is_done": "true",
"created_at": "string",
}
}
}
),
400: openapi.Response(
schema=None,
description="Failure",
examples={
"application/json": {
"status_code": 400,
"status": "failure",
"message": "malumot tahrirlanmadi",
"data": "string"
}
}
),
500: openapi.Response(
schema=None,
description="Server Error",
examples={
"application/json": {
"status_code": 500,
"status": "error",
"message": "xatolik",
"data": "string"
}
}
),
404: openapi.Response(
schema=None,
description="Not Found",
examples={
"application/json": {
"status_code": 404,
"status": "failure",
"message": "Plan topilmadi",
"data": {}
}
}
)
}
)
def patch(self, request, id):
try:
obj = Plan.objects.filter(id=id, user=request.user).first()
if not obj:
return self.failure_response(message="Plan topilmadi", data={}, status_code=404)
serializer = self.serializer_class(data=request.data, instance=obj)
if serializer.is_valid():
obj = serializer.save()
created_data = PlanSerializer(obj).data
return self.success_response(
data=created_data,
message='malumot tahrirlandi',
status_code=200
)
return self.failure_response(data=serializer.errors, message='malumot tahrirlanmadi')
except Exception as e:
return self.error_response(data=str(e), message='xatolik')
class PlanDeleteApiView(views.APIView, ResponseMixin):
permission_classes = [permissions.IsAuthenticated]
@swagger_auto_schema(
responses={
204: openapi.Response(
schema=None,
description="Success",
examples={
"application/json": {
"status_code": 200,
"status": "success",
"message": "Plan o'chirildi",
"data": {}
}
}
),
500: openapi.Response(
schema=None,
description="Server Error",
examples={
"application/json": {
"status_code": 500,
"status": "error",
"message": "xatolik",
"data": "string"
}
}
),
404: openapi.Response(
schema=None,
description="Not Found",
examples={
"application/json": {
"status_code": 404,
"status": "failure",
"message": "Plan topilmadi",
"data": {}
}
}
)
}
)
def delete(self, request, id):
try:
obj = Plan.objects.filter(id=id, user=request.user).first()
if not obj:
return self.failure_response(message="Plan topilmadi", status_code=404, data={})
obj.delete()
return self.success_response(message="Plan o'chirildi", status_code=204, data={})
except Exception as e:
return self.error_response(data=str(e), message="xatolik")