Blame view

app/models.py 5.14 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
44
45
46
47
48
49
50
# 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")


d6b9daca   hitier   Feed categories a...
51
52
class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
a2074730   hitier   Feed projects labels
53
54
55
    name = db.Column(db.String)
    labels = relationship("Label", back_populates="category")
    projects = relationship("ProjectLabel", back_populates="category")
d6b9daca   hitier   Feed categories a...
56
57


a2074730   hitier   Feed projects labels
58
class Label(db.Model):
d6b9daca   hitier   Feed categories a...
59
    id = db.Column(db.Integer, primary_key=True)
a2074730   hitier   Feed projects labels
60
    name = db.Column(db.String, unique=True)
d6b9daca   hitier   Feed categories a...
61
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
a2074730   hitier   Feed projects labels
62
63
64
65
66
67
68
69
70
71
72
73
74
    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")

d6b9daca   hitier   Feed categories a...
75

a2074730   hitier   Feed projects labels
76
77
78
#
# Agents
#
d6b9daca   hitier   Feed categories a...
79

ad3a824d   hitier   Add more agent fi...
80
81
82
class AgentBap(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(16))
f3dbebc2   hitier   New agent relatio...
83
    agents = relationship("Agent", back_populates="bap")
ad3a824d   hitier   Add more agent fi...
84
85
86
87
88


class AgentGrade(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(16))
f3dbebc2   hitier   New agent relatio...
89
    agents = relationship("Agent", back_populates="grade")
ad3a824d   hitier   Add more agent fi...
90
91
92
93
94


class AgentStatus(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(16))
f3dbebc2   hitier   New agent relatio...
95
    agents = relationship("Agent", back_populates="status")
ad3a824d   hitier   Add more agent fi...
96
97
98
99
100


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


142e2e9d   hitier   New Formable clas...
104
class Agent(db.Model, Formable):
2784d9cc   hitier   Add models for ch...
105
106
107
    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...
108
109
    virtual = db.Column(db.Integer)  # integer boolean
    permanent = db.Column(db.Integer)  # integer boolean
ad3a824d   hitier   Add more agent fi...
110
111
112
113
    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...
114
115
116
117
    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...
118

ca0797d6   hitier   New agent edit form
119
    @property
ca0797d6   hitier   New agent edit form
120
121
122
    def fullname(self):
        return f"{self.secondname} {self.firstname}"

bb0ea5a5   hitier   New charge add form
123
124
125
126
    @property
    def namefull(self):
        return f"{self.firstname} {self.secondname}"

142e2e9d   hitier   New Formable clas...
127
128
129
    # 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
130

142e2e9d   hitier   New Formable clas...
131
    def to_struct(self):
ca0797d6   hitier   New agent edit form
132
        """
142e2e9d   hitier   New Formable clas...
133
        overide parent method to include one key: agent.fullname
ca0797d6   hitier   New agent edit form
134
135
        :return:
        """
142e2e9d   hitier   New Formable clas...
136
137
        _struct = super(Agent, self).to_struct()
        _struct['fullname'] = self.fullname
142e2e9d   hitier   New Formable clas...
138
        return _struct
ca0797d6   hitier   New agent edit form
139

2784d9cc   hitier   Add models for ch...
140

2784d9cc   hitier   Add models for ch...
141
142
143
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...
144
    abbr = db.Column(db.String(50), unique=True)
2784d9cc   hitier   Add models for ch...
145
146


2784d9cc   hitier   Add models for ch...
147
148
149
150
151
152
153
154
155
156
157
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...
158
class Charge(db.Model, Formable):
2784d9cc   hitier   Add models for ch...
159
160
161
162
    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...
163
    capacity_id = db.Column(db.Integer, db.ForeignKey('capacity.id'))
2784d9cc   hitier   Add models for ch...
164
165
    period_id = db.Column(db.Integer, db.ForeignKey('period.id'))
    charge_rate = db.Column(db.Integer)
12de0b1d   hitier   Now charge add ro...
166

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