add management command for products and orders app
This commit is contained in:
52
core/apps/orders/management/commands/import_orders.py
Normal file
52
core/apps/orders/management/commands/import_orders.py
Normal 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 ✅"))
|
||||
18
core/apps/orders/migrations/0029_alter_order_status.py
Normal file
18
core/apps/orders/migrations/0029_alter_order_status.py
Normal 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),
|
||||
),
|
||||
]
|
||||
@@ -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'),
|
||||
)
|
||||
|
||||
23
core/apps/products/management/commands/import_folder.py
Normal file
23
core/apps/products/management/commands/import_folder.py
Normal 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")
|
||||
37
core/apps/products/management/commands/import_products.py
Normal file
37
core/apps/products/management/commands/import_products.py
Normal 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")
|
||||
22
core/apps/products/management/commands/import_units.py
Normal file
22
core/apps/products/management/commands/import_units.py
Normal 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")
|
||||
@@ -23,5 +23,4 @@ class SubFolder(BaseModel):
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'Sub Papka'
|
||||
verbose_name_plural = 'Sub Papkalar'
|
||||
|
||||
verbose_name_plural = 'Sub Papkalar'
|
||||
Reference in New Issue
Block a user