Blame view

app/models.py 7.25 KB
76ef4fbe   hitier   Click, Sqlalchemy...
1
from flask_sqlalchemy import SQLAlchemy
454728d5   hitier   Use sqlalchemy re...
2
from sqlalchemy.orm import relationship
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
#
a2074730   hitier   Feed projects labels
54

a53e2fff   hitier   New category/labe...
55
class Project(db.Model, Formable):
a2074730   hitier   Feed projects labels
56
57
58
59
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    labels = relationship("ProjectLabel", back_populates="project")

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

3c53ad45   hitier   Project edit work...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    def from_request(self, form_request):
        """
        overide parent method to deal with category labels

        :param form_request:
        :return:
        """
        self.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.labels.append(n_pl)


a53e2fff   hitier   New category/labe...
80
81
82
83
    def to_struct(self):
        """
        overide parent method to include one key: agent.fullname

3c53ad45   hitier   Project edit work...
84
85
        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...
86
87
88
89
        where category key contains labels for that project.
        :return:
        """
        _struct = super(Project, self).to_struct()
3c53ad45   hitier   Project edit work...
90
        _struct['labels'] = [_l.label.name for _l in self.labels]
9b43874a   hitier   Change project_st...
91
        _struct['category_labels'] = {}
a53e2fff   hitier   New category/labe...
92
93
94
95
96
        for _c in Category.query.all():
            category_labels = []
            for _l in self.labels:
                if _l.label in [_cl.label for _cl in _c.labels]:
                    category_labels.append(_l.label.name)
9b43874a   hitier   Change project_st...
97
            _struct['category_labels'][_c.name] = category_labels
a53e2fff   hitier   New category/labe...
98
99
100
101
102
103
104
105
106
        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...
107
    id = db.Column(db.Integer, primary_key=True)
a53e2fff   hitier   New category/labe...
108
109
110
111
    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="labels")
    label = relationship("Label", back_populates="projects")
d6b9daca   hitier   Feed categories a...
112
113


a2074730   hitier   Feed projects labels
114
class Label(db.Model):
d6b9daca   hitier   Feed categories a...
115
    id = db.Column(db.Integer, primary_key=True)
a2074730   hitier   Feed projects labels
116
    name = db.Column(db.String, unique=True)
d6b9daca   hitier   Feed categories a...
117
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
a2074730   hitier   Feed projects labels
118
    projects = relationship("ProjectLabel", back_populates="label")
a53e2fff   hitier   New category/labe...
119
    categories = relationship("CategoryLabel", back_populates="label")
a2074730   hitier   Feed projects labels
120
121


a53e2fff   hitier   New category/labe...
122
123
124
125
126
127
128
129
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
130
    label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
a53e2fff   hitier   New category/labe...
131
132
    category = relationship("Category", back_populates="labels")
    label = relationship("Label", back_populates="categories")
a2074730   hitier   Feed projects labels
133

a53e2fff   hitier   New category/labe...
134
135
136
137
138
139

class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    labels = relationship("Label", back_populates="category")
    labels = relationship("CategoryLabel", back_populates="category")
a2074730   hitier   Feed projects labels
140

d6b9daca   hitier   Feed categories a...
141

a2074730   hitier   Feed projects labels
142
143
144
#
# Agents
#
d6b9daca   hitier   Feed categories a...
145

ad3a824d   hitier   Add more agent fi...
146
147
148
class AgentBap(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(16))
f3dbebc2   hitier   New agent relatio...
149
    agents = relationship("Agent", back_populates="bap")
ad3a824d   hitier   Add more agent fi...
150
151
152
153
154


class AgentGrade(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(16))
f3dbebc2   hitier   New agent relatio...
155
    agents = relationship("Agent", back_populates="grade")
ad3a824d   hitier   Add more agent fi...
156
157
158
159
160


class AgentStatus(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(16))
f3dbebc2   hitier   New agent relatio...
161
    agents = relationship("Agent", back_populates="status")
ad3a824d   hitier   Add more agent fi...
162
163
164
165
166


class Company(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(16))
f3dbebc2   hitier   New agent relatio...
167
    agents = relationship("Agent", back_populates="company")
ad3a824d   hitier   Add more agent fi...
168
169


142e2e9d   hitier   New Formable clas...
170
class Agent(db.Model, Formable):
2784d9cc   hitier   Add models for ch...
171
172
173
    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...
174
175
    virtual = db.Column(db.Integer)  # integer boolean
    permanent = db.Column(db.Integer)  # integer boolean
ad3a824d   hitier   Add more agent fi...
176
177
178
179
    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...
180
181
182
183
    grade = relationship("AgentGrade", back_populates="agents")
    bap = relationship("AgentBap", back_populates="agents")
    status = relationship("AgentStatus", back_populates="agents")
    company = relationship("Company", back_populates="agents")
2784d9cc   hitier   Add models for ch...
184

ca0797d6   hitier   New agent edit form
185
    @property
ca0797d6   hitier   New agent edit form
186
187
188
    def fullname(self):
        return f"{self.secondname} {self.firstname}"

bb0ea5a5   hitier   New charge add form
189
190
191
192
    @property
    def namefull(self):
        return f"{self.firstname} {self.secondname}"

142e2e9d   hitier   New Formable clas...
193
194
195
    # 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
196

142e2e9d   hitier   New Formable clas...
197
    def to_struct(self):
ca0797d6   hitier   New agent edit form
198
        """
142e2e9d   hitier   New Formable clas...
199
        overide parent method to include one key: agent.fullname
ca0797d6   hitier   New agent edit form
200
201
        :return:
        """
142e2e9d   hitier   New Formable clas...
202
203
        _struct = super(Agent, self).to_struct()
        _struct['fullname'] = self.fullname
142e2e9d   hitier   New Formable clas...
204
        return _struct
ca0797d6   hitier   New agent edit form
205

2784d9cc   hitier   Add models for ch...
206

2784d9cc   hitier   Add models for ch...
207
208
209
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...
210
    abbr = db.Column(db.String(50), unique=True)
2784d9cc   hitier   Add models for ch...
211
212


2784d9cc   hitier   Add models for ch...
213
214
215
216
217
218
219
220
221
222
223
class Capacity(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)


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...
224
class Charge(db.Model, Formable):
2784d9cc   hitier   Add models for ch...
225
226
227
228
    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...
229
    capacity_id = db.Column(db.Integer, db.ForeignKey('capacity.id'))
2784d9cc   hitier   Add models for ch...
230
231
    period_id = db.Column(db.Integer, db.ForeignKey('period.id'))
    charge_rate = db.Column(db.Integer)
12de0b1d   hitier   Now charge add ro...
232

142e2e9d   hitier   New Formable clas...
233
234
235
    # Overwrite Formable default to fit our own members
    #
    export_keys = ['agent_id', 'project_id', 'service_id', 'capacity_id', 'period_id', 'charge_rate']