blabla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.4 KiB

2 months ago
2 months ago
2 months ago
2 months ago
  1. from flask import (
  2. Blueprint, flash, redirect, render_template, request, session, url_for
  3. )
  4. from create_db import Deck, Card, get_session
  5. from deck import get_deck, probabilistic_deck_generator
  6. from settings import get_settings
  7. from auth import login
  8. bp = Blueprint('menu', __name__, url_prefix='/menu')
  9. @bp.route("/", methods=("GET", "POST"))
  10. def index():
  11. deck_status = []
  12. dbsession = get_session()
  13. if 'user_id' not in session:
  14. return login()
  15. if 'user_id' in session:
  16. user_id = session['user_id']
  17. username = session['username']
  18. print(username)
  19. old_deck = dbsession.query(Deck).filter(Deck.completed == 0, Deck.owner_id == user_id).all()
  20. if old_deck != []:
  21. deck_status = "old"
  22. if request.method == "POST":
  23. action = request.form.get("menu", False) #internetna rešitev, nevem kako, ampak dela, tj. dobi info iz meni buttonov
  24. print(action)
  25. if action == "new_session":
  26. old_deck = dbsession.query(Deck).filter(Deck.completed == 0, Deck.owner_id == user_id).all()
  27. if old_deck != []:
  28. return redirect(url_for("deck"))
  29. #preverimo če so sploh karte v collectionu
  30. print("userID", user_id)
  31. c = dbsession.query(Card).filter(Card.owner_id == user_id).all()
  32. if c == []:
  33. print("ne najdem collectiona")
  34. return render_template("error/no_cards_in_collection.html", username=username)
  35. #ustvari novi vnos v Deck
  36. user_settings = get_settings(user_id)
  37. if user_settings['max_new'] == "0" and user_settings['max_due'] == "0":
  38. flash("Error: Attempted to make deck with 0 cards.")
  39. return render_template("menu.html")
  40. deck = probabilistic_deck_generator(user_id, int(user_settings['max_new']), int(user_settings['max_due']))
  41. cards_by_id = get_deck(deck)
  42. if cards_by_id == "":
  43. return render_template("error/no_cards_in_collection.html", username=username)
  44. else:
  45. number_of_cards = len(cards_by_id.split(","))
  46. print(cards_by_id, number_of_cards)
  47. deck = Deck(cards_by_id=cards_by_id, owner_id=user_id, number_of_cards=number_of_cards, current_card=0, completed=False)
  48. dbsession.add(deck)
  49. dbsession.commit()
  50. dbsession.close()
  51. return redirect(url_for("deck"))
  52. elif action == "matches":
  53. return redirect(url_for("matches.index"))
  54. elif action == "upload":
  55. return redirect(url_for("upload.index"))
  56. elif action == "settings":
  57. settings = get_settings(user_id)
  58. return render_template("settings.html", username=username, user_id=user_id, settings=settings)
  59. elif action == "instructions":
  60. return render_template("instructions.html", username=username, user_id=user_id)
  61. # elif action == "about":
  62. # return render_template("about.html", username=username, user_id=user_id)
  63. return render_template("menu.html", username=username, deck_status=deck_status)