Commit ca0797d66e3df3a2de7c578fe115a1dac126fef2

Authored by hitier
1 parent 7bca61b9

New agent edit form

app/main/routes.py
1 -  
2 -from flask import render_template, make_response, current_app, redirect, url_for, request 1 +from flask import render_template, make_response, current_app, redirect, url_for, request, flash
3 from flask_login import login_required, current_user 2 from flask_login import login_required, current_user
4 3
5 from . import bp 4 from . import bp
6 5
7 -from app.models import Agent, Project, Service, Capacity, Period 6 +from app.models import Agent, Project, Service, Capacity, Period, db
8 from app import db_mgr 7 from app import db_mgr
9 from app.auth.routes import role_required 8 from app.auth.routes import role_required
10 9
@@ -112,7 +111,42 @@ def agent(agent_id): @@ -112,7 +111,42 @@ def agent(agent_id):
112 return render_template('agent.html', 111 return render_template('agent.html',
113 agent=this_agent, 112 agent=this_agent,
114 charges=charges_table, 113 charges=charges_table,
115 - subtitle="{} {}".format(this_agent.firstname, this_agent.secondname)) 114 + subtitle=f"{this_agent.fullname}")
  115 +
  116 +
  117 +@bp.route('/agent/create', methods=('POST', 'GET'))
  118 +@bp.route('/agent/<agent_id>/edit', methods=('POST', 'GET'))
  119 +@role_required('service')
  120 +def agent_edit(agent_id=None):
  121 + # Make the form, filled with existing agent if updating
  122 + #
  123 + if request.method == 'GET':
  124 + if agent_id:
  125 + this_agent = Agent.query.get(int(agent_id))
  126 + else:
  127 + this_agent = Agent()
  128 + # export to structure for jinja display
  129 + agent_struct = this_agent.to_struct()
  130 + return render_template('agent_edit.html', agent=agent_struct)
  131 + # Or submit for db writing
  132 + #
  133 + elif request.method == 'POST':
  134 + agent_id = request.form['agent_id']
  135 + if agent_id:
  136 + # then update existing
  137 + this_agent = Agent.query.get(int(agent_id))
  138 + done_string = "mis à jour."
  139 + else:
  140 + # or create from scratch
  141 + this_agent = Agent()
  142 + done_string = "ajouté."
  143 + # fill orm with form and write to db
  144 + this_agent.from_request(request)
  145 + db.session.add(this_agent)
  146 + db.session.commit()
  147 + # we're done
  148 + flash(f"Agent {this_agent.fullname} "+done_string)
  149 + return redirect(url_for('main.agent', agent_id=this_agent.id))
116 150
117 151
118 # - - - - - - - - - - - - - - - - - - - - REST API - - - - - - - - - - - - - - - - - - - - 152 # - - - - - - - - - - - - - - - - - - - - REST API - - - - - - - - - - - - - - - - - - - -
@@ -141,4 +175,3 @@ def charge_agent_csv(agent_id): @@ -141,4 +175,3 @@ def charge_agent_csv(agent_id):
141 resp = make_response("\n".join(csv_table)) 175 resp = make_response("\n".join(csv_table))
142 resp.headers['Content-Type'] = 'text/plain;charset=utf8' 176 resp.headers['Content-Type'] = 'text/plain;charset=utf8'
143 return resp 177 return resp
144 -  
app/main/templates/agent.html
@@ -64,8 +64,7 @@ @@ -64,8 +64,7 @@
64 <dt class="col-sm-2 text-right">Besoin en formation :</dt> 64 <dt class="col-sm-2 text-right">Besoin en formation :</dt>
65 <dd class="col-sm-10 text-left"></dd> 65 <dd class="col-sm-10 text-left"></dd>
66 </dl> 66 </dl>
67 -  
68 - <a class="card-link disabled" href="#">Modifier</a> 67 + <a class="card-link" href="{{url_for('main.agent_edit', agent_id=agent.id)}}">Modifier</a>
69 </div> 68 </div>
70 </div> 69 </div>
71 70
@@ -103,7 +102,7 @@ @@ -103,7 +102,7 @@
103 <script> 102 <script>
104 build_chart("#projects_chart", 103 build_chart("#projects_chart",
105 "{{url_for('main.charge_agent_csv', agent_id=agent.id)}}", 104 "{{url_for('main.charge_agent_csv', agent_id=agent.id)}}",
106 - "{{agent.secondname}}" + " {{agent.firstname}}", 105 + "{{agent.fullname}}",
107 "project"); 106 "project");
108 </script> 107 </script>
109 {% endblock %} 108 {% endblock %}
app/main/templates/agent_edit.html
1 {% extends "base_page.html" %} 1 {% extends "base_page.html" %}
2 -{% block content %}  
3 -{% if agent %}  
4 -Modifier l'agent {{ agent.name}}. 2 +{# Set the title that will be used in base_page #}
  3 +{% if agent['agent_id'] != '' %}
  4 + {% set subtitle = "Modifier l'agent"+ agent['fullname'] +":" %}
