Blame view

app/main/routes.py 13.5 KB
3c53ad45   hitier   Project edit work...
1
2
from pprint import pprint

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

from . import bp

1e327d7a   hitier   New categories page
8
from app.models import Agent, Project, Service, Capacity, Period, db, Company, AgentGrade, AgentStatus, AgentBap, \
ec68fbf2   hitier   New labels page
9
    Charge, Category, Label
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
def projects():
    # get projects list
f8e1465a   hitier   New projects list...
50
    all_projects = db_mgr.projects()
3dae0d18   hitier   Projects list
51
52
    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

55d58635   hitier   New projects stat...
57
58
59
60
61
62
63
@bp.route('/projects/stats')
@role_required('project')
def projects_stats():
    num_projects = len(Project.query.all())
    return render_template('projects_stats.html', subtitle="Statistiques des projets ({})".format(num_projects))


d6836d5e   hitier   Agents list
64
@bp.route('/agents')
dcafa5d0   hitier   Protect routes by...
65
@role_required('project')
d6836d5e   hitier   Agents list
66
def agents():
d6836d5e   hitier   Agents list
67
    # get agents list
2cb0a345   hitier   Add 2 columns in ...
68
    all_agents = db_mgr.agents()
d6836d5e   hitier   Agents list
69
70
    num_agents = len(all_agents)
    # pass to template
a7e64a99   hitier   Services list
71
    return render_template('agents.html', subtitle="Liste des agents ({})".format(num_agents),
d6836d5e   hitier   Agents list
72
                           agents=all_agents)
980708a2   hitier   Capacities list
73

8b3ab81d   hitier   All charges now i...
74

980708a2   hitier   Capacities list
75
@bp.route('/capacities')
dcafa5d0   hitier   Protect routes by...
76
@login_required
980708a2   hitier   Capacities list
77
78
79
80
81
82
83
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
84

8b3ab81d   hitier   All charges now i...
85

ec68fbf2   hitier   New labels page
86
87
88
89
90
91
92
93
94
95
96
@bp.route('/labels')
@login_required
def labels():
    # get labels list
    all_labels = Label.query.order_by(Label.name).all()
    num_labels = len(all_labels)
    # pass to template
    return render_template('labels.html', subtitle="Liste des labels ({})".format(num_labels),
                           labels=all_labels)


1e327d7a   hitier   New categories page
97
98
99
100
101
102
103
104
105
106
107
@bp.route('/categories')
@login_required
def categories():
    # get categories list
    all_categories = Category.query.order_by(Category.name).all()
    num_categories = len(all_categories)
    # pass to template
    return render_template('categories.html', subtitle="Liste des categories ({})".format(num_categories),
                           categories=all_categories)


6cbf4b75   hitier   Periods list
108
@bp.route('/periods')
dcafa5d0   hitier   Protect routes by...
109
@login_required
6cbf4b75   hitier   Periods list
110
def periods():
1e327d7a   hitier   New categories page
111
    # get categories list
6cbf4b75   hitier   Periods list
112
113
114
115
116
    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
117

8b3ab81d   hitier   All charges now i...
118

d7a4e41b   hitier   New project page:...
119
120
121
122
123
@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...
124
    charges_table = db_mgr.charges_by_project(project_id)
d7a4e41b   hitier   New project page:...
125
    return render_template('project.html',
40126f91   hitier   Update project ro...
126
                           project=this_project.to_struct(),
0d6506cb   hitier   New project_charg...
127
                           charges=charges_table,
d7a4e41b   hitier   New project page:...
128
129
                           subtitle="{}".format(this_project.name))

c8b7caf5   hitier   Agent now uses co...
130

d9f5cfc9   hitier   New agent page dy...
131
@bp.route('/agent/<agent_id>')
dcafa5d0   hitier   Protect routes by...
132
@role_required('agent')
d9f5cfc9   hitier   New agent page dy...
133
def agent(agent_id):
dcafa5d0   hitier   Protect routes by...
134
135
    # 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...
136
    charges_table = db_mgr.charges_by_agent_tabled(agent_id)
d9f5cfc9   hitier   New agent page dy...
137
    return render_template('agent.html',
dcafa5d0   hitier   Protect routes by...
138
                           agent=this_agent,
70da5dd5   hitier   Insert charges ta...
139
                           charges=charges_table,
ca0797d6   hitier   New agent edit form
140
141
142
                           subtitle=f"{this_agent.fullname}")


