Blame view

app/main/routes.py 6.63 KB
ca0797d6   hitier   New agent edit form
1
from flask import render_template, make_response, current_app, redirect, url_for, request, flash
3b0d5feb   hitier   New Site_Login ca...
2
from flask_login import login_required, current_user
8bd0f3cb   hitier   Fix #25: Flask Bl...
3
4
5

from . import bp

ac2fa9fd   hitier   Use select_option...
6
from app.models import Agent, Project, Service, Capacity, Period, db, Company, AgentGrade, AgentStatus, AgentBap
dcafa5d0   hitier   Protect routes by...
7
8
from app import db_mgr
from app.auth.routes import role_required
d6836d5e   hitier   Agents list
9

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

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

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


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


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


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

a7e64a99   hitier   Services list
53

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

8b3ab81d   hitier   All charges now i...
64

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

8b3ab81d   hitier   All charges now i...
75

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

8b3ab81d   hitier   All charges now i...
86

9183e41e   hitier   Change add charge...
87
@bp.route('/charge/add')
dcafa5d0   hitier   Protect routes by...
88
@role_required('service')
9183e41e   hitier   Change add charge...
89
def charge_add():
bb0ea5a5   hitier   New charge add form
90
91
92
93
94
95
96
97
98
99
100
    this_agents = Agent.query.order_by(Agent.firstname).all()
    this_projects = Project.query.order_by(Project.name).all()
    this_services = Service.query.order_by(Service.name).all()
    this_periods = Period.query.order_by(Period.id).all()
    this_capacities = Capacity.query.order_by(Capacity.name).all()
    return render_template('charge.html', subtitle="Affecter un agent",
                           projects=this_projects,
                           services=this_services,
                           periods=this_periods,
                           capacities=this_capacities,
                           agents=this_agents)
71b91c22   hitier   New json route fo...
101
102


d7a4e41b   hitier   New project page:...
103
104
105
106
107
@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...
108
    charges_table = db_mgr.charges_by_project(project_id)
d7a4e41b   hitier   New project page:...
109
110
    return render_template('project.html',
                           project=this_project,
0d6506cb   hitier   New project_charg...
111
                           charges=charges_table,
d7a4e41b   hitier   New project page:...
112
113
                           subtitle="{}".format(this_project.name))

c8b7caf5   hitier   Agent now uses co...
114

d9f5cfc9   hitier   New agent page dy...
115
@bp.route('/agent/<agent_id>')
dcafa5d0   hitier   Protect routes by...
116
@role_required('agent')
d9f5cfc9   hitier   New agent page dy...
117
def agent(agent_id):
dcafa5d0   hitier   Protect routes by...
118
119
    # 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...
120
    charges_table = db_mgr.charges_by_agent_tabled(agent_id)
d9f5cfc9   hitier   New agent page dy...
121
    return render_template('agent.html',
dcafa5d0   hitier   Protect routes by...
122
                           agent=this_agent,
70da5dd5   hitier   Insert charges ta...
123
                           charges=charges_table,
ca0797d6   hitier   New agent edit form
124
125
126
127
128
129
130
131
132
133
                           subtitle=f"{this_agent.fullname}")


@bp.route('/agent/create', methods=('POST', 'GET'))
@bp.route('/agent/<agent_id>/edit', methods=('POST', 'GET'))
@role_required('service')
def agent_edit(agent_id=None):
    # Make the form, filled with existing agent if updating
    #
    if request.method == 'GET':
ac2fa9fd   hitier   Use select_option...
134
135
136
137
        companies = Company.query.all()
        grades = AgentGrade.query.all()
        statuses = AgentStatus.query.all()
        baps = AgentBap.query.all()
ca0797d6   hitier   New agent edit form
138
139
140
141
142
143
        if agent_id:
            this_agent = Agent.query.get(int(agent_id))
        else:
            this_agent = Agent()
        # export to structure for jinja display
        agent_struct = this_agent.to_struct()
ac2fa9fd   hitier   Use select_option...
144
145
146
147
148
        return render_template('agent_edit.html', agent=agent_struct,
                               companies=companies,
                               statuses=statuses,
                               baps=baps,
                               grades=grades)
ca0797d6   hitier   New agent edit form
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    # Or submit for db writing
    #
    elif request.method == 'POST':
        agent_id = request.form['agent_id']
        if agent_id:
            # then update existing
            this_agent = Agent.query.get(int(agent_id))
            done_string = "mis à jour."
        else:
            # or create from scratch
            this_agent = Agent()
            done_string = "ajouté."
        # fill orm with form and write to db
        this_agent.from_request(request)
        db.session.add(this_agent)
        db.session.commit()
        # we're done
ac2fa9fd   hitier   Use select_option...
166
        flash(f"Agent {this_agent.fullname} " + done_string)
ca0797d6   hitier   New agent edit form
167
        return redirect(url_for('main.agent', agent_id=this_agent.id))
1b604951   hitier   Rearrange code
168
169


64515ad9   hitier   New agent charges...
170
# - - - - - - - - - - - - - - - - - - - -  REST API - - - - - - - - - - - - - - - - - - - -
1b604951   hitier   Rearrange code
171

f25c2405   hitier   Add corresponding...
172
173
174
175
176
177
178
179
180
181
182
183
@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...
184
185

@bp.route('/charge/agent/<agent_id>')
1b604951   hitier   Rearrange code
186
@role_required('service')
64515ad9   hitier   New agent charges...
187
188
def charge_agent_csv(agent_id):
    csv_table = []
c8b7caf5   hitier   Agent now uses co...
189
    for line in db_mgr.charges_by_agent_stacked(agent_id):
64515ad9   hitier   New agent charges...
190
191
192
193
        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 ...
194
    resp.headers['Content-Type'] = 'text/plain;charset=utf8'
64515ad9   hitier   New agent charges...
195
    return resp