feat: acople de schedule.view_reservation al BFF y creacion/asociacion de comisiones a materias
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from flask import Blueprint, jsonify, request
|
||||
from datetime import datetime
|
||||
import re
|
||||
from app.utils.jwt_decorators import jwt_required
|
||||
from app.models.user import User
|
||||
from app.models.role import Role, Permission, SYSTEM_MODULES
|
||||
@@ -196,12 +197,22 @@ def get_commissions():
|
||||
if search not in s_name and search not in c_code:
|
||||
continue
|
||||
|
||||
career_name = 'Carrera General'
|
||||
if c.subject and hasattr(c.subject, 'career') and c.subject.career:
|
||||
career_name = c.subject.career.name
|
||||
|
||||
commissions_data.append({
|
||||
'id': c.id,
|
||||
'code': c.code,
|
||||
'full_code': c.get_full_code() if hasattr(c, 'get_full_code') else c.code,
|
||||
'subject_id': c.subject_id,
|
||||
'subject_name': c.subject.name if c.subject else 'Sin materia',
|
||||
'subject_code': c.subject.code if c.subject else '-',
|
||||
'career_name': career_name,
|
||||
'semester': getattr(c, 'semester', '1C') or '1C',
|
||||
'year': getattr(c, 'year', 2026) or 2026,
|
||||
'teacher_id': c.teacher_id,
|
||||
'teacher_name': c.teacher_name if hasattr(c, 'teacher_name') else (c.teacher.name if c.teacher else None),
|
||||
'schedule': c.schedule or 'A coordinar',
|
||||
'shift': c.shift or 'Mañana',
|
||||
'max_students': c.max_students or 35,
|
||||
@@ -210,7 +221,16 @@ def get_commissions():
|
||||
'virtual_link': getattr(c, 'virtual_link', '') or ''
|
||||
})
|
||||
|
||||
subjects_data = [{'id': s.id, 'name': s.name, 'code': s.code} for s in subjects]
|
||||
subjects_data = []
|
||||
for s in subjects:
|
||||
c_name = s.career.name if (hasattr(s, 'career') and s.career) else 'Carrera General'
|
||||
subjects_data.append({
|
||||
'id': s.id,
|
||||
'name': s.name,
|
||||
'code': s.code,
|
||||
'career': c_name,
|
||||
'career_name': c_name
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'status': 'success',
|
||||
@@ -599,14 +619,14 @@ def create_commission():
|
||||
data = request.get_json(silent=True) or request.form.to_dict() or {}
|
||||
subject_id = data.get('subject_id')
|
||||
code = data.get('code', '').strip()
|
||||
semester = data.get('semester', '1C').strip()
|
||||
semester = (data.get('semester') or '1C').strip()
|
||||
year = int(data.get('year', datetime.now().year) or datetime.now().year)
|
||||
teacher_id = data.get('teacher_id')
|
||||
teacher_id = int(teacher_id) if teacher_id else None
|
||||
teacher_id = int(teacher_id) if (teacher_id and str(teacher_id).strip()) else None
|
||||
max_students = int(data.get('max_students', 35) or 35)
|
||||
schedule = data.get('schedule', '').strip()
|
||||
shift = data.get('shift', 'Mañana').strip()
|
||||
virtual_link = data.get('virtual_link', '').strip()
|
||||
schedule = (data.get('schedule') or '').strip()
|
||||
shift = (data.get('shift') or 'Mañana').strip()
|
||||
virtual_link = (data.get('virtual_link') or '').strip()
|
||||
active = data.get('active', True)
|
||||
if isinstance(active, str):
|
||||
active = active.lower() in ['true', '1', 'on']
|
||||
@@ -614,6 +634,38 @@ def create_commission():
|
||||
if not subject_id or not code:
|
||||
return jsonify({'error': 'ValidationError', 'message': 'La asignatura y código de comisión son obligatorios.'}), 400
|
||||
|
||||
subject = Subject.query.get(int(subject_id))
|
||||
if not subject:
|
||||
return jsonify({'error': 'NotFound', 'message': f'La asignatura con ID {subject_id} no existe.'}), 404
|
||||
|
||||
# Generar enlace automático si no fue provisto
|
||||
if not virtual_link:
|
||||
s_clean = re.sub(r'[^a-zA-Z0-9]', '', subject.code).lower()[:6] or 'subj'
|
||||
c_clean = re.sub(r'[^a-zA-Z0-9]', '', code).lower()[:4] or 'c1'
|
||||
virtual_link = f"https://meet.google.com/edu-{s_clean}-{c_clean}"
|
||||
|
||||
existing = Commission.query.filter_by(
|
||||
subject_id=int(subject_id),
|
||||
code=code,
|
||||
semester=semester,
|
||||
year=year
|
||||
).first()
|
||||
|
||||
if existing:
|
||||
existing.teacher_id = teacher_id
|
||||
existing.max_students = max_students
|
||||
existing.schedule = schedule
|
||||
existing.shift = shift
|
||||
if virtual_link:
|
||||
existing.virtual_link = virtual_link
|
||||
existing.active = bool(active)
|
||||
db.session.commit()
|
||||
return jsonify({
|
||||
'status': 'success',
|
||||
'message': f'Comisión {code} asociada a {subject.name} actualizada exitosamente.',
|
||||
'commission': existing.to_dict()
|
||||
}), 200
|
||||
|
||||
commission = Commission(
|
||||
subject_id=int(subject_id),
|
||||
code=code,
|
||||
@@ -631,10 +683,11 @@ def create_commission():
|
||||
|
||||
return jsonify({
|
||||
'status': 'success',
|
||||
'message': 'Comisión creada exitosamente.',
|
||||
'message': f'Comisión {code} asociada exitosamente a {subject.name}.',
|
||||
'commission': commission.to_dict()
|
||||
}), 201
|
||||
|
||||
|
||||
@api_admin_bp.route('/commissions/<int:id>', methods=['PUT'])
|
||||
@jwt_required
|
||||
def update_commission(id):
|
||||
|
||||
Reference in New Issue
Block a user