2022-08-10 14:22:09 +02:00
|
|
|
from random import choice
|
|
|
|
from datetime import date, timedelta
|
2022-08-07 17:46:52 +02:00
|
|
|
from flask import 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-10 14:22:09 +02:00
|
|
|
"""
|
|
|
|
testing
|
|
|
|
"""
|
|
|
|
def autofill_ratings():
|
|
|
|
dbsession = get_session()
|
|
|
|
for i in range(100):
|
|
|
|
card = Rating(user_id="1", card_id="10", rating_value=choice(["Yes", "Maybe", "No"]), rating_time=date.today()-timedelta(choice([i for i in range(100)])))
|
|
|
|
dbsession.add(card)
|
|
|
|
dbsession.commit()
|
|
|
|
dbsession.close()
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
Main fuctions
|
|
|
|
"""
|
|
|
|
def calculate_interval(sorted_rates):
|
|
|
|
maybe_factor = 1.2
|
|
|
|
no_factor = 2.1
|
|
|
|
|
|
|
|
print(sorted_rates[0].rating_value)
|
|
|
|
if sorted_rates == []:
|
|
|
|
#this is a new card, but interval can't be zero cuz multiplication... may have to handeled differently
|
|
|
|
return 1
|
|
|
|
elif sorted_rates[0].rating_value == "Yes":
|
|
|
|
return 1
|
|
|
|
elif sorted_rates[0].rating_value == "Maybe":
|
|
|
|
sorted_rates.pop(0)
|
|
|
|
return calculate_interval(sorted_rates) * maybe_factor
|
|
|
|
elif sorted_rates[0].rating_value == "No":
|
|
|
|
sorted_rates.pop(0)
|
|
|
|
return calculate_interval(sorted_rates) * no_factor
|
|
|
|
elif sorted_rates[0].rating_value == "Delete":
|
|
|
|
return "Deleted"
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
if sorted_rates_by_card == None:
|
|
|
|
print("ni kart... kaj zdaj") #@TODO
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
interval = calculate_interval(sorted_rates_by_card)
|
|
|
|
return round(interval)
|
|
|
|
|
|
|
|
#print(get_interval(card_id="10"))
|
|
|
|
|
|
|
|
def is_due(card_id):
|
|
|
|
dbsession = get_session()
|
|
|
|
interval = get_interval(card_id)
|
|
|
|
|
|
|
|
last_rating_date = dbsession.query(Rating).filter(Rating.card_id == card_id).order_by(desc(Rating.rating_time)).first()
|
|
|
|
due_date = last_rating_date.rating_time + timedelta(interval)
|
|
|
|
|
|
|
|
return date.today() >= due_date
|
|
|
|
|
|
|
|
|
|
|
|
def list_of_due_cardby_ids(user_id):
|
|
|
|
dbsession = get_session()
|
|
|
|
cards = dbsession.query(Card).filter(Card.owner_id == user_id).all()
|
|
|
|
l = []
|
|
|
|
for card in cards:
|
|
|
|
if is_due(card.id):
|
|
|
|
l.append(card.id)
|
|
|
|
return l
|
|
|
|
|
|
|
|
|
|
|
|
def list_of_new_cards(user_id):
|
|
|
|
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)
|
|
|
|
return l
|
|
|
|
|
|
|
|
|
|
|
|
def next_card(user_id):
|
|
|
|
"""
|
|
|
|
find a due card in user's collection. A due card is last_review + interval <= current_date. There could be no new card.
|
|
|
|
"""
|
|
|
|
dbsession = get_session()
|
|
|
|
#verjetno ne bo tako easy
|
|
|
|
next_card = dbsession.query(Rating).filter().first()
|
|
|
|
|
|
|
|
return None
|
2022-08-07 17:46:52 +02:00
|
|
|
|
|
|
|
def sr_session():
|
|
|
|
dbsession = get_session()
|
|
|
|
|
|
|
|
#check if user in
|
|
|
|
if not 'user_id' in session:
|
|
|
|
redirect(url_for('login'))
|
|
|
|
user_id = session['user_id']
|
|
|
|
username = session['username']
|
|
|
|
|
2022-08-10 14:22:09 +02:00
|
|
|
#what is the next card, None means no next card, aka deck done...
|
|
|
|
show_card = next_card(user_id)
|
2022-08-07 17:46:52 +02:00
|
|
|
|
|
|
|
# Smo oddali obrazec?
|
|
|
|
if request.method == 'POST':
|
2022-08-10 14:22:09 +02:00
|
|
|
#get card_if from the card, rendered from the template, data actually from the template...
|
2022-08-07 17:46:52 +02:00
|
|
|
card_id = request.form.get('card_id', None)
|
|
|
|
if not card_id:
|
|
|
|
raise Exception("card_id je nujen!")
|
|
|
|
|
2022-08-10 14:22:09 +02:00
|
|
|
#this is the rendered card aka the card we are rating and submiting the data on
|
2022-08-07 17:46:52 +02:00
|
|
|
submit_card = dbsession.query(Card).get(card_id)
|
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-07 17:46:52 +02:00
|
|
|
|
2022-08-10 14:22:09 +02:00
|
|
|
# Share
|
|
|
|
#Share button pressed? then reset the form - hence the if-else
|
2022-08-07 17:46:52 +02:00
|
|
|
share_request = request.form.get("share", None)
|
|
|
|
if share_request:
|
|
|
|
# @TODO logika za share!
|
|
|
|
share(submit_card, user_id)
|
|
|
|
|
2022-08-10 14:22:09 +02:00
|
|
|
# Rate
|
|
|
|
#If it's not a share it's a rate!
|
2022-08-07 17:46:52 +02:00
|
|
|
else:
|
2022-08-10 14:22:09 +02:00
|
|
|
rate = request.form.get('rate', None) #is this get somehow dangerous?
|
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
|
|
|
|
|
|
|
if rate == "Yes":
|
2022-08-10 14:22:09 +02:00
|
|
|
pass
|
2022-08-07 17:46:52 +02:00
|
|
|
|
|
|
|
elif rate == "Maybe":
|
2022-08-10 14:22:09 +02:00
|
|
|
pass
|
|
|
|
|
2022-08-07 17:46:52 +02:00
|
|
|
elif rate == "No":
|
2022-08-10 14:22:09 +02:00
|
|
|
pass
|
|
|
|
|
2022-08-07 17:46:52 +02:00
|
|
|
elif rate == "Delete":
|
2022-08-10 14:22:09 +02:00
|
|
|
#@TODO this should delete the file in some cases.
|
|
|
|
pass
|
2022-08-07 17:46:52 +02:00
|
|
|
|
|
|
|
# Any more cards?
|
2022-08-10 14:22:09 +02:00
|
|
|
|
|
|
|
#Display from
|
2022-08-13 15:43:08 +02:00
|
|
|
return render_template("deck.html", username=username, card=show_card)
|