products: add product import
This commit is contained in:
@@ -5,5 +5,6 @@ from core.apps.products.models.product import Product
|
|||||||
|
|
||||||
@admin.register(Product)
|
@admin.register(Product)
|
||||||
class ProductAdmin(admin.ModelAdmin):
|
class ProductAdmin(admin.ModelAdmin):
|
||||||
list_display = ['id', 'name']
|
list_display = ['id', 'name', 'folder', 'sub_folder']
|
||||||
search_fields = ['name']
|
search_fields = ['name']
|
||||||
|
list_filter = ['folder', 'sub_folder']
|
||||||
@@ -1,8 +1,35 @@
|
|||||||
import json
|
import json, requests
|
||||||
|
|
||||||
from django.core.management import BaseCommand
|
from django.core.management import BaseCommand
|
||||||
|
|
||||||
from core.apps.products.models import Folder
|
from core.apps.products.models import Folder, SubFolder, Product, Unity
|
||||||
|
|
||||||
|
def get_folder_products(folder_id):
|
||||||
|
url = f"https://backend.app.uyqur.uz/main/category/view/product?category_id={folder_id}&size=500"
|
||||||
|
headers = {
|
||||||
|
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MTgzMzg2OSwiZXhwIjoxNzYxOTIwMjY5LCJuYmYiOjE3NjE4MzM4NjksImp0aSI6IjZSQWE1RzlyT0pGbXF1T2kiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.ACT7oxl-A2eit_bzxeal2jF_xLa0klFObNBpp1HuheE"
|
||||||
|
}
|
||||||
|
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response.json()
|
||||||
|
return None
|
||||||
|
|
||||||
|
def create_product(data, folder, unit):
|
||||||
|
type = {
|
||||||
|
"PRD": "PRODUCT",
|
||||||
|
"HR": "HUMAN_RESOURCE",
|
||||||
|
"MEC": "MECHANISM",
|
||||||
|
}
|
||||||
|
Product.objects.get_or_create(
|
||||||
|
name=data['name']['uz'],
|
||||||
|
defaults={
|
||||||
|
"folder": folder,
|
||||||
|
"type":type.get(data['resource']['symbol']['en']) if type.get(data['resource']['symbol']['en']) else "PRD",
|
||||||
|
"product_code": data['code'],
|
||||||
|
"unity": unit,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
@@ -16,8 +43,14 @@ class Command(BaseCommand):
|
|||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
|
|
||||||
for item in data['data']:
|
for item in data['data']:
|
||||||
Folder.objects.get_or_create(
|
folder, created = Folder.objects.get_or_create(
|
||||||
name=item['name']['uz']
|
name=item['name']['uz']
|
||||||
)
|
)
|
||||||
|
data = get_folder_products(item['id'])
|
||||||
|
for product in data['data']['data']:
|
||||||
|
unit_name = product['unit']['name']['uz']
|
||||||
|
unit, created = Unity.objects.get_or_create(value=unit_name)
|
||||||
|
print(folder.name, product['name']['uz'])
|
||||||
|
create_product(data=product, folder=folder, unit=unit)
|
||||||
|
|
||||||
self.stdout.write("Folderlar qoshildi")
|
self.stdout.write("Folderlar qoshildi")
|
||||||
@@ -2,20 +2,49 @@ import json, requests
|
|||||||
|
|
||||||
from django.core.management import BaseCommand
|
from django.core.management import BaseCommand
|
||||||
|
|
||||||
from core.apps.products.models import Folder, SubFolder
|
from core.apps.products.models import Folder, SubFolder, Product, Unity
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MTgzMzg2OSwiZXhwIjoxNzYxOTIwMjY5LCJuYmYiOjE3NjE4MzM4NjksImp0aSI6IjZSQWE1RzlyT0pGbXF1T2kiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.ACT7oxl-A2eit_bzxeal2jF_xLa0klFObNBpp1HuheE"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_product_sub_folder(folder_id):
|
def get_product_sub_folder(folder_id):
|
||||||
url = f'https://backend.app.uyqur.uz/main/category/view/subcategory?category_id={folder_id}'
|
url = f'https://backend.app.uyqur.uz/main/category/view/subcategory?category_id={folder_id}'
|
||||||
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MTgzMzg2OSwiZXhwIjoxNzYxOTIwMjY5LCJuYmYiOjE3NjE4MzM4NjksImp0aSI6IjZSQWE1RzlyT0pGbXF1T2kiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.ACT7oxl-A2eit_bzxeal2jF_xLa0klFObNBpp1HuheE"
|
|
||||||
|
|
||||||
res = requests.get(url, headers={"Authorization": f"Bearer {token}"})
|
res = requests.get(url, headers=headers)
|
||||||
|
|
||||||
if res.status_code == 200:
|
if res.status_code == 200:
|
||||||
return res.json()
|
return res.json()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_sub_folder_products(sub_folder_id):
|
||||||
|
url = f"https://backend.app.uyqur.uz/main/subcategory/view?subcategory_id={sub_folder_id}&size=500"
|
||||||
|
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response.json()
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def create_product(data, folder, unit, sub_folder):
|
||||||
|
type = {
|
||||||
|
"PRD": "PRODUCT",
|
||||||
|
"HR": "HUMAN_RESOURCE",
|
||||||
|
"MEC": "MECHANISM",
|
||||||
|
}
|
||||||
|
Product.objects.get_or_create(
|
||||||
|
name=data['name']['uz'],
|
||||||
|
defaults={
|
||||||
|
"folder": folder,
|
||||||
|
"sub_folder": sub_folder,
|
||||||
|
"type":type.get(data['resource']['symbol']['en']) if type.get(data['resource']['symbol']['en']) else "PRD",
|
||||||
|
"product_code": data['code'],
|
||||||
|
"unity": unit,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument("file_path", type=str, help="Path to JSON file")
|
parser.add_argument("file_path", type=str, help="Path to JSON file")
|
||||||
@@ -32,5 +61,13 @@ class Command(BaseCommand):
|
|||||||
sub_folders = get_product_sub_folder(folder_id)
|
sub_folders = get_product_sub_folder(folder_id)
|
||||||
if sub_folders:
|
if sub_folders:
|
||||||
for sub_folder in sub_folders['data']:
|
for sub_folder in sub_folders['data']:
|
||||||
SubFolder.objects.get_or_create(name=sub_folder['name']['uz'], folder=folder)
|
sub_folder_obj, created = SubFolder.objects.get_or_create(name=sub_folder['name']['uz'], folder=folder)
|
||||||
self.stdout.write("Folderlar qoshildi")
|
data = get_sub_folder_products(sub_folder['id'])
|
||||||
|
print(sub_folder['id'])
|
||||||
|
for product in data['data']['data']:
|
||||||
|
unit_name = product['unit']['name']['uz']
|
||||||
|
unit, created = Unity.objects.get_or_create(value=unit_name)
|
||||||
|
print(folder.name, product['name']['uz'])
|
||||||
|
create_product(data=product, folder=folder, unit=unit, sub_folder=sub_folder_obj)
|
||||||
|
|
||||||
|
self.stdout.write("Sub Folderlar qoshildi")
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
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")
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
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")
|
|
||||||
Reference in New Issue
Block a user