fix(): fix models for get flags
This commit is contained in:
1 parent
77ebb0e3a8
commit
81ff26fdb8
4 files changed
+64
-37
No files matched your search
+35
-26
@@ -1,6 +1,7 @@
|
||||
from flask import jsonify, request
|
||||
from db.handlers.product_handler import get_products_with_filters, get_product_by_id
|
||||
from flask import Blueprint
|
||||
import re
|
||||
|
||||
api_product = Blueprint('api_product', __name__, url_prefix='/products')
|
||||
|
||||
@@ -15,6 +16,23 @@ def clean_filter_value(value):
|
||||
return cleaned
|
||||
return value
|
||||
|
||||
def parse_size(size_str):
|
||||
"""
|
||||
Парсит строку размера вида "110х2,2х500" и возвращает словарь с ключами diameter, wall_thickness, length.
|
||||
"""
|
||||
if not size_str:
|
||||
return {}
|
||||
# Заменяем запятую на точку для толщины
|
||||
normalized = size_str.replace(',', '.')
|
||||
match = re.match(r'(\d+)х([\d\.]+)х(\d+)', normalized)
|
||||
if match:
|
||||
return {
|
||||
'diameter': int(match.group(1)),
|
||||
'wall_thickness': float(match.group(2)),
|
||||
'length': int(match.group(3))
|
||||
}
|
||||
return {}
|
||||
|
||||
@api_product.route('/', methods=['GET'], strict_slashes=False)
|
||||
def get_products():
|
||||
try:
|
||||
@@ -30,42 +48,33 @@ def get_products():
|
||||
if limit < 1 or limit > 100:
|
||||
limit = 9
|
||||
|
||||
# ✅ ИСПРАВЛЕННЫЙ МАППИНГ
|
||||
filter_mapping = {
|
||||
'attr_1': 'diameter',
|
||||
'attr_2': 'length',
|
||||
'attr_3': 'wall_thickness', # (опционально) тоже толщина
|
||||
'attr_4': 'color',
|
||||
'attr_5': 'stiffness_class', # SN2, SN4
|
||||
'attr_6': 'wall_thickness', # <- теперь attr_6 ищет толщину стенки
|
||||
'attr_7': 'angle',
|
||||
'attr_8': 'fitting_type',
|
||||
}
|
||||
|
||||
filters = {}
|
||||
excluded_params = {'categoryId', 'page', 'limit'}
|
||||
|
||||
# Обрабатываем все параметры запроса
|
||||
for key, value in request.args.items():
|
||||
if key in excluded_params:
|
||||
continue
|
||||
|
||||
real_key = filter_mapping.get(key, key)
|
||||
cleaned = clean_filter_value(value)
|
||||
if cleaned is None:
|
||||
continue
|
||||
|
||||
if ',' in value:
|
||||
parts = value.split(',')
|
||||
cleaned_parts = []
|
||||
for part in parts:
|
||||
cleaned = clean_filter_value(part)
|
||||
if cleaned is not None:
|
||||
cleaned_parts.append(cleaned)
|
||||
if cleaned_parts:
|
||||
filters[real_key] = cleaned_parts
|
||||
# Специальная обработка параметра size
|
||||
if key == 'size':
|
||||
size_parts = parse_size(cleaned)
|
||||
for size_key, size_val in size_parts.items():
|
||||
# Добавляем как одиночные фильтры (без списка)
|
||||
filters[size_key] = size_val
|
||||
else:
|
||||
cleaned = clean_filter_value(value)
|
||||
if cleaned is not None:
|
||||
filters[real_key] = cleaned
|
||||
# Остальные фильтры (color, stiffness_class, и т.д.)
|
||||
if ',' in cleaned:
|
||||
parts = [p.strip() for p in cleaned.split(',') if p.strip()]
|
||||
if parts:
|
||||
filters[key] = parts
|
||||
else:
|
||||
filters[key] = cleaned
|
||||
|
||||
# Отладочный вывод (убедитесь, что здесь видно правильный ключ)
|
||||
print("=== FINAL FILTERS ===", filters)
|
||||
|
||||
result = get_products_with_filters(
|
||||
|
||||
Reference in new issue
Block a user