context_processors.py 5.79 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1 2
# -*- coding: utf-8 -*-

Nicolas Joyard's avatar
Nicolas Joyard committed
3 4
from datetime import datetime

Nicolas Joyard's avatar
Nicolas Joyard committed
5
from flask import session, url_for
Nicolas Joyard's avatar
Nicolas Joyard committed
6

7
from ..models import Action, Parlementaire, User
Nicolas Joyard's avatar
Nicolas Joyard committed
8 9
from ..models.constants import (CHAMBRES, ETAPES, ETAPES_BY_ORDRE,
                                ETAPE_AR_RECU, ETAPE_A_CONFIRMER,
Nicolas Joyard's avatar
Nicolas Joyard committed
10
                                ETAPE_A_ENVOYER, ETAPE_COM_A_MODERER,
11 12 13
                                ETAPE_COM_PUBLIE, ETAPE_COURRIEL,
                                ETAPE_DOC_MASQUE, ETAPE_DOC_PUBLIE,
                                ETAPE_ENVOYE, ETAPE_NA, ETAPE_REPONSE_NEGATIVE,
Nicolas Joyard's avatar
Nicolas Joyard committed
14 15
                                ETAPE_REPONSE_POSITIVE, ETAPE_DEMANDE_CADA,
                                ETAPE_INCOMPETENCE_CADA, ETAPE_REQUETE_TA)
16

Nicolas Joyard's avatar
Nicolas Joyard committed
17 18 19

def setup(app):

Nicolas Joyard's avatar
Nicolas Joyard committed
20 21 22
    @app.context_processor
    def inject_timestamp():
        return {
Nicolas Joyard's avatar
Nicolas Joyard committed
23
            'manet_timestamp': int(datetime.now().timestamp() / 3600)
Nicolas Joyard's avatar
Nicolas Joyard committed
24 25
        }

Nicolas Joyard's avatar
Nicolas Joyard committed
26 27 28 29 30 31 32
    @app.context_processor
    def inject_user_info():
        return {
            'is_logged': bool(session.get('user')),
            'is_admin': session.get('user') and session['user']['admin']
        }

Nicolas Joyard's avatar
Nicolas Joyard committed
33 34 35 36 37 38 39 40 41
    @app.context_processor
    def inject_piwik():
        piwik = None
        if app.config['PIWIK_HOST']:
            piwik = {
                'host': app.config['PIWIK_HOST'],
                'id': app.config['PIWIK_ID']
            }

Nicolas Joyard's avatar
Nicolas Joyard committed
42
        return {'piwik': piwik}
Nicolas Joyard's avatar
Nicolas Joyard committed
43 44 45

    @app.context_processor
    def inject_menu():
Nicolas Joyard's avatar
Nicolas Joyard committed
46 47 48 49 50 51
        menu = [
            {
                'url': url_for('home'),
                'label': 'Accueil',
                'endpoint': 'home',
            },
52 53
            {
                'url': url_for('historique'),
Nicolas Joyard's avatar
Nicolas Joyard committed
54
                'label': 'Qu\'est-ce que l\'IRFM ?',
55 56
                'endpoint': 'historique',
            },
57 58 59 60 61
            {
                'url': url_for('derives'),
                'label': 'Les dérives de l\'IRFM',
                'endpoint': 'derives',
            },
Nicolas Joyard's avatar
Nicolas Joyard committed
62 63 64 65 66
            {
                'url': url_for('faq'),
                'label': 'FAQ',
                'endpoint': 'faq',
            },
Nicolas Joyard's avatar
Nicolas Joyard committed
67 68 69 70 71 72 73
            {
                'url': url_for('parlementaires'),
                'label': 'Liste des parlementaires',
                'endpoint': 'parlementaires',
            },
        ]

Nicolas Joyard's avatar
Nicolas Joyard committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        if session.get('user'):
            acts = Action.query \
                         .filter(Parlementaire.id == Action.parlementaire_id) \
                         .filter(Action.etape == ETAPE_A_CONFIRMER) \
                         .filter(Action.user_id == session['user']['id']) \
                         .exists()

            nb = Parlementaire.query.filter(acts) \
                                    .filter(Parlementaire.etape ==
                                            ETAPE_A_CONFIRMER) \
                                    .count()

            label = 'Mes actions'
            if nb > 0:
                title = '%s envoi%s à confirmer' % (nb, 's' if nb > 1 else '')
                label += ' <span class="badge" data-toggle="tooltip" ' \
                         'title="%s">%s</span>' % (title, nb)

            menu += [
                {
                    'url': url_for('mes_actions'),
                    'label': '%s' % label,
                    'endpoint': 'mes_actions'
                }
            ]

