routes.py
4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
from pprint import pprint
from flask import render_template, make_response, current_app, redirect, url_for, request
from flask_login import login_required, current_user
from . import bp
from app.models import Agent, Project, Service, Capacity, Period
from app import db_mgr
from app.auth.routes import role_required
@bp.before_request
def site_login():
try:
if current_app.config['SITE_LOGIN'] \
and not current_user.is_authenticated:
return redirect(url_for('auth.login'))
except KeyError:
# no such config, juste ignore
pass
@bp.before_request
def catch_all_route():
current_app.logger.debug(f"{request.method} {request.path}")
@bp.route('/')
def index():
return render_template('index.html', subtitle="Page d'accueil")
@bp.route('/services')
@role_required('service')
def services():
# get services list
all_services = Service.query.order_by(Service.name).all()
num_services = len(all_services)
# pass to template
return render_template('services.html', subtitle="Liste des services ({})".format(num_services),
services=all_services)
@bp.route('/projects')
@role_required('project')
def projects():
# get projects list
all_projects = db_mgr.projects()
num_projects = len(all_projects)
# pass to template
return render_template('projects.html', subtitle="Liste des projets ({})".format(num_projects),
projects=all_projects)
@bp.route('/agents')
@role_required('project')
def agents():
# get agents list
all_agents = db_mgr.agents()
num_agents = len(all_agents)
# pass to template
return render_template('agents.html', subtitle="Liste des agents ({})".format(num_agents),
agents=all_agents)
@bp.route('/capacities')
@login_required
def capacities():
# get capacities list
all_capacities = Capacity.query.order_by(Capacity.name).all()
num_capacities = len(all_capacities)
# pass to template
return render_template('capacities.html', subtitle="Liste des fonctions ({})".format(num_capacities),
capacities=all_capacities)
@bp.route('/periods')
@login_required
def periods():
# get capacities list
all_periods = Period.query.order_by(Period.name).all()
num_periods = len(all_periods)
# pass to template
return render_template('periods.html', subtitle="Liste des périodes ({})".format(num_periods),
periods=all_periods)
@bp.route('/charge/add')
@role_required('service')
def charge_add():
return render_template('charge.html', subtitle="Affecter un agent")
@bp.route('/project/<project_id>')
@role_required('project')
def project(project_id):
# TODO: am i the project manager ?
this_project = Project.query.get(int(project_id))
charges_table = db_mgr.charges_by_project(project_id)
return render_template('project.html',
project=this_project,
charges=charges_table,
subtitle="{}".format(this_project.name))
@bp.route('/agent/<agent_id>')
@role_required('agent')
def agent(agent_id):
# TODO: am i the agent, the service or project manager , or the admin ?
this_agent = Agent.query.get(int(agent_id))
charges_table=db_mgr.charges_by_agent_tabled(agent_id)
return render_template('agent.html',
agent=this_agent,
charges=charges_table,
subtitle="{} {}".format(this_agent.firstname, this_agent.secondname))
# - - - - - - - - - - - - - - - - - - - - REST API - - - - - - - - - - - - - - - - - - - -
@bp.route('/charge/agent/csv/<agent_id>')
@role_required('service')
def charge_agent_csv(agent_id):
csv_table = []
for line in db_mgr.charges_by_agent_tabled(agent_id):
line = [cell.replace(",", "-") for cell in line]
line_string = ",".join(line)
csv_table.append(line_string)
resp = make_response("\n".join(csv_table))
resp.headers['Content-Type'] = 'text/plain;charset=utf8'
return resp
@bp.route('/charge/agent/json/<agent_id>')
@role_required('service')
def charge_agent_json(agent_id):
stacked_charges = db_mgr.charges_by_agent_stacked(agent_id)
resp = make_response(json.dumps(stacked_charges))
resp.headers['Content-Type'] = 'application/json'
return resp