nowa funckcja zmiana kolejnosci produktów
This commit is contained in:
37
app.py
37
app.py
@@ -123,6 +123,7 @@ class Item(db.Model):
|
||||
note = db.Column(db.Text, nullable=True)
|
||||
not_purchased = db.Column(db.Boolean, default=False)
|
||||
not_purchased_reason = db.Column(db.Text, nullable=True)
|
||||
position = db.Column(db.Integer, default=0)
|
||||
|
||||
|
||||
class SuggestedProduct(db.Model):
|
||||
@@ -211,7 +212,7 @@ def allowed_file(filename):
|
||||
|
||||
def get_list_details(list_id):
|
||||
shopping_list = ShoppingList.query.get_or_404(list_id)
|
||||
items = Item.query.filter_by(list_id=list_id).all()
|
||||
items = Item.query.filter_by(list_id=list_id).order_by(Item.position.asc()).all()
|
||||
receipt_pattern = f"list_{list_id}"
|
||||
all_files = os.listdir(app.config["UPLOAD_FOLDER"])
|
||||
receipt_files = [f for f in all_files if receipt_pattern in f]
|
||||
@@ -220,6 +221,7 @@ def get_list_details(list_id):
|
||||
return shopping_list, items, receipt_files, expenses, total_expense
|
||||
|
||||
|
||||
|
||||
def generate_share_token(length=8):
|
||||
"""Generuje token do udostępniania. Parametr `length` to liczba znaków (domyślnie 4)."""
|
||||
return secrets.token_hex(length // 2)
|
||||
@@ -265,7 +267,7 @@ def admin_required(f):
|
||||
|
||||
|
||||
def get_progress(list_id):
|
||||
items = Item.query.filter_by(list_id=list_id).all()
|
||||
items = Item.query.filter_by(list_id=list_id).order_by(Item.position.asc()).all()
|
||||
total_count = len(items)
|
||||
purchased_count = len([i for i in items if i.purchased])
|
||||
percent = (purchased_count / total_count * 100) if total_count > 0 else 0
|
||||
@@ -980,6 +982,27 @@ def uploaded_file(filename):
|
||||
return response
|
||||
|
||||
|
||||
@app.route('/reorder_items', methods=['POST'])
|
||||
@login_required
|
||||
def reorder_items():
|
||||
data = request.get_json()
|
||||
list_id = data.get('list_id')
|
||||
order = data.get('order')
|
||||
|
||||
for index, item_id in enumerate(order):
|
||||
item = db.session.get(Item, item_id)
|
||||
if item and item.list_id == list_id:
|
||||
item.position = index
|
||||
db.session.commit()
|
||||
|
||||
socketio.emit("items_reordered", {
|
||||
"list_id": list_id,
|
||||
"order": order
|
||||
}, to=str(list_id))
|
||||
|
||||
return jsonify(success=True)
|
||||
|
||||
|
||||
@app.route("/admin")
|
||||
@login_required
|
||||
@admin_required
|
||||
@@ -1737,7 +1760,6 @@ def handle_add_item(data):
|
||||
except:
|
||||
quantity = 1
|
||||
|
||||
# Szukamy istniejącego itemu w tej liście (ignorując wielkość liter)
|
||||
existing_item = Item.query.filter(
|
||||
Item.list_id == list_id,
|
||||
func.lower(Item.name) == name.lower(),
|
||||
@@ -1758,10 +1780,15 @@ def handle_add_item(data):
|
||||
to=str(list_id),
|
||||
)
|
||||
else:
|
||||
max_position = db.session.query(func.max(Item.position)).filter_by(list_id=list_id).scalar()
|
||||
if max_position is None:
|
||||
max_position = 0
|
||||
|
||||
new_item = Item(
|
||||
list_id=list_id,
|
||||
name=name,
|
||||
quantity=quantity,
|
||||
position=max_position + 1,
|
||||
added_by=current_user.id if current_user.is_authenticated else None,
|
||||
)
|
||||
db.session.add(new_item)
|
||||
@@ -1788,7 +1815,6 @@ def handle_add_item(data):
|
||||
include_self=True,
|
||||
)
|
||||
|
||||
# Aktualizacja postępu
|
||||
purchased_count, total_count, percent = get_progress(list_id)
|
||||
|
||||
emit(
|
||||
@@ -1802,6 +1828,7 @@ def handle_add_item(data):
|
||||
)
|
||||
|
||||
|
||||
|
||||
@socketio.on("check_item")
|
||||
def handle_check_item(data):
|
||||
# item = Item.query.get(data["item_id"])
|
||||
@@ -1855,7 +1882,7 @@ def handle_uncheck_item(data):
|
||||
@socketio.on("request_full_list")
|
||||
def handle_request_full_list(data):
|
||||
list_id = data["list_id"]
|
||||
items = Item.query.filter_by(list_id=list_id).all()
|
||||
items = Item.query.filter_by(list_id=list_id).order_by(Item.position.asc()).all()
|
||||
|
||||
items_data = []
|
||||
for item in items:
|
||||
|
Reference in New Issue
Block a user