2022-10-30 23:25:20 +01:00
|
|
|
from random import choice
|
2022-08-10 14:22:09 +02:00
|
|
|
from datetime import date, timedelta
|
2022-08-14 14:07:31 +02:00
|
|
|
from flask import flash, session, redirect, url_for, request, render_template
|
2022-08-10 14:22:09 +02:00
|
|
|
from sqlalchemy import desc
|
2022-08-07 17:46:52 +02:00
|
|
|
from share import share
|
|
|
|
from create_db import Card, Rating, get_session
|
2022-08-13 15:44:43 +02:00
|
|
|
from settings import get_settings
|
2022-08-10 14:22:09 +02:00
|
|
|
"""
|
|
|
|
testing
|
|
|
|
"""
|
|
|
|
def autofill_ratings():
|
|
|
|
dbsession = get_session()
|
|
|
|
for i in range(100):
|
2022-08-16 10:50:41 +02:00
|
|
|
card = Rating(user_id="1", card_id=choice([10, 13, 14, 75]), rating_value=choice(["Maybe", "No"]), rating_time=date.today()-timedelta(choice([i for i in range(100)])))
|
2022-08-10 14:22:09 +02:00
|
|
|
dbsession.add(card)
|
|
|
|
dbsession.commit()
|
|
|
|
dbsession.close()
|
|
|
|
return None
|
|
|
|
|
2022-08-16 10:50:41 +02:00
|
|
|
"""
|
|
|
|
Config
|
|
|
|
"""
|
|
|
|
maybe_factor = 1.2
|
|
|
|
no_factor = 2.1
|
|
|
|
|
2022-08-10 14:22:09 +02:00
|
|
|
"""
|
|
|
|
Main fuctions
|
|
|
|
"""
|
2022-08-16 10:50:41 +02:00
|
|
|
def calculate_interval(sorted_rates, maybe_factor, no_factor):
|
2022-08-10 14:22:09 +02:00
|
|
|
if sorted_rates == []:
|
|
|
|
return 1
|
|
|
|
elif sorted_rates[0].rating_value == "Yes":
|
|
|
|
return 1
|
|
|
|
elif sorted_rates[0].rating_value == "Maybe":
|
|
|
|
sorted_rates.pop(0)
|
2022-08-16 10:50:41 +02:00
|
|
|
return calculate_interval(sorted_rates, maybe_factor, no_factor) * maybe_factor
|
2022-08-10 14:22:09 +02:00
|
|
|
elif sorted_rates[0].rating_value == "No":
|
|
|
|
sorted_rates.pop(0)
|
2022-08-16 10:50:41 +02:00
|
|
|
return calculate_interval(sorted_rates, maybe_factor, no_factor) * no_factor
|
2022-08-10 14:22:09 +02:00
|
|
|
elif sorted_rates[0].rating_value == "Delete":
|
2022-08-14 14:07:31 +02:00
|
|
|
return -1 #"Deleted"
|
2022-08-10 14:22:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_interval(card_id):
|
|
|
|
"""
|
|
|
|
takes card_id looks at the history of rates and gives the current interval
|
|
|
|
"""
|
|
|
|
dbsession = get_session()
|
|
|
|
sorted_rates_by_card = dbsession.query(Rating).filter(Rating.card_id == card_id).order_by(desc(Rating.rating_time)).all()
|
2022-08-14 14:07:31 +02:00
|
|
|
dbsession.close()
|
2022-08-10 14:22:09 +02:00
|
|
|
if sorted_rates_by_card == None:
|
|
|
|
print("ni kart... kaj zdaj") #@TODO
|
|
|
|
return None
|
|
|
|
else:
|
2022-08-16 10:50:41 +02:00
|
|
|
interval = calculate_interval(sorted_rates_by_card, maybe_factor, no_factor)
|
2022-08-10 14:22:09 +02:00
|
|
|
return round(interval)
|
|
|
|
|
2022-08-16 10:50:41 +02:00
|
|
|
#to vključuje tudi new...
|
2022-08-10 14:22:09 +02:00
|
|
|
def is_due(card_id):
|
|
|
|
dbsession = get_session()
|
|
|
|
interval = get_interval(card_id)
|
2022-08-14 14:07:31 +02:00
|
|
|
if interval < 0:
|
|
|
|
return False
|
2022-08-10 14:22:09 +02:00
|
|
|
|
|
|
|
last_rating_date = dbsession.query(Rating).filter(Rating.card_id == card_id).order_by(desc(Rating.rating_time)).first()
|
2022-08-14 14:07:31 +02:00
|
|
|
if last_rating_date == None:
|
2022-09-05 20:27:07 +02:00
|
|
|
dbsession.close()
|
2022-08-14 14:07:31 +02:00
|
|
|
return True
|
2022-08-10 14:22:09 +02:00
|
|
|
due_date = last_rating_date.rating_time + timedelta(interval)
|
2022-08-14 14:07:31 +02:00
|
|
|
dbsession.close()
|
2022-08-10 14:22:09 +02:00
|
|
|
return date.today() >= due_date
|
|
|
|
|
2022-10-31 18:24:47 +01:00
|
|
|
def is_new(card_id):
|
|
|
|
dbsession = get_session()
|
|
|
|
rating = dbsession.query(Rating).filter(Rating.card_id == card_id).first()
|
|
|
|
dbsession.close()
|
|
|
|
if rating == None:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-08-10 14:22:09 +02:00
|
|
|
|
2022-08-16 10:50:41 +02:00
|
|
|
def list_of_due_cards_by_ids(user_id):
|
2022-10-31 18:24:47 +01:00
|
|
|
"this should not include new cards"
|
2022-08-10 14:22:09 +02:00
|
|
|
dbsession = get_session()
|
|
|
|
cards = dbsession.query(Card).filter(Card.owner_id == user_id).all()
|
|
|
|
l = []
|
|
|
|
for card in cards:
|
2022-10-31 18:24:47 +01:00
|
|
|
if is_new(card.id):
|
|
|
|
pass
|
|
|
|
elif is_due(card.id):
|
2022-08-10 14:22:09 +02:00
|
|
|
l.append(card.id)
|
2022-08-14 14:07:31 +02:00
|
|
|
dbsession.close()
|
2022-08-10 14:22:09 +02:00
|
|
|
return l
|
|
|
|
|
|
|
|
|
2022-08-13 15:44:43 +02:00
|
|
|
def list_of_new_cards_by_ids(user_id):
|
2022-08-10 14:22:09 +02:00
|
|
|
dbsession = get_session()
|
|
|
|
cards = dbsession.query(Card).filter(Card.owner_id == user_id).all()
|
|
|
|
l = []
|
|
|
|
for card in cards:
|
|
|
|
rating = dbsession.query(Rating).filter(Rating.card_id == card.id).first()
|
|
|
|
if rating == None:
|
|
|
|
l.append(card.id)
|
2022-08-14 14:07:31 +02:00
|
|
|
dbsession.close()
|
2022-08-10 14:22:09 +02:00
|
|
|
return l
|
|
|
|
|
2023-02-05 20:43:20 +01:00
|
|
|
|
2022-10-29 19:50:51 +02:00
|
|
|
def rated_today_by_staus (card_status, user_id):
|
2022-08-10 14:22:09 +02:00
|
|
|
"""
|
2022-08-13 15:44:43 +02:00
|
|
|
Returns number of cards rated today by user by status (new or due)
|
2022-08-10 14:22:09 +02:00
|
|
|
"""
|
|
|
|
dbsession = get_session()
|
2022-08-13 15:44:43 +02:00
|
|
|
today = date.today()
|
|
|
|
|
2022-10-29 19:50:51 +02:00
|
|
|
ratings_today = dbsession.query(Rating).filter(
|
|
|
|
Rating.rating_time == today,
|
|
|
|
Rating.user_id == user_id
|
|
|
|
)
|
2022-08-13 15:44:43 +02:00
|
|
|
n = 0
|
|
|
|
d = 0
|
|
|
|
for rating in ratings_today:
|
2023-02-05 20:43:20 +01:00
|
|
|
count_all_rates_of_card = dbsession.query(Rating).filter(rating.card_id == Rating.card_id).count()
|
2023-02-05 19:41:50 +01:00
|
|
|
print("count_all_rates_of_card: ", count_all_rates_of_card)
|
2022-08-13 15:44:43 +02:00
|
|
|
|
|
|
|
if count_all_rates_of_card == 1:
|
|
|
|
n+=1
|
|
|
|
elif count_all_rates_of_card > 1:
|
|
|
|
d+=1
|
|
|
|
else:
|
|
|
|
print("wtf")
|
|
|
|
raise Exception("Count be wrong! There should be at least one rating")
|
|
|
|
|
2022-08-14 14:07:31 +02:00
|
|
|
dbsession.close()
|
2022-08-13 15:44:43 +02:00
|
|
|
if card_status == "new":
|
|
|
|
return n
|
|
|
|
elif card_status == "due":
|
|
|
|
return d
|
|
|
|
else:
|
|
|
|
raise Exception("must input either 'new' or 'due' as parameter")
|
|
|
|
|
|
|
|
|
2022-08-14 14:07:31 +02:00
|
|
|
def get_a_card_by_status(user_id, card_status):
|
2022-08-13 15:44:43 +02:00
|
|
|
if card_status == "new":
|
2022-08-14 14:07:31 +02:00
|
|
|
l = list_of_new_cards_by_ids(user_id)
|
|
|
|
card_id = l[0]
|
2022-08-13 15:44:43 +02:00
|
|
|
if card_status == "due":
|
2022-08-16 10:50:41 +02:00
|
|
|
l = list_of_due_cards_by_ids(user_id)
|
2022-08-14 14:07:31 +02:00
|
|
|
card_id = l[0]
|
2022-08-13 16:41:49 +02:00
|
|
|
return card_id
|
2022-08-07 17:46:52 +02:00
|
|
|
|
2022-08-16 10:50:41 +02:00
|
|
|
"""
|
|
|
|
Export functions
|
|
|
|
"""
|
2022-10-31 17:40:25 +01:00
|
|
|
def remaining_items(max_items, total_queue, rated_today):
|
|
|
|
"""
|
|
|
|
gives number of remaining items. Either upto max or if total runs out earlier.
|
|
|
|
"""
|
|
|
|
n_of_remaining = min((max_items-rated_today), total_queue)
|
|
|
|
return n_of_remaining
|
|
|
|
|
2022-08-16 10:50:41 +02:00
|
|
|
def schedule_status(user_id):
|
|
|
|
"""
|
|
|
|
Returns information needed for start/continue/done menu ui
|
|
|
|
"""
|
|
|
|
user_settings = get_settings(user_id)
|
|
|
|
max_new = int(user_settings["max_new"])
|
|
|
|
max_due = int(user_settings["max_due"])
|
|
|
|
|
2022-10-29 19:50:51 +02:00
|
|
|
rated_new = rated_today_by_staus("new", user_id)
|
|
|
|
rated_due = rated_today_by_staus("due", user_id)
|
2022-08-16 10:50:41 +02:00
|
|
|
|
2022-10-31 17:40:25 +01:00
|
|
|
total_new = len(list_of_new_cards_by_ids(user_id))
|
2022-10-31 18:24:47 +01:00
|
|
|
total_due = len(list_of_due_cards_by_ids(user_id))
|
2022-10-30 23:25:20 +01:00
|
|
|
|
2022-10-31 17:40:25 +01:00
|
|
|
remaining_new_today = remaining_items(max_new, total_new, rated_new)
|
|
|
|
remaining_due_today = remaining_items(max_due, total_due, rated_due)
|
|
|
|
remaining_total_today = remaining_due_today + remaining_new_today
|
2022-08-16 10:50:41 +02:00
|
|
|
|
2022-10-31 17:40:25 +01:00
|
|
|
if remaining_total_today == 0:
|
2022-10-29 19:50:51 +02:00
|
|
|
return "done"
|
|
|
|
elif rated_due+rated_new == 0:
|
2022-08-16 10:50:41 +02:00
|
|
|
return "start"
|
|
|
|
else:
|
2022-10-31 17:40:25 +01:00
|
|
|
return "continue"
|
2022-08-16 10:50:41 +02:00
|
|
|
|
|
|
|
def remaining_by_status(user_id, status):
|
|
|
|
if status == "new":
|
|
|
|
l = len(list_of_new_cards_by_ids(user_id))
|
|
|
|
elif status == "due":
|
|
|
|
l = len(list_of_due_cards_by_ids(user_id))
|
|
|
|
return l
|
|
|
|
|
2022-08-13 15:44:43 +02:00
|
|
|
"""
|
|
|
|
Engine
|
|
|
|
"""
|
2022-08-07 17:46:52 +02:00
|
|
|
def sr_session():
|
|
|
|
dbsession = get_session()
|
|
|
|
|
2022-08-13 15:44:43 +02:00
|
|
|
#check if user in session
|
2022-08-07 17:46:52 +02:00
|
|
|
if not 'user_id' in session:
|
|
|
|
redirect(url_for('login'))
|
|
|
|
user_id = session['user_id']
|
|
|
|
username = session['username']
|
|
|
|
|
2022-08-14 14:07:31 +02:00
|
|
|
|
2022-08-14 17:34:24 +02:00
|
|
|
#form sent?
|
2022-08-07 17:46:52 +02:00
|
|
|
if request.method == 'POST':
|
2022-08-14 14:07:31 +02:00
|
|
|
#get card_id from the card, rendered from the template, data actually from the template... why tho
|
2022-08-07 17:46:52 +02:00
|
|
|
card_id = request.form.get('card_id', None)
|
|
|
|
if not card_id:
|
2022-08-13 15:44:43 +02:00
|
|
|
raise Exception("card_id necesarry!")
|
2022-08-07 17:46:52 +02:00
|
|
|
|
2022-08-10 14:22:09 +02:00
|
|
|
#@TODO check if this card is part of the user's deck to prevent possibile hack?
|
2022-08-14 14:07:31 +02:00
|
|
|
submit_card = dbsession.query(Card).get(card_id)
|
2022-08-07 17:46:52 +02:00
|
|
|
|
2022-08-14 14:07:31 +02:00
|
|
|
# Share
|
|
|
|
#@@TODO, this should not change the card
|
2022-08-07 17:46:52 +02:00
|
|
|
share_request = request.form.get("share", None)
|
|
|
|
if share_request:
|
|
|
|
share(submit_card, user_id)
|
2022-08-14 14:07:31 +02:00
|
|
|
#Rate
|
2022-08-07 17:46:52 +02:00
|
|
|
else:
|
2022-08-14 17:34:24 +02:00
|
|
|
rate = request.form.get('rate', None)
|
2022-08-07 17:46:52 +02:00
|
|
|
if not rate:
|
2022-08-10 14:22:09 +02:00
|
|
|
raise Exception("Need rate info!")
|
2022-08-07 17:46:52 +02:00
|
|
|
|
2022-08-14 14:07:31 +02:00
|
|
|
r = Rating(user_id=user_id, card_id=submit_card.id, rating_value=rate, rating_time=date.today())
|
2022-08-13 16:41:49 +02:00
|
|
|
dbsession.add(r)
|
2022-08-14 14:07:31 +02:00
|
|
|
dbsession.commit()
|
|
|
|
dbsession.close()
|
2022-08-14 17:34:24 +02:00
|
|
|
|
|
|
|
user_settings = get_settings(user_id)
|
2022-10-30 23:25:20 +01:00
|
|
|
max_new = int(user_settings["max_new"])
|
|
|
|
max_due = int(user_settings["max_due"])
|
2022-08-10 14:22:09 +02:00
|
|
|
|
2022-10-31 17:40:25 +01:00
|
|
|
total_new = len(list_of_new_cards_by_ids(user_id))
|
2022-10-31 18:24:47 +01:00
|
|
|
total_due = len(list_of_due_cards_by_ids(user_id))
|
2022-10-30 23:25:20 +01:00
|
|
|
|
2022-10-29 19:50:51 +02:00
|
|
|
rated_today_new = rated_today_by_staus("new", user_id)
|
|
|
|
rated_today_due = rated_today_by_staus("due", user_id)
|
2022-08-14 17:34:24 +02:00
|
|
|
|
2022-10-31 17:40:25 +01:00
|
|
|
remaining_new_today = remaining_items(max_new, total_new, rated_today_new)
|
|
|
|
remaining_due_today = remaining_items(max_due, total_due, rated_today_due)
|
|
|
|
|
|
|
|
print("remaining_new_today: ", remaining_new_today)
|
|
|
|
print("remaining_due_today: ", remaining_due_today)
|
2022-08-14 17:34:24 +02:00
|
|
|
|
2022-11-01 10:55:07 +01:00
|
|
|
print("list_of_due_cards_by_ids: ", list_of_due_cards_by_ids(user_id))
|
|
|
|
print("list_of_new_cards_by_ids: ", list_of_new_cards_by_ids(user_id))
|
|
|
|
|
2022-10-31 17:40:25 +01:00
|
|
|
#checks if there are any new/due cards left for today and gets the next one.
|
|
|
|
#max can be more than all scheduled, min decides which is the limt.
|
|
|
|
if remaining_new_today > 0:
|
2022-11-01 10:55:07 +01:00
|
|
|
print("gettin new") ##TODO problem je, ker na produkciji zgleda kot da jemlje skoz new, čeprav bi moral due
|
2023-02-05 20:43:20 +01:00
|
|
|
next_card_id=get_a_card_by_status(user_id, "new")
|
2022-10-31 17:40:25 +01:00
|
|
|
elif remaining_due_today > 0:
|
2022-11-01 10:55:07 +01:00
|
|
|
print("getting due")
|
2023-02-05 20:43:20 +01:00
|
|
|
next_card_id=get_a_card_by_status(user_id, "due")
|
2022-08-14 17:34:24 +02:00
|
|
|
else:
|
|
|
|
dbsession.close()
|
|
|
|
flash("no more cards today")
|
|
|
|
return redirect("/menu")
|
2022-08-07 17:46:52 +02:00
|
|
|
|
2023-02-05 20:43:20 +01:00
|
|
|
show_card = dbsession.query(Card).get(next_card_id)
|
|
|
|
interval = get_interval(next_card_id)
|
2022-08-14 17:34:24 +02:00
|
|
|
|
2022-08-16 10:50:41 +02:00
|
|
|
#these factors should be better packaged
|
|
|
|
return render_template("deck.html", username=username, card=show_card, maybe_in=round(interval*maybe_factor), no_in=round(no_factor*interval))
|