Newer
Older
from flask import Blueprint, flash, redirect, render_template, request, url_for
from flask_login import current_user, login_required
from sqlalchemy import and_
from app import messages
from app.database import db
from app.forms.project import ProjectForm
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")

Andrea Callia D'Iddio
committed
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()
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"])
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()

Andrea Callia D'Iddio
committed
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")
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()

Andrea Callia D'Iddio
committed
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

Andrea Callia D'Iddio
committed
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"))
Andrea Callia D'Iddio
committed
@bp.route("/projects/shortlisted")
Andrea Callia D'Iddio
committed
@login_required
Andrea Callia D'Iddio
committed
def projects_ranking():
projects: list[Shortlisting] = (
Shortlisting.query.join(Project)
.filter(Project.proposer == current_user.username)
.order_by(Shortlisting.staff_ranking)
Andrea Callia D'Iddio
committed
.all()
)
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
db.session.commit()
return "", 204