nove funkcije
parent
5082836714
commit
0292a8e795
|
@ -1,10 +1,10 @@
|
|||
from random import choice
|
||||
from random import choice, choices
|
||||
from datetime import date, timedelta
|
||||
from flask import session, redirect, url_for, request, render_template
|
||||
from sqlalchemy import desc
|
||||
from share import share
|
||||
from create_db import Card, Rating, get_session
|
||||
|
||||
from settings import get_settings
|
||||
"""
|
||||
testing
|
||||
"""
|
||||
|
@ -17,7 +17,6 @@ def autofill_ratings():
|
|||
dbsession.close()
|
||||
return None
|
||||
|
||||
|
||||
"""
|
||||
Main fuctions
|
||||
"""
|
||||
|
@ -54,7 +53,6 @@ def get_interval(card_id):
|
|||
interval = calculate_interval(sorted_rates_by_card)
|
||||
return round(interval)
|
||||
|
||||
#print(get_interval(card_id="10"))
|
||||
|
||||
def is_due(card_id):
|
||||
dbsession = get_session()
|
||||
|
@ -76,7 +74,7 @@ def list_of_due_cardby_ids(user_id):
|
|||
return l
|
||||
|
||||
|
||||
def list_of_new_cards(user_id):
|
||||
def list_of_new_cards_by_ids(user_id):
|
||||
dbsession = get_session()
|
||||
cards = dbsession.query(Card).filter(Card.owner_id == user_id).all()
|
||||
l = []
|
||||
|
@ -87,36 +85,90 @@ def list_of_new_cards(user_id):
|
|||
return l
|
||||
|
||||
|
||||
def next_card(user_id):
|
||||
def make_deck(user_id, max_new, max_due):
|
||||
due_cards = list_of_due_cardby_ids
|
||||
new_cards = list_of_new_cards_by_ids
|
||||
|
||||
due_deck = []
|
||||
due_deck.append(choices(due_cards, k=max_due))
|
||||
|
||||
new_deck = []
|
||||
new_deck.append(choices(new_cards, k=max_new))
|
||||
|
||||
deck = new_deck + due_deck
|
||||
return deck
|
||||
|
||||
|
||||
def rated_today_by_staus (card_status):
|
||||
"""
|
||||
find a due card in user's collection. A due card is last_review + interval <= current_date. There could be no new card.
|
||||
Returns number of cards rated today by user by status (new or due)
|
||||
"""
|
||||
dbsession = get_session()
|
||||
#verjetno ne bo tako easy
|
||||
next_card = dbsession.query(Rating).filter().first()
|
||||
today = date.today()
|
||||
|
||||
ratings_today = dbsession.query(Rating).filter(Rating.rating_time == today)
|
||||
n = 0
|
||||
d = 0
|
||||
for rating in ratings_today:
|
||||
count_all_rates_of_card = dbsession.query(Card).filter(Card.id == rating.card_id).count()
|
||||
|
||||
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")
|
||||
|
||||
if card_status == "new":
|
||||
return n
|
||||
elif card_status == "due":
|
||||
return d
|
||||
else:
|
||||
raise Exception("must input either 'new' or 'due' as parameter")
|
||||
|
||||
|
||||
def get_a_card_by_status(card_status):
|
||||
if card_status == "new":
|
||||
l = lis
|
||||
if card_status == "due":
|
||||
|
||||
return None
|
||||
|
||||
"""
|
||||
Engine
|
||||
"""
|
||||
def sr_session():
|
||||
dbsession = get_session()
|
||||
|
||||
#check if user in
|
||||
#check if user in session
|
||||
if not 'user_id' in session:
|
||||
redirect(url_for('login'))
|
||||
user_id = session['user_id']
|
||||
username = session['username']
|
||||
|
||||
#what is the next card, None means no next card, aka deck done...
|
||||
show_card = next_card(user_id)
|
||||
user_settings = get_settings(user_id)
|
||||
|
||||
max_new = user_settings["max_new"]
|
||||
max_due = user_settings["max_due"]
|
||||
|
||||
rated_today_new = rated_today_by_staus("new")
|
||||
rated_today_due = rated_today_by_staus("due")
|
||||
|
||||
# Smo oddali obrazec?
|
||||
if request.method == 'POST':
|
||||
#get card_if from the card, rendered from the template, data actually from the template...
|
||||
card_id = request.form.get('card_id', None)
|
||||
if not card_id:
|
||||
raise Exception("card_id je nujen!")
|
||||
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?
|
||||
|
||||
|
|
Loading…
Reference in New Issue