Skip to content
Snippets Groups Projects
staff.py 3.99 KiB
Newer Older
from datetime import datetime

Ivan Procaccini's avatar
Ivan Procaccini committed
from flask import Blueprint, flash, redirect, render_template, request, url_for
from flask_login import current_user, login_required
Ivan Procaccini's avatar
Ivan Procaccini committed
from app import messages
from app.database import db
from app.forms.project import ProjectForm
Ivan Procaccini's avatar
Ivan Procaccini committed
from app.models.project import Project
from app.models.shortlist import Shortlisting
from app.utils.decorators import staff_only
bp = Blueprint("staff", __name__, url_prefix="/staff")


def shortlist_proposer(project):
    shortlisting = Shortlisting(student=project.on_behalf, project_id=project.id)
    db.session.add(shortlisting)
    db.session.commit()


def unshortlist_old_proposer(project: Project):
    shortlisting: Shortlisting = (
        Shortlisting.query.filter(Shortlisting.project_id == project.id)
        .filter_by(student=project.on_behalf)
        .first()
    )
    if shortlisting:
        db.session.delete(shortlisting)
        db.session.commit()


@bp.route("/projects")
    active_projects: list[Project] = (
        Project.query.filter_by(proposer=current_user.username)
        .filter(Project.deleted.is_(None))
        .all()
    )
    return render_template("pages/staff/projects.html", projects=active_projects)
@bp.route("/projects/create", methods=["GET", "POST"])
def create_project():
    form = ProjectForm()
    if form.validate_on_submit():
        project = Project(
            title=form.title.data,
            description=form.description.data,
            proposer=current_user.username,
            on_behalf=form.on_behalf.data,
        )
        db.session.add(project)
        db.session.commit()
        if project.on_behalf:
            shortlist_proposer(project)
        return redirect(url_for("staff.projects"))
    return render_template("pages/project-form.html", form=form)
@bp.route("/projects/<project_id>/delete")
@login_required
def delete_project(project_id):
    if project := Project.query.get(project_id):
        project.deleted = datetime.utcnow()
        db.session.commit()
    else:
        flash(messages.PROJECT_NOT_FOUND)
    return redirect(url_for("staff.projects"))


@bp.route("/projects/<project_id>/edit", methods=["GET", "POST"])
@login_required
def edit_project(project_id):
    if project := Project.query.get(project_id):
        form = ProjectForm()
        on_behalf_changed = project.on_behalf != form.on_behalf.data
        if on_behalf_changed and project.on_behalf:
            unshortlist_old_proposer(project)
        if form.validate_on_submit():
            project.title = form.title.data
            project.description = form.description.data
            project.on_behalf = form.on_behalf.data
            db.session.commit()
            if project.on_behalf and on_behalf_changed:
                shortlist_proposer(project)
            return redirect(url_for("staff.projects"))
        form = ProjectForm(obj=project)
        return render_template("pages/project-form.html", form=form, project=project)
    flash(messages.PROJECT_NOT_FOUND)
    return redirect(url_for("staff.projects"))
@bp.route("/projects/shortlisted")
def projects_ranking():
    projects: list[Shortlisting] = (
        Shortlisting.query.join(Project)
        .filter(Project.proposer == current_user.username)
        .order_by(Shortlisting.staff_ranking)
    return render_template("pages/staff/shortlisted-projects.html", projects=projects)


@bp.route("/projects/rankings", methods=["PUT"])
@login_required
def update_ranking():
    shortlisting_ids = request.json
    current_shortlist: list[Shortlisting] = Shortlisting.query.join(Project).filter(
        and_(
            Shortlisting.id.in_(shortlisting_ids),
            Project.proposer == current_user.username,
        )
    )
    for shortlisting in current_shortlist:
        shortlisting.staff_ranking = shortlisting_ids.index(shortlisting.id) + 1