Blame view

app/models.py 11.2 KB
76ef4fbe   hitier   Click, Sqlalchemy...
1
from flask_sqlalchemy import SQLAlchemy
d1067150   hitier   Form_manager() ap...
2
from sqlalchemy.orm import relationship, column_property
76ef4fbe   hitier   Click, Sqlalchemy...
3
4
5
6

db = SQLAlchemy()


a2074730   hitier   Feed projects labels
7
#
142e2e9d   hitier   New Formable clas...
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
#
#

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
        """
e20cd5d4   hitier   Now import/export...
34
        _struct = {'id': self.id}
142e2e9d   hitier   New Formable clas...
35
36
37
38
39
40
41
        for key in self.export_keys:
            _value = getattr(self, key)
            _struct[key] = '' if _value is None else _value
        return _struct


#
a2074730   hitier   Feed projects labels
42
43
# Categorized projects
#
a53e2fff   hitier   New category/labe...
44
45
46
47
48
49
50
51
52
53
# 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
#
b304465a   hitier   Edit and save Pro...
54
class ProjectStatus(db.Model, Formable):
79f0ebb0   hitier   Added projects st...
55
56
57
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))

b304465a   hitier   Edit and save Pro...
58
59
60
    # add keys to import/export
    export_keys = ['name']

a2074730   hitier   Feed projects labels
61

a53e2fff   hitier   New category/labe...
62
class Project(db.Model, Formable):
a2074730   hitier   Feed projects labels
63
    id = db.Column(db.Integer, primary_key=True)
aadf1790   hitier   Ensure uniq proje...
64
    name = db.Column(db.String, unique=True)
4ab23e54   hitier   Rename ORM attrib...
65
    project_labels = relationship("ProjectLabel", back_populates="project")
a2074730   hitier   Feed projects labels
66

3c53ad45   hitier   Project edit work...
67
    # add keys to import/export
a53e2fff   hitier   New category/labe...
68
    export_keys = ['name']
a2074730   hitier   Feed projects labels
69

3c53ad45   hitier   Project edit work...
70
71
72
73
74
75
76
    def from_request(self, form_request):
        """
        overide parent method to deal with category labels

        :param form_request:
        :return:
        """
85ee3eec   hitier   Completed Label e...
77
        super(Project, self).from_request(form_request)
4ab23e54   hitier   Rename ORM attrib...
78
        self.project_labels.clear()
3c53ad45   hitier   Project edit work...
79
80
81
82
83
84
        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)
4ab23e54   hitier   Rename ORM attrib...
85
            self.project_labels.append(n_pl)
3c53ad45   hitier   Project edit work...
86

a53e2fff   hitier   New category/labe...
87
88
89
90
    def to_struct(self):
        """
        overide parent method to include one key: agent.fullname

3c53ad45   hitier   Project edit work...
91
92
        Mainly we add the 'labels' element containing list of label names
        and we add the 'category_labels' element containing a dict
a53e2fff   hitier   New category/labe...
93
94
95
96
        where category key contains labels for that project.
        :return:
        """
        _struct = super(Project, self).to_struct()
4ab23e54   hitier   Rename ORM attrib...
97
        _struct['labels'] = [_l.label.name for _l in self.project_labels]
9b43874a   hitier   Change project_st...
98
        _struct['category_labels'] = {}
a53e2fff   hitier   New category/labe...
99
100
        for _c in Category.query.all():
            category_labels = []
4ab23e54   hitier   Rename ORM attrib...
101
102
            for _l in self.project_labels:
                if _l.label in [_cl.label for _cl in _c.category_labels]:
a53e2fff   hitier   New category/labe...
103
                    category_labels.append(_l.label.name)
9b43874a   hitier   Change project_st...
104
            _struct['category_labels'][_c.name] = category_labels
a53e2fff   hitier   New category/labe...
105
106
107
108
109
110
111
112
113
        return _struct


class ProjectLabel(db.Model):
    """
    Labelling projects.
    On project can have many labels.
    And one label will be set to many projects
    """
d6b9daca   hitier   Feed categories a...
114
    id = db.Column(db.Integer, primary_key=True)
a53e2fff   hitier   New category/labe...
115
116
    project_id = db.Column(db.Integer, db.ForeignKey('project.id'))
    label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
4ab23e54   hitier   Rename ORM attrib...
117
118
    project = relationship("Project", back_populates="project_labels")
    label = relationship("Label", back_populates="project_labels")
d6b9daca   hitier   Feed categories a...
119
120


85ee3eec   hitier   Completed Label e...
121
class Label(db.Model, Formable):
d6b9daca   hitier   Feed categories a...
122
    id = db.Column(db.Integer, primary_key=True)
a2074730   hitier   Feed projects labels
123
    name = db.Column(db.String, unique=True)
d6b9daca   hitier   Feed categories a...
124
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
4ab23e54   hitier   Rename ORM attrib...
125
126
    project_labels = relationship("ProjectLabel", back_populates="label")
    category_labels = relationship("CategoryLabel", back_populates="label")
a2074730   hitier   Feed projects labels
127

85ee3eec   hitier   Completed Label e...
128
129
130
131
132
133
134
135
136
137
    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)
4ab23e54   hitier   Rename ORM attrib...
138
        self.category_labels.clear()
85ee3eec   hitier   Completed Label e...
139
140
141
        for _c_id in form_request.form.getlist("categories"):
            n_c = Category.query.get(int(_c_id))
            _cl = CategoryLabel(category=n_c, label=self)
4ab23e54   hitier   Rename ORM attrib...
142
            self.category_labels.append(_cl)
85ee3eec   hitier   Completed Label e...
143
144
145
146
147
148
149
150
151

    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()
9c5c9883   hitier   New label.categor...
152
        _struct['categories'] = [_c.name for _c in self.categories]
85ee3eec   hitier   Completed Label e...
153
154
        return _struct

9c5c9883   hitier   New label.categor...
155
156
157
    @property
    def categories(self):
        """
