-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.py
96 lines (81 loc) · 3.01 KB
/
controller.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
class Controller:
def __init__(self, model):
""""The goal of this class is to coordinate the model's and the view's
efforts, routing the current update to an appropriate action."""
self.model = model
self.views = { }
self.model.setController(self)
def addView(self, view):
self.views[view.id] = view
view.setController(self)
# MODEL FUNCTIONS
def tryToUnlock(self, user):
"""Tries to unlock the attendance list."""
answer = 'Not enabled!'
if user in self.model.admins:
self.model.lockedAttendance = False
answer = 'Unlocked! :D'
return answer
def tryToLock(self, user):
"""Tries to lock the attendance list."""
answer = 'What are you doing here?'
if user in self.model.admins:
self.model.lockedAttendance = True
answer = 'Locked! :x'
return answer
def isLocked(self):
"""Checks if the attendance list is locked."""
return self.model.lockedAttendance
def saveUser(self, answers):
"""Saves the answer for the given user. Must be a list as
described: [int(id), str(name), str(email), str(origin)]."""
queries = ('id', 'name', 'email', 'origin')
user = { }
for i in range(len(queries)):
user[queries[i]] = answers[i]
self.model.addUser(user)
# VIEW FUNCTIONS
def deal(self, view, text):
"""Central function for dealing with regular user messages."""
if text == '/start':
return self.start(view)
elif text == '/sign':
return self.sign(view)
elif view.isInFillMode():
return self.fill(view, text)
else:
return view.THANK_YOU
def start(self, view):
"""Answers to the /start command."""
if view.id in self.model.getIds():
view.leaveFillMode()
return view.THANK_YOU
else:
view.firstQuestion()
return self.fill(view, view.id)
def sign(self, view):
"""Answers to the /sign command."""
if view.id in self.model.getIds():
view.leaveFillMode()
if not view.isInFillMode():
self.saveUser(view.getAnswers())
self.model.signAttendance(view.id)
return view.SIGNED_MESSAGE
else:
return view.ANSWER_EVERYTHING
def fill(self, view, text):
"""Implements the workflow for filling the attendance list."""
view.addAnswer(text)
if view.currentQuestion < len(view.POSSIBLE_QUERIES):
question = view.POSSIBLE_QUERIES[view.currentQuestion]
if question == 'name':
answer = view.NAME_MESSAGE
elif question == 'email':
answer = view.EMAIL_MESSAGE
elif question == 'origin':
answer = view.ORIGIN_MESSAGE
view.nextQuestion()
else:
answer = view.THANK_YOU
view.leaveFillMode()
return answer