contentmatcher/app.py

158 lines
5.0 KiB
Python
Raw Normal View History

2022-04-25 00:32:17 +02:00
import os
from flask import Flask, redirect, render_template, session, request, url_for, flash
2022-04-25 00:32:17 +02:00
2022-05-20 00:01:56 +02:00
from auth import login_required, bp as auth_bp
from deck import bp as deck_bp
from menu import bp as menu_bp
from upload import bp as upload_bp
2022-06-13 14:25:17 +02:00
from share import share
2022-06-14 02:27:05 +02:00
from matches import bp as matches_bp
2022-06-23 14:41:11 +02:00
from settings import bp as settings_bp
2022-05-22 16:52:01 +02:00
2022-06-14 14:47:12 +02:00
from create_db import Card, Deck, get_session
2022-06-07 00:28:11 +02:00
from config import CONFIG
2022-04-25 00:32:17 +02:00
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY=CONFIG['SECRET_KEY']
)
2022-05-20 00:01:56 +02:00
2022-05-17 21:27:51 +02:00
@app.route('/')
2022-05-20 00:01:56 +02:00
@login_required
2022-05-17 21:27:51 +02:00
def index():
2022-05-20 00:01:56 +02:00
return 'redirecting' # meče skoz nazaj na login TODO
# Ce je napaka baze, naredi rollback
@app.errorhandler(500)
def internal_error(error):
session.rollback()
return render_template('500.html'), 500
2022-06-08 20:56:11 +02:00
@app.route('/deck/index', methods=["GET", "POST"])
2022-05-20 00:01:56 +02:00
def deck():
2022-06-14 14:47:12 +02:00
dbsession = get_session()
2022-06-12 18:46:07 +02:00
if not 'user_id' in session:
return index()
user_id = session['user_id']
username = session['username']
#pokliče na bazo, da dobi str card idjev, ga spremeni v list in srevira karte po idjih
deck_query = dbsession.query(Deck).filter(Deck.owner_id == user_id)
deck_object = deck_query.filter(Deck.completed == False).first()
# @TODO: ce deck, ne obstaja, kaj naj zaj jas?
2022-06-14 02:27:05 +02:00
if len(deck_object.cards_by_id.split(",")) == 0:
deck_query.filter(Deck)
return index()
2022-06-12 18:46:07 +02:00
deck_id = deck_object.id
# Smo oddali obrazec?
if request.method == 'POST':
card_id = request.form.get('card_id', None)
if not card_id:
raise Exception("card_id je nujen!")
submit_card = dbsession.query(Card).get(card_id)
# @TODO preveri, ali je card del trenutnega decka!
2022-06-12 18:46:07 +02:00
# Ali damo share? Potem nastavi na share in ponovi obrazec
2022-06-13 14:25:17 +02:00
share_request = request.form.get("share", None)
if share_request:
2022-06-12 18:46:07 +02:00
# @TODO logika za share!
2022-06-13 14:25:17 +02:00
share(submit_card, user_id)
2022-06-12 18:46:07 +02:00
# Če ne, gre za rate!
else:
rate = request.form.get('rate', None) #je to nevarno??
print(rate)
2022-06-12 18:46:07 +02:00
if not rate:
raise Exception("manjka rate info!")
if rate == "Yes":
2022-06-12 18:46:07 +02:00
submit_card.interest_rate = 1
2022-06-07 00:28:11 +02:00
elif rate == "Maybe":
k = 0.5
2022-06-12 18:46:07 +02:00
print(submit_card)
submit_card.interest_rate= abs(submit_card.interest_rate*k)
elif rate == "No":
k = 0.1
2022-06-12 18:46:07 +02:00
submit_card.interest_rate = abs(submit_card.interest_rate*k)
2022-06-15 02:12:05 +02:00
elif rate == "Delete":
submit_card.interest_rate = 0
#@TODO to bi lahko zbrisalo tudi file v določenih primerih
2022-06-12 18:46:07 +02:00
# zaporedno število trenutnega carda v decku
next_card = deck_object.current_card + 1
# Ali je deck končan?
if next_card >= deck_object.number_of_cards:
deck_object.completed = True
dbsession.commit()
dbsession.close()
flash("Deck rating finished!")
2022-06-12 18:46:07 +02:00
return redirect(url_for("menu.index"))
deck_object.current_card = next_card
dbsession.commit()
# Loudamo naslednjo karto v decku
show_card_index = deck_object.current_card
show_card_id = deck_object.cards_by_id.split(",")[show_card_index]
2022-06-14 14:47:12 +02:00
print("GET CARD PLS", show_card_id)
2022-06-12 18:46:07 +02:00
show_card = dbsession.query(Card).get(show_card_id)
dbsession.close()
2022-06-12 18:46:07 +02:00
if not show_card:
2022-06-14 14:47:12 +02:00
# @TODO how to handle missing card?
#deck_object.completed = 1
#dbsession.commit()
#dbsession.close()
2022-06-14 02:31:35 +02:00
print("show_card missing")
2022-06-14 02:27:05 +02:00
return render_template("error/no_cards_in_collection.html")
#raise Exception("Ne najdem naslednje karte")
2022-06-12 18:46:07 +02:00
# Prikaži obrazec
return render_template("deck/index.html", username=username, card=show_card)
@app.route("/share_button", methods=["GET", "POST"])
2022-06-14 02:27:05 +02:00
def share_button():
2022-06-12 18:46:07 +02:00
print("share!!!")
return redirect(url_for("deck"))
2022-05-22 16:52:01 +02:00
2022-05-20 00:01:56 +02:00
@app.route('/logout')
def logout():
session.pop("user_id", None)
return index()
2022-06-04 14:21:28 +02:00
@app.route('/match')
def match():
return "match"
2022-06-04 14:21:28 +02:00
2022-06-14 02:27:05 +02:00
2022-05-20 00:01:56 +02:00
#app.register_blueprint(bp) # ??
app.register_blueprint(auth_bp)
app.register_blueprint(deck_bp)
app.register_blueprint(menu_bp)
app.register_blueprint(upload_bp)
2022-06-14 02:27:05 +02:00
app.register_blueprint(matches_bp)
2022-06-23 14:41:11 +02:00
app.register_blueprint(settings_bp)
2022-05-20 00:01:56 +02:00
app.add_url_rule('/', endpoint='index')
2022-05-13 18:23:15 +02:00
return app
2022-05-15 22:12:18 +02:00
app = create_app()
app.run(debug=True, host="0.0.0.0")