admin uchun list api chiqarilfi
This commit is contained in:
31
core/apps/dashboard/serializers/dis_product.py
Normal file
31
core/apps/dashboard/serializers/dis_product.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# rest framework
|
||||
from rest_framework import serializers
|
||||
|
||||
# orders
|
||||
from core.apps.orders.models import DistributedProduct
|
||||
|
||||
|
||||
class DistributedProductListSerializer(serializers.ModelSerializer):
|
||||
product = serializers.SerializerMethodField(method_name='get_product')
|
||||
user = serializers.SerializerMethodField(method_name='get_user')
|
||||
|
||||
class Meta:
|
||||
model = DistributedProduct
|
||||
fields = [
|
||||
'id', 'product', 'quantity', 'employee_name', 'quantity', 'user', 'created_at'
|
||||
]
|
||||
|
||||
def get_user(self, obj):
|
||||
return {
|
||||
"id": obj.user.id,
|
||||
"first_name": obj.user.first_name,
|
||||
"last_name": obj.user.last_name,
|
||||
}
|
||||
|
||||
def get_product(self, obj):
|
||||
return {
|
||||
"id": obj.product.id,
|
||||
"name": obj.product.name,
|
||||
"price": obj.product.price,
|
||||
}
|
||||
|
||||
@@ -33,6 +33,8 @@ from core.apps.dashboard.views.payment import PaymentListApiView
|
||||
from core.apps.dashboard.views.location import LocationViewSet, UserLocationViewSet
|
||||
# support
|
||||
from core.apps.dashboard.views.support import SupportListApiView
|
||||
# distibuted products
|
||||
from core.apps.dashboard.views.dis_prod import DistributedProductListApiView
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
@@ -74,6 +76,12 @@ urlpatterns = [
|
||||
[
|
||||
path('list/', SupportListApiView.as_view(), name='support-list-api'),
|
||||
]
|
||||
)),
|
||||
# -------------- distributed products --------------
|
||||
path('distributed_product/', include(
|
||||
[
|
||||
path('list/', DistributedProductListApiView.as_view(), name='distributed-product-list-api'),
|
||||
]
|
||||
))
|
||||
]
|
||||
|
||||
|
||||
90
core/apps/dashboard/views/dis_prod.py
Normal file
90
core/apps/dashboard/views/dis_prod.py
Normal file
@@ -0,0 +1,90 @@
|
||||
# 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
|
||||
|
||||
# orders
|
||||
from core.apps.orders.models import DistributedProduct
|
||||
|
||||
# shared
|
||||
from core.apps.shared.utils.response_mixin import ResponseMixin
|
||||
|
||||
# dashboard
|
||||
from core.apps.dashboard.serializers.dis_product import DistributedProductListSerializer
|
||||
|
||||
|
||||
class DistributedProductListApiView(generics.GenericAPIView, ResponseMixin):
|
||||
serializer_class = DistributedProductListSerializer
|
||||
queryset = DistributedProduct.objects.all()
|
||||
permission_classes = [permissions.IsAdminUser]
|
||||
|
||||
@swagger_auto_schema(
|
||||
tags=['Admin Orders'],
|
||||
manual_parameters=[
|
||||
openapi.Parameter(
|
||||
in_=openapi.IN_QUERY,
|
||||
name="product",
|
||||
description="product 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:
|
||||
product_name = request.query_params.get('product', None)
|
||||
date = request.query_params.get('date', None)
|
||||
user_full_name = request.query_params.get('user', None)
|
||||
|
||||
queryset = self.queryset.all()
|
||||
|
||||
# filters
|
||||
if product_name is not None:
|
||||
queryset = queryset.filter(product__name__istartswith=product_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'
|
||||
)
|
||||
Reference in New Issue
Block a user