product load finally works
rodzic
7c8ed587e3
commit
d2fb27a2c3
|
@ -143,3 +143,4 @@ artel/static/
|
|||
|
||||
# media
|
||||
artel/media/*
|
||||
artel/store/data/*
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import logging
|
||||
import time
|
||||
import requests
|
||||
import pandas as pd
|
||||
|
||||
from django.core import files
|
||||
|
||||
from django.core.files.base import ContentFile
|
||||
from store.models import (
|
||||
ProductTemplate,
|
||||
ProductCategoryParamValue,
|
||||
ProductCategoryParam,
|
||||
Product,
|
||||
ProductImage
|
||||
)
|
||||
|
@ -29,28 +30,37 @@ class TemplateLoader(BaseLoader):
|
|||
|
||||
class ProductLoader(BaseLoader):
|
||||
|
||||
def _get_images(self, row) -> list[files.ContentFile]:
|
||||
urls = row["images"]
|
||||
def _clear(self):
|
||||
Product.objects.all().delete()
|
||||
|
||||
def __init__(self, path, param_names, clear=False):
|
||||
super().__init__(path)
|
||||
self.param_names = param_names
|
||||
if clear:
|
||||
self._clear()
|
||||
|
||||
def _get_images(self, row) -> list[ContentFile]:
|
||||
url = row["images"]
|
||||
images = []
|
||||
for url in urls:
|
||||
response = requests.get(url, stream=True)
|
||||
if response.status_code == 200:
|
||||
data = response.raw
|
||||
file_name = url.split("/")[-1]
|
||||
image = files.ContentFile(data, name=file_name)
|
||||
response = requests.get(url+"/download", stream=True)
|
||||
print(response.status_code)
|
||||
if response.status_code == 200:
|
||||
data = response.content
|
||||
image = ContentFile(data, name=row["template"])
|
||||
images.append(image)
|
||||
return images
|
||||
|
||||
def _process_row(self, row):
|
||||
template = ProductTemplate.objects.get(code=row["template"])
|
||||
price = float(row["price"])
|
||||
price = float(row["price"].strip("zł").replace(",", "."))
|
||||
name = row["name"]
|
||||
available = bool(row["available"])
|
||||
params = []
|
||||
for param in row["params"]:
|
||||
key, value = param
|
||||
param = ProductCategoryParamValue.objects.get(param__key=key, value=value)
|
||||
params.append(param)
|
||||
for key in self.param_names:
|
||||
value = row[key]
|
||||
param, _ = ProductCategoryParam.objects.get_or_create(key=key, category=template.category)
|
||||
param_value, _ = ProductCategoryParamValue.objects.get_or_create(param=param, value=value)
|
||||
params.append(param_value)
|
||||
product = Product.objects.get_or_create_by_params(template=template, params=params)
|
||||
product.price = price
|
||||
product.name = name
|
||||
|
@ -66,6 +76,7 @@ class ProductLoader(BaseLoader):
|
|||
data = self.load_data()
|
||||
products = []
|
||||
for _, row in data.iterrows():
|
||||
time.sleep(5)
|
||||
try:
|
||||
product = self._process_row(row)
|
||||
except Exception as e:
|
||||
|
|
|
@ -9,5 +9,5 @@ class Command(BaseCommand):
|
|||
help = "Load products from csv file"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
loader = ProductLoader(settings.PRODUCTS_CSV_PATH)
|
||||
loader = ProductLoader(settings.PRODUCTS_CSV_PATH, ["mocowanie", "format", "wykonanie"], True)
|
||||
loader.process()
|
||||
|
|
Ładowanie…
Reference in New Issue