ux i funkcja masowego dodwania produktu

This commit is contained in:
Mateusz Gruszczyński
2025-07-09 10:22:35 +02:00
parent 1dfbdb1eea
commit 1561ea1ab6
4 changed files with 113 additions and 50 deletions

41
app.py
View File

@@ -98,7 +98,7 @@ class Item(db.Model):
class SuggestedProduct(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150), unique=True, nullable=False)
usage_count = db.Column(db.Integer, default=0)
class Expense(db.Model):
id = db.Column(db.Integer, primary_key=True)
list_id = db.Column(db.Integer, db.ForeignKey('shopping_list.id'))
@@ -561,6 +561,39 @@ def suggest_products():
suggestions = SuggestedProduct.query.filter(SuggestedProduct.name.ilike(f'%{query}%')).limit(5).all()
return {'suggestions': [s.name for s in suggestions]}
@app.route('/all_products')
def all_products():
query = request.args.get('q', '')
top_products_query = SuggestedProduct.query
if query:
top_products_query = top_products_query.filter(SuggestedProduct.name.ilike(f'%{query}%'))
top_products = (
top_products_query
.order_by(SuggestedProduct.usage_count.desc(), SuggestedProduct.name.asc())
.limit(20)
.all()
)
top_names = [s.name for s in top_products]
rest_query = SuggestedProduct.query
if query:
rest_query = rest_query.filter(SuggestedProduct.name.ilike(f'%{query}%'))
if top_names:
rest_query = rest_query.filter(~SuggestedProduct.name.in_(top_names))
rest_products = (
rest_query
.order_by(SuggestedProduct.name.asc())
.limit(200)
.all()
)
all_names = top_names + [s.name for s in rest_products]
return {'allproducts': all_names}
@app.route('/upload_receipt/<int:list_id>', methods=['POST'])
def upload_receipt(list_id):
if 'receipt' not in request.files:
@@ -596,12 +629,6 @@ def uploaded_file(filename):
response.headers['Content-Type'] = mime
return response
@app.route('/all_products')
@login_required
def all_products():
suggestions = SuggestedProduct.query.order_by(SuggestedProduct.name).all()
return jsonify([s.name for s in suggestions])
@app.route('/admin')
@login_required
@admin_required