12de0b1d   hitier   Now charge add ro...
143
# - - - - - - - - - - - - - - - - - - - -  FORMS  - - - - - - - - - - - - - - - - - - - - #
30af2165   hitier   New edit/delete b...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
@bp.route('/category/<category_id>/delete', methods=('POST', 'GET'))
@role_required('admin')
def category_delete(category_id=None):
    this_category = Category.query.get(int(category_id))
    flash("Pas encore implémenté")
    # flash(f"Category {this_category.name} effacé")
    return redirect(url_for('main.categories'))

@bp.route('/label/<label_id>/delete', methods=('POST', 'GET'))
@role_required('admin')
def label_delete(label_id=None):
    this_label = Label.query.get(int(label_id))
    flash("Pas encore implémenté")
    # flash(f"Label {this_label.name} effacé")
    return redirect(url_for('main.labels'))
85ee3eec   hitier   Completed Label e...
159

61e8ddf2   hitier   New labels and ca...
160
161
162
163
@bp.route('/label/create', methods=('POST', 'GET'))
@bp.route('/label/<label_id>/edit', methods=('POST', 'GET'))
@role_required('admin')
def label_edit(label_id=None):
85ee3eec   hitier   Completed Label e...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    if request.method == 'GET':
        if label_id:
            this_label = Label.query.get(int(label_id))
        else:
            this_label = Label()
        label_struct = this_label.to_struct()
        all_categories = Category.query.order_by(Category.name).all()
        return render_template('label_form.html', label=label_struct, categories=all_categories)
    elif request.method == 'POST':
        try:
            label_id = request.form['label_id']
        except KeyError:
            label_id = None
        if label_id:
            # then update existing
            this_label = Label.query.get(int(label_id))
            done_string = "mis à jour."
        else:
            # or create from scratch
            this_label = Label()
            done_string = "ajouté."
        # fill orm with form and write to db
        this_label.from_request(request)
        db.session.add(this_label)
        db.session.commit()
        # we're done
        flash(f"Label {this_label.name} (#{this_label.id}) " + done_string)
        return redirect(url_for('main.labels'))
61e8ddf2   hitier   New labels and ca...
192
193
194


@bp.route('/category/create', methods=('POST', 'GET'))
fc06cbe5   hitier   Category edit for...
195
@bp.route('/category/<category_id>/edit', methods=('POST', 'GET'))
61e8ddf2   hitier   New labels and ca...
196
197
@role_required('admin')
def category_edit(category_id=None):
fc06cbe5   hitier   Category edit for...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    if request.method == 'GET':
        if category_id:
            this_category = Category.query.get(int(category_id))
        else:
            this_category = Category()
        category_struct = this_category.to_struct()
        all_labels = Label.query.order_by(Label.name).all()
        return render_template('category_form.html', category=category_struct, labels=all_labels)
    elif request.method == 'POST':
        try:
            category_id = request.form['category_id']
        except KeyError:
            category_id = None
        if category_id:
            # then update existing
            this_category = Category.query.get(int(category_id))
            done_string = "mis à jour."
        else:
            # or create from scratch
            this_category = Category()
            done_string = "ajouté."
        # fill orm with form and write to db
        this_category.from_request(request)
        db.session.add(this_category)
        db.session.commit()
        # we're done
        flash(f"Category {this_category.name} (#{this_category.id}) " + done_string)
        return redirect(url_for('main.categories'))
61e8ddf2   hitier   New labels and ca...
226

12de0b1d   hitier   Now charge add ro...
227

a540c9b1   hitier   New project form ...
228
229
230
231
@bp.route('/project/create', methods=('POST', 'GET'))
@bp.route('/project/<project_id>/edit', methods=('POST', 'GET'))
@role_required('service')
def project_edit(project_id=None):
3c53ad45   hitier   Project edit work...
232
233
234
235
236
237
    if request.method == 'GET':
        if project_id:
            this_project = Project.query.get(int(project_id))
        else:
            this_project = Project()
        project_struct = this_project.to_struct()
85ee3eec   hitier   Completed Label e...
238
        all_categories = db_mgr.category_labels()
3c53ad45   hitier   Project edit work...
239
        return render_template('project_form.html', project=project_struct,
85ee3eec   hitier   Completed Label e...
240
                               categories=all_categories)
