fixup! feat(DB): Added utils file for better initializing db
This commit is contained in:
1 parent
332f952394
commit
04789e7e8f
1 file changed
+67
-9
+67
-9
@@ -1,14 +1,15 @@
|
||||
import re
|
||||
import sqlite3
|
||||
from loguru import logger as log
|
||||
|
||||
def add_category(conn, name, description=None, parent_id=None):
|
||||
def add_category(conn, name, description=None, parent_id=None, image_path=None, page_number=1):
|
||||
cursor = conn.cursor()
|
||||
|
||||
try:
|
||||
cursor.execute('''
|
||||
INSERT OR IGNORE INTO categories (name, description, parent_id)
|
||||
VALUES (?, ?, ?)
|
||||
''', (name, description, parent_id))
|
||||
INSERT OR IGNORE INTO categories (name, description, parent_id, image_path, page_number)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
''', (name, description, parent_id, image_path, page_number))
|
||||
|
||||
cursor.execute('SELECT id FROM categories WHERE name = ?', (name,))
|
||||
result = cursor.fetchone()
|
||||
@@ -58,8 +59,8 @@ def parse_product_data(product_text):
|
||||
'stiffness_class': stiffness_class
|
||||
}
|
||||
|
||||
def add_product(conn, product_text, category_id):
|
||||
"""Добавление товара с парсингом параметров"""
|
||||
def add_product(conn, product_text, category_id, image_path=None, page_number=1):
|
||||
"""Добавление товара с парсингом параметров, изображением и номером страницы"""
|
||||
cursor = conn.cursor()
|
||||
|
||||
product_data = parse_product_data(product_text)
|
||||
@@ -69,8 +70,8 @@ def add_product(conn, product_text, category_id):
|
||||
INSERT INTO products (
|
||||
name, full_name, description, category_id,
|
||||
diameter, wall_thickness, length, color,
|
||||
application_type, stiffness_class
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
application_type, stiffness_class, image_path, page_number
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
''', (
|
||||
product_data['short_name'],
|
||||
product_data['full_name'],
|
||||
@@ -81,7 +82,9 @@ def add_product(conn, product_text, category_id):
|
||||
product_data['length'],
|
||||
product_data['color'],
|
||||
product_data['application_type'],
|
||||
product_data['stiffness_class']
|
||||
product_data['stiffness_class'],
|
||||
image_path,
|
||||
page_number
|
||||
))
|
||||
|
||||
product_id = cursor.lastrowid
|
||||
@@ -111,3 +114,58 @@ def add_product(conn, product_text, category_id):
|
||||
except sqlite3.Error as e:
|
||||
log.error(f"Ошибка при добавлении товара: {e}")
|
||||
return None
|
||||
|
||||
def get_categories_by_page(conn, page_number=1, items_per_page=9):
|
||||
"""Получить категории для определенной страницы"""
|
||||
cursor = conn.cursor()
|
||||
offset = (page_number - 1) * items_per_page
|
||||
|
||||
cursor.execute('''
|
||||
SELECT * FROM categories
|
||||
WHERE page_number = ?
|
||||
ORDER BY id
|
||||
LIMIT ? OFFSET ?
|
||||
''', (page_number, items_per_page, offset))
|
||||
|
||||
return cursor.fetchall()
|
||||
|
||||
def get_products_by_page(conn, page_number=1, items_per_page=9, category_id=None):
|
||||
"""Получить товары для определенной страницы"""
|
||||
cursor = conn.cursor()
|
||||
offset = (page_number - 1) * items_per_page
|
||||
|
||||
if category_id:
|
||||
cursor.execute('''
|
||||
SELECT * FROM products
|
||||
WHERE page_number = ? AND category_id = ?
|
||||
ORDER BY id
|
||||
LIMIT ? OFFSET ?
|
||||
''', (page_number, category_id, items_per_page, offset))
|
||||
else:
|
||||
cursor.execute('''
|
||||
SELECT * FROM products
|
||||
WHERE page_number = ?
|
||||
ORDER BY id
|
||||
LIMIT ? OFFSET ?
|
||||
''', (page_number, items_per_page, offset))
|
||||
|
||||
return cursor.fetchall()
|
||||
|
||||
def get_total_categories_pages(conn, items_per_page=9):
|
||||
"""Получить общее количество страниц категорий"""
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT COUNT(*) FROM categories')
|
||||
total_categories = cursor.fetchone()[0]
|
||||
return (total_categories + items_per_page - 1) // items_per_page
|
||||
|
||||
def get_total_products_pages(conn, items_per_page=9, category_id=None):
|
||||
"""Получить общее количество страниц товаров"""
|
||||
cursor = conn.cursor()
|
||||
|
||||
if category_id:
|
||||
cursor.execute('SELECT COUNT(*) FROM products WHERE category_id = ?', (category_id,))
|
||||
else:
|
||||
cursor.execute('SELECT COUNT(*) FROM products')
|
||||
|
||||
total_products = cursor.fetchone()[0]
|
||||
return (total_products + items_per_page - 1) // items_per_page
|
||||
Reference in new issue
Block a user