Commit 142e2e9d6db1cee9bd49d09232807746241d7eca

Authored by hitier
1 parent c151302b

New Formable class decorates orm classes

adding form import/export facilities.
Showing 2 changed files with 49 additions and 39 deletions   Show diff stats
app/main/routes.py
@@ -157,7 +157,7 @@ def agent_edit(agent_id=None): @@ -157,7 +157,7 @@ def agent_edit(agent_id=None):
157 db.session.add(this_agent) 157 db.session.add(this_agent)
158 db.session.commit() 158 db.session.commit()
159 # we're done 159 # we're done
160 - flash(f"Agent {this_agent.fullname} " + done_string) 160 + flash(f"Agent {this_agent.fullname} (#{this_agent.id}) " + done_string)
161 return redirect(url_for('main.agent', agent_id=this_agent.id)) 161 return redirect(url_for('main.agent', agent_id=this_agent.id))
162 162
163 163
@@ -5,6 +5,40 @@ db = SQLAlchemy() @@ -5,6 +5,40 @@ db = SQLAlchemy()
5 5
6 6
7 # 7 #
  8 +#
  9 +#
  10 +
  11 +class Formable:
  12 + """
  13 + Parent class allowing some html form facilities
  14 +
  15 + """
  16 + export_keys = []
  17 +
  18 + def from_request(self, form_request):
  19 + """
  20 + Get a form request structure and fill in our fields
  21 +
  22 + :param form_request:
  23 + :return:
  24 + """
  25 + for key in self.export_keys:
  26 + setattr(self, key, form_request.form.get(key))
  27 +
  28 + def to_struct(self):
  29 + """
  30 + Export the orm object to a structure easily used in jinja
  31 +
  32 + :return: nothing
  33 + """
  34 + _struct = {}
  35 + for key in self.export_keys:
  36 + _value = getattr(self, key)
  37 + _struct[key] = '' if _value is None else _value
  38 + return _struct
  39 +
  40 +
  41 +#
8 # Categorized projects 42 # Categorized projects
9 # 43 #
10 44
@@ -67,7 +101,7 @@ class Company(db.Model): @@ -67,7 +101,7 @@ class Company(db.Model):
67 agents = relationship("Agent", back_populates="company") 101 agents = relationship("Agent", back_populates="company")
68 102
69 103
70 -class Agent(db.Model): 104 +class Agent(db.Model, Formable):
71 id = db.Column(db.Integer, primary_key=True) 105 id = db.Column(db.Integer, primary_key=True)
72 firstname = db.Column(db.String(100)) 106 firstname = db.Column(db.String(100))
73 secondname = db.Column(db.String(100)) 107 secondname = db.Column(db.String(100))
@@ -83,10 +117,6 @@ class Agent(db.Model): @@ -83,10 +117,6 @@ class Agent(db.Model):
83 company = relationship("Company", back_populates="agents") 117 company = relationship("Company", back_populates="agents")
84 118
85 @property 119 @property
86 - def export_keys(self):  
87 - return ['firstname', 'secondname', 'virtual', 'permanent', 'company_id', 'status_id', 'grade_id', 'bap_id']  
88 -  
89 - @property  
90 def fullname(self): 120 def fullname(self):
91 return f"{self.secondname} {self.firstname}" 121 return f"{self.secondname} {self.firstname}"
92 122
@@ -94,28 +124,19 @@ class Agent(db.Model): @@ -94,28 +124,19 @@ class Agent(db.Model):
94 def namefull(self): 124 def namefull(self):
95 return f"{self.firstname} {self.secondname}" 125 return f"{self.firstname} {self.secondname}"
96 126
97 - def to_struct(self):  
98 - """  
99 - Export the orm object to a structure easily used in jinja 127 + # has to be set as we inherit Formable
  128 + #
  129 + export_keys = ['firstname', 'secondname', 'virtual', 'permanent', 'company_id', 'status_id', 'grade_id', 'bap_id']
100 130
101 - :return: nothing  
102 - """  
103 - _struct = {'agent_id': self.id if self.id else '',  
104 - 'fullname': self.fullname}  
105 - for key in self.export_keys:  
106 - _value = getattr(self, key)  
107 - _struct[key] = '' if _value is None else _value  
108 - return _struct  
109 -  
110 - def from_request(self, form_request): 131 + def to_struct(self):
111 """ 132 """
112 - Get a form request structure and fill in our fields  
113 -  
114 - :param form_request: 133 + overide parent method to include one key: agent.fullname
115 :return: 134 :return:
116 """ 135 """
117 - for key in self.export_keys:  
118 - setattr(self, key, form_request.form.get(key)) 136 + _struct = super(Agent, self).to_struct()
  137 + _struct['fullname'] = self.fullname
  138 + _struct['agent_id'] = self.id if self.id else ''
  139 + return _struct
119 140
120 141
121 class Service(db.Model): 142 class Service(db.Model):
@@ -135,7 +156,7 @@ class Period(db.Model): @@ -135,7 +156,7 @@ class Period(db.Model):
135 num_months = db.Column(db.Integer) 156 num_months = db.Column(db.Integer)
136 157
137 158
138 -class Charge(db.Model): 159 +class Charge(db.Model, Formable):
139 id = db.Column(db.Integer, primary_key=True) 160 id = db.Column(db.Integer, primary_key=True)
140 agent_id = db.Column(db.Integer, db.ForeignKey('agent.id')) 161 agent_id = db.Column(db.Integer, db.ForeignKey('agent.id'))
141 project_id = db.Column(db.Integer, db.ForeignKey('project.id')) 162 project_id = db.Column(db.Integer, db.ForeignKey('project.id'))
@@ -144,17 +165,6 @@ class Charge(db.Model): @@ -144,17 +165,6 @@ class Charge(db.Model):
144 period_id = db.Column(db.Integer, db.ForeignKey('period.id')) 165 period_id = db.Column(db.Integer, db.ForeignKey('period.id'))
145 charge_rate = db.Column(db.Integer) 166 charge_rate = db.Column(db.Integer)
146 167
147 - @property  
148 - def export_keys(self):  
149 - return ['agent_id', 'project_id', 'service_id', 'capacity_id', 'period_id', 'charge_rate']  
150 -  
151 - def from_request(self, form_request):  
152 - """  
153 - Get a form request structure and fill in our fields  
154 -  
155 - :param form_request:  
156 - :return:  
157 - """  
158 - for key in self.export_keys:  
159 - setattr(self, key, form_request.form.get(key))  
160 - 168 + # Overwrite Formable default to fit our own members
  169 + #
  170 + export_keys = ['agent_id', 'project_id', 'service_id', 'capacity_id', 'period_id', 'charge_rate']