suppurt list api admin uchun chiqarildi

This commit is contained in:
behruz-dev
2025-12-03 15:50:42 +05:00
parent 9276124cea
commit 731abb6936
3 changed files with 137 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
# rest framework
from rest_framework import serializers
# shared
from core.apps.shared.models import Support
class SupportListSerializer(serializers.ModelSerializer):
user = serializers.SerializerMethodField(method_name='get_user')
district = serializers.SerializerMethodField(method_name='get_district')
class Meta:
model = Support
fields = [
'id', 'problem', 'date', 'type', 'district', 'user', 'created_at'
]
def get_district(self, obj):
return {
'id': obj.district.id,
'name': obj.district.name,
}
def get_user(self, obj):
return {
'id': obj.user.id,
'first_name': obj.user.first_name,
'last_name': obj.user.last_name,
}

View File

@@ -31,6 +31,8 @@ from core.apps.dashboard.views.order import OrderViewSet
from core.apps.dashboard.views.payment import PaymentListApiView from core.apps.dashboard.views.payment import PaymentListApiView
# location # location
from core.apps.dashboard.views.location import LocationViewSet, UserLocationViewSet from core.apps.dashboard.views.location import LocationViewSet, UserLocationViewSet
# support
from core.apps.dashboard.views.support import SupportListApiView
urlpatterns = [ urlpatterns = [
@@ -66,6 +68,12 @@ urlpatterns = [
[ [
path('list/', PaymentListApiView.as_view(), name='payment-list-api'), path('list/', PaymentListApiView.as_view(), name='payment-list-api'),
] ]
)),
# -------------- support --------------
path('support/', include(
[
path('list/', SupportListApiView.as_view(), name='support-list-api'),
]
)) ))
] ]

View File

@@ -0,0 +1,99 @@
# django
from django.db.models import Q
# rest framework
from rest_framework import generics, permissions
# drf yasg
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
# shared
from core.apps.shared.models import Support
from core.apps.shared.utils.response_mixin import ResponseMixin
# dashboard
from core.apps.dashboard.serializers.support import SupportListSerializer
class SupportListApiView(generics.GenericAPIView, ResponseMixin):
serializer_class = SupportListSerializer
queryset = Support.objects.all()
permission_classes = [permissions.IsAdminUser]
@swagger_auto_schema(
tags=['Admin Support'],
manual_parameters=[
openapi.Parameter(
in_=openapi.IN_QUERY,
name="problem",
description="problem text bo'yicha filter",
required=False,
type=openapi.TYPE_STRING,
),
openapi.Parameter(
in_=openapi.IN_QUERY,
name="district",
description="tuman name bo'yicha filter",
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,
),
openapi.Parameter(
in_=openapi.IN_QUERY,
name="date",
description="date bo'yicha qidirish",
required=False,
type=openapi.FORMAT_DATE,
),
],
)
def get(self, request):
try:
problem = request.query_params.get('problem', None)
district_name = request.query_params.get('district', None)
date = request.query_params.get('date', None)
user_full_name = request.query_params.get('user', None)
queryset = self.queryset.all()
# filters
if problem is not None:
queryset = queryset.filter(problem__istartswith=problem)
if district_name is not None:
queryset = queryset.filter(district__name__istartswith=district_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)
)
if date is not None:
queryset = queryset.filter(date=date)
page = self.paginate_queryset(queryset)
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(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, iltimos backend dasturchiga murojaat qiling'
)