4e9d7814   hitier   New category.labe...
158
        :return: list of categories linked to that label
9c5c9883   hitier   New label.categor...
159
        """
4e9d7814   hitier   New category.labe...
160
        return [_cl.category for _cl in self.category_labels]
9c5c9883   hitier   New label.categor...
161

a2074730   hitier   Feed projects labels
162

a53e2fff   hitier   New category/labe...
163
164
165
166
167
168
169
170
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'))
a2074730   hitier   Feed projects labels
171
    label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
4ab23e54   hitier   Rename ORM attrib...
172
173
    category = relationship("Category", back_populates="category_labels")
    label = relationship("Label", back_populates="category_labels")
a2074730   hitier   Feed projects labels
174

a53e2fff   hitier   New category/labe...
175

fc06cbe5   hitier   Category edit for...
176
class Category(db.Model, Formable):
a53e2fff   hitier   New category/labe...
177
    id = db.Column(db.Integer, primary_key=True)
aadf1790   hitier   Ensure uniq proje...
178
    name = db.Column(db.String, unique=True)
4ab23e54   hitier   Rename ORM attrib...
179
    category_labels = relationship("CategoryLabel", back_populates="category")
a2074730   hitier   Feed projects labels
180

fc06cbe5   hitier   Category edit for...
181
182
183
184
185
186
187
188
189
190
    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)
4ab23e54   hitier   Rename ORM attrib...
191
        self.category_labels.clear()
fc06cbe5   hitier   Category edit for...
192
193
194
        for _l_id in form_request.form.getlist("labels"):
            n_c = Label.query.get(int(_l_id))
            _cl = CategoryLabel(label=n_c, category=self)
4ab23e54   hitier   Rename ORM attrib...
195
            self.category_labels.append(_cl)
fc06cbe5   hitier   Category edit for...
196
197
198
199
200
201
202
203
204

    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()
4ab23e54   hitier   Rename ORM attrib...
205
        _struct['labels'] = [_cl.label.name for _cl in self.category_labels]
fc06cbe5   hitier   Category edit for...
206
207
        return _struct

4e9d7814   hitier   New category.labe...
208
209
210
211
212
213
214
    @property
    def labels(self):
        """
        :return: list of labels linked to that category
        """
        return [_cl.label for _cl in self.category_labels]

d6b9daca   hitier   Feed categories a...
215

a2074730   hitier   Feed projects labels
216
#
3707ccfb   hitier   Update db model f...
217
218
219
220
221
222
223
224
225
226
227
# 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'))


#
a2074730   hitier   Feed projects labels
228
229
# Agents
#
d6b9daca   hitier   Feed categories a...
230

ee193a09   hitier   Added employments
231
232
233
234
235
236
237
238
239
240
241
242
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))


48c6f9a0   hitier   Added skills
243
244
245
246
247
248
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)


bae9b898   hitier   New routes: Respo...
249
class AgentResponsability(db.Model, Formable):
0f1c3228   hitier   Added responsabil...
250
251
252
253
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    abbr = db.Column(db.String(50), unique=True)

bae9b898   hitier   New routes: Respo...
254
255
    export_keys = ['name']

0f1c3228   hitier   Added responsabil...
256

ad3a824d   hitier   Add more agent fi...
257
258
class AgentBap(db.Model):
    id = db.Column(db.Integer, primary_key=True)
aadf1790   hitier   Ensure uniq proje...
259
    name = db.Column(db.String(16), unique=True)
f3dbebc2   hitier   New agent relatio...
260
    agents = relationship("Agent", back_populates="bap")
ad3a824d   hitier   Add more agent fi...
261
262
263
264


class AgentGrade(db.Model):
    id = db.Column(db.Integer, primary_key=True)
aadf1790   hitier   Ensure uniq proje...
265
    name = db.Column(db.String(16), unique=True)
f3dbebc2   hitier   New agent relatio...
266
    agents = relationship("Agent", back_populates="grade")
ad3a824d   hitier   Add more agent fi...
267
268
269
270


class AgentStatus(db.Model):
    id = db.Column(db.Integer, primary_key=True)
aadf1790   hitier   Ensure uniq proje...
271
    name = db.Column(db.String(16), unique=True)
f3dbebc2   hitier   New agent relatio...
272
    agents = relationship("Agent", back_populates="status")
ad3a824d   hitier   Add more agent fi...
273
274
275
276


class Company(db.Model):
    id = db.Column(db.Integer, primary_key=True)
aadf1790   hitier   Ensure uniq proje...
277
    name = db.Column(db.String(16), unique=True)
f3dbebc2   hitier   New agent relatio...
278
    agents = relationship("Agent", back_populates="company")
ad3a824d   hitier   Add more agent fi...
279
280


142e2e9d   hitier   New Formable clas...
281
class Agent(db.Model, Formable):
2784d9cc   hitier   Add models for ch...
282
283
284
    id = db.Column(db.Integer, primary_key=True)
    firstname = db.Column(db.String(100))
    secondname = db.Column(db.String(100))
f3dbebc2   hitier   New agent relatio...
285
286
    virtual = db.Column(db.Integer)  # integer boolean
    permanent = db.Column(db.Integer)  # integer boolean
ad3a824d   hitier   Add more agent fi...
287
288
289
290
    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'))
f3dbebc2   hitier   New agent relatio...
291
292
293
294
    grade = relationship("AgentGrade", back_populates="agents")
    bap = relationship("AgentBap", back_populates="agents")
    status = relationship("AgentStatus", back_populates="agents")
    company = relationship("Company", back_populates="agents")
1f54a207   hitier   New routes and tp...
295
    name = column_property(firstname + " " + secondname)
2784d9cc   hitier   Add models for ch...
296

ca0797d6   hitier   New agent edit form
297
    @property
ca0797d6   hitier   New agent edit form
298
299
300
    def fullname(self):
        return f"{self.secondname} {self.firstname}"

bb0ea5a5   hitier   New charge add form
301
302
303
304
    @property
    def namefull(self):
        return f"{self.firstname} {self.secondname}"

142e2e9d   hitier   New Formable clas...
305
306
307
    # has to be set as we inherit Formable
    #
    export_keys = ['firstname', 'secondname', 'virtual', 'permanent', 'company_id', 'status_id', 'grade_id', 'bap_id']
ca0797d6   hitier   New agent edit form
308

142e2e9d   hitier   New Formable clas...
309
    def to_struct(self):
ca0797d6   hitier   New agent edit form
310
        """
