2022-10-22 10:55:10 +03:00
|
|
|
|
import re
|
2022-10-21 23:34:41 +03:00
|
|
|
|
from ast import literal_eval
|
|
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
2022-10-22 00:09:26 +03:00
|
|
|
|
from search.models import (
|
|
|
|
|
Product,
|
|
|
|
|
Category,
|
|
|
|
|
UnitCharacteristic,
|
|
|
|
|
ProductUnitCharacteristic,
|
|
|
|
|
ProductCharacteristic,
|
|
|
|
|
Characteristic,
|
|
|
|
|
)
|
2022-10-21 23:34:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load():
|
|
|
|
|
with open("data.json", "r", encoding="utf-16") as f:
|
|
|
|
|
data = literal_eval(f.read())
|
|
|
|
|
|
|
|
|
|
for el in data:
|
|
|
|
|
Product.objects.get_or_create(id=el)
|
|
|
|
|
print(el["characteristic"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_excel():
|
2022-10-22 22:29:49 +03:00
|
|
|
|
df = pd.read_excel("media/data.xlsx", sheet_name="Запрос1")
|
2022-10-21 23:34:41 +03:00
|
|
|
|
for row in range(df.shape[0]):
|
2022-10-22 03:09:39 +03:00
|
|
|
|
try:
|
|
|
|
|
print(df.iat[row, 0], df.iat[row, 1], df.iat[row, 2])
|
|
|
|
|
product = Product(id=df.iat[row, 0])
|
|
|
|
|
product.name = df.iat[row, 1]
|
|
|
|
|
category = Category.objects.get_or_create(name=df.iat[row, 2])[0]
|
|
|
|
|
product.category = category
|
|
|
|
|
product.save()
|
|
|
|
|
if df.iat[row, 4]:
|
|
|
|
|
for cat in literal_eval(df.iat[row, 4]):
|
2022-10-22 22:29:49 +03:00
|
|
|
|
if "Value" in cat:
|
2022-10-22 03:09:39 +03:00
|
|
|
|
if "Unit" in cat:
|
2022-10-22 22:29:49 +03:00
|
|
|
|
pr = ProductUnitCharacteristic.objects.get_or_create(
|
2022-10-22 03:09:39 +03:00
|
|
|
|
characteristic=UnitCharacteristic.objects.get_or_create(
|
|
|
|
|
name=cat["Name"],
|
|
|
|
|
value=cat["Value"],
|
|
|
|
|
unit=cat["Unit"],
|
|
|
|
|
)[0],
|
|
|
|
|
product=product,
|
2022-10-22 22:29:49 +03:00
|
|
|
|
)[0]
|
|
|
|
|
nums = re.findall(
|
|
|
|
|
"[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?",
|
|
|
|
|
cat["Value"],
|
2022-10-22 03:09:39 +03:00
|
|
|
|
)
|
2022-10-22 22:29:49 +03:00
|
|
|
|
if len(nums) == 1:
|
|
|
|
|
pr.numeric_value_min = int(
|
|
|
|
|
float(nums[0].replace(",", "."))
|
|
|
|
|
)
|
|
|
|
|
pr.numeric_value_max = int(
|
|
|
|
|
float(nums[0].replace(",", "."))
|
|
|
|
|
)
|
|
|
|
|
pr.save()
|
|
|
|
|
elif len(nums):
|
|
|
|
|
nums = [int(float(x.replace(",", "."))) for x in nums]
|
|
|
|
|
min_num = min(nums)
|
|
|
|
|
max_num = max(nums)
|
|
|
|
|
pr.numeric_value_min = min_num
|
|
|
|
|
pr.numeric_value_max = max_num
|
|
|
|
|
pr.save()
|
2022-10-22 03:09:39 +03:00
|
|
|
|
else:
|
|
|
|
|
ProductCharacteristic.objects.get_or_create(
|
|
|
|
|
characteristic=Characteristic.objects.get_or_create(
|
|
|
|
|
name=cat["Name"], value=cat["Value"]
|
|
|
|
|
)[0],
|
|
|
|
|
product=product,
|
|
|
|
|
)
|
|
|
|
|
except BaseException:
|
2022-10-22 22:29:49 +03:00
|
|
|
|
try:
|
|
|
|
|
product.delete()
|
|
|
|
|
except Exception:
|
|
|
|
|
continue
|
2022-10-22 10:55:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_unit_character():
|
|
|
|
|
for el in UnitCharacteristic.objects.all():
|
2022-10-22 16:14:43 +03:00
|
|
|
|
nums = re.findall(
|
|
|
|
|
"[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", el.value
|
|
|
|
|
)
|
|
|
|
|
if len(nums) == 1:
|
2022-10-22 10:55:10 +03:00
|
|
|
|
try:
|
2022-10-22 16:14:43 +03:00
|
|
|
|
el.numeric_value_min = int(float(nums[0].replace(",", ".")))
|
|
|
|
|
el.numeric_value_max = int(float(nums[0].replace(",", ".")))
|
|
|
|
|
el.save()
|
|
|
|
|
except ValueError:
|
|
|
|
|
el.delete()
|
|
|
|
|
elif len(nums):
|
|
|
|
|
try:
|
|
|
|
|
nums = [int(float(x.replace(",", "."))) for x in nums]
|
|
|
|
|
min_num = min(nums)
|
|
|
|
|
max_num = max(nums)
|
|
|
|
|
el.numeric_value_min = min_num
|
|
|
|
|
el.numeric_value_max = max_num
|
2022-10-22 10:55:10 +03:00
|
|
|
|
el.save()
|
|
|
|
|
except ValueError:
|
|
|
|
|
el.delete()
|