3c53ad45   hitier   Project edit work...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    elif request.method == 'POST':
        try:
            project_id = request.form['project_id']
        except KeyError:
            project_id = None
        if project_id:
            # then update existing
            this_project = Project.query.get(int(project_id))
            done_string = "mis à jour."
        else:
            # or create from scratch
            this_project = Project()
            done_string = "ajouté."
        # fill orm with form and write to db
        this_project.from_request(request)
        db.session.add(this_project)
        db.session.commit()
        # we're done
        flash(f"Project {this_project.name} (#{this_project.id}) " + done_string)
        return redirect(url_for('main.project', project_id=this_project.id))
a540c9b1   hitier   New project form ...
261
262


ca0797d6   hitier   New agent edit form
263
264
265
266
267
268
269
@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...
270
271
272
273
        companies = Company.query.all()
        grades = AgentGrade.query.all()
        statuses = AgentStatus.query.all()
        baps = AgentBap.query.all()
ca0797d6   hitier   New agent edit form
274
275
276
277
278
279
        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()
e6499574   hitier   Rename form templ...
280
        return render_template('agent_form.html', agent=agent_struct,
ac2fa9fd   hitier   Use select_option...
281
282
283
284
                               companies=companies,
                               statuses=statuses,
                               baps=baps,
                               grades=grades)
ca0797d6   hitier   New agent edit form
285
286
287
    # Or submit for db writing
    #
    elif request.method == 'POST':
e20cd5d4   hitier   Now import/export...
288
289
290
291
        try:
            agent_id = request.form['agent_id']
        except KeyError:
            agent_id = None
ca0797d6   hitier   New agent edit form
292
293
294
295
296
297
298
299
300
301
302
303
304
        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
142e2e9d   hitier   New Formable clas...
305
        flash(f"Agent {this_agent.fullname} (#{this_agent.id}) " + done_string)
ca0797d6   hitier   New agent edit form
306
        return redirect(url_for('main.agent', agent_id=this_agent.id))
12de0b1d   hitier   Now charge add ro...
307
308
309
310
311
312


@bp.route('/charge/add', methods=('POST', 'GET'))
@role_required('service')
def charge_add():
    if request.method == 'GET':
09551925   hitier   New charge add pa...
313
314
315
316
317
318
319
320
        try:
            this_agent = Agent.query.get(int(request.args['agent_id']))
        except KeyError:
            this_agent = None
        try:
            this_project = Project.query.get(int(request.args['project_id']))
        except KeyError:
            this_project = None
12de0b1d   hitier   Now charge add ro...
321
322
323
324
325
        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()
e6499574   hitier   Rename form templ...
326
        return render_template('charge_form.html', subtitle="Affecter un agent",
09551925   hitier   New charge add pa...
327
328
                               agent=this_agent,
                               project=this_project,
12de0b1d   hitier   Now charge add ro...
329
330
331
332
333
334
335
336
337
338
339
340
341
                               projects=this_projects,
                               services=this_services,
                               periods=this_periods,
                               capacities=this_capacities,
                               agents=this_agents)
    elif request.method == 'POST':
        this_agent = Agent.query.get(request.form.get('agent_id'))
        this_charge = Charge()
        this_charge.from_request(request)
        db.session.add(this_charge)
        db.session.commit()
        flash(f"Nouvelle charge pour l'agent {this_agent.fullname}")
        return redirect(url_for('main.agent', agent_id=this_agent.id))
1b604951   hitier   Rearrange code
342
343


64515ad9   hitier   New agent charges...
344
# - - - - - - - - - - - - - - - - - - - -  REST API - - - - - - - - - - - - - - - - - - - -
1b604951   hitier   Rearrange code
345

f25c2405   hitier   Add corresponding...
346
347
348
349
350
351
352
353
354
355
356
357
@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...
358
359

@bp.route('/charge/agent/<agent_id>')
1b604951   hitier   Rearrange code
360
@role_required('service')
64515ad9   hitier   New agent charges...
361
362
def charge_agent_csv(agent_id):
    csv_table = []
c8b7caf5   hitier   Agent now uses co...
363
    for line in db_mgr.charges_by_agent_stacked(agent_id):
64515ad9   hitier   New agent charges...
364
365
366
367
        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 ...
368
    resp.headers['Content-Type'] = 'text/plain;charset=utf8'
64515ad9   hitier   New agent charges...
369
    return resp
95880b0d   hitier   New charges_for_p...
370
371
372
373
374
375
376
377
378
379
380
381
382


@bp.route('/charge/projects')
@role_required('project')
def projects_stats_csv():
    csv_table = []
    for line in db_mgr.charges_for_projects_stacked():
        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