Nicolas Joyard's avatar
Nicolas Joyard committed
100 101
        if session.get('user') and session.get('user')['admin']:
            menu += [
Nicolas Joyard's avatar
Nicolas Joyard committed
102
                {
Nicolas Joyard's avatar
Nicolas Joyard committed
103 104
                    'url': url_for('admin_recent'),
                    'label': '<span class="admin">Actions récentes</span>',
Nicolas Joyard's avatar
Nicolas Joyard committed
105
                    'endpoint': 'admin_recent'
106
                }
Nicolas Joyard's avatar
Nicolas Joyard committed
107
            ]
Nicolas Joyard's avatar
Nicolas Joyard committed
108

Nicolas Joyard's avatar
Nicolas Joyard committed
109 110 111 112
            nb_aconfirmer = Parlementaire.query \
                .filter(Parlementaire.etape == ETAPE_A_CONFIRMER).count()

            if nb_aconfirmer > 0:
Nicolas Joyard's avatar
Nicolas Joyard committed
113
                badge = ' <span class="badge">%s</span>' % nb_aconfirmer
Nicolas Joyard's avatar
Nicolas Joyard committed
114 115 116
                menu += [
                    {
                        'url': url_for('admin_en_attente'),
Nicolas Joyard's avatar
Nicolas Joyard committed
117 118
                        'label': '<span class="admin">À confirmer</span> %s'
                                 % badge,
Nicolas Joyard's avatar
Nicolas Joyard committed
119
                        'endpoint': 'admin_en_attente'
Nicolas Joyard's avatar
Nicolas Joyard committed
120 121 122
                    }
                ]

123
            nb_moderer = Action.query \
124
                               .join(Action.user) \
Nicolas Joyard's avatar
Nicolas Joyard committed
125
                               .filter(Action.etape == ETAPE_COM_A_MODERER) \
126
                               .filter(User.nick != '!rc') \
127 128 129
                               .count()

            if nb_moderer > 0:
Nicolas Joyard's avatar
Nicolas Joyard committed
130
                badge = ' <span class="badge">%s</span>' % nb_moderer
131 132 133
                menu += [
                    {
                        'url': url_for('admin_commentaires'),
Nicolas Joyard's avatar
Nicolas Joyard committed
134 135
                        'label': '<span class="admin">À modérer</span> %s'
                                 % badge,
136 137 138 139
                        'endpoint': 'admin_commentaires'
                    }
                ]

Nicolas Joyard's avatar
Nicolas Joyard committed
140
        return {'menu': menu}
141 142

    @app.context_processor
Nicolas Joyard's avatar
Nicolas Joyard committed
143
    def inject_constante():
144
        return {
Nicolas Joyard's avatar
Nicolas Joyard committed
145 146
            'etapes_by_ordre': ETAPES_BY_ORDRE,
            'etapes': ETAPES,
147
            'ordres': {
148 149
                'ETAPE_COM_PUBLIE': ETAPE_COM_PUBLIE,
                'ETAPE_COM_A_MODERER': ETAPE_COM_A_MODERER,
150 151
                'ETAPE_DOC_MASQUE': ETAPE_DOC_MASQUE,
                'ETAPE_DOC_PUBLIE': ETAPE_DOC_PUBLIE,
152
                'ETAPE_COURRIEL': ETAPE_COURRIEL,
153 154 155 156
                'ETAPE_NA': ETAPE_NA,
                'ETAPE_A_ENVOYER': ETAPE_A_ENVOYER,
                'ETAPE_A_CONFIRMER': ETAPE_A_CONFIRMER,
                'ETAPE_ENVOYE': ETAPE_ENVOYE,
Nicolas Joyard's avatar
Nicolas Joyard committed
157
                'ETAPE_AR_RECU': ETAPE_AR_RECU,
158
                'ETAPE_REPONSE_POSITIVE': ETAPE_REPONSE_POSITIVE,
159
                'ETAPE_REPONSE_NEGATIVE': ETAPE_REPONSE_NEGATIVE,
Nicolas Joyard's avatar
Nicolas Joyard committed
160 161 162
                'ETAPE_DEMANDE_CADA': ETAPE_DEMANDE_CADA,
                'ETAPE_INCOMPETENCE_CADA': ETAPE_INCOMPETENCE_CADA,
                'ETAPE_REQUETE_TA': ETAPE_REQUETE_TA
Nicolas Joyard's avatar
Nicolas Joyard committed
163 164
            },
            'chambres': CHAMBRES
165
        }