change order apis
This commit is contained in:
@@ -1,14 +1,9 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from core.apps.orders.models import Order, OrderApplication
|
||||
from core.apps.orders.models import Order
|
||||
|
||||
|
||||
@admin.register(Order)
|
||||
class OrderAdmin(admin.ModelAdmin):
|
||||
list_display = ['quantity', 'product', 'unity', 'project', 'wherehouse']
|
||||
list_display = ['unity', 'project', 'wherehouse']
|
||||
|
||||
|
||||
@admin.register(OrderApplication)
|
||||
class OrderApplicationAdmin(admin.ModelAdmin):
|
||||
list_display = ['employee', 'status']
|
||||
|
||||
16
core/apps/orders/migrations/0003_delete_orderapplication.py
Normal file
16
core/apps/orders/migrations/0003_delete_orderapplication.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# Generated by Django 5.2.4 on 2025-08-04 10:04
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('orders', '0002_order_employee_order_status'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.DeleteModel(
|
||||
name='OrderApplication',
|
||||
),
|
||||
]
|
||||
@@ -40,23 +40,4 @@ class Order(BaseModel):
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("Buyurtma")
|
||||
verbose_name_plural = _("Buyurtmalar")
|
||||
|
||||
|
||||
class OrderApplication(BaseModel):
|
||||
STATUS = (
|
||||
('NEW', 'yangi'),
|
||||
('CANCELLED', "bekor qilindi"),
|
||||
('ACCEPTED', 'qabul qilindi'),
|
||||
)
|
||||
|
||||
orders = models.ManyToManyField(Order, related_name="applications")
|
||||
employee = models.ForeignKey(User, on_delete=models.CASCADE, related_name='applications')
|
||||
status = models.CharField(max_length=20, choices=STATUS)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.employee} application"
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("Ariza")
|
||||
verbose_name_plural = _("Arizalar")
|
||||
verbose_name_plural = _("Buyurtmalar")
|
||||
@@ -2,7 +2,7 @@ from django.db import transaction
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
from core.apps.orders.models import Order, OrderApplication
|
||||
from core.apps.orders.models import Order
|
||||
# products
|
||||
from core.apps.products.models import Product, Unity
|
||||
from core.apps.products.serializers.product import ProductListSerializer
|
||||
@@ -35,7 +35,7 @@ class OrderCreateSerializer(serializers.Serializer):
|
||||
wherehouse = WhereHouse.objects.get(id=data['wherehouse_id'])
|
||||
project = Project.objects.get(id=data['project_id'])
|
||||
if data.get('project_department_id'):
|
||||
project_department = ProjectDepartment.objects.get(
|
||||
ProjectDepartment.objects.get(
|
||||
id=data['project_department_id']
|
||||
)
|
||||
except Product.DoesNotExist:
|
||||
@@ -57,7 +57,20 @@ class OrderCreateSerializer(serializers.Serializer):
|
||||
data['wherehouse'] = wherehouse
|
||||
data['project'] = project
|
||||
return data
|
||||
|
||||
|
||||
def create(self, validated_data):
|
||||
with transaction.atomic():
|
||||
order = Order.objects.create(
|
||||
product=validated_data.get('product'),
|
||||
unity=validated_data.get('unity'),
|
||||
wherehouse=validated_data.get('wherehouse'),
|
||||
project=validated_data.get('project'),
|
||||
project_department=validated_data.get('project_department'),
|
||||
quantity=validated_data.get('quantity'),
|
||||
date=validated_data.get('date')
|
||||
)
|
||||
return order
|
||||
|
||||
|
||||
class OrderListSerializer(serializers.ModelSerializer):
|
||||
product = ProductListSerializer()
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
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,19 +1,13 @@
|
||||
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/', application_views.OrderApplicationCreateApiView.as_view()),
|
||||
path('list/', application_views.OrderApplicationListApiView.as_view()),
|
||||
]
|
||||
)),
|
||||
path('order/', include(
|
||||
[
|
||||
path('list/', order_views.OrderListApiView.as_view()),
|
||||
path('create/', order_views.OrderCreateApiView.as_view()),
|
||||
]
|
||||
)),
|
||||
]
|
||||
@@ -14,4 +14,18 @@ class OrderListApiView(generics.ListAPIView):
|
||||
)
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
pagination_class = CustomPageNumberPagination
|
||||
pagination_class = CustomPageNumberPagination
|
||||
|
||||
|
||||
|
||||
class OrderCreateApiView(generics.CreateAPIView):
|
||||
serializer_class = serializers.OrderCreateSerializer
|
||||
queryset = Order.objects.all()
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
|
||||
def get_serializer_context(self):
|
||||
context = super().get_serializer_context()
|
||||
context['user'] = self.request.user
|
||||
return context
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
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 = []
|
||||
Reference in New Issue
Block a user