diff --git a/app/main/routes.py b/app/main/routes.py index e158097..805bcc4 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -1,10 +1,9 @@ - -from flask import render_template, make_response, current_app, redirect, url_for, request +from flask import render_template, make_response, current_app, redirect, url_for, request, flash from flask_login import login_required, current_user from . import bp -from app.models import Agent, Project, Service, Capacity, Period +from app.models import Agent, Project, Service, Capacity, Period, db from app import db_mgr from app.auth.routes import role_required @@ -112,7 +111,42 @@ def agent(agent_id): return render_template('agent.html', agent=this_agent, charges=charges_table, - subtitle="{} {}".format(this_agent.firstname, this_agent.secondname)) + subtitle=f"{this_agent.fullname}") + + +@bp.route('/agent/create', methods=('POST', 'GET')) +@bp.route('/agent//edit', methods=('POST', 'GET')) +@role_required('service') +def agent_edit(agent_id=None): + # Make the form, filled with existing agent if updating + # + if request.method == 'GET': + if agent_id: + this_agent = Agent.query.get(int(agent_id)) + else: + this_agent = Agent() + # export to structure for jinja display + agent_struct = this_agent.to_struct() + return render_template('agent_edit.html', agent=agent_struct) + # Or submit for db writing + # + elif request.method == 'POST': + agent_id = request.form['agent_id'] + if agent_id: + # then update existing + this_agent = Agent.query.get(int(agent_id)) + done_string = "mis à jour." + else: + # or create from scratch + this_agent = Agent() + done_string = "ajouté." + # fill orm with form and write to db + this_agent.from_request(request) + db.session.add(this_agent) + db.session.commit() + # we're done + flash(f"Agent {this_agent.fullname} "+done_string) + return redirect(url_for('main.agent', agent_id=this_agent.id)) # - - - - - - - - - - - - - - - - - - - - REST API - - - - - - - - - - - - - - - - - - - - @@ -141,4 +175,3 @@ def charge_agent_csv(agent_id): resp = make_response("\n".join(csv_table)) resp.headers['Content-Type'] = 'text/plain;charset=utf8' return resp - diff --git a/app/main/templates/agent.html b/app/main/templates/agent.html index f4d1b24..e17edbd 100644 --- a/app/main/templates/agent.html +++ b/app/main/templates/agent.html @@ -64,8 +64,7 @@
Besoin en formation :
- - Modifier + Modifier @@ -103,7 +102,7 @@ {% endblock %} diff --git a/app/main/templates/agent_edit.html b/app/main/templates/agent_edit.html index 4990d4d..2919111 100644 --- a/app/main/templates/agent_edit.html +++ b/app/main/templates/agent_edit.html @@ -1,42 +1,47 @@ {% extends "base_page.html" %} -{% block content %} -{% if agent %} -Modifier l'agent {{ agent.name}}. +{# Set the title that will be used in base_page #} +{% if agent['agent_id'] != '' %} + {% set subtitle = "Modifier l'agent"+ agent['fullname'] +":" %} {% else %} -Ajouter un nouvel agent. + {% set subtitle = "Ajouter un nouvel agent:" %} {% endif %} -
- {% if agent %} - - {% endif %} -
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- -
+{% block content %} +
+ {% if agent %} + + {% endif %} +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
{% endblock %} diff --git a/app/models.py b/app/models.py index 828e54f..cbc3013 100644 --- a/app/models.py +++ b/app/models.py @@ -82,6 +82,37 @@ class Agent(db.Model): status = relationship("AgentStatus", back_populates="agents") 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}" + + def to_struct(self): + """ + Export the orm object to a structure easily used in jinja + + :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): + """ + 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[key]) + class Service(db.Model): id = db.Column(db.Integer, primary_key=True) -- libgit2 0.21.2