-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.py
203 lines (173 loc) · 7.66 KB
/
card.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from db import db
from datetime import datetime, timedelta
from supermemo2 import SMTwo
from sqlalchemy.sql.functions import func
# import pandas as pd
import logging
logging.basicConfig(level=logging.INFO)
class Media(db.Model):
__tablename__ = 'media'
id = db.Column(db.Integer, primary_key=True)
link = db.Column(db.Text, nullable=False)
card_id = db.Column(db.Integer,
db.ForeignKey('card.id', ondelete='CASCADE'),
nullable=False)
# card = db.relationship('Card', back_populates='media')
class History(db.Model):
__tablename__ = 'history'
id = db.Column(db.Integer, primary_key=True)
card_id = db.Column(db.Integer, db.ForeignKey('card.id',
ondelete='CASCADE'))
review_date = db.Column(db.DateTime, nullable=False) # yyyy-mm-dd format
quality = db.Column(db.Integer, nullable=False)
# card = db.relationship('Card', back_populates='history')
class Card(db.Model):
__tablename__ = 'card'
## identifiers and user-facing card components ##
id = db.Column(db.Integer, primary_key=True)
english = db.Column(db.Text, nullable=False)
media = db.relationship(
'Media',
uselist=True, # indicates should be loaded as list
# (not scalar)
backref='card',
# back_populates='card',
# cascade = 'all, delete, delete-orphan',
passive_deletes=True)
deck_id = db.Column(db.Integer,
db.ForeignKey('deck.id', ondelete='CASCADE'),
nullable=False)
# practice_id = db.Column(db.Integer,
# db.ForeignKey('deck.id', ondelete='CASCADE'),
# nullable=True)
# deck = db.relationship('Deck', back_populates='cards')
## performance-related ##
last_review_date = db.Column(db.DateTime, nullable=True)
last_EF = db.Column(db.Float, default=-1, nullable=False)
last_interval = db.Column(db.Integer, default=-1, nullable=False)
repetitions = db.Column(db.Integer, default=0, nullable=False)
next_review_date = db.Column(db.DateTime,
default=datetime.now,
nullable=False)
quality = db.Column(db.Integer, default=0, nullable=False)
review_again = db.Column(db.Boolean, default=False)
history = db.relationship(
'History',
uselist=True,
# cascade = 'all, delete, delete-orphan',
# backref = 'card',
# back_populates = 'card',
passive_deletes=True)
## other helpful attributes ##
description = db.Column(db.Text, nullable=True)
hint = db.Column(db.Text, nullable=True)
importance = db.Column(db.Integer, nullable=True)
def __init__(self, english, mp4s, deck_id, **kwargs):
# user only needs to pass in english, media, link it to a deck somehow ?
# and can optionally pass in description, hint, and importance
# self.media = sqlalchemy.orm.collections.InstrumentedList object
kwargs['english'] = english
kwargs['deck_id'] = deck_id
card_id = self.generate_id()
self.id = card_id
super(Card, self).__init__(**kwargs)
db.session.commit()
# todo: add something to check if the super() is updating self.id
# right now when we are testing, self.id and card_id are the same so
# we don't know if super() is overriding anything (with the same value)
logging.debug('self.id', self.id)
logging.debug('card_id', card_id)
# all_cards = Card.query.all()
# logging.info('all_cards')
# logging.info(all_cards)
# if all_cards:
# this_card = all_cards[-1]
# logging.info('card should have empty media')
# logging.info(this_card.media)
# else:
# logging.info('no cards created')
# # print('inside card constructor, now printing links:')
# for link in mp4s:
# # TODO: add error handling to ensure mp4s is not empty
# logging.info('attempting to add link ' + link + ' to card ' +
# english)
# try:
# db.session.add(Media(link=link, card_id=card_id))
# except Exception as e:
# logging.info(e)
db.session.commit()
# self.deck_id = ??
# TODO: how do we add a deck id ???
def generate_id(self):
""" stupid simple id generator that returns autoincrementing integers
for card ids to get around inability to access self.id inside the
__init__() method
"""
db_size = db.session.query(func.count(Card.id)).scalar()
if db_size == 0:
return 1
else:
# if the db contains any cards, we have to find the current highest
# id separately because its possible that max_id != # cards (e.g.
# if we deleted any cards)
max_id = db.session.query(func.max(Card.id)).scalar()
return max_id + 1
def add_media(self, mp4s):
for link in mp4s:
# TODO: add error handling to ensure mp4s is not empty
logging.info('attempting to add link ' + link + ' to card ' +
self.english)
try:
db.session.add(Media(link=link, card_id=self.id))
except Exception as e:
logging.info(e)
db.session.commit()
# def add_to_practice(self):
# """ add the card to its deck's practice table """
# self.practice_id = self.deck_id
# db.session.commit()
# def remove_from_practice(self):
# """ remove the card from its deck's practice table """
# self.practice_id = None
# db.session.commit()
def update_quality(self, quality):
self.quality = quality
if self.last_review_date == None: # new card
sm2 = SMTwo.first_review(self.quality, datetime.today().date())
else:
sm2 = SMTwo(self.last_EF, self.last_interval, self.repetitions)
# sm2 = SMTwo(float(self.last_EF), int(self.last_interval),
# int(self.repetitions))
sm2.review(self.quality, datetime.today().date())
self.last_review_date = datetime.today().date()
self.last_EF = sm2.easiness
self.repetitions = sm2.repetitions
self.last_interval = sm2.interval
interval = timedelta(days=sm2.interval)
# TODO: update algorithm for shorter intervals (e.g. 10 min)
self.next_review_date = (self.last_review_date + interval)
if quality <= 2:
self.review_again = True
else:
self.review_again = False
review_instance = History(review_date=self.last_review_date,
quality=self.quality,
card_id=self.id)
db.session.add(review_instance)
db.session.commit()
logging.info('self.next_review_date: ' + str(self.next_review_date))
logging.info('self.review_again: ' + str(self.review_again))
# TODO: create function to get Hint
def get_hint(self):
""" placeholder, when user clicks 'Get Hint' button, hint appears
we update the hint variable from the ASL Browser notes text file
"""
#Create dictionary from the ASL Browser notes which stores the mp4 file and hint text
hint = ''
self.hint = hint
db.session.commit()
def delete(card):
# delete all associated media and history objects first
# now delete the card itself
# TODO: add a custom function for mass deletion (e.g. clearing an entire db)
pass