forked from exhuma/lost-tracker
/
core.py
175 lines (147 loc) · 4.7 KB
/
core.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
from lost_tracker.models import (
Group,
Station,
Form,
get_state,
GroupStation,
get_form_score_by_group,
STATE_FINISHED,
STATE_UNKNOWN,
STATE_ARRIVED,
DIR_A,
DIR_B)
def get_matrix(stations, groups):
"""
Returns a 2-dimensional array containing an entry for each group.
Each row has the group as first element, and all stations as subsequent
elements. For example::
[
[group1, station1, station2, station3, ... ],
[group2, station1, station2, station3, ... ],
[group3, station1, station2, station3, ... ],
...
]
"""
# TODO: make this a list of dicts or a list of namedtuples!
state_matrix = []
for group in groups:
tmp = [group]
for station in stations:
tmp.append(get_state(group.id, station.id))
state_matrix.append(tmp)
return state_matrix
def get_state_sum(state_matrix):
"""
Creates a list where each element contains the sum of "unknown", "arrived"
and "finished" states for each station.
"""
# TODO: make this a list of namedtuples!
sums = []
if state_matrix:
sums = [[0, 0, 0] for _ in state_matrix[0][1:]]
for row in state_matrix:
for i, state in enumerate(row[1:]):
if not state:
sums[i][STATE_UNKNOWN] += 1
continue
if state.state == STATE_UNKNOWN:
sums[i][STATE_UNKNOWN] += 1
elif state.state == STATE_ARRIVED:
sums[i][STATE_ARRIVED] += 1
elif state.state == STATE_FINISHED:
sums[i][STATE_FINISHED] += 1
return sums
def get_grps():
"""
Returns all groups from the database as :py:class:`Group` instances.
"""
groups = Group.query
groups = groups.order_by(Group.order)
groups = groups.all()
return groups
def get_grps_by_id(group_id):
"""
Returns a group from the database as :py:class:`Group` instance by his id.
"""
group = Group.query
group = group.filter_by(id=group_id)
group = group.first()
return group
def add_grp(grp_name, contact, phone, direction, start_time, session):
"""
Creates a new group in the database.
"""
if direction not in (DIR_A, DIR_B):
raise ValueError('{0!r} is not among the supported values '
'for "direction" which are: {1!r}, {2!r}'.format(
DIR_A, DIR_B))
new_grp = Group(grp_name, contact, phone, direction, start_time)
session.add(new_grp)
return ("Group {0} with Contact {1} / {2} was successfully added into the "
"DB. The given start-time is {3} and the direction is {4}".format(
grp_name,
contact,
phone,
start_time,
direction))
def get_stations():
"""
Returns all stations from the database as :py:class:`Station` instances.
"""
stations = Station.query
stations = stations.order_by(Station.order)
stations = stations.all()
return stations
def get_stat_by_name(name):
"""
Returns a :py:class:`Station` by class name. Can be ``None`` if no
matching station is found.
"""
qry = Station.query
qry = qry.filter_by(name=name)
qry = qry.first()
return qry
def add_station(stat_name, contact, phone, session):
"""
Creates a new :py:class:`Station` in the database.
"""
new_station = Station(stat_name, contact, phone)
session.add(new_station)
return "Station {0} added. Contact: {1} / {2}".format(
stat_name, contact, phone)
def add_form_db(form_id, name, max_score, session):
tmp_form = get_form_by_id(form_id)
if tmp_form:
tmp_form.name = name
tmp_form.max_score = max_score
return "Form {0} updated: {1} - max: {2}".format(
form_id, name, max_score)
else:
new_form = Form(form_id, name, max_score)
session.add(new_form)
return "Form added: {0} - {1} - max: {2}".format(
form_id, name, max_score)
def get_forms():
"""
Returns all forms from the database as :py:class`Form` instances.
"""
forms = Form.query
forms = forms.order_by(Form.id)
forms = forms.all()
return forms
def get_form_by_id(id):
"""
Returns a :py:class:`Form` by class id.
"""
qry = Form.query
qry = qry.filter_by(id=id)
qry = qry.first()
return qry
def get_score_by_group(group_id):
"""
Returns the actual score for a group.
"""
qry = GroupStation.query
qry = qry.filter_by(group_id=group_id)
station_score = qry.all()
form_score = get_form_score_by_group(group_id)