5 {% else %} 5 {% else %}
6 -Ajouter un nouvel agent. 6 + {% set subtitle = "Ajouter un nouvel agent:" %}
7 {% endif %} 7 {% endif %}
8 -<form action="{{url_for('main.agent_edit')}}" method="post">  
9 - {% if agent %}  
10 - <input class="form-control" id="agent_id" name="agent_id" type="hidden" value="{{agent.id}}">  
11 - {% endif %}  
12 - <div class="form-group">  
13 - <label for="firstname">Nom</label>  
14 - <input class="form-control" id="firstname" name="firstname" type="text" value="{{agent.firstname}}">  
15 - </div>  
16 - <div class="form-group">  
17 - <label for="secondname">Prénom</label>  
18 - <input class="form-control" id="secondname" name="secondname" type="text" value="{{agent.secondname}}">  
19 - </div>  
20 - <div class="form-group">  
21 - <label for="virtual">Virtuel</label>  
22 - <input class="form-control" id="virtual" name="virtual" type="text" value="{{agent.virtual}}">  
23 - </div>  
24 - <div class="form-group">  
25 - <label for="company">Structure</label>  
26 - <input class="form-control" id="company" name="company" type="text" value="{{agent.company.name}}">  
27 - </div>  
28 - <div class="form-group">  
29 - <label for="grade">Grade</label>  
30 - <input class="form-control" id="grade" name="grade" type="text" value="{{agent.grade.name}}">  
31 - </div>  
32 - <div class="form-group">  
33 - <label for="status">Statut</label>  
34 - <input class="form-control" id="status" name="status" type="text" value="{{agent.status.name}}">  
35 - </div>  
36 - <div class="form-group">  
37 - <label for="bap">Bap</label>  
38 - <input class="form-control" id="bap" name="bap" type="text" value="{{agent.bap.name}}">  
39 - </div>  
40 - <input class="btn btn-dark" type="submit" value="Valider">  
41 -</form> 8 +{% block content %}
  9 + <form action="{{ url_for('main.agent_edit') }}" method="post">
  10 + {% if agent %}
  11 + <input class="form-control" id="agent_id" name="agent_id" type="hidden" value="{{ agent['agent_id'] }}">
  12 + {% endif %}
  13 + <div class="form-group">
  14 + <label for="firstname">Nom</label>
  15 + <input class="form-control" id="firstname" name="firstname" type="text" value="{{ agent['firstname'] }}">
  16 + </div>
  17 + <div class="form-group">
  18 + <label for="secondname">Prénom</label>
  19 + <input class="form-control" id="secondname" name="secondname" type="text" value="{{ agent['secondname'] }}">
  20 + </div>
  21 + <div class="form-group">
  22 + <label for="virtual">Virtuel</label>
  23 + <input class="form-control" id="virtual" name="virtual" type="text" value="{{ agent['virtual'] }}">
  24 + </div>
  25 + <div class="form-group">
  26 + <label for="company">Structure</label>
  27 + <input class="form-control" id="company_id" name="company_id" type="text" value="{{ agent['company_id'] }}">
  28 + </div>
  29 + <div class="form-group">
  30 + <label for="grade">Grade</label>
  31 + <input class="form-control" id="grade_id" name="grade_id" type="text" value="{{ agent['grade_id'] }}">
  32 + </div>
  33 + <div class="form-group">
  34 + <label for="status">Statut</label>
  35 + <input class="form-control" id="status_id" name="status_id" type="text" value="{{ agent['status_id'] }}">
  36 + </div>
  37 + <div class="form-group">
  38 + <label for="bap">Bap</label>
  39 + <input class="form-control" id="bap_id" name="bap_id" type="text" value="{{ agent['bap_id'] }}">
  40 + </div>
  41 + <div class="form-group">
  42 + <label for="permanent">Permanent</label>
  43 + <input class="form-control" id="permanent" name="permanent" type="text" value="{{ agent['permanent'] }}">
  44 + </div>
  45 + <input class="btn btn-dark" type="submit" value="Valider">
  46 + </form>
42 {% endblock %} 47 {% endblock %}
@@ -82,6 +82,37 @@ class Agent(db.Model): @@ -82,6 +82,37 @@ class Agent(db.Model):
82 status = relationship("AgentStatus", back_populates="agents") 82 status = relationship("AgentStatus", back_populates="agents")
83 company = relationship("Company", back_populates="agents") 83 company = relationship("Company", back_populates="agents")
84 84
  85 + @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):
  91 + return f"{self.secondname} {self.firstname}"
  92 +
  93 + def to_struct(self):
  94 + """
  95 + Export the orm object to a structure easily used in jinja
  96 +
  97 + :return: nothing
  98 + """
  99 + _struct = {'agent_id': self.id if self.id else '',
  100 + 'fullname': self.fullname}
  101 + for key in self.export_keys:
  102 + _value = getattr(self, key)
  103 + _struct[key] = '' if _value is None else _value
  104 + return _struct
  105 +
  106 + def from_request(self, form_request):
  107 + """
  108 + Get a form request structure and fill in our fields
  109 +
  110 + :param form_request:
  111 + :return:
  112 + """
  113 + for key in self.export_keys:
  114 + setattr(self, key, form_request.form[key])
  115 +
85 116
86 class Service(db.Model): 117 class Service(db.Model):
87 id = db.Column(db.Integer, primary_key=True) 118 id = db.Column(db.Integer, primary_key=True)