fix(db): Added auto generating slug for products
This commit is contained in:
1 parent
0c15ff3059
commit
098a6aa38a
4 files changed
+176
-37
No files matched your search
@@ -101,6 +101,7 @@ def get_products_with_filters(
|
||||
items.append({
|
||||
"id": row['id'],
|
||||
"name": row['name'],
|
||||
"slug": row['slug'],
|
||||
"description": row['description'],
|
||||
"categoryId": row['category_id'],
|
||||
"images": [row['image_path']] if row['image_path'] else ["/images/products/default.jpg"],
|
||||
@@ -144,7 +145,7 @@ def get_product_by_id(product_id: int, db_name: str = 'pipe_store.db') -> Option
|
||||
SELECT p.id, p.name, p.description,
|
||||
p.diameter, p.wall_thickness, p.length, p.color,
|
||||
p.application_type, p.stiffness_class, p.image_path,
|
||||
p.created_at, p.category_id
|
||||
p.slug, p.created_at, p.category_id
|
||||
FROM products p
|
||||
WHERE p.id = ?
|
||||
""", (product_id,))
|
||||
@@ -192,6 +193,7 @@ def get_product_by_id(product_id: int, db_name: str = 'pipe_store.db') -> Option
|
||||
return {
|
||||
"id": row['id'],
|
||||
"name": row['name'],
|
||||
"slug": row['slug'],
|
||||
"description": full_description,
|
||||
"price": None,
|
||||
"categoryId": row['category_id'],
|
||||
@@ -207,6 +209,82 @@ def get_product_by_id(product_id: int, db_name: str = 'pipe_store.db') -> Option
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def get_product_by_slug(slug: str, db_name: str = 'pipe_store.db') -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Получить товар по slug с полной информацией
|
||||
"""
|
||||
conn = sqlite3.connect(db_name)
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
|
||||
try:
|
||||
cursor.execute("""
|
||||
SELECT p.id, p.name, p.description,
|
||||
p.diameter, p.wall_thickness, p.length, p.color,
|
||||
p.application_type, p.stiffness_class, p.image_path,
|
||||
p.slug, p.created_at, p.category_id
|
||||
FROM products p
|
||||
WHERE p.slug = ?
|
||||
""", (slug,))
|
||||
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
|
||||
product_id = row['id']
|
||||
|
||||
images = get_product_images(product_id, row['image_path'], conn)
|
||||
|
||||
cursor.execute("""
|
||||
SELECT filter_key, value
|
||||
FROM product_filters
|
||||
WHERE product_id = ?
|
||||
""", (product_id,))
|
||||
|
||||
extra_attributes = {}
|
||||
for filter_row in cursor.fetchall():
|
||||
extra_attributes[filter_row['filter_key']] = filter_row['value']
|
||||
|
||||
full_description = generate_full_description(row)
|
||||
|
||||
size = None
|
||||
if row['diameter'] and row['wall_thickness'] and row['length'] and row['length'] > 0:
|
||||
size = f"{row['diameter']}x{row['wall_thickness']}x{row['length']}"
|
||||
amount = get_pack_amount(product_id, row['diameter'])
|
||||
|
||||
attributes = {
|
||||
"color": row['color'],
|
||||
"amount": amount,
|
||||
"size": size,
|
||||
"diameter": str(row['diameter']) if row['diameter'] else None,
|
||||
"wall_thickness": str(row['wall_thickness']) if row['wall_thickness'] else None,
|
||||
"length": str(row['length']) if row['length'] and row['length'] > 0 else None,
|
||||
"stiffness_class": row['stiffness_class'],
|
||||
"application_type": row['application_type'],
|
||||
}
|
||||
|
||||
attributes.update(extra_attributes)
|
||||
|
||||
return {
|
||||
"id": row['id'],
|
||||
"name": row['name'],
|
||||
"slug": row['slug'],
|
||||
"description": full_description,
|
||||
"price": None,
|
||||
"categoryId": row['category_id'],
|
||||
"images": images,
|
||||
"attributes": attributes,
|
||||
"inStock": check_stock(product_id, conn),
|
||||
"created_at": row['created_at']
|
||||
}
|
||||
|
||||
except sqlite3.Error as e:
|
||||
log.error(f"Ошибка при получении товара по slug {slug}: {e}")
|
||||
return None
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_product_images(product_id: int, main_image: Optional[str], conn: sqlite3.Connection) -> List[str]:
|
||||
"""Получить список изображений товара"""
|
||||
images = []
|
||||
@@ -278,6 +356,7 @@ def get_product_list(page=1, limit=100, category_id=None):
|
||||
products.append({
|
||||
'id': row['id'],
|
||||
'name': row['name'],
|
||||
'slug': row['slug'],
|
||||
'description': row['description']
|
||||
})
|
||||
return products
|
||||
|
||||
Reference in new issue
Block a user