fix(db): Added auto generating slug for products

This commit is contained in:
Black_Raven committed 2026-07-26 20:16:57 +03:00
1 parent 0c15ff3059
commit 098a6aa38a
4 files changed
+176 -37

No files matched your search

+23 -4
View File
@@ -1,6 +1,7 @@
import re
import sqlite3
from loguru import logger as log
from utils.slugify import slugify
def add_category(conn, name, description=None, parent_id=None, image_path=None):
cursor = conn.cursor()
@@ -76,7 +77,7 @@ def parse_product_data(product_text):
def add_product(conn, product_text, category_id, image_path=None,
diameter=None, wall_thickness=None, length=None,
color=None, application_type=None, stiffness_class=None,
fitting_type=None, angle=None):
fitting_type=None, angle=None, slug=None, artikul=None):
"""
Добавление товара с возможностью явного указания параметров
Если параметры не указаны, они парсятся из product_text
@@ -113,13 +114,30 @@ def add_product(conn, product_text, category_id, image_path=None,
description = ", ".join(description_parts)
if fitting_type:
slug_source = product_text
else:
slug_parts = [product_text]
if stiffness_class:
slug_parts.append(stiffness_class)
if diameter:
slug_parts.append(f"d{diameter}")
if wall_thickness:
slug_parts.append(f"stenka{int(wall_thickness)}")
if length and length > 0:
slug_parts.append(f"l{length}")
slug_source = " ".join(slug_parts)
slug_value = slug or slugify(slug_source)
if artikul:
slug_value = f"{slug_value}-{artikul.lower()}"
try:
cursor.execute('''
INSERT INTO products (
name, description, category_id,
diameter, wall_thickness, length, color,
application_type, stiffness_class, image_path
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
application_type, stiffness_class, image_path, slug
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
product_text,
description,
@@ -130,7 +148,8 @@ def add_product(conn, product_text, category_id, image_path=None,
color,
application_type,
stiffness_class,
image_path
image_path,
slug_value
))
product_id = cursor.lastrowid