contentmatcher/menu.py

79 lines
3.4 KiB
Python
Raw Normal View History

2022-06-04 21:58:05 +02:00
from flask import (
2022-06-23 22:23:39 +02:00
Blueprint, flash, redirect, render_template, request, session, url_for
2022-06-04 21:58:05 +02:00
)
2022-06-14 14:47:12 +02:00
from create_db import Deck, Card, get_session
from deck import get_deck, probabilistic_deck_generator
from settings import get_settings
2022-06-14 02:26:38 +02:00
from auth import login
2022-06-04 21:58:05 +02:00
bp = Blueprint('menu', __name__, url_prefix='/menu')
@bp.route("/", methods=("GET", "POST"))
def index():
2022-06-15 02:06:59 +02:00
deck_status = []
2022-06-14 14:47:12 +02:00
dbsession = get_session()
2022-06-14 02:26:38 +02:00
if 'user_id' not in session:
return login()
2022-06-04 21:58:05 +02:00
if 'user_id' in session:
user_id = session['user_id']
username = session['username']
print(username)
2022-06-15 02:06:59 +02:00
old_deck = dbsession.query(Deck).filter(Deck.completed == 0, Deck.owner_id == user_id).all()
if old_deck != []:
deck_status = "old"
if request.method == "POST":
2022-06-04 21:58:05 +02:00
action = request.form.get("menu", False) #internetna rešitev, nevem kako, ampak dela, tj. dobi info iz meni buttonov
print(action)
if action == "new_session":
2022-06-15 02:06:59 +02:00
old_deck = dbsession.query(Deck).filter(Deck.completed == 0, Deck.owner_id == user_id).all()
if old_deck != []:
return redirect(url_for("deck"))
2022-06-14 02:26:38 +02:00
#preverimo če so sploh karte v collectionu
2022-06-14 14:47:12 +02:00
print("userID", user_id)
2022-06-14 02:26:38 +02:00
c = dbsession.query(Card).filter(Card.owner_id == user_id).all()
2022-06-26 18:30:11 +02:00
2022-06-14 02:26:38 +02:00
if c == []:
print("ne najdem collectiona")
2022-06-14 14:47:12 +02:00
return render_template("error/no_cards_in_collection.html", username=username)
2022-06-14 02:26:38 +02:00
#ustvari novi vnos v Deck
user_settings = get_settings(user_id)
2022-06-26 18:30:11 +02:00
if user_settings['max_new'] == "0" and user_settings['max_due'] == "0":
flash("Error: Attempted to make deck with 0 cards.")
2022-08-12 19:51:55 +02:00
return render_template("menu.html")
2022-06-26 18:30:11 +02:00
deck = probabilistic_deck_generator(user_id, int(user_settings['max_new']), int(user_settings['max_due']))
cards_by_id = get_deck(deck)
2022-06-26 18:30:11 +02:00
if cards_by_id == "":
return render_template("error/no_cards_in_collection.html", username=username)
2022-06-14 02:26:38 +02:00
else:
number_of_cards = len(cards_by_id.split(","))
print(cards_by_id, number_of_cards)
deck = Deck(cards_by_id=cards_by_id, owner_id=user_id, number_of_cards=number_of_cards, current_card=0, completed=False)
dbsession.add(deck)
dbsession.commit()
dbsession.close()
2022-06-14 02:26:38 +02:00
return redirect(url_for("deck"))
2022-06-04 21:58:05 +02:00
elif action == "matches":
2022-06-14 02:26:38 +02:00
return redirect(url_for("matches.index"))
2022-06-04 21:58:05 +02:00
elif action == "upload":
return redirect(url_for("upload.index"))
2022-06-04 21:58:05 +02:00
elif action == "settings":
2022-06-23 16:01:16 +02:00
settings = get_settings(user_id)
return render_template("settings.html", username=username, user_id=user_id, settings=settings)
2022-06-04 21:58:05 +02:00
elif action == "instructions":
return render_template("instructions.html", username=username, user_id=user_id)
2022-08-12 19:51:55 +02:00
# elif action == "about":
# return render_template("about.html", username=username, user_id=user_id)
2022-06-04 21:58:05 +02:00
2022-08-12 19:51:55 +02:00
return render_template("menu.html", username=username, deck_status=deck_status)