from flask import session, redirect, url_for,request, flash, render_template from create_db import get_session, Deck, Card from share import share """ Refactoring the 'deck' function from app.py rabi še deck funkcionalnosti, ki so bile prej v menu.py itd... """ def prob_session(): dbsession = get_session() if not 'user_id' in session: redirect(url_for('login')) 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? if len(deck_object.cards_by_id.split(",")) == 0: deck_query.filter(Deck) redirect(url_for('login')) # 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! # Ali damo share? Potem nastavi na share in ponovi obrazec share_request = request.form.get("share", None) if share_request: # @TODO logika za share! share(submit_card, user_id) # Če ne, gre za rate! else: rate = request.form.get('rate', None) #je to nevarno?? print(rate) if not rate: raise Exception("manjka rate info!") if rate == "Yes": submit_card.interest_rate = 1 elif rate == "Maybe": k = 0.5 print(submit_card) submit_card.interest_rate= abs(submit_card.interest_rate*k) elif rate == "No": k = 0.1 submit_card.interest_rate = abs(submit_card.interest_rate*k) elif rate == "Delete": submit_card.interest_rate = 0 #@TODO to bi lahko zbrisalo tudi file v določenih primerih # 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!") 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] print("GET CARD PLS", show_card_id) show_card = dbsession.query(Card).get(show_card_id) dbsession.close() if not show_card: # @TODO how to handle missing card? #deck_object.completed = 1 #dbsession.commit() #dbsession.close() print("show_card missing") return render_template("error/no_cards_in_collection.html") #raise Exception("Ne najdem naslednje karte") # Prikaži obrazec return render_template("deck.html", username=username, card=show_card) """ Old menu function def index(): deck_status = [] dbsession = get_session() if 'user_id' not in session: return login() if 'user_id' in session: user_id = session['user_id'] username = session['username'] print(username) 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": 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": old_deck = dbsession.query(Deck).filter(Deck.completed == 0, Deck.owner_id == user_id).all() if old_deck != []: return redirect(url_for("deck")) #preverimo če so sploh karte v collectionu print("userID", user_id) c = dbsession.query(Card).filter(Card.owner_id == user_id).all() if c == []: print("ne najdem collectiona") return render_template("error/no_cards_in_collection.html", username=username) #ustvari novi vnos v Deck user_settings = get_settings(user_id) if user_settings['max_new'] == "0" and user_settings['max_due'] == "0": flash("Error: Attempted to make deck with 0 cards.") return render_template("menu.html") deck = probabilistic_deck_generator(user_id, int(user_settings['max_new']), int(user_settings['max_due'])) cards_by_id = get_deck(deck) if cards_by_id == "": return render_template("error/no_cards_in_collection.html", username=username) 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() return redirect(url_for("deck")) elif action == "matches": return redirect(url_for("matches.index")) elif action == "upload": return redirect(url_for("upload.index")) elif action == "settings": settings = get_settings(user_id) return render_template("settings.html", username=username, user_id=user_id, settings=settings) elif action == "instructions": return render_template("instructions.html", username=username, user_id=user_id) # elif action == "about": # return render_template("about.html", username=username, user_id=user_id) return render_template("menu.html", username=username, deck_status=deck_status) """