From 1831ceae8089ed74f822cf41b90c582487925fa1 Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Mon, 4 Aug 2025 10:13:14 +0500 Subject: [PATCH] change order apis --- config/settings/base.py | 2 +- core/apps/orders/admin/order.py | 7 +--- .../0003_delete_orderapplication.py | 16 ++++++++ core/apps/orders/models/order.py | 21 +--------- core/apps/orders/serializers/order.py | 19 +++++++-- .../orders/serializers/order_application.py | 41 ------------------- core/apps/orders/urls.py | 8 +--- core/apps/orders/views/order.py | 16 +++++++- core/apps/orders/views/order_application.py | 27 ------------ 9 files changed, 51 insertions(+), 106 deletions(-) create mode 100644 core/apps/orders/migrations/0003_delete_orderapplication.py delete mode 100644 core/apps/orders/serializers/order_application.py delete mode 100644 core/apps/orders/views/order_application.py diff --git a/config/settings/base.py b/config/settings/base.py index 64676de..dd1ff5f 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -145,6 +145,6 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' AUTH_USER_MODEL = 'accounts.User' -SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') +# SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') from config.conf import * diff --git a/core/apps/orders/admin/order.py b/core/apps/orders/admin/order.py index a7f69c5..3b4c4b9 100644 --- a/core/apps/orders/admin/order.py +++ b/core/apps/orders/admin/order.py @@ -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'] diff --git a/core/apps/orders/migrations/0003_delete_orderapplication.py b/core/apps/orders/migrations/0003_delete_orderapplication.py new file mode 100644 index 0000000..3a1ee3c --- /dev/null +++ b/core/apps/orders/migrations/0003_delete_orderapplication.py @@ -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', + ), + ] diff --git a/core/apps/orders/models/order.py b/core/apps/orders/models/order.py index 5e621aa..dbff2e5 100644 --- a/core/apps/orders/models/order.py +++ b/core/apps/orders/models/order.py @@ -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") \ No newline at end of file + verbose_name_plural = _("Buyurtmalar") \ No newline at end of file diff --git a/core/apps/orders/serializers/order.py b/core/apps/orders/serializers/order.py index 2ef3141..bab4c89 100644 --- a/core/apps/orders/serializers/order.py +++ b/core/apps/orders/serializers/order.py @@ -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() diff --git a/core/apps/orders/serializers/order_application.py b/core/apps/orders/serializers/order_application.py deleted file mode 100644 index 988a705..0000000 --- a/core/apps/orders/serializers/order_application.py +++ /dev/null @@ -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' - ] \ No newline at end of file diff --git a/core/apps/orders/urls.py b/core/apps/orders/urls.py index 5fdcb9d..929f16f 100644 --- a/core/apps/orders/urls.py +++ b/core/apps/orders/urls.py @@ -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()), ] )), ] \ No newline at end of file diff --git a/core/apps/orders/views/order.py b/core/apps/orders/views/order.py index 677e6d5..06e73c4 100644 --- a/core/apps/orders/views/order.py +++ b/core/apps/orders/views/order.py @@ -14,4 +14,18 @@ class OrderListApiView(generics.ListAPIView): ) permission_classes = [HasRolePermission] required_permissions = [] - pagination_class = CustomPageNumberPagination \ No newline at end of file + 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 + \ No newline at end of file diff --git a/core/apps/orders/views/order_application.py b/core/apps/orders/views/order_application.py deleted file mode 100644 index 595e51a..0000000 --- a/core/apps/orders/views/order_application.py +++ /dev/null @@ -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 = [] \ No newline at end of file