add new apis
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
REST_FRAMEWORK = {
|
||||
"DEFAULT_AUTHENTICATION_CLASSES": [
|
||||
'rest_framework.authentication.SessionAuthentication',
|
||||
'rest_framework.authentication.BasicAuthentication'
|
||||
'rest_framework.authentication.BasicAuthentication',
|
||||
'rest_framework_simplejwt.authentication.JWTAuthentication',
|
||||
]
|
||||
],
|
||||
'DEFAULT_PAGINATION_CLASS': 'core.apps.shared.paginations.custom.CustomPageNumberPagination',
|
||||
'PAGE_SIZE': 10
|
||||
}
|
||||
@@ -3,7 +3,7 @@ from rest_framework.response import Response
|
||||
|
||||
from core.apps.company.models import Company
|
||||
from core.apps.company.serializers import company as serializers
|
||||
|
||||
from core.apps.shared.paginations.custom import CustomPageNumberPagination
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ class CompanyListApiView(generics.ListAPIView):
|
||||
queryset = Company.objects.all()
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
pagination_class = CustomPageNumberPagination
|
||||
|
||||
|
||||
class CompanyDetailApiView(generics.RetrieveAPIView):
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# Generated by Django 5.2.4 on 2025-08-01 16:41
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('orders', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='order',
|
||||
name='employee',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='orders', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='order',
|
||||
name='status',
|
||||
field=models.CharField(choices=[('NEW', 'yangi'), ('CANCELLED', 'bekor qilindi'), ('ACCEPTED', 'qabul qilindi')], default='NEW', max_length=20),
|
||||
),
|
||||
]
|
||||
@@ -9,6 +9,12 @@ from core.apps.wherehouse.models import WhereHouse
|
||||
|
||||
|
||||
class Order(BaseModel):
|
||||
STATUS = (
|
||||
('NEW', 'yangi'),
|
||||
('CANCELLED', "bekor qilindi"),
|
||||
('ACCEPTED', 'qabul qilindi'),
|
||||
)
|
||||
|
||||
product = models.ForeignKey(
|
||||
Product, on_delete=models.CASCADE, related_name='orders'
|
||||
)
|
||||
@@ -26,7 +32,9 @@ class Order(BaseModel):
|
||||
)
|
||||
date = models.DateField()
|
||||
quantity = models.PositiveBigIntegerField(default=1)
|
||||
|
||||
status = models.CharField(max_length=20, choices=STATUS, default="NEW")
|
||||
employee = models.ForeignKey(User, on_delete=models.CASCADE, related_name='orders', null=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.product} {self.unity} quantity order"
|
||||
|
||||
|
||||
@@ -3,9 +3,20 @@ from django.db import transaction
|
||||
from rest_framework import serializers
|
||||
|
||||
from core.apps.orders.models import Order, OrderApplication
|
||||
# products
|
||||
from core.apps.products.models import Product, Unity
|
||||
from core.apps.products.serializers.product import ProductListSerializer
|
||||
from core.apps.products.serializers.unity import UnityListSerializer
|
||||
# wherehouse
|
||||
from core.apps.wherehouse.models import WhereHouse
|
||||
from core.apps.wherehouse.serializers.wherehouse import WhereHouseListSerializer
|
||||
# projects
|
||||
from core.apps.projects.models import Project, ProjectDepartment
|
||||
from core.apps.projects.serializers.project import (
|
||||
ProjectListSerializer,
|
||||
ProjectDepartmentListSerializer
|
||||
)
|
||||
|
||||
|
||||
|
||||
class OrderCreateSerializer(serializers.Serializer):
|
||||
@@ -48,28 +59,16 @@ class OrderCreateSerializer(serializers.Serializer):
|
||||
return data
|
||||
|
||||
|
||||
class OrderApplicationCreateSerializer(serializers.Serializer):
|
||||
orders = serializers.ListSerializer(child=OrderCreateSerializer())
|
||||
class OrderListSerializer(serializers.ModelSerializer):
|
||||
product = ProductListSerializer()
|
||||
unity = UnityListSerializer()
|
||||
project = ProjectListSerializer()
|
||||
project_department = ProjectDepartmentListSerializer()
|
||||
wherehouse = WhereHouseListSerializer()
|
||||
|
||||
def create(self, validated_data):
|
||||
employee = self.context.get('user')
|
||||
orders_data = validated_data.pop('orders')
|
||||
application = OrderApplication.objects.create(
|
||||
employee=employee, status="NEW"
|
||||
)
|
||||
|
||||
order_objs = []
|
||||
for order_data in orders_data:
|
||||
order_objs.append(Order(
|
||||
product=order_data['product'],
|
||||
unity=order_data['unity'],
|
||||
quantity=order_data['quantity'],
|
||||
wherehouse=order_data['wherehouse'],
|
||||
project=order_data['project'],
|
||||
project_department=order_data.get('project_department'),
|
||||
date=order_data['date']
|
||||
))
|
||||
|
||||
created_orders = Order.objects.bulk_create(order_objs)
|
||||
application.orders.add(*created_orders)
|
||||
return application
|
||||
class Meta:
|
||||
model = Order
|
||||
fields = [
|
||||
'id', 'product', 'unity', 'quantity', 'project', 'project_department',
|
||||
'wherehouse', 'date', 'status', 'employee'
|
||||
]
|
||||
41
core/apps/orders/serializers/order_application.py
Normal file
41
core/apps/orders/serializers/order_application.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from core.apps.orders.models import OrderApplication, Order
|
||||
from core.apps.orders.serializers.order import OrderCreateSerializer, OrderListSerializer
|
||||
|
||||
|
||||
class OrderApplicationCreateSerializer(serializers.Serializer):
|
||||
orders = serializers.ListSerializer(child=OrderCreateSerializer())
|
||||
|
||||
def create(self, validated_data):
|
||||
employee = self.context.get('user')
|
||||
orders_data = validated_data.pop('orders')
|
||||
application = OrderApplication.objects.create(
|
||||
employee=employee, status="NEW"
|
||||
)
|
||||
|
||||
order_objs = []
|
||||
for order_data in orders_data:
|
||||
order_objs.append(Order(
|
||||
product=order_data['product'],
|
||||
unity=order_data['unity'],
|
||||
quantity=order_data['quantity'],
|
||||
wherehouse=order_data['wherehouse'],
|
||||
project=order_data['project'],
|
||||
project_department=order_data.get('project_department'),
|
||||
date=order_data['date']
|
||||
))
|
||||
|
||||
created_orders = Order.objects.bulk_create(order_objs)
|
||||
application.orders.add(*created_orders)
|
||||
return application
|
||||
|
||||
|
||||
class OrderApplicationListSerializer(serializers.ModelSerializer):
|
||||
orders = OrderListSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = OrderApplication
|
||||
fields = [
|
||||
'id', 'employee', 'status', 'orders'
|
||||
]
|
||||
@@ -1,11 +1,19 @@
|
||||
from django.urls import path, include
|
||||
|
||||
from core.apps.orders.views import order_application as application_views
|
||||
from core.apps.orders.views import order as order_views
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('order_application/', include(
|
||||
[
|
||||
path('create/', order_views.OrderApplicationCreateApiView.as_view()),
|
||||
path('create/', application_views.OrderApplicationCreateApiView.as_view()),
|
||||
path('list/', application_views.OrderApplicationListApiView.as_view()),
|
||||
]
|
||||
)),
|
||||
path('order/', include(
|
||||
[
|
||||
path('list/', order_views.OrderListApiView.as_view()),
|
||||
]
|
||||
)),
|
||||
]
|
||||
@@ -1,18 +1,17 @@
|
||||
from rest_framework import generics, response
|
||||
from rest_framework import generics, status
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.apps.orders.models import Order, OrderApplication
|
||||
from core.apps.orders.serializers import order as serializers
|
||||
from core.apps.orders.models import Order
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
from core.apps.shared.paginations.custom import CustomPageNumberPagination
|
||||
|
||||
|
||||
class OrderApplicationCreateApiView(generics.CreateAPIView):
|
||||
serializer_class = serializers.OrderApplicationCreateSerializer
|
||||
queryset = OrderApplication.objects.all()
|
||||
class OrderListApiView(generics.ListAPIView):
|
||||
serializer_class = serializers.OrderListSerializer
|
||||
queryset = Order.objects.select_related(
|
||||
'product', 'unity', 'project', 'project_department', 'wherehouse'
|
||||
)
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
|
||||
def get_serializer_context(self):
|
||||
context = super().get_serializer_context()
|
||||
context['user'] = self.request.user
|
||||
return context
|
||||
pagination_class = CustomPageNumberPagination
|
||||
27
core/apps/orders/views/order_application.py
Normal file
27
core/apps/orders/views/order_application.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from rest_framework import generics, response
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.apps.orders.models import Order, OrderApplication
|
||||
from core.apps.orders.serializers import order_application as serializers
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
from core.apps.shared.paginations.custom import CustomPageNumberPagination
|
||||
|
||||
|
||||
class OrderApplicationCreateApiView(generics.CreateAPIView):
|
||||
serializer_class = serializers.OrderApplicationCreateSerializer
|
||||
queryset = OrderApplication.objects.all()
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
|
||||
def get_serializer_context(self):
|
||||
context = super().get_serializer_context()
|
||||
context['user'] = self.request.user
|
||||
return context
|
||||
|
||||
|
||||
class OrderApplicationListApiView(generics.ListAPIView):
|
||||
queryset = OrderApplication.objects.prefetch_related('orders')
|
||||
serializer_class = serializers.OrderApplicationListSerializer
|
||||
pagination_class = CustomPageNumberPagination
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
18
core/apps/products/migrations/0004_product_type.py
Normal file
18
core/apps/products/migrations/0004_product_type.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.2.4 on 2025-08-01 17:27
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0003_unity'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('TANGIBLE', "ushlab bo'ladigan"), ('INTANGIBLE', "ushlab bo'lmaydigan")], default='TANGIBLE', max_length=20),
|
||||
),
|
||||
]
|
||||
@@ -5,7 +5,13 @@ from core.apps.shared.models import BaseModel
|
||||
|
||||
|
||||
class Product(BaseModel):
|
||||
TYPE = (
|
||||
("TANGIBLE", "ushlab bo'ladigan"),
|
||||
("INTANGIBLE", "ushlab bo'lmaydigan"),
|
||||
)
|
||||
|
||||
name = models.CharField(max_length=200)
|
||||
type = models.CharField(max_length=20, choices=TYPE, default="TANGIBLE")
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -7,5 +7,5 @@ class ProductListSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = [
|
||||
'id', 'name'
|
||||
'id', 'name', 'type'
|
||||
]
|
||||
@@ -4,10 +4,12 @@ from rest_framework.response import Response
|
||||
from core.apps.products.models.product import Product
|
||||
from core.apps.products.serializers import product as serializers
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
from core.apps.shared.paginations.custom import CustomPageNumberPagination
|
||||
|
||||
|
||||
class ProductListApiView(generics.ListAPIView):
|
||||
serializer_class = serializers.ProductListSerializer
|
||||
queryset = Product.objects.all()
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
required_permissions = []
|
||||
pagination_class = CustomPageNumberPagination
|
||||
@@ -4,10 +4,12 @@ from rest_framework.response import Response
|
||||
from core.apps.products.models import Unity
|
||||
from core.apps.products.serializers import unity as serializers
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
from core.apps.shared.paginations.custom import CustomPageNumberPagination
|
||||
|
||||
|
||||
class UnityListApiView(generics.ListAPIView):
|
||||
serializer_class = serializers.UnityListSerializer
|
||||
queryset = Unity.objects.all()
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
required_permissions = []
|
||||
pagination_class = CustomPageNumberPagination
|
||||
@@ -4,6 +4,7 @@ from rest_framework.response import Response
|
||||
from core.apps.projects.models.project import Project, ProjectDepartment
|
||||
from core.apps.projects.serializers import project as serializers
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
from core.apps.shared.paginations.custom import CustomPageNumberPagination
|
||||
|
||||
|
||||
class ProjectListApiView(generics.ListAPIView):
|
||||
@@ -11,6 +12,7 @@ class ProjectListApiView(generics.ListAPIView):
|
||||
queryset = Project.objects.all()
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
pagination_class = CustomPageNumberPagination
|
||||
|
||||
|
||||
class ProjectDetailApiView(generics.RetrieveAPIView):
|
||||
|
||||
0
core/apps/shared/paginations/__init__.py
Normal file
0
core/apps/shared/paginations/__init__.py
Normal file
22
core/apps/shared/paginations/custom.py
Normal file
22
core/apps/shared/paginations/custom.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
|
||||
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from rest_framework.response import Response
|
||||
|
||||
class CustomPageNumberPagination(PageNumberPagination):
|
||||
page_size = 10
|
||||
page_query_param = 'page'
|
||||
page_size_query_param = 'page_size'
|
||||
max_page_size = 100
|
||||
|
||||
def get_paginated_response(self, data):
|
||||
return Response({
|
||||
'total': self.page.paginator.count,
|
||||
'page': self.page.number,
|
||||
'page_size': self.get_page_size(self.request),
|
||||
'total_pages': self.page.paginator.num_pages,
|
||||
'has_next': self.page.has_next(),
|
||||
'has_previous': self.page.has_previous(),
|
||||
'results': data
|
||||
})
|
||||
Reference in New Issue
Block a user