diff --git a/app/main/routes.py b/app/main/routes.py index 5bebe3a..6d1e7df 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -157,7 +157,7 @@ def agent_edit(agent_id=None): db.session.add(this_agent) db.session.commit() # we're done - flash(f"Agent {this_agent.fullname} " + done_string) + flash(f"Agent {this_agent.fullname} (#{this_agent.id}) " + done_string) return redirect(url_for('main.agent', agent_id=this_agent.id)) diff --git a/app/models.py b/app/models.py index 0e3f6d7..70ce918 100644 --- a/app/models.py +++ b/app/models.py @@ -5,6 +5,40 @@ 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 = {} + for key in self.export_keys: + _value = getattr(self, key) + _struct[key] = '' if _value is None else _value + return _struct + + +# # Categorized projects # @@ -67,7 +101,7 @@ class Company(db.Model): agents = relationship("Agent", back_populates="company") -class Agent(db.Model): +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)) @@ -83,10 +117,6 @@ class Agent(db.Model): company = relationship("Company", back_populates="agents") @property - def export_keys(self): - return ['firstname', 'secondname', 'virtual', 'permanent', 'company_id', 'status_id', 'grade_id', 'bap_id'] - - @property def fullname(self): return f"{self.secondname} {self.firstname}" @@ -94,28 +124,19 @@ class Agent(db.Model): def namefull(self): return f"{self.firstname} {self.secondname}" - def to_struct(self): - """ - Export the orm object to a structure easily used in jinja + # has to be set as we inherit Formable + # + export_keys = ['firstname', 'secondname', 'virtual', 'permanent', 'company_id', 'status_id', 'grade_id', 'bap_id'] - :return: nothing - """ - _struct = {'agent_id': self.id if self.id else '', - 'fullname': self.fullname} - for key in self.export_keys: - _value = getattr(self, key) - _struct[key] = '' if _value is None else _value - return _struct - - def from_request(self, form_request): + def to_struct(self): """ - Get a form request structure and fill in our fields - - :param form_request: + overide parent method to include one key: agent.fullname :return: """ - for key in self.export_keys: - setattr(self, key, form_request.form.get(key)) + _struct = super(Agent, self).to_struct() + _struct['fullname'] = self.fullname + _struct['agent_id'] = self.id if self.id else '' + return _struct class Service(db.Model): @@ -135,7 +156,7 @@ class Period(db.Model): num_months = db.Column(db.Integer) -class Charge(db.Model): +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')) @@ -144,17 +165,6 @@ class Charge(db.Model): period_id = db.Column(db.Integer, db.ForeignKey('period.id')) charge_rate = db.Column(db.Integer) - @property - def export_keys(self): - return ['agent_id', 'project_id', 'service_id', 'capacity_id', 'period_id', 'charge_rate'] - - 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)) - + # Overwrite Formable default to fit our own members + # + export_keys = ['agent_id', 'project_id', 'service_id', 'capacity_id', 'period_id', 'charge_rate'] -- libgit2 0.21.2