feat: dropdown filtra marcas y modelos por ano seleccionado
Al seleccionar ano, solo muestra marcas/modelos disponibles para ese ano. Toyota 2020: 92 modelos vs 625 sin filtro. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -248,26 +248,35 @@ NORTH_AMERICA_BRANDS = REGION_BRANDS['north-america']
|
||||
@app.route('/api/catalog/brands')
|
||||
def api_catalog_brands():
|
||||
region = request.args.get('region', 'north-america')
|
||||
year_id = request.args.get('year_id', type=int)
|
||||
session = Session()
|
||||
try:
|
||||
params = {}
|
||||
year_filter = ""
|
||||
if year_id:
|
||||
year_filter = " AND mye.year_id = :year_id"
|
||||
params['year_id'] = year_id
|
||||
|
||||
if region == 'all':
|
||||
rows = session.execute(text("""
|
||||
SELECT DISTINCT b.id_brand, b.name_brand
|
||||
FROM brands b
|
||||
JOIN models m ON m.brand_id = b.id_brand
|
||||
JOIN model_year_engine mye ON mye.model_id = m.id_model
|
||||
WHERE 1=1""" + year_filter + """
|
||||
ORDER BY b.name_brand
|
||||
""")).mappings().all()
|
||||
"""), params).mappings().all()
|
||||
else:
|
||||
brand_list = list(REGION_BRANDS.get(region, NORTH_AMERICA_BRANDS))
|
||||
params['brands'] = brand_list
|
||||
rows = session.execute(text("""
|
||||
SELECT DISTINCT b.id_brand, b.name_brand
|
||||
FROM brands b
|
||||
JOIN models m ON m.brand_id = b.id_brand
|
||||
JOIN model_year_engine mye ON mye.model_id = m.id_model
|
||||
WHERE b.name_brand = ANY(:brands)
|
||||
WHERE b.name_brand = ANY(:brands)""" + year_filter + """
|
||||
ORDER BY b.name_brand
|
||||
"""), {'brands': brand_list}).mappings().all()
|
||||
"""), params).mappings().all()
|
||||
return jsonify([{'id_brand': r['id_brand'], 'name_brand': r['name_brand']} for r in rows])
|
||||
finally:
|
||||
session.close()
|
||||
@@ -276,17 +285,23 @@ def api_catalog_brands():
|
||||
@app.route('/api/catalog/models')
|
||||
def api_catalog_models():
|
||||
brand_id = request.args.get('brand_id', type=int)
|
||||
year_id = request.args.get('year_id', type=int)
|
||||
if not brand_id:
|
||||
return jsonify({'error': 'brand_id required'}), 400
|
||||
session = Session()
|
||||
try:
|
||||
params = {'brand_id': brand_id}
|
||||
year_filter = ""
|
||||
if year_id:
|
||||
year_filter = " AND mye.year_id = :year_id"
|
||||
params['year_id'] = year_id
|
||||
rows = session.execute(text("""
|
||||
SELECT DISTINCT m.id_model, m.name_model
|
||||
FROM models m
|
||||
JOIN model_year_engine mye ON mye.model_id = m.id_model
|
||||
WHERE m.brand_id = :brand_id
|
||||
WHERE m.brand_id = :brand_id""" + year_filter + """
|
||||
ORDER BY m.name_model
|
||||
"""), {'brand_id': brand_id}).mappings().all()
|
||||
"""), params).mappings().all()
|
||||
return jsonify([{'id_model': r['id_model'], 'name_model': r['name_model']} for r in rows])
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
Reference in New Issue
Block a user