Blame view

app/main/routes.py 4.49 KB
71b91c22   hitier   New json route fo...
1

3b0d5feb   hitier   New Site_Login ca...
2
3
from flask import render_template, make_response, current_app, redirect, url_for, request
from flask_login import login_required, current_user
8bd0f3cb   hitier   Fix #25: Flask Bl...
4
5
6

from . import bp

6cbf4b75   hitier   Periods list
7
from app.models import Agent, Project, Service, Capacity, Period
dcafa5d0   hitier   Protect routes by...
8
9
from app import db_mgr
from app.auth.routes import role_required
d6836d5e   hitier   Agents list
10

8bd0f3cb   hitier   Fix #25: Flask Bl...
11

3b0d5feb   hitier   New Site_Login ca...
12
13
14
15
16
17
18
19
20
21
@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

1b604951   hitier   Rearrange code
22

3b0d5feb   hitier   New Site_Login ca...
23
24
@bp.before_request
def catch_all_route():
1b604951   hitier   Rearrange code
25
    current_app.logger.debug(f"{request.method} {request.path}")
3b0d5feb   hitier   New Site_Login ca...
26
27


8bd0f3cb   hitier   Fix #25: Flask Bl...
28
29
@bp.route('/')
def index():
a7e64a99   hitier   Services list
30
31
32
33
    return render_template('index.html', subtitle="Page d'accueil")


@bp.route('/services')
dcafa5d0   hitier   Protect routes by...
34
@role_required('service')
a7e64a99   hitier   Services list
35
36
37
38
39
def services():
    # get services list
    all_services = Service.query.order_by(Service.name).all()
    num_services = len(all_services)
    # pass to template
6cbf4b75   hitier   Periods list
40
    return render_template('services.html', subtitle="Liste des services ({})".format(num_services),
a7e64a99   hitier   Services list
41
                           services=all_services)
d6836d5e   hitier   Agents list
42
43


3dae0d18   hitier   Projects list
44
@bp.route('/projects')
dcafa5d0   hitier   Protect routes by...
45
@role_required('project')
3dae0d18   hitier   Projects list
46
47
def projects():
    # get projects list
f8e1465a   hitier   New projects list...
48
    all_projects = db_mgr.projects()
3dae0d18   hitier   Projects list
49
50
    num_projects = len(all_projects)
    # pass to template
a7e64a99   hitier   Services list
51
    return render_template('projects.html', subtitle="Liste des projets ({})".format(num_projects),
3dae0d18   hitier   Projects list
52
53
                           projects=all_projects)

a7e64a99   hitier   Services list
54

d6836d5e   hitier   Agents list
55
@bp.route('/agents')
dcafa5d0   hitier   Protect routes by...
56
@role_required('project')
d6836d5e   hitier   Agents list
57
def agents():
d6836d5e   hitier   Agents list
58
    # get agents list
2cb0a345   hitier   Add 2 columns in ...
59
    all_agents = db_mgr.agents()
d6836d5e   hitier   Agents list
60
61
    num_agents = len(all_agents)
    # pass to template
a7e64a99   hitier   Services list
62
    return render_template('agents.html', subtitle="Liste des agents ({})".format(num_agents),
d6836d5e   hitier   Agents list
63
                           agents=all_agents)
980708a2   hitier   Capacities list
64

8b3ab81d   hitier   All charges now i...
65

980708a2   hitier   Capacities list
66
@bp.route('/capacities')
dcafa5d0   hitier   Protect routes by...
67
@login_required
980708a2   hitier   Capacities list
68
69
70
71
72
73
74
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)
6cbf4b75   hitier   Periods list
75

8b3ab81d   hitier   All charges now i...
76

6cbf4b75   hitier   Periods list
77
@bp.route('/periods')
dcafa5d0   hitier   Protect routes by...
78
@login_required
6cbf4b75   hitier   Periods list
79
80
81
82
83
84
85
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)
412b041b   hitier   Add charge
86

8b3ab81d   hitier   All charges now i...
87

9183e41e   hitier   Change add charge...
88
@bp.route('/charge/add')
dcafa5d0   hitier   Protect routes by...
89
@role_required('service')
9183e41e   hitier   Change add charge...
90
def charge_add():
71b91c22   hitier   New json route fo...
91
92
93
    return render_template('charge.html', subtitle="Affecter un agent")


d7a4e41b   hitier   New project page:...
94
95
96
97
98
@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))
0d6506cb   hitier   New project_charg...
99
    charges_table = db_mgr.charges_by_project(project_id)
d7a4e41b   hitier   New project page:...
100
101
    return render_template('project.html',
                           project=this_project,
0d6506cb   hitier   New project_charg...
102
                           charges=charges_table,
d7a4e41b   hitier   New project page:...
103
104
                           subtitle="{}".format(this_project.name))

c8b7caf5   hitier   Agent now uses co...
105

d9f5cfc9   hitier   New agent page dy...
106
@bp.route('/agent/<agent_id>')
dcafa5d0   hitier   Protect routes by...
107
@role_required('agent')
d9f5cfc9   hitier   New agent page dy...
108
def agent(agent_id):
dcafa5d0   hitier   Protect routes by...
109
110
    # TODO: am i the agent, the service or project manager , or the admin ?
    this_agent = Agent.query.get(int(agent_id))
c8b7caf5   hitier   Agent now uses co...
111
    charges_table = db_mgr.charges_by_agent_tabled(agent_id)
d9f5cfc9   hitier   New agent page dy...
112
    return render_template('agent.html',
dcafa5d0   hitier   Protect routes by...
113
                           agent=this_agent,
70da5dd5   hitier   Insert charges ta...
114
                           charges=charges_table,
e5449e3e   hitier   Fix wrong agents ...
115
                           subtitle="{} {}".format(this_agent.firstname, this_agent.secondname))
1b604951   hitier   Rearrange code
116
117


64515ad9   hitier   New agent charges...
118
# - - - - - - - - - - - - - - - - - - - -  REST API - - - - - - - - - - - - - - - - - - - -
1b604951   hitier   Rearrange code
119

f25c2405   hitier   Add corresponding...
120
121
122
123
124
125
126
127
128
129
130
131
@bp.route('/charge/project/<project_id>/<category>')
@role_required('project')
def charge_project_csv(project_id, category):
    csv_table = []
    for line in db_mgr.charges_by_project_stacked(project_id, category):
        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

c8b7caf5   hitier   Agent now uses co...
132
133

@bp.route('/charge/agent/<agent_id>')
1b604951   hitier   Rearrange code
134
@role_required('service')
64515ad9   hitier   New agent charges...
135
136
def charge_agent_csv(agent_id):
    csv_table = []
c8b7caf5   hitier   Agent now uses co...
137
    for line in db_mgr.charges_by_agent_stacked(agent_id):
64515ad9   hitier   New agent charges...
138
139
140
141
        line = [cell.replace(",", "-") for cell in line]
        line_string = ",".join(line)
        csv_table.append(line_string)
    resp = make_response("\n".join(csv_table))
17b05589   hitier   Change agent csv ...
142
    resp.headers['Content-Type'] = 'text/plain;charset=utf8'
64515ad9   hitier   New agent charges...
143
    return resp