-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeck.py
116 lines (105 loc) · 4.91 KB
/
Deck.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
# Program FlashCards
#
# Copyright 2020 Ertugrul Harman
#
# E-mail : [email protected]
# Twitter : https://twitter.com/harmancode
# Web : https://harman.page
#
# This file is part of Flashcards.
#
# Flashcards is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from datetime import datetime
from typing import Optional
from Flashcard import Flashcard
class Deck:
MAXIMUM_LENGTH_OF_DECK_TITLE = 50
MAXIMUM_LENGTH_OF_DECK_SHORT_TITLE = 20
MAXIMUM_AMOUNT_OF_FLASHCARDS = 10000
def __init__(self, deck_id, title, last_study_datetime: Optional[datetime] = None):
"""
Deck class represents every deck in the Program, that holds flashcards. Decks are simply sets of flashcards.
:param int deck_id: Unique identifer for the Deck. Provided by the database.
:param str title: Title of the deck. Max length is kept in a class constant: MAXIMUM_LENGTH_OF_DECK_TITLE
:param Optional[datetime] last_study_datetime: When this deck was studied for the last time. It can be None.
"""
self.deck_id = deck_id
self.title = title
self.flashcards: [Flashcard] = []
self.due_flashcards: [Flashcard] = []
# last_study_datetime will hold the last study date and time. For compatibility with SQlite3,
# ISO8601 string format will be used as follows:
# YYYY-MM-DD HH:MM:SS.SSS
self.last_study_datetime = last_study_datetime
def get_truncated_title(self) -> str:
"""
Truncates title according to the Deck.MAXIMUM_LENGTH_OF_DECK_SHORT_TITLE value. It is used where limited space is
available on the view.
:return: Title of the deck truncated according to the Deck.MAXIMUM_LENGTH_OF_DECK_SHORT_TITLE value
:rtype: str
"""
truncated_deck_title = self.title[:Deck.MAXIMUM_LENGTH_OF_DECK_SHORT_TITLE]
if len(self.title) > Deck.MAXIMUM_LENGTH_OF_DECK_SHORT_TITLE:
truncated_deck_title += "..."
return truncated_deck_title
def record_last_study_datetime(self, database_manager) -> None:
"""
Update deck's last.study_datetime record in the database.
:param database_manager: DatabaseManager object kept in the main controller (Program)
:type database_manager: DatabaseManager
"""
database_manager.update_deck_in_db(self.deck_id, self.title, self.last_study_datetime)
def set_last_study_datetime(self, database_manager) -> None:
"""
Updates deck's last_study_datetime attribute in memory and in database.
:param database_manager: DatabaseManager object kept in the main controller (Program)
:type database_manager: DatabaseManager
"""
self.last_study_datetime = datetime.now()
self.record_last_study_datetime(database_manager)
def get_last_study_datetime_as_formatted_string(self) -> str:
"""
See return statement.
:return: last_study_datetime attribute as string in "%m/%d/%Y" date string format
:rtype: str
"""
# last_study_datetime_string = self.last_study_datetime.strftime("%m/%d/%Y, %H:%M")
last_study_datetime_string = ""
if self.last_study_datetime is not None:
last_study_datetime_string = self.last_study_datetime.strftime("%m/%d/%Y")
return last_study_datetime_string
def set_due_flashcards(self, database_manager) -> None:
"""
Loads due flashcards from the database and puts them in self.due_flashcards list attribute.
:param database_manager: DatabaseManager object kept in the main controller (Program)
:type database_manager: DatabaseManager
"""
database_manager.load_due_flashcards(self)
def append_to_flashcards(self, flashcard) -> int:
"""
Adds a new flashcard object to the list of flashcards
:param flashcard: Flashcard object to be added
:type flashcard: Flashcard
:return: Error codes; 0 for success, 1 for too many flashcards, 2 for object type error.
:rtype: int
"""
if isinstance(flashcard, Flashcard):
if len(self.flashcards) < Deck.MAXIMUM_AMOUNT_OF_FLASHCARDS:
self.flashcards.append(flashcard)
return 0
else:
print("Too many flashcards")
return 1
else:
return 2