<commit_before># -*- coding: utf8 -*-
import os
import uuid
import collections
from flask import Flask, render_template, request, session, redirect, url_for
from flask import abort, Response
from werkzeug.exceptions import HTTPException
from werkzeug.wrappers import Response

import model
import emails
import controller
from config import config

app = Flask('enseigner')
salt = config['password_salt']
assert len(salt) >= 20, 'Secret key is not long enough'
app.secret_key = salt

TUTORS_EMAIL_SUBJECT = u'ENSeigner — Participation à la séance du %(date)s'
TUTORS_EMAIL_CONTENT = u'''Bonjour $nom_tuteur,

Voici, comme chaque semaine, le lien vers le formulaire pour participer à
la séance de samedi prochain en tant que tuteur-trice :
$lien_formulaire_tuteur

Cordialement,
Les reponsables du soutien'''

STUDENTS_EMAIL_SUBJECT = u'ENSeigner — Inscription à la séance du %(date)s'
STUDENTS_EMAIL_CONTENT = u'''Bonjour $nom_eleve,

Voici, comme chaque semaine, le lien vers le formulaire pour t’inscrire à
la séance de samedi prochain en temps qu’élève :
$lien_formulaire_eleve

Cordialement,
Les reponsables du soutien'''

class AdminOnly(HTTPException):
    def get_response(self, environ=None):
        html = render_template('erreur.html', admin_only=True,
                error_message=u'Accès réservé aux responsables du soutien.')
        return Response(html, mimetype='text/html')

def require_admin(f):
    def newf(**kwargs):
        try:
            id = session.get('tutor_id', None)
            if not id:
                raise model.NotFound()
            tutor = model.Tutor.get(id)
        except model.NotFound:
            url = url_for(f.__name__, **kwargs).lstrip('/')
            return redirect(url_for('connexion', redirect_url=url))
        else:
            if tutor.is_admin:
                return f(**kwargs)
            else:
                raise AdminOnly()
    newf.__name__ = f.__name__
    return newf


# From http://flask.pocoo.org/snippets/3/
@app.before_request
def csrf_protect():
    if request.method == "POST":
        token = session.pop('_csrf_token', None)
        if not token or token != request.form.get('_csrf_token'):
            abort(403)
def generate_csrf_token():
    if '_csrf_token' not in session:
        session['_csrf_token'] = str(uuid.uuid4())
    return session['_csrf_token']
app.jinja_env.globals['csrf_token'] = generate_csrf_token        

@app.route('/')
def accueil():
    return render_template('accueil.html')

@app.route('/inscription/')
def inscription():
    return render_template('inscription.html')

@app.route('/prochaines_seances/')
def prochaines_seances():
    return render_template('prochaines_seances.html')

@app.route('/gestion_soutien/')
@require_admin
def gestion_soutien():
    return render_template('gestion_soutien/index.html',
            sessions=model.Session.all())

