-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.py
326 lines (281 loc) · 10.9 KB
/
controllers.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import re
from marshmallow.exceptions import ValidationError
import pendulum
import views
class TaskController:
"""Logic connecting views, user input and Task data.
Args:
data_repo (:obj:`DataRepo`): Collection of data bindings.
Attributes:
tasks (:obj:`list` of :obj:`Task`): Current set of Task objects.
"""
def __init__(self, data_repo):
self.data_repo = data_repo
try:
self.tasks = self.data_repo.get_records()
except ValidationError as err:
print("Problem with source data: {}".format(err))
exit(1)
def render_view(self, view, confirmation=False, error=None):
"""Call current view's print to screen method, passing in any confirmation or error messages.
Args:
view (:obj:`View`): Currently selected view.
confirmation (bool): Confirmation signal for task actions.
error (:obj:): Any error string, exception or dict returned from task actions.
Returns:
present_view (:obj:`method`): Print view to screen and get user input.
"""
if confirmation:
return view.present_view(confirmation=confirmation)
if error:
return view.present_view(error=error)
return view.present_view()
def start(self):
"""Present app root view and bind options to methods.
"""
index_choices = {
"a": self.add_new_task,
"b": self.search_existing,
"c": self.quit,
}
index_view = views.MainView(index_choices)
user_choice = self.render_view(index_view)
index_choices[user_choice]()
def add_new_task(self):
"""Create a new task from user input.
Returns:
start (:obj:`method`): Redirect to main root view.
"""
new_task_view = views.NewTaskView()
new_task_success = False
view_options = None
while not new_task_success:
if view_options:
task = self.render_view(new_task_view, **view_options)
else:
task = self.render_view(new_task_view)
try:
self.tasks.append(self.data_repo.add_record(task))
new_task_success = True
except ValidationError as err:
view_options = {"confirmation": False, "error": err}
continue
else:
view_options = {"confirmation": True, "error": None}
self.render_view(new_task_view, **view_options)
return self.start()
def search_existing(self):
"""Present list of search methods and handle choice.
Collects search results and presents calls to handle_search_results.
"""
search_methods = {
"a": self.date_search,
"b": self.date_range_search,
"c": self.time_search,
"d": self.text_search,
"e": self.regex_search,
"f": self.start,
}
search_view = views.SearchView(search_methods)
user_choice = self.render_view(search_view)
if user_choice == "f":
return search_methods[user_choice]()
else:
search_result = search_methods[user_choice](search_view)
while not search_result:
user_choice = self.render_view(search_view, error="No results found")
if user_choice == "f":
return search_methods[user_choice]()
search_result = search_methods[user_choice](search_view)
editing = True
while editing:
editing = self.handle_search_results(search_result)
self.search_existing()
def handle_search_results(self, search_result):
"""Present search results to user, process edit/delete requests.
Args:
search_result (:obj:`list` of :obj:`Task`): Tasks matching search parameters.
Returns:
(bool): True/False switch to stay in edit mode or return to search results.
"""
result_choices = {"e": self.edit_task, "d": self.delete_task}
result_view = views.ResultView(search_result)
result_action, task = self.render_view(result_view)
if not result_action:
return False
edit_confirmed = False
action_result = result_choices[result_action](task)
while not edit_confirmed:
if not action_result:
return False
if isinstance(action_result, dict):
action_result = self.edit_task(
action_result["task"], action_result["error"]
)
continue
elif action_result in "back":
return True
else:
edit_confirmed = True
return edit_confirmed
def sort_result(self, results, field):
"""Sort search results by given Task attribute
Args:
results (:obj:`list` of :obj:`Task`): Tasks matching given search criteria
field (str): Name of Task attribute to sort by.
Returns:
(:obj:`list` of :obj:`Task:): List of Task objects sorted by attribute.
"""
return sorted(results, key=lambda x: getattr(x, field))
def date_search(self, view):
"""Present view for date search parameter input.
Args:
view (:obj:`View`): View instance
Returns:
result (:obj:`list` of :obj:`Task`): Task objects matching search criteria
None: If negative search result.
"""
date = view.exact_date()
try:
date_stamp = pendulum.from_format(date, "DD/MM/YYYY").naive().timestamp()
except ValueError as err:
print("\n{}".format(err))
return None
result = [task for task in self.tasks if task.date.timestamp() == date_stamp]
if result:
return self.sort_result(result, 'date')
return None
def date_range_search(self, view):
"""Present view for date range search parameters.
Args:
view (:obj:`View`): View instance
Returns:
result (:obj:`list` of :obj:`Task`): Task objects matching search criteria
None: If negative search result.
Raises:
ValueError: Raised if nonsensical date range is passed.
"""
start_date, end_date = view.date_range()
try:
start_date_stamp = pendulum.from_format(
start_date, "DD/MM/YYYY"
).timestamp()
end_date_stamp = pendulum.from_format(end_date, "DD/MM/YYYY").timestamp()
if start_date_stamp > end_date_stamp:
raise ValueError("Start date must be earlier than end date!")
except ValueError as err:
print("\n{}".format(err))
return None
result = [
task
for task in self.tasks
if start_date_stamp <= task.date.timestamp() <= end_date_stamp
]
if result:
return self.sort_result(result, 'date')
return None
def time_search(self, view):
"""Present view for time spent search.
Args:
view (:obj:`View): View instance
Returns:
result (:obj:`list` of :obj:`Task`): Task objects matching search criteria
None: If negative search result.
"""
time_value = view.time_spent_lookup()
try:
result = [task for task in self.tasks if task.time_spent == int(time_value)]
except ValueError:
print("Please enter correct time value in whole minutes.")
return None
if result:
return self.sort_result(result, 'date')
return None
def text_search(self, view):
"""Present view for text search string input.
Args:
view (:obj:`View`): View instance
Returns:
result (:obj:`list` of :obj:`Task`): Task objects matching search criteria
None: If negative search result.
"""
match_text = view.exact_match()
result = [
task
for task in self.tasks
if any(
match_text.lower() in str(val).lower() for val in vars(task).values()
)
]
if result:
return self.sort_result(result, 'date')
return None
def regex_search(self, view):
"""Present view for regex string input.
Args:
view (:obj:`View`): View instance
Returns:
result (:obj:`list` of :obj:`Task`): Task objects matching search criteria
None: If negative search result.
"""
regex_pattern = view.regex_pattern()
try:
pattern_match = re.compile(regex_pattern, re.IGNORECASE)
except re.error:
print("Invalid regex pattern entered, please check and try again.")
return None
else:
result = [
task
for task in self.tasks
if any(
pattern_match.findall(val)
for val in vars(task).values()
if isinstance(val, str)
)
]
if result:
return self.sort_result(result, 'date')
return None
def edit_task(self, task, error=None):
"""Calls EditView and affects changes requested by user.
Args:
task (:obj:`Task`): Task object to edit
error: Any error details from a failed edit
Returns:
"back" (str): String literal to indicate user is finished editing.
"""
edit_view = views.EditView(task)
task_changes = self.render_view(edit_view, error=error)
while task_changes:
if len(task_changes["content"]) == 0:
task_changes["content"] = None
try:
valid_data = self.data_repo.validate_fields(
{task_changes["field"]: task_changes["content"]}
)
except ValidationError as err:
error = err
task_changes = self.render_view(edit_view, error=error)
continue
setattr(task, task_changes["field"], valid_data[task_changes["field"]])
self.data_repo.save_changes(self.tasks)
print("Task {} edited.".format(task_changes["field"]))
task_changes = self.render_view(edit_view, error=error)
return "back"
def delete_task(self, task):
"""Deletes specified task from system.
Args:
task (:obj:`Task`): Task to delete
Returns:
False when record no longer exists.
"""
for idx, cur_task in enumerate(self.tasks):
if cur_task is task:
del self.tasks[idx]
self.data_repo.save_changes(self.tasks)
print("\nDeleted.\n")
return False
def quit(self):
"""Exit app"""
exit()