Blame view

app/main/routes.py 3.91 KB
71b91c22   hitier   New json route fo...
1
import json
64515ad9   hitier   New agent charges...
2
from pprint import pprint
71b91c22   hitier   New json route fo...
3

3b0d5feb   hitier   New Site_Login ca...
4
5
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...
6
7
8

from . import bp

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

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

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

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


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


@bp.route('/services')
dcafa5d0   hitier   Protect routes by...
36
@role_required('service')
a7e64a99   hitier   Services list
37
38
39
40
41
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
42
    return render_template('services.html', subtitle="Liste des services ({})".format(num_services),
a7e64a99   hitier   Services list
43
                           services=all_services)
d6836d5e   hitier   Agents list
44
45


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

a7e64a99   hitier   Services list
56

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

8b3ab81d   hitier   All charges now i...
67

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

8b3ab81d   hitier   All charges now i...
78

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

8b3ab81d   hitier   All charges now i...
89

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


d9f5cfc9   hitier   New agent page dy...
96
@bp.route('/agent/<agent_id>')
dcafa5d0   hitier   Protect routes by...
97
@role_required('agent')
d9f5cfc9   hitier   New agent page dy...
98
def agent(agent_id):
dcafa5d0   hitier   Protect routes by...
99
100
    # TODO: am i the agent, the service or project manager , or the admin ?
    this_agent = Agent.query.get(int(agent_id))
70da5dd5   hitier   Insert charges ta...
101
102
    charges_table=db_mgr.charges_by_agent_tabled(agent_id)
    pprint(charges_table)
d9f5cfc9   hitier   New agent page dy...
103
    return render_template('agent.html',
dcafa5d0   hitier   Protect routes by...
104
                           agent=this_agent,
70da5dd5   hitier   Insert charges ta...
105
                           charges=charges_table,
e5449e3e   hitier   Fix wrong agents ...
106
                           subtitle="{} {}".format(this_agent.firstname, this_agent.secondname))
1b604951   hitier   Rearrange code
107
108


64515ad9   hitier   New agent charges...
109
# - - - - - - - - - - - - - - - - - - - -  REST API - - - - - - - - - - - - - - - - - - - -
1b604951   hitier   Rearrange code
110

64515ad9   hitier   New agent charges...
111
@bp.route('/charge/agent/csv/<agent_id>')
1b604951   hitier   Rearrange code
112
@role_required('service')
64515ad9   hitier   New agent charges...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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/csv'
    return resp


@bp.route('/charge/agent/json/<agent_id>')
@role_required('service')
def charge_agent_json(agent_id):
    resp = make_response(json.dumps(db_mgr.charges_by_agent_stacked(agent_id)))
1b604951   hitier   Rearrange code
128
129
    resp.headers['Content-Type'] = 'application/json'
    return resp