142e2e9d   hitier   New Formable clas...
311
        overide parent method to include one key: agent.fullname
ca0797d6   hitier   New agent edit form
312
313
        :return:
        """
142e2e9d   hitier   New Formable clas...
314
315
        _struct = super(Agent, self).to_struct()
        _struct['fullname'] = self.fullname
142e2e9d   hitier   New Formable clas...
316
        return _struct
ca0797d6   hitier   New agent edit form
317

2784d9cc   hitier   Add models for ch...
318

2784d9cc   hitier   Add models for ch...
319
320
321
class Service(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
1002c994   hitier   Also feed ids fro...
322
    abbr = db.Column(db.String(50), unique=True)
2784d9cc   hitier   Add models for ch...
323
324


1f54a207   hitier   New routes and tp...
325
class Capacity(db.Model, Formable):
2784d9cc   hitier   Add models for ch...
326
327
328
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)

1f54a207   hitier   New routes and tp...
329
330
    export_keys = ['name']

2784d9cc   hitier   Add models for ch...
331
332
333
334
335
336
337

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)


142e2e9d   hitier   New Formable clas...
338
class Charge(db.Model, Formable):
2784d9cc   hitier   Add models for ch...
339
340
341
342
    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'))
582b4b3a   hitier   Add capacity comp...
343
    capacity_id = db.Column(db.Integer, db.ForeignKey('capacity.id'))
2784d9cc   hitier   Add models for ch...
344
345
    period_id = db.Column(db.Integer, db.ForeignKey('period.id'))
    charge_rate = db.Column(db.Integer)
12de0b1d   hitier   Now charge add ro...
346

142e2e9d   hitier   New Formable clas...
347
348
349
    # Overwrite Formable default to fit our own members
    #
    export_keys = ['agent_id', 'project_id', 'service_id', 'capacity_id', 'period_id', 'charge_rate']
7db93273   hitier   Set charge_rate n...
350
351
352
353
354
355
356
357
358
359

    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
4e9d7814   hitier   New category.labe...
360
        self.charge_rate = 100 * float(self.charge_rate)
7db93273   hitier   Set charge_rate n...
361
        print(self.charge_rate)