add management command for products and orders app

This commit is contained in:
behruz-dev
2025-10-24 15:17:59 +05:00
parent e82aa0950f
commit 4cda8468fc
11 changed files with 40994 additions and 2 deletions

View File

@@ -0,0 +1,52 @@
import json
from datetime import datetime
from django.core.management.base import BaseCommand
from core.apps.orders.models import Order
from core.apps.products.models import Unity, Product
from core.apps.projects.models import ProjectFolder
from core.apps.wherehouse.models import WhereHouse
from core.apps.accounts.models import User
class Command(BaseCommand):
help = "Import orders from JSON file"
def add_arguments(self, parser):
parser.add_argument("file_path", type=str, help="Path to JSON file")
def handle(self, *args, **options):
file_path = options['file_path']
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
for item in data['data']['data']:
product_name = item['product']['name']['uz']
unit_name = item['unit']['name']['uz']
warehouse_name = item['warehouse']['name']
project_name = item['project']['name']
creator_name = item['creator']['full_name']
product = Product.objects.filter(name=product_name).first()
unity = Unity.objects.filter(value=unit_name).first()
wherehouse = WhereHouse.objects.filter(name=warehouse_name).first()
project_folder = ProjectFolder.objects.filter(name=project_name).first()
user = User.objects.filter(full_name=creator_name).first()
delivery_date = datetime.strptime(item['delivery_date'], "%d.%m.%Y").date()
created_at = datetime.strptime(item['created_at'], "%d.%m.%Y %H:%M")
Order.objects.update_or_create(
status=item['status'].upper(),
product=product,
unity=unity,
wherehouse=wherehouse,
project_folder=project_folder,
employee=user,
quantity=item['quantity'],
currency=item['currency']['symbol'].lower(),
created_at=created_at,
date=delivery_date
)
self.stdout.write(self.style.SUCCESS("Orders imported successfully ✅"))

View File

@@ -0,0 +1,18 @@
# Generated by Django 5.2.4 on 2025-10-24 12:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('orders', '0028_alter_order_quantity'),
]
operations = [
migrations.AlterField(
model_name='order',
name='status',
field=models.CharField(choices=[('NEW', 'yangi'), ('OPEN', 'ochiq'), ('CANCELLED', 'bekor qilindi'), ('ACCEPTED', 'qabul qilindi')], default='NEW', max_length=20),
),
]

View File

@@ -12,6 +12,7 @@ from core.apps.counterparty.models import Counterparty
class Order(BaseModel):
STATUS = (
('NEW', 'yangi'),
("OPEN", 'ochiq'),
('CANCELLED', "bekor qilindi"),
('ACCEPTED', 'qabul qilindi'),
)