routes.py 3.18 KB
import json

from flask import render_template, make_response, current_app
from flask_login import login_required

from . import bp

from app.models import Agent, Project, Service, Capacity, Period
from app import db_mgr
from app.auth.routes import role_required


@bp.route('/')
def index():
    return render_template('index.html', subtitle="Page d'accueil")


@bp.route('/services')
@role_required('service')
def services():
    # get services list
    all_services = Service.query.order_by(Service.name).all()
    num_services = len(all_services)
    # pass to template
    return render_template('services.html', subtitle="Liste des services ({})".format(num_services),
                           services=all_services)


@bp.route('/projects')
@role_required('project')
def projects():
    # get projects list
    all_projects = Project.query.order_by(Project.name).all()
    num_projects = len(all_projects)
    # pass to template
    return render_template('projects.html', subtitle="Liste des projets ({})".format(num_projects),
                           projects=all_projects)


@bp.route('/agents')
@role_required('project')
def agents():
    current_app.logger.info("Accessing agents page")
    # get agents list
    all_agents = Agent.query.order_by(Agent.firstname).all()
    num_agents = len(all_agents)
    # pass to template
    return render_template('agents.html', subtitle="Liste des agents ({})".format(num_agents),
                           agents=all_agents)


@bp.route('/capacities')
@login_required
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)


@bp.route('/periods')
@login_required
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)


@bp.route('/charge/add')
@role_required('service')
def charge_add():
    return render_template('charge.html', subtitle="Affecter un agent")


@bp.route('/charge/agent/<agent_id>')
@role_required('service')
def charge_agent(agent_id):
    current_app.logger.info("Accessing agent {} page".format(agent_id))
    agent_charges = []
    for [period, charge] in db_mgr.charges_by_agent(agent_id):
        agent_charges.append({"charge": charge, "periode": period})
    # agent_charges = [["charge", "periode"]]+db_mgr.charges_by_agent(agent_id)#agent_charges
    resp = make_response(json.dumps(agent_charges))
    resp.headers['Content-Type'] = 'application/json'
    return resp


@bp.route('/agent/<agent_id>')
@role_required('agent')
def agent(agent_id):
    # TODO: am i the agent, the service or project manager , or the admin ?
    this_agent = Agent.query.get(int(agent_id))
    return render_template('agent.html',
                           agent=this_agent,
                           subtitle="{} {}".format(agent.firstname, agent.secondname))