models.py
5.14 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
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship
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
#
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
labels = relationship("ProjectLabel", back_populates="project")
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
labels = relationship("Label", back_populates="category")
projects = relationship("ProjectLabel", back_populates="category")
class Label(db.Model):
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'))
category = relationship("Category", back_populates="labels")
projects = relationship("ProjectLabel", back_populates="label")
class ProjectLabel(db.Model):
project_id = db.Column(db.Integer, db.ForeignKey('project.id'), primary_key=True)
category_id = db.Column(db.Integer, db.ForeignKey('category.id'), primary_key=True)
label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
project = relationship("Project", back_populates="labels")
category = relationship("Category", back_populates="projects")
label = relationship("Label", back_populates="projects")
#
# Agents
#
class AgentBap(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16))
agents = relationship("Agent", back_populates="bap")
class AgentGrade(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16))
agents = relationship("Agent", back_populates="grade")
class AgentStatus(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16))
agents = relationship("Agent", back_populates="status")
class Company(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16))
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")
@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):
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)
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']