Merge pull request 'feature/product_sheet_load' (#5) from feature/product_sheet_load into main
Reviewed-on: #5pull/6/head^2
commit
2ec6d388b8
|
@ -143,3 +143,4 @@ artel/static/
|
|||
|
||||
# media
|
||||
artel/media/*
|
||||
artel/store/data/*
|
||||
|
|
|
@ -73,9 +73,14 @@ class ProductTemplateConfigForm(forms.Form):
|
|||
def _create_dynamic_fields(self, template: ProductTemplate):
|
||||
category_params = template.category.category_params.all()
|
||||
for param in category_params:
|
||||
queryset = ProductCategoryParamValue.objects.filter(param=param)
|
||||
if queryset.count() >= 4:
|
||||
widget = forms.Select(attrs={"class": "form-select"})
|
||||
else:
|
||||
widget = ButtonToggleSelect(attrs={"class": "btn-group btn-group-toggle"})
|
||||
self.fields[param.key] = forms.ModelChoiceField(
|
||||
queryset=ProductCategoryParamValue.objects.filter(param=param),
|
||||
widget=ButtonToggleSelect(attrs={"class": "btn-group btn-group-toggle"}),
|
||||
queryset=queryset,
|
||||
widget=widget,
|
||||
)
|
||||
|
||||
def __init__(
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -133,7 +133,7 @@ class ProductCategoryParamValue(ClusterableModel):
|
|||
return
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.param.key}: {self.value}"
|
||||
return str(self.value)
|
||||
|
||||
|
||||
class ProductTemplate(ClusterableModel):
|
||||
|
|
|
@ -10,19 +10,3 @@
|
|||
{% endfor %}
|
||||
</div>
|
||||
{% endwith %}
|
||||
<!--
|
||||
{% with id=widget.attrs.id %}
|
||||
<div{% if id %} id="{{ id }}"{% endif %}
|
||||
{% if widget.attrs.class %} class="{{ widget.attrs.class }}"{% endif %}
|
||||
>
|
||||
{% for group, options, index in widget.optgroups %}
|
||||
{% if group %}
|
||||
<div><label>{{ group }}</label>{% endif %}{% for option in options %}<div>
|
||||
{% include option.template_name with widget=option %}</div>{% endfor %}{% if group %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endwith %}
|
||||
|
||||
-->
|
||||
|
|
Ładowanie…
Reference in New Issue