zmiany w /all_products, laczenie item i sugested
This commit is contained in:
55
app.py
55
app.py
@@ -45,7 +45,7 @@ from flask_socketio import SocketIO, emit, join_room
|
||||
from config import Config
|
||||
from PIL import Image, ExifTags, ImageFilter, ImageOps
|
||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||
from sqlalchemy import func, extract, inspect, or_, case, text, and_
|
||||
from sqlalchemy import func, extract, inspect, or_, case, text, and_, literal
|
||||
from sqlalchemy.orm import joinedload, load_only, aliased
|
||||
from collections import defaultdict, deque
|
||||
from functools import wraps
|
||||
@@ -2008,7 +2008,7 @@ def suggest_products():
|
||||
return {"suggestions": [s.name for s in suggestions]}
|
||||
|
||||
|
||||
@app.route("/all_products")
|
||||
""" @app.route("/all_products")
|
||||
def all_products():
|
||||
sort = request.args.get("sort", "popularity")
|
||||
limit = request.args.get("limit", type=int) or 100
|
||||
@@ -2061,8 +2061,59 @@ def all_products():
|
||||
|
||||
return jsonify(
|
||||
{"products": products, "total_count": (total_count if products else len(products))}
|
||||
) """
|
||||
|
||||
@app.route("/all_products")
|
||||
def all_products():
|
||||
sort = request.args.get("sort", "popularity")
|
||||
limit = request.args.get("limit", type=int) or 100
|
||||
offset = request.args.get("offset", type=int) or 0
|
||||
|
||||
# Produkty z Item z faktyczną popularnością
|
||||
products_from_items = (
|
||||
db.session.query(
|
||||
func.lower(func.trim(Item.name)).label("normalized_name"),
|
||||
func.min(Item.name).label("display_name"),
|
||||
func.count(func.distinct(Item.list_id)).label("count"),
|
||||
)
|
||||
.group_by("normalized_name")
|
||||
)
|
||||
|
||||
products_from_suggested = (
|
||||
db.session.query(
|
||||
func.lower(func.trim(SuggestedProduct.name)).label("normalized_name"),
|
||||
func.min(SuggestedProduct.name).label("display_name"),
|
||||
db.literal(1).label("count"),
|
||||
)
|
||||
.filter(~func.lower(func.trim(SuggestedProduct.name)).in_(
|
||||
db.session.query(func.lower(func.trim(Item.name)))
|
||||
))
|
||||
.group_by("normalized_name")
|
||||
)
|
||||
|
||||
union_q = products_from_items.union_all(products_from_suggested).subquery()
|
||||
|
||||
final_q = (
|
||||
db.session.query(
|
||||
union_q.c.normalized_name,
|
||||
func.min(union_q.c.display_name).label("display_name"),
|
||||
func.sum(union_q.c.count).label("count"),
|
||||
)
|
||||
.group_by(union_q.c.normalized_name)
|
||||
)
|
||||
|
||||
if sort == "alphabetical":
|
||||
final_q = final_q.order_by(func.lower(final_q.c.display_name).asc())
|
||||
else:
|
||||
final_q = final_q.order_by(func.sum(union_q.c.count).desc(), func.lower(final_q.c.display_name).asc())
|
||||
|
||||
total_count = final_q.count()
|
||||
products = final_q.offset(offset).limit(limit).all()
|
||||
|
||||
out = [{"name": row.display_name, "count": row.count} for row in products]
|
||||
|
||||
return jsonify({"products": out, "total_count": total_count})
|
||||
|
||||
|
||||
|
||||
@app.route("/upload_receipt/<int:list_id>", methods=["POST"])
|
||||
|
Reference in New Issue
Block a user