Commit 12de0b1ddfc1e68d95d35a79a80c88a6c8dfb7de
1 parent
7daae4a7
Exists in
master
and in
4 other branches
Now charge add route creates new charge
Showing
2 changed files
with
43 additions
and
17 deletions
Show diff stats
app/main/routes.py
... | ... | @@ -3,7 +3,7 @@ from flask_login import login_required, current_user |
3 | 3 | |
4 | 4 | from . import bp |
5 | 5 | |
6 | -from app.models import Agent, Project, Service, Capacity, Period, db, Company, AgentGrade, AgentStatus, AgentBap | |
6 | +from app.models import Agent, Project, Service, Capacity, Period, db, Company, AgentGrade, AgentStatus, AgentBap, Charge | |
7 | 7 | from app import db_mgr |
8 | 8 | from app.auth.routes import role_required |
9 | 9 | |
... | ... | @@ -84,22 +84,6 @@ def periods(): |
84 | 84 | periods=all_periods) |
85 | 85 | |
86 | 86 | |
87 | -@bp.route('/charge/add') | |
88 | -@role_required('service') | |
89 | -def charge_add(): | |
90 | - this_agents = Agent.query.order_by(Agent.firstname).all() | |
91 | - this_projects = Project.query.order_by(Project.name).all() | |
92 | - this_services = Service.query.order_by(Service.name).all() | |
93 | - this_periods = Period.query.order_by(Period.id).all() | |
94 | - this_capacities = Capacity.query.order_by(Capacity.name).all() | |
95 | - return render_template('charge.html', subtitle="Affecter un agent", | |
96 | - projects=this_projects, | |
97 | - services=this_services, | |
98 | - periods=this_periods, | |
99 | - capacities=this_capacities, | |
100 | - agents=this_agents) | |
101 | - | |
102 | - | |
103 | 87 | @bp.route('/project/<project_id>') |
104 | 88 | @role_required('project') |
105 | 89 | def project(project_id): |
... | ... | @@ -124,6 +108,8 @@ def agent(agent_id): |
124 | 108 | subtitle=f"{this_agent.fullname}") |
125 | 109 | |
126 | 110 | |
111 | +# - - - - - - - - - - - - - - - - - - - - FORMS - - - - - - - - - - - - - - - - - - - - # | |
112 | + | |
127 | 113 | @bp.route('/agent/create', methods=('POST', 'GET')) |
128 | 114 | @bp.route('/agent/<agent_id>/edit', methods=('POST', 'GET')) |
129 | 115 | @role_required('service') |
... | ... | @@ -167,6 +153,31 @@ def agent_edit(agent_id=None): |
167 | 153 | return redirect(url_for('main.agent', agent_id=this_agent.id)) |
168 | 154 | |
169 | 155 | |
156 | +@bp.route('/charge/add', methods=('POST', 'GET')) | |
157 | +@role_required('service') | |
158 | +def charge_add(): | |
159 | + if request.method == 'GET': | |
160 | + this_agents = Agent.query.order_by(Agent.firstname).all() | |
161 | + this_projects = Project.query.order_by(Project.name).all() | |
162 | + this_services = Service.query.order_by(Service.name).all() | |
163 | + this_periods = Period.query.order_by(Period.id).all() | |
164 | + this_capacities = Capacity.query.order_by(Capacity.name).all() | |
165 | + return render_template('charge.html', subtitle="Affecter un agent", | |
166 | + projects=this_projects, | |
167 | + services=this_services, | |
168 | + periods=this_periods, | |
169 | + capacities=this_capacities, | |
170 | + agents=this_agents) | |
171 | + elif request.method == 'POST': | |
172 | + this_agent = Agent.query.get(request.form.get('agent_id')) | |
173 | + this_charge = Charge() | |
174 | + this_charge.from_request(request) | |
175 | + db.session.add(this_charge) | |
176 | + db.session.commit() | |
177 | + flash(f"Nouvelle charge pour l'agent {this_agent.fullname}") | |
178 | + return redirect(url_for('main.agent', agent_id=this_agent.id)) | |
179 | + | |
180 | + | |
170 | 181 | # - - - - - - - - - - - - - - - - - - - - REST API - - - - - - - - - - - - - - - - - - - - |
171 | 182 | |
172 | 183 | @bp.route('/charge/project/<project_id>/<category>') | ... | ... |
app/models.py
... | ... | @@ -143,3 +143,18 @@ class Charge(db.Model): |
143 | 143 | capacity_id = db.Column(db.Integer, db.ForeignKey('capacity.id')) |
144 | 144 | period_id = db.Column(db.Integer, db.ForeignKey('period.id')) |
145 | 145 | charge_rate = db.Column(db.Integer) |
146 | + | |
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 | + | ... | ... |