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): class Order(BaseModel):
STATUS = ( STATUS = (
('NEW', 'yangi'), ('NEW', 'yangi'),
("OPEN", 'ochiq'),
('CANCELLED', "bekor qilindi"), ('CANCELLED', "bekor qilindi"),
('ACCEPTED', 'qabul qilindi'), ('ACCEPTED', 'qabul qilindi'),
) )

View File

@@ -0,0 +1,23 @@
import json
from django.core.management import BaseCommand
from core.apps.products.models import Folder
class Command(BaseCommand):
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') as f:
data = json.load(f)
for item in data['data']:
Folder.objects.get_or_create(
name=item['name']['uz']
)
self.stdout.write("Folderlar qoshildi")

View File

@@ -0,0 +1,37 @@
import json
from datetime import datetime
from django.core.management import BaseCommand
from core.apps.products.models import Product, Unity
class Command(BaseCommand):
help = "Import products 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') as f:
data = json.load(f)
for item in data['data']['data']:
name = item['name']['uz']
code = item['code']
type = str(item['resource']['type']).upper()
unit_name = item['unit']['name']['uz']
unit = Unity.objects.filter(value=unit_name).first()
Product.objects.get_or_create(
name=name,
defaults={
"product_code": code,
"type": type,
"unity": unit
}
)
self.stdout.write("Products added")

View File

@@ -0,0 +1,22 @@
import json
from django.core.management import BaseCommand
from core.apps.products.models import Unity
class Command(BaseCommand):
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') as f:
data = json.load(f)
for item in data['data']:
Unity.objects.get_or_create(
value=item['name']['uz']
)
self.stdout.write("Units added")

View File

@@ -23,5 +23,4 @@ class SubFolder(BaseModel):
class Meta: class Meta:
verbose_name = 'Sub Papka' verbose_name = 'Sub Papka'
verbose_name_plural = 'Sub Papkalar' verbose_name_plural = 'Sub Papkalar'

32042
data/product.json Normal file

File diff suppressed because it is too large Load Diff

107
data/product_folder.json Normal file
View File

@@ -0,0 +1,107 @@
{
"data": [
{
"id": 43,
"name": {
"ru": "Товарная база Узбекистана",
"uz": "O'zbekiston mahsulotlar bazasi"
},
"color": null,
"status": "active",
"products_count": 2000
},
{
"id": 44,
"name": {
"uz": "Marketing",
"ru": "Marketing"
},
"color": null,
"status": "active",
"products_count": 208
},
{
"id": 49,
"name": {
"uz": "Inventar",
"en": "Inventar",
"ru": "Inventar"
},
"color": null,
"status": "active",
"products_count": 82
},
{
"id": 51,
"name": {
"ru": "административный расходы",
"en": "административный расходы",
"uz": "административный расходы"
},
"color": null,
"status": "active",
"products_count": 81
},
{
"id": 55,
"name": {
"ru": "Qurilish montaj ishlari",
"en": "Qurilish montaj ishlari",
"uz": "Qurilish montaj ishlari"
},
"color": null,
"status": "active",
"products_count": 32
},
{
"id": 78,
"name": {
"uz": "NARYAD",
"en": "NARYAD",
"ru": "NARYAD"
},
"color": null,
"status": "active",
"products_count": 7
},
{
"id": 83,
"name": {
"uz": "OFIS INVENTARLARI",
"en": "OFIS INVENTARLARI",
"ru": "OFIS INVENTARLARI"
},
"color": null,
"status": "active",
"products_count": 105
},
{
"id": 148,
"name": {
"uz": "Texnika xizmati",
"en": "Texnika xizmati",
"ru": "Texnika xizmati"
},
"color": "#1E90FF",
"status": "active",
"products_count": 7
},
{
"id": 149,
"name": {
"uz": "Marketing xizmati",
"en": "Marketing xizmati",
"ru": "Marketing xizmati"
},
"color": "#1E90FF",
"status": "active",
"products_count": 5
}
],
"message": {
"uz": "Muvaffaqiyatli!",
"ru": "Успешно!",
"en": "Success!",
"tr": "Başarılı!"
}
}

1634
data/unit.json Normal file

File diff suppressed because it is too large Load Diff

7057
order.json Normal file

File diff suppressed because it is too large Load Diff