models.py
11.2 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship, column_property
db = SQLAlchemy()
#
#
#
class Formable:
"""
Parent class allowing some html form facilities
"""
export_keys = []
def from_request(self, form_request):
"""
Get a form request structure and fill in our fields
:param form_request:
:return:
"""
for key in self.export_keys:
setattr(self, key, form_request.form.get(key))
def to_struct(self):
"""
Export the orm object to a structure easily used in jinja
:return: nothing
"""
_struct = {'id': self.id}
for key in self.export_keys:
_value = getattr(self, key)
_struct[key] = '' if _value is None else _value
return _struct
#
# Categorized projects
#
# There is one label list,
# each label belongs to one or more categories.
#
# The projects are labelled by one or more label.
#
# Thus this is modeled with classes
# Project, Label and Category
# And many_to_many association are done through
# ProjectLabel and CategoryLabel
#
class ProjectStatus(db.Model, Formable):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
# add keys to import/export
export_keys = ['name']
class Project(db.Model, Formable):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True)
project_labels = relationship("ProjectLabel", back_populates="project")
# add keys to import/export
export_keys = ['name']
def from_request(self, form_request):
"""
overide parent method to deal with category labels
:param form_request:
:return:
"""
super(Project, self).from_request(form_request)
self.project_labels.clear()
form_labels = []
for _c in Category.query.all():
form_labels = form_labels + form_request.form.getlist(_c.name)
for label_id in form_labels:
n_l = Label.query.get(int(label_id))
n_pl = ProjectLabel(project=self, label=n_l)
self.project_labels.append(n_pl)
def to_struct(self):
"""
overide parent method to include one key: agent.fullname
Mainly we add the 'labels' element containing list of label names
and we add the 'category_labels' element containing a dict
where category key contains labels for that project.
:return:
"""
_struct = super(Project, self).to_struct()
_struct['labels'] = [_l.label.name for _l in self.project_labels]
_struct['category_labels'] = {}
for _c in Category.query.all():
category_labels = []
for _l in self.project_labels:
if _l.label in [_cl.label for _cl in _c.category_labels]:
category_labels.append(_l.label.name)
_struct['category_labels'][_c.name] = category_labels
return _struct
class ProjectLabel(db.Model):
"""
Labelling projects.
On project can have many labels.
And one label will be set to many projects
"""
id = db.Column(db.Integer, primary_key=True)
project_id = db.Column(db.Integer, db.ForeignKey('project.id'))
label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
project = relationship("Project", back_populates="project_labels")
label = relationship("Label", back_populates="project_labels")
class Label(db.Model, Formable):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True)
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
project_labels = relationship("ProjectLabel", back_populates="label")
category_labels = relationship("CategoryLabel", back_populates="label")
export_keys = ['name']
def from_request(self, form_request):
"""
overide parent method to deal with category labels
:param form_request:
:return:
"""
super(Label, self).from_request(form_request)
self.category_labels.clear()
for _c_id in form_request.form.getlist("categories"):
n_c = Category.query.get(int(_c_id))
_cl = CategoryLabel(category=n_c, label=self)
self.category_labels.append(_cl)
def to_struct(self):
"""
overide parent method to include one key: categories
Mainly we add the 'categories' element containing list of categories names
:return:
"""
_struct = super(Label, self).to_struct()
_struct['categories'] = [_c.name for _c in self.categories]
return _struct
@property
def categories(self):
"""
:return: list of categories linked to that label
"""
return [_cl.category for _cl in self.category_labels]
class CategoryLabel(db.Model):
"""
Categorizing labels:
one label can be added to many categories
one category hosts many labels
"""
id = db.Column(db.Integer, primary_key=True)
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
category = relationship("Category", back_populates="category_labels")
label = relationship("Label", back_populates="category_labels")
class Category(db.Model, Formable):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, unique=True)
category_labels = relationship("CategoryLabel", back_populates="category")
export_keys = ['name']
def from_request(self, form_request):
"""
overide parent method to deal with category labels
:param form_request:
:return:
"""
super(Category, self).from_request(form_request)
self.category_labels.clear()
for _l_id in form_request.form.getlist("labels"):
n_c = Label.query.get(int(_l_id))
_cl = CategoryLabel(label=n_c, category=self)
self.category_labels.append(_cl)
def to_struct(self):
"""
overide parent method to include one key: labels
Mainly we add the 'labels' element containing list of labels names
:return:
"""
_struct = super(Category, self).to_struct()
_struct['labels'] = [_cl.label.name for _cl in self.category_labels]
return _struct
@property
def labels(self):
"""
:return: list of labels linked to that category
"""
return [_cl.label for _cl in self.category_labels]
#
# History
#
class AgentHistory(db.Model):
id = db.Column(db.Integer, primary_key=True)
period_id = db.Column(db.Integer, db.ForeignKey('period.id'))
agent_id = db.Column(db.Integer, db.ForeignKey('agent.id'))
status_id = db.Column(db.Integer, db.ForeignKey('agent_status.id'))
#
# Agents
#
class EmploymentType(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
bap_code = db.Column(db.String(1))
bap_name = db.Column(db.String(100))
pro_family = db.Column(db.String(100))
pro_type_code = db.Column(db.String(5))
status = db.Column(db.String(50))
old_type_code = db.Column(db.String(5))
old_name = db.Column(db.String(100))
class AgentSkill(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
abbr = db.Column(db.String(50), unique=True)
class AgentResponsability(db.Model, Formable):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
abbr = db.Column(db.String(50), unique=True)
export_keys = ['name']
class AgentBap(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16), unique=True)
agents = relationship("Agent", back_populates="bap")
class AgentGrade(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16), unique=True)
agents = relationship("Agent", back_populates="grade")
class AgentStatus(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16), unique=True)
agents = relationship("Agent", back_populates="status")
class Company(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16), unique=True)
agents = relationship("Agent", back_populates="company")
class Agent(db.Model, Formable):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(100))
secondname = db.Column(db.String(100))
virtual = db.Column(db.Integer) # integer boolean
permanent = db.Column(db.Integer) # integer boolean
company_id = db.Column(db.Integer, db.ForeignKey('company.id'))
grade_id = db.Column(db.Integer, db.ForeignKey('agent_grade.id'))
status_id = db.Column(db.Integer, db.ForeignKey('agent_status.id'))
bap_id = db.Column(db.Integer, db.ForeignKey('agent_bap.id'))
grade = relationship("AgentGrade", back_populates="agents")
bap = relationship("AgentBap", back_populates="agents")
status = relationship("AgentStatus", back_populates="agents")
company = relationship("Company", back_populates="agents")
name = column_property(firstname + " " + secondname)
@property
def fullname(self):
return f"{self.secondname} {self.firstname}"
@property
def namefull(self):
return f"{self.firstname} {self.secondname}"
# has to be set as we inherit Formable
#
export_keys = ['firstname', 'secondname', 'virtual', 'permanent', 'company_id', 'status_id', 'grade_id', 'bap_id']
def to_struct(self):
"""
overide parent method to include one key: agent.fullname
:return:
"""
_struct = super(Agent, self).to_struct()
_struct['fullname'] = self.fullname
return _struct
class Service(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
abbr = db.Column(db.String(50), unique=True)
class Capacity(db.Model, Formable):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
export_keys = ['name']
class Period(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
num_months = db.Column(db.Integer)
class Charge(db.Model, Formable):
id = db.Column(db.Integer, primary_key=True)
agent_id = db.Column(db.Integer, db.ForeignKey('agent.id'))
project_id = db.Column(db.Integer, db.ForeignKey('project.id'))
service_id = db.Column(db.Integer, db.ForeignKey('service.id'))
capacity_id = db.Column(db.Integer, db.ForeignKey('capacity.id'))
period_id = db.Column(db.Integer, db.ForeignKey('period.id'))
charge_rate = db.Column(db.Integer)
# Overwrite Formable default to fit our own members
#
export_keys = ['agent_id', 'project_id', 'service_id', 'capacity_id', 'period_id', 'charge_rate']
def from_request(self, form_request):
"""
overide parent method to deal with category labels
:param form_request:
:return:
"""
super(Charge, self).from_request(form_request)
# convert the charge_rate from etp float to integer percent
self.charge_rate = 100 * float(self.charge_rate)
print(self.charge_rate)