degugging but still needs two rates per card

master
Kostanjevec 2022-08-14 14:07:31 +02:00
parent 49c6c7687c
commit bf1c2fae96
1 changed files with 42 additions and 37 deletions

View File

@ -1,6 +1,6 @@
from random import choice, choices from random import choice, choices
from datetime import date, timedelta from datetime import date, timedelta
from flask import session, redirect, url_for, request, render_template from flask import flash, session, redirect, url_for, request, render_template
from sqlalchemy import desc from sqlalchemy import desc
from share import share from share import share
from create_db import Card, Rating, get_session from create_db import Card, Rating, get_session
@ -24,9 +24,7 @@ def calculate_interval(sorted_rates):
maybe_factor = 1.2 maybe_factor = 1.2
no_factor = 2.1 no_factor = 2.1
print(sorted_rates[0].rating_value)
if sorted_rates == []: if sorted_rates == []:
#this is a new card, but interval can't be zero cuz multiplication... may have to handeled differently
return 1 return 1
elif sorted_rates[0].rating_value == "Yes": elif sorted_rates[0].rating_value == "Yes":
return 1 return 1
@ -37,7 +35,7 @@ def calculate_interval(sorted_rates):
sorted_rates.pop(0) sorted_rates.pop(0)
return calculate_interval(sorted_rates) * no_factor return calculate_interval(sorted_rates) * no_factor
elif sorted_rates[0].rating_value == "Delete": elif sorted_rates[0].rating_value == "Delete":
return "Deleted" return -1 #"Deleted"
def get_interval(card_id): def get_interval(card_id):
@ -46,6 +44,7 @@ def get_interval(card_id):
""" """
dbsession = get_session() dbsession = get_session()
sorted_rates_by_card = dbsession.query(Rating).filter(Rating.card_id == card_id).order_by(desc(Rating.rating_time)).all() sorted_rates_by_card = dbsession.query(Rating).filter(Rating.card_id == card_id).order_by(desc(Rating.rating_time)).all()
dbsession.close()
if sorted_rates_by_card == None: if sorted_rates_by_card == None:
print("ni kart... kaj zdaj") #@TODO print("ni kart... kaj zdaj") #@TODO
return None return None
@ -57,10 +56,14 @@ def get_interval(card_id):
def is_due(card_id): def is_due(card_id):
dbsession = get_session() dbsession = get_session()
interval = get_interval(card_id) interval = get_interval(card_id)
if interval < 0:
return False
last_rating_date = dbsession.query(Rating).filter(Rating.card_id == card_id).order_by(desc(Rating.rating_time)).first() last_rating_date = dbsession.query(Rating).filter(Rating.card_id == card_id).order_by(desc(Rating.rating_time)).first()
if last_rating_date == None:
return True
due_date = last_rating_date.rating_time + timedelta(interval) due_date = last_rating_date.rating_time + timedelta(interval)
dbsession.close()
return date.today() >= due_date return date.today() >= due_date
@ -71,6 +74,7 @@ def list_of_due_card_by_ids(user_id):
for card in cards: for card in cards:
if is_due(card.id): if is_due(card.id):
l.append(card.id) l.append(card.id)
dbsession.close()
return l return l
@ -82,9 +86,10 @@ def list_of_new_cards_by_ids(user_id):
rating = dbsession.query(Rating).filter(Rating.card_id == card.id).first() rating = dbsession.query(Rating).filter(Rating.card_id == card.id).first()
if rating == None: if rating == None:
l.append(card.id) l.append(card.id)
dbsession.close()
return l return l
#obsolete
def make_deck(user_id, max_new, max_due): def make_deck(user_id, max_new, max_due):
due_cards = list_of_due_card_by_ids(user_id) due_cards = list_of_due_card_by_ids(user_id)
new_cards = list_of_new_cards_by_ids(user_id) new_cards = list_of_new_cards_by_ids(user_id)
@ -120,6 +125,7 @@ def rated_today_by_staus (card_status):
print("wtf") print("wtf")
raise Exception("Count be wrong! There should be at least one rating") raise Exception("Count be wrong! There should be at least one rating")
dbsession.close()
if card_status == "new": if card_status == "new":
return n return n
elif card_status == "due": elif card_status == "due":
@ -128,13 +134,15 @@ def rated_today_by_staus (card_status):
raise Exception("must input either 'new' or 'due' as parameter") raise Exception("must input either 'new' or 'due' as parameter")
def get_a_card_by_status(card_status): def get_a_card_by_status(user_id, card_status):
if card_status == "new": if card_status == "new":
l = list_of_new_cards_by_ids l = list_of_new_cards_by_ids(user_id)
card_id = choice(list_of_due_card_by_ids) card_id = l[0]
print(l, "00000000" , card_id)
if card_status == "due": if card_status == "due":
l = list_of_due_card_by_ids l = list_of_due_card_by_ids(user_id)
card_id = choice(list_of_due_card_by_ids) card_id = l[0]
print(l, "00000000" , card_id)
return card_id return card_id
@ -151,58 +159,55 @@ def sr_session():
username = session['username'] username = session['username']
user_settings = get_settings(user_id) user_settings = get_settings(user_id)
max_new = user_settings["max_new"] max_new = user_settings["max_new"]
max_due = user_settings["max_due"] max_due = user_settings["max_due"]
all_new = len(list_of_new_cards_by_ids(user_id))
all_due = len(list_of_due_card_by_ids(user_id))
rated_today_new = rated_today_by_staus("new") rated_today_new = rated_today_by_staus("new")
rated_today_due = rated_today_by_staus("due") rated_today_due = rated_today_by_staus("due")
#checks if there are any new/due cards left for today and gets the next one. max can be less than all scheduled, min decides which is the limt.
if min(int(max_new), all_new) - int(rated_today_new) > 0:
new_card_id=get_a_card_by_status(user_id, "new")
elif min(int(max_due), all_due) - int(rated_today_due) > 0:
new_card_id=get_a_card_by_status(user_id, "due")
else:
dbsession.close()
flash("no more cards today")
return redirect("/menu")
# Smo oddali obrazec? # Smo oddali obrazec?
if request.method == 'POST': if request.method == 'POST':
#get card_if from the card, rendered from the template, data actually from the template... #get card_id from the card, rendered from the template, data actually from the template... why tho
card_id = request.form.get('card_id', None) card_id = request.form.get('card_id', None)
if not card_id: if not card_id:
raise Exception("card_id necesarry!") raise Exception("card_id necesarry!")
#this is the rendered card aka the card we are rating and submiting the data on
if max_new - rated_today_new > 0:
card_id=get_a_card_by_status("new")
elif max_due - rated_today_due >0:
card_id=get_a_card_by_status("due")
submit_card = dbsession.query(Card).get(card_id)
#@TODO check if this card is part of the user's deck to prevent possibile hack? #@TODO check if this card is part of the user's deck to prevent possibile hack?
submit_card = dbsession.query(Card).get(card_id)
# Share # Share
#@@TODO, this should not change the card
#Share button pressed? then reset the form - hence the if-else #Share button pressed? then reset the form - hence the if-else
share_request = request.form.get("share", None) share_request = request.form.get("share", None)
if share_request: if share_request:
# @TODO logika za share!
share(submit_card, user_id) share(submit_card, user_id)
#Rate #Rate
#If it's not a share it's a rate!
else: else:
rate = request.form.get('rate', None) #is this get somehow dangerous? rate = request.form.get('rate', None) #is this get somehow dangerous?
if not rate: if not rate:
raise Exception("Need rate info!") raise Exception("Need rate info!")
r = Rating(user_id=user_id, card_id=card_id, rating_value=rate, rating_time=date.today()) r = Rating(user_id=user_id, card_id=submit_card.id, rating_value=rate, rating_time=date.today())
dbsession.add(r) dbsession.add(r)
dbsession.commit dbsession.commit()
dbsession.close dbsession.close()
# Loudamo naslednjo karto v decku
#TODO We need this but i don't know it's neccessay to distingush between submit/show
show_card = card_id
# Prikaži obrazec
show_card = dbsession.query(Card).get(new_card_id)
dbsession.close()
#Display from #Display from
return render_template("deck.html", username=username, card=show_card) return render_template("deck.html", username=username, card=show_card)