diff --git a/app.py b/app.py index 70520a4..c7c8055 100644 --- a/app.py +++ b/app.py @@ -1159,30 +1159,19 @@ def save_pdf_as_webp(file, path): def get_active_months_query(visible_lists_query=None): if db.engine.name in ("sqlite",): - month_col = func.strftime("%Y-%m", ShoppingList.created_at) + def month_expr(col): return func.strftime("%Y-%m", col) elif db.engine.name in ("mysql", "mariadb"): - month_col = func.date_format(ShoppingList.created_at, "%Y-%m") - else: #PGSQL - month_col = func.to_char(ShoppingList.created_at, "YYYY-MM") - - month_col = month_col.label("month") + def month_expr(col): return func.date_format(col, "%Y-%m") + else: # PostgreSQL + def month_expr(col): return func.to_char(col, "YYYY-MM") if visible_lists_query is not None: - inner = ( - db.session.query(month_col) - .select_from(visible_lists_query.subquery()) - .filter(month_col.isnot(None)) - .distinct() - .subquery() - ) + s = visible_lists_query.subquery() + month_sel = month_expr(s.c.created_at).label("month") + inner = db.session.query(month_sel).filter(month_sel.isnot(None)).distinct().subquery() else: - inner = ( - db.session.query(month_col) - .select_from(ShoppingList) - .filter(ShoppingList.created_at.isnot(None)) - .distinct() - .subquery() - ) + month_sel = month_expr(ShoppingList.created_at).label("month") + inner = db.session.query(month_sel).filter(ShoppingList.created_at.isnot(None)).distinct().subquery() rows = db.session.query(inner.c.month).order_by(inner.c.month).all() return [r.month for r in rows]