127 lines
4.0 KiB
Python
127 lines
4.0 KiB
Python
from flask import Blueprint, render_template, abort
|
|
from flask_login import login_required, current_user
|
|
from app import db
|
|
from app.models.subject import Commission
|
|
from app.models.enrollment import StudentEnrollment
|
|
from app.models.classroom import Classroom
|
|
|
|
my_subjects_bp = Blueprint('my_subjects', __name__, url_prefix='/mis-materias')
|
|
|
|
|
|
@my_subjects_bp.route('/')
|
|
@login_required
|
|
def my_subjects_list():
|
|
"""Show the commissions assigned to the current user (teacher or student)."""
|
|
role = current_user.get_institutional_role()
|
|
|
|
commissions = []
|
|
|
|
if role in ['admin', 'bedelia']:
|
|
# Admin/Bedelia: see all active commissions
|
|
commissions = (
|
|
Commission.query
|
|
.filter_by(active=True)
|
|
.order_by(Commission.year.desc(), Commission.semester.asc(), Commission.code.asc())
|
|
.all()
|
|
)
|
|
view_mode = 'all'
|
|
|
|
elif role == 'docente':
|
|
# Teacher: only commissions where they are assigned as teacher
|
|
commissions = (
|
|
Commission.query
|
|
.filter_by(teacher_id=current_user.id, active=True)
|
|
.order_by(Commission.year.desc(), Commission.semester.asc(), Commission.code.asc())
|
|
.all()
|
|
)
|
|
view_mode = 'teacher'
|
|
|
|
elif role == 'alumno':
|
|
# Student: only commissions where they are enrolled
|
|
enrollments = (
|
|
StudentEnrollment.query
|
|
.filter_by(student_id=current_user.id, status='activo')
|
|
.all()
|
|
)
|
|
commission_ids = [e.commission_id for e in enrollments]
|
|
commissions = (
|
|
Commission.query
|
|
.filter(Commission.id.in_(commission_ids), Commission.active == True)
|
|
.order_by(Commission.year.desc(), Commission.semester.asc(), Commission.code.asc())
|
|
.all()
|
|
)
|
|
view_mode = 'student'
|
|
else:
|
|
commissions = []
|
|
view_mode = 'none'
|
|
|
|
return render_template(
|
|
'my_subjects/list.html',
|
|
commissions=commissions,
|
|
view_mode=view_mode,
|
|
role=role
|
|
)
|
|
|
|
|
|
@my_subjects_bp.route('/<int:commission_id>')
|
|
@login_required
|
|
def my_subject_detail(commission_id):
|
|
"""Detail view of a single commission for the current user."""
|
|
role = current_user.get_institutional_role()
|
|
commission = Commission.query.get_or_404(commission_id)
|
|
|
|
# Access control: make sure the user is allowed to see this commission
|
|
if role in ['admin', 'bedelia']:
|
|
pass # full access
|
|
elif role == 'docente':
|
|
if commission.teacher_id != current_user.id:
|
|
abort(403)
|
|
elif role == 'alumno':
|
|
enrollment = StudentEnrollment.query.filter_by(
|
|
student_id=current_user.id,
|
|
commission_id=commission_id
|
|
).first()
|
|
if not enrollment:
|
|
abort(403)
|
|
else:
|
|
abort(403)
|
|
|
|
# Get enrolled students (visible to teachers and admin/bedelia)
|
|
enrollments = []
|
|
if role in ['admin', 'bedelia', 'docente']:
|
|
enrollments = (
|
|
StudentEnrollment.query
|
|
.filter_by(commission_id=commission_id)
|
|
.order_by(StudentEnrollment.enrolled_at.asc())
|
|
.all()
|
|
)
|
|
|
|
# Try to get classroom info from reservations linked to this commission
|
|
from app.models.reservation import Reservation
|
|
from sqlalchemy import func
|
|
from datetime import datetime
|
|
|
|
# Recent or upcoming reservation linked to this commission
|
|
recent_reservation = (
|
|
Reservation.query
|
|
.filter(
|
|
Reservation.commission_id == commission_id,
|
|
Reservation.start_time >= datetime.utcnow()
|
|
)
|
|
.order_by(Reservation.start_time.asc())
|
|
.first()
|
|
)
|
|
|
|
classroom = None
|
|
if recent_reservation and recent_reservation.classroom_id:
|
|
classroom = Classroom.query.get(recent_reservation.classroom_id)
|
|
|
|
return render_template(
|
|
'my_subjects/detail.html',
|
|
commission=commission,
|
|
enrollments=enrollments,
|
|
classroom=classroom,
|
|
recent_reservation=recent_reservation,
|
|
role=role
|
|
)
|