fix: update bedelia views for today and weekly reservations, available rooms and unassigned commissions
This commit is contained in:
@@ -213,6 +213,8 @@ def get_commissions():
|
||||
elif active == 'false':
|
||||
query = query.filter(Commission.active == False)
|
||||
|
||||
unassigned_room = request.args.get('unassigned_room', '').strip().lower()
|
||||
|
||||
commissions = query.order_by(Commission.id.asc()).all()
|
||||
subjects = Subject.query.order_by(Subject.name.asc()).all()
|
||||
|
||||
@@ -220,6 +222,13 @@ def get_commissions():
|
||||
careers = Career.query.order_by(Career.name.asc()).all()
|
||||
careers_data = [{'id': cr.id, 'name': cr.name, 'code': cr.code} for cr in careers]
|
||||
|
||||
# Pre-cargar reservas activas para enriquecer con asignación de aula
|
||||
reservations_by_comm = {}
|
||||
all_res = Reservation.query.filter(Reservation.commission_id.isnot(None)).order_by(Reservation.start_time.asc()).all()
|
||||
for r in all_res:
|
||||
if r.commission_id not in reservations_by_comm:
|
||||
reservations_by_comm[r.commission_id] = r
|
||||
|
||||
commissions_data = []
|
||||
for c in commissions:
|
||||
if search:
|
||||
@@ -228,11 +237,25 @@ def get_commissions():
|
||||
if search not in s_name and search not in c_code:
|
||||
continue
|
||||
|
||||
res = reservations_by_comm.get(c.id)
|
||||
c_room = res.classroom if res else None
|
||||
c_code_val = c_room.code if c_room else None
|
||||
is_virt_room = bool(c_room.is_virtual or 'virtual' in (c_room.code or '').lower() or 'virtual' in (c_room.building or '').lower()) if c_room else False
|
||||
has_v_link = bool(c.virtual_link and c.virtual_link.strip())
|
||||
has_classroom = bool(c_code_val or has_v_link)
|
||||
|
||||
if unassigned_room in ['true', '1'] and has_classroom:
|
||||
continue
|
||||
if unassigned_room in ['false', '0'] and not has_classroom:
|
||||
continue
|
||||
|
||||
career_name = c.career_name if hasattr(c, 'career_name') else 'Carrera General'
|
||||
career_names = c.career_names if hasattr(c, 'career_names') else career_name
|
||||
careers_list = c.careers if hasattr(c, 'careers') else []
|
||||
career_id_val = c.career_id if hasattr(c, 'career_id') else (c.subject.career_id if c.subject else None)
|
||||
|
||||
display_room = c_code_val or ('Campus Virtual' if has_v_link else 'Sin Aula')
|
||||
|
||||
commissions_data.append({
|
||||
'id': c.id,
|
||||
'code': c.code,
|
||||
@@ -256,7 +279,11 @@ def get_commissions():
|
||||
'max_students': c.max_students or 35,
|
||||
'current_students': getattr(c, 'current_students', 0) or 0,
|
||||
'active': c.active,
|
||||
'virtual_link': getattr(c, 'virtual_link', '') or ''
|
||||
'virtual_link': getattr(c, 'virtual_link', '') or '',
|
||||
'has_classroom': has_classroom,
|
||||
'classroom_name': display_room,
|
||||
'classroom_code': c_code_val,
|
||||
'is_virtual': is_virt_room or has_v_link
|
||||
})
|
||||
|
||||
subjects_data = []
|
||||
|
||||
@@ -150,7 +150,7 @@ def get_stats():
|
||||
upcoming_reservations = [serialize_reservation(r) for r in upcoming_reservations_q]
|
||||
milestones = [serialize_milestone(m) for m in milestones_q]
|
||||
|
||||
# ---- Bedelía: Comisiones sin aula asignada (semana y mes) ----
|
||||
# ---- Bedelía: Comisiones sin aula asignada (semana, mes y general) ----
|
||||
import calendar
|
||||
# Calcular rango de semana actual (lunes a domingo)
|
||||
today_dt = datetime.utcnow()
|
||||
@@ -158,6 +158,13 @@ def get_stats():
|
||||
week_start = week_start.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
week_end = week_start + timedelta(days=6, hours=23, minutes=59, seconds=59)
|
||||
|
||||
# Reservas de la semana actual completa para Bedelía
|
||||
week_reservations_q = Reservation.query.filter(
|
||||
Reservation.start_time >= week_start,
|
||||
Reservation.start_time <= week_end
|
||||
).order_by(Reservation.start_time.asc()).all()
|
||||
week_schedule_list = [serialize_reservation(r) for r in week_reservations_q]
|
||||
|
||||
# Calcular rango del mes actual
|
||||
month_start = today_dt.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
||||
last_day = calendar.monthrange(today_dt.year, today_dt.month)[1]
|
||||
@@ -183,10 +190,18 @@ def get_stats():
|
||||
Reservation.start_time <= month_end
|
||||
).distinct().all()
|
||||
)
|
||||
# IDs de comisiones que tienen AL MENOS una reserva en general
|
||||
comm_ids_with_any_res = set(
|
||||
r[0] for r in db.session.query(Reservation.commission_id).filter(
|
||||
Reservation.commission_id.isnot(None)
|
||||
).distinct().all()
|
||||
)
|
||||
|
||||
unassigned_commissions_week = []
|
||||
unassigned_commissions_month = []
|
||||
unassigned_commissions_general = []
|
||||
for comm in all_active_commissions:
|
||||
has_v_link = bool(comm.virtual_link and comm.virtual_link.strip())
|
||||
comm_data = {
|
||||
'id': comm.id,
|
||||
'code': comm.code,
|
||||
@@ -197,14 +212,24 @@ def get_stats():
|
||||
'schedule': comm.schedule or 'Sin horario',
|
||||
'shift': comm.shift or 'Sin turno',
|
||||
'max_students': comm.max_students,
|
||||
'current_students': comm.current_students
|
||||
'current_students': comm.current_students,
|
||||
'virtual_link': comm.virtual_link or '',
|
||||
'has_virtual_link': has_v_link
|
||||
}
|
||||
if comm.id not in comm_ids_with_res_week:
|
||||
unassigned_commissions_week.append(comm_data)
|
||||
if comm.id not in comm_ids_with_res_month:
|
||||
unassigned_commissions_month.append(comm_data)
|
||||
if comm.id not in comm_ids_with_any_res and not has_v_link:
|
||||
unassigned_commissions_general.append(comm_data)
|
||||
|
||||
# ---- Bedelía: Aulas disponibles (sin reservas) en la semana y mes ----
|
||||
# ---- Bedelía: Aulas disponibles (sin reservas) hoy, semana y mes ----
|
||||
classroom_ids_used_today = set(
|
||||
r[0] for r in db.session.query(Reservation.classroom_id).filter(
|
||||
Reservation.classroom_id.isnot(None),
|
||||
func.date(Reservation.start_time) == today
|
||||
).distinct().all()
|
||||
)
|
||||
classroom_ids_used_week = set(
|
||||
r[0] for r in db.session.query(Reservation.classroom_id).filter(
|
||||
Reservation.classroom_id.isnot(None),
|
||||
@@ -220,6 +245,7 @@ def get_stats():
|
||||
).distinct().all()
|
||||
)
|
||||
|
||||
available_classrooms_today = []
|
||||
available_classrooms_week = []
|
||||
available_classrooms_month = []
|
||||
for c in all_active_classrooms:
|
||||
@@ -231,6 +257,8 @@ def get_stats():
|
||||
'capacity': c.capacity,
|
||||
'is_virtual': getattr(c, 'is_virtual', False) or 'virtual' in (c.building or '').lower() or 'virtual' in (c.code or '').lower()
|
||||
}
|
||||
if c.id not in classroom_ids_used_today:
|
||||
available_classrooms_today.append(c_data)
|
||||
if c.id not in classroom_ids_used_week:
|
||||
available_classrooms_week.append(c_data)
|
||||
if c.id not in classroom_ids_used_month:
|
||||
@@ -257,9 +285,12 @@ def get_stats():
|
||||
audit_data = []
|
||||
pending_reservations = []
|
||||
today_schedule_list = []
|
||||
week_schedule_list = []
|
||||
upcoming_reservations = []
|
||||
unassigned_commissions_week = []
|
||||
unassigned_commissions_month = []
|
||||
unassigned_commissions_general = []
|
||||
available_classrooms_today = []
|
||||
available_classrooms_week = []
|
||||
available_classrooms_month = []
|
||||
total_active_commissions = 0
|
||||
@@ -306,6 +337,7 @@ def get_stats():
|
||||
'monthly_data': monthly_data,
|
||||
'pending_reservations': pending_reservations,
|
||||
'today_schedule_list': today_schedule_list,
|
||||
'week_schedule_list': week_schedule_list,
|
||||
'upcoming_reservations': upcoming_reservations,
|
||||
'milestones': milestones,
|
||||
'ui': {
|
||||
@@ -314,6 +346,8 @@ def get_stats():
|
||||
'bedelia': {
|
||||
'unassigned_commissions_week': unassigned_commissions_week,
|
||||
'unassigned_commissions_month': unassigned_commissions_month,
|
||||
'unassigned_commissions_general': unassigned_commissions_general,
|
||||
'available_classrooms_today': available_classrooms_today,
|
||||
'available_classrooms_week': available_classrooms_week,
|
||||
'available_classrooms_month': available_classrooms_month
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user