tarqatilgan mahsulotlar uchun api chiqarildi
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
from .product import *
|
||||
from .order import *
|
||||
from .order import *
|
||||
from .distributed_prod import *
|
||||
8
core/apps/orders/admin/distributed_prod.py
Normal file
8
core/apps/orders/admin/distributed_prod.py
Normal file
@@ -0,0 +1,8 @@
|
||||
# django
|
||||
from django.contrib import admin
|
||||
|
||||
# orders
|
||||
from core.apps.orders.models import DistributedProduct
|
||||
|
||||
|
||||
admin.site.register(DistributedProduct)
|
||||
32
core/apps/orders/migrations/0006_distributedproduct.py
Normal file
32
core/apps/orders/migrations/0006_distributedproduct.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# Generated by Django 5.2 on 2025-12-03 10:56
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('orders', '0005_payment'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='DistributedProduct',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('date', models.DateField()),
|
||||
('employee_name', models.CharField(max_length=200)),
|
||||
('quantity', models.PositiveIntegerField()),
|
||||
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='distributed_products', to='orders.product')),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='distributed_products', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -1,4 +1,5 @@
|
||||
from .product import *
|
||||
from .order_item import *
|
||||
from .order import *
|
||||
from .payment import *
|
||||
from .payment import *
|
||||
from .distributed_product import *
|
||||
21
core/apps/orders/models/distributed_product.py
Normal file
21
core/apps/orders/models/distributed_product.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# django
|
||||
from django.db import models
|
||||
|
||||
# shared
|
||||
from core.apps.shared.models import BaseModel
|
||||
# orders
|
||||
from core.apps.orders.models import Product
|
||||
# accounts
|
||||
from core.apps.accounts.models import User
|
||||
|
||||
|
||||
class DistributedProduct(BaseModel):
|
||||
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='distributed_products')
|
||||
date = models.DateField()
|
||||
employee_name = models.CharField(max_length=200)
|
||||
quantity = models.PositiveIntegerField()
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='distributed_products')
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.product} recieved for {self.employee_name}, quantity -> {self.quantity}x'
|
||||
|
||||
32
core/apps/orders/serializers/distributed_product.py
Normal file
32
core/apps/orders/serializers/distributed_product.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# django
|
||||
from django.db import transaction
|
||||
|
||||
# rest framework
|
||||
from rest_framework import serializers
|
||||
|
||||
# orders
|
||||
from core.apps.orders.models import DistributedProduct, Product
|
||||
|
||||
|
||||
class DistributedProductCreateSerializer(serializers.Serializer):
|
||||
product_id = serializers.IntegerField()
|
||||
date = serializers.DateField()
|
||||
employee_name = serializers.CharField()
|
||||
quantity = serializers.IntegerField()
|
||||
|
||||
def validate(self, data):
|
||||
product = Product.objects.filter(id=data['product_id']).first()
|
||||
if not product:
|
||||
raise serializers.ValidationError({'product': "product not found"})
|
||||
data['product'] = product
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
with transaction.atomic():
|
||||
return DistributedProduct.objects.create(
|
||||
product=validated_data.get('product'),
|
||||
date=validated_data.get('date'),
|
||||
employee_name=validated_data.get('employee_name'),
|
||||
quantity=validated_data.get('quantity'),
|
||||
user=self.context.get('user'),
|
||||
)
|
||||
@@ -4,6 +4,8 @@ from django.urls import path, include
|
||||
from core.apps.orders.views import product as product_view
|
||||
# orders order views
|
||||
from core.apps.orders.views import order as order_view
|
||||
# orders distributed product
|
||||
from core.apps.orders.views import distributed_product as dp_view
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
@@ -20,4 +22,10 @@ urlpatterns = [
|
||||
]
|
||||
)),
|
||||
|
||||
|
||||
path('distributed_product/', include(
|
||||
[
|
||||
path('create/', dp_view.DistributedProductCreateApiView.as_view(), name='distributed-product-create-api'),
|
||||
]
|
||||
)),
|
||||
]
|
||||
|
||||
37
core/apps/orders/views/distributed_product.py
Normal file
37
core/apps/orders/views/distributed_product.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# rest framework
|
||||
from rest_framework import generics, permissions
|
||||
|
||||
# orders
|
||||
from core.apps.orders.models import DistributedProduct
|
||||
from core.apps.orders.serializers.distributed_product import DistributedProductCreateSerializer
|
||||
|
||||
# shared
|
||||
from core.apps.shared.utils.response_mixin import ResponseMixin
|
||||
|
||||
|
||||
class DistributedProductCreateApiView(generics.GenericAPIView, ResponseMixin):
|
||||
serializer_class = DistributedProductCreateSerializer
|
||||
queryset = DistributedProduct.objects.all()
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def post(self, request):
|
||||
try:
|
||||
serializer = self.serializer_class(data=request.data, context={'user': request.user})
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return self.success_response(
|
||||
data={},
|
||||
message="form created",
|
||||
status_code=201
|
||||
)
|
||||
return self.failure_response(
|
||||
data={},
|
||||
message="yuborilmadi, iltimos kiritayotgan malumotingizni tekshirib koring",
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
return self.failure_response(
|
||||
data=str(e),
|
||||
message="xatolik, backend dasturchiga murojaat qiling"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user