@app.route('/gestion_soutien/liste_tuteurs_seance/')
@require_admin
def liste_tuteurs_seance():
    session = model.Session.get(int(request.args['session']))
    rows = controller.get_tutor_registration_list_rows(session)

    if request.args.get('download', 'false') == 'true':
        def pred(row):
            return '%s;%s;%s;%s;%s' % (
                    row.tutor.name,
                    row.tutor.email,
                    ', '.join(x.name for x in row.subjects1),
                    ', '.join(x.name for x in row.subjects2),
                    row.comment)
        response = Response('\n'.join(map(pred, rows)),
                mimetype='text/csv')
        response.headers["Content-Disposition"] = "attachment; filename=Tuteurs_seance.csv"
        return response
    else:
        return render_template('gestion_soutien/liste_tuteurs_seance.html',
                session=session,<commit_msg>Download as ODS instead of CSV.<commit_after># -*- coding: utf8 -*-
import os
import uuid
import odswriter
import collections
from cStringIO import StringIO
from flask import Flask, render_template, request, session, redirect, url_for
from flask import abort, Response
from werkzeug.exceptions import HTTPException
from werkzeug.wrappers import Response

import model
import emails
import controller
from config import config

app = Flask('enseigner')
salt = config['password_salt']
assert len(salt) >= 20, 'Secret key is not long enough'
app.secret_key = salt

TUTORS_EMAIL_SUBJECT = u'ENSeigner — Participation à la séance du %(date)s'
TUTORS_EMAIL_CONTENT = u'''Bonjour $nom_tuteur,

Voici, comme chaque semaine, le lien vers le formulaire pour participer à
la séance de samedi prochain en tant que tuteur-trice :
$lien_formulaire_tuteur

Cordialement,
Les reponsables du soutien'''

STUDENTS_EMAIL_SUBJECT = u'ENSeigner — Inscription à la séance du %(date)s'
STUDENTS_EMAIL_CONTENT = u'''Bonjour $nom_eleve,

Voici, comme chaque semaine, le lien vers le formulaire pour t’inscrire à
la séance de samedi prochain en temps qu’élève :
$lien_formulaire_eleve

Cordialement,
Les reponsables du soutien'''

class AdminOnly(HTTPException):
    def get_response(self, environ=None):
        html = render_template('erreur.html', admin_only=True,
                error_message=u'Accès réservé aux responsables du soutien.')
        return Response(html, mimetype='text/html')

def require_admin(f):
    def newf(**kwargs):
        try:
            id = session.get('tutor_id', None)
            if not id:
                raise model.NotFound()
            tutor = model.Tutor.get(id)
        except model.NotFound:
            url = url_for(f.__name__, **kwargs).lstrip('/')
            return redirect(url_for('connexion', redirect_url=url))
        else:
            if tutor.is_admin:
                return f(**kwargs)
            else:
                raise AdminOnly()
    newf.__name__ = f.__name__
    return newf


# From http://flask.pocoo.org/snippets/3/
@app.before_request
def csrf_protect():
    if request.method == "POST":
        token = session.pop('_csrf_token', None)
        if not token or token != request.form.get('_csrf_token'):
            abort(403)
def generate_csrf_token():
    if '_csrf_token' not in session:
        session['_csrf_token'] = str(uuid.uuid4())
    return session['_csrf_token']
app.jinja_env.globals['csrf_token'] = generate_csrf_token        

@app.route('/')
def accueil():
    return render_template('accueil.html')

@app.route('/inscription/')
def inscription():
    return render_template('inscription.html')

@app.route('/prochaines_seances/')
def prochaines_seances():
    return render_template('prochaines_seances.html')

@app.route('/gestion_soutien/')
@require_admin
def gestion_soutien():
    return render_template('gestion_soutien/index.html',
            sessions=model.Session.all())

@app.route('/gestion_soutien/liste_tuteurs_seance/')
@require_admin
def liste_tuteurs_seance():
    session = model.Session.get(int(request.args['session']))
    rows = controller.get_tutor_registration_list_rows(session)

    if request.args.get('download', 'false') == 'true':
        def pred(row):
            return (
                    row.tutor.name,
                    row.tutor.email,
                    ', '.join(x.name for x in row.subjects1),
                    ', '.join(x.name for x in row.subjects2),
                    row.comment)
        io = StringIO()
        with odswriter.writer(io) as odsfile:
            for row in rows:
                odsfile.writerow(pred(row))
        io.seek(0)
        response = Response(io,
                mimetype='application/vnd.oasis.opendocument.spreadsheet')
        response.headers["Content-Disposition"] = "attachment; filename=Tuteurs_seance.ods"
        return response
    else:
        return render_template('gestion_soutien/liste_tuteurs_seance.html',
                session=session,
{'fraction_non_alphanumeric': 0.11068260701889703, 'fraction_numerical': 0.002699575780948708, 'mean_word_length': 3.1360978203083465, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1731563', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2456, 'n_words': 580}