Commit b80c3b38 authored by Nicolas Joyard's avatar Nicolas Joyard

Ajout pages d'admin

parent 8137670d
......@@ -3,6 +3,7 @@
from .context_processors import setup as setup_cp
from .filters import setup as setup_filters
from .admin import setup_routes as setup_admin
from .home import setup_routes as setup_home
from .parlementaires import setup_routes as setup_parl
from .pdf import setup_routes as setup_pdf
......@@ -16,3 +17,4 @@ def setup_routes(app):
setup_session(app)
setup_parl(app)
setup_pdf(app)
setup_admin(app)
# -*- coding: utf-8 -*-
from flask import render_template
from sqlalchemy.orm import contains_eager, joinedload
from .util import redirect_back, require_admin
from ..models import db, Action, Etape, Parlementaire
from ..models.constants import ETAPE_A_ENVOYER, ETAPE_A_CONFIRMER
def setup_routes(app):
@app.route('/admin/recent', endpoint='admin_recent')
@require_admin
def admin_recent():
qs = Action.query.options(joinedload(Action.parlementaire)) \
.options(joinedload(Action.etape)) \
.order_by(Action.date.desc()) \
.limit(500) \
.all()
return render_template('admin_recent.html.j2',
titre='Actions récentes',
actions=qs)
@app.route('/admin/en-attente', endpoint='admin_en_attente')
@require_admin
def admin_en_attente():
parls = db.session.query(Parlementaire.id) \
.join(Parlementaire.etape) \
.filter(Etape.ordre == ETAPE_A_CONFIRMER) \
.subquery()
qs = Action.query.join(Action.etape) \
.filter(Action.parlementaire_id.in_(parls)) \
.filter(Etape.ordre == ETAPE_A_CONFIRMER) \
.order_by(Action.date) \
.all()
return render_template('admin_recent.html.j2',
titre='Actions en attente',
actions=qs)
@app.route('/admin/delete/<id>', endpoint='admin_delete')
@require_admin
def admin_delete(id):
action = Action.query.filter_by(id=id).first()
if action:
parl_id = action.parlementaire_id
db.session.delete(action)
db.session.flush()
last_action = Action.query \
.join(Action.etape) \
.filter(Action.parlementaire_id == parl_id) \
.order_by(Etape.ordre.desc()) \
.first()
if last_action:
etape = last_action.etape
else:
etape = Etape.query.filter_by(ordre=ETAPE_A_ENVOYER).first()
parl = Parlementaire.query.filter_by(id=parl_id).first()
parl.etape = etape
db.session.commit()
return redirect_back()
# -*- coding: utf-8 -*-
from flask import url_for
from flask import session, url_for
from ..models.constants import (ETAPE_NA, ETAPE_A_ENVOYER, ETAPE_A_CONFIRMER,
ETAPE_ENVOYE)
......@@ -21,20 +21,34 @@ def setup(app):
@app.context_processor
def inject_menu():
return {
'menu': [
menu = [
{
'url': url_for('home'),
'label': 'Accueil',
'endpoint': 'home',
},
{
'url': url_for('parlementaires'),
'label': 'Liste des parlementaires',
'endpoint': 'parlementaires',
},
]
if session.get('user') and session.get('user')['admin']:
menu += [
{
'url': url_for('home'),
'label': 'Accueil',
'endpoint': 'home',
'url': url_for('admin_recent'),
'label': '<span class="admin">Actions récentes</span>',
'endpoint': 'admin_recent"'
},
{
'url': url_for('parlementaires'),
'label': 'Liste des parlementaires',
'endpoint': 'parlementaires',
'url': url_for('admin_en_attente'),
'label': '<span class="admin">Actions en attente</span>',
'endpoint': 'admin_en_attente"'
},
]
}
return {'menu': menu}
@app.context_processor
def inject_etapes():
......
......@@ -61,3 +61,12 @@ def require_user(f):
return f(*args, **kwargs)
return decorator
def require_admin(f):
def decorator(*args, **kwargs):
if not session.get('user') or not session.get('user')['admin']:
return not_found()
return f(*args, **kwargs)
return decorator
......@@ -63,6 +63,10 @@ th.col-right {
font-weight: bold;
}
.nav .admin {
color: red;
}
.nav li.login {
float: right;
}
......
{% extends "_base.html.j2" %}
{% block content %}
<section class="panel panel-default">
<header class="panel-heading">
<b>{{ titre }}</b>
</header>
<table class="table table-condensed table-striped">
<thead>
<tr>
<th>Parlementaire</th>
<th>Etape</th>
<th>Date</th>
<th>Utilisateur</th>
<th>Fichier joint</th>
<th></th>
</tr>
</thead>
<tbody>
{% for action in actions %}
<tr>
<td>{{ action.parlementaire.nom_complet }}</td>
<td>{{ action.etape|label_etape }}</td>
<td>{{ action.date.strftime('%c') }}</td>
<td>
{% if action.nick == '!rc' %}
<img class="rc-small" src="{{ url_for('static', filename='rc.png') }}">
{% else %}
{{ action.nick }} ({{ action.email }}) depuis {{ action.ip }}
{% endif %}
</td>
<td>{{ action.attachment or '' }}</td>
<td>
<a class="btn btn-danger" href="{{ url_for('admin_delete', id=action.id) }}">Supprimer</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</section>
{% endblock %}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment