feat(sheets): refactor google sheets importer with granular entities and interactive preview grid v3.4.3

This commit is contained in:
2026-10-07 22:51:41 -03:00
parent e1c2c6e8c6
commit 22c545e2df
12 changed files with 1001 additions and 195 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ from flask_babel import Babel, gettext as _
from config.config import Config
# Initialize extensions
__version__ = "3.4.2"
__version__ = "3.4.3"
db = SQLAlchemy()
migrate = Migrate()
cors = CORS()
+32 -4
View File
@@ -530,7 +530,7 @@ def get_stats():
@api_dashboard_bp.route('/sync-sheets', methods=['POST'])
@jwt_required
def sync_google_sheets():
"""Ejecuta la sincronización real con las hojas de cálculo de Google Sheets con soporte de filtros."""
"""Ejecuta la sincronización real con las hojas de cálculo de Google Sheets con soporte de filtros y entidades."""
from app.services.sheets_importer import GoogleSheetsImporter
try:
data = request.get_json(silent=True) or {}
@@ -538,16 +538,32 @@ def sync_google_sheets():
filter_career = data.get('filter_career')
filter_subject = data.get('filter_subject')
filter_day = data.get('filter_day')
filter_days = data.get('filter_days')
manual_date = data.get('manual_date')
single_date_only = bool(data.get('single_date_only', False))
# Opciones granulares de entidades
import_careers = data.get('import_careers', True)
import_subjects = data.get('import_subjects', True)
import_commissions = data.get('import_commissions', True)
import_classrooms = data.get('import_classrooms', True)
import_reservations = data.get('import_reservations', True)
selected_rows = data.get('selected_rows')
importer = GoogleSheetsImporter(base_url=custom_url)
stats = importer.sync(
filter_career=filter_career,
filter_subject=filter_subject,
filter_day=filter_day,
filter_days=filter_days,
manual_date=manual_date,
single_date_only=single_date_only
single_date_only=single_date_only,
import_careers=import_careers,
import_subjects=import_subjects,
import_commissions=import_commissions,
import_classrooms=import_classrooms,
import_reservations=import_reservations,
selected_rows=selected_rows
)
return jsonify({
'status': 'success',
@@ -563,13 +579,25 @@ def sync_google_sheets():
@api_dashboard_bp.route('/sheets-inspect', methods=['GET', 'POST'])
@jwt_required
def inspect_google_sheets():
"""Devuelve metadatos, carreras y materias del Google Sheet online para filtros interactivos."""
"""Devuelve metadatos, carreras, materias y previsualización de registros según los filtros aplicados."""
from app.services.sheets_importer import GoogleSheetsImporter
try:
data = request.get_json(silent=True) or {}
custom_url = data.get('sheets_url') or request.args.get('sheets_url')
filter_career = data.get('filter_career') or request.args.get('filter_career')
filter_subject = data.get('filter_subject') or request.args.get('filter_subject')
filter_day = data.get('filter_day') or request.args.get('filter_day')
filter_days = data.get('filter_days') or request.args.get('filter_days')
manual_date = data.get('manual_date') or request.args.get('manual_date')
importer = GoogleSheetsImporter(base_url=custom_url)
metadata = importer.inspect_sheet_content()
metadata = importer.inspect_sheet_content(
filter_career=filter_career,
filter_subject=filter_subject,
filter_day=filter_day,
filter_days=filter_days,
manual_date=manual_date
)
return jsonify({
'status': 'success',
'metadata': metadata,
+1 -1
View File
@@ -7,7 +7,7 @@ OPENAPI_SPEC = {
"info": {
"title": "Edu-Space REST API",
"description": "Enterprise Academic & Physical Space Management REST API with JWT Authentication and Role-Based Access Control.",
"version": "3.4.2"
"version": "3.4.3"
},
"servers": [
{
+274 -119
View File
@@ -352,21 +352,55 @@ class GoogleSheetsImporter:
except Exception:
return None, None
def inspect_sheet_content(self):
def inspect_sheet_content(self, filter_career=None, filter_subject=None, filter_day=None, filter_days=None, manual_date=None):
"""
Descarga e inspecciona las hojas del Sheet online para extraer:
- Días disponibles con cantidad de filas
- Carreras únicas
- Materias únicas con sus códigos, nombres y carreras
- Grilla de pre-visualización interactiva (preview_rows) según los filtros aplicados
"""
sheets_to_process = self.get_sheets_config()
days_info = []
careers_set = set()
subjects_map = {}
total_rows = 0
preview_rows = []
# Normalizar días seleccionados
allowed_days = None
if filter_days:
if isinstance(filter_days, str):
allowed_days = [normalize_day_name(d) for d in filter_days.split(',') if d.strip()]
elif isinstance(filter_days, (list, set)):
allowed_days = [normalize_day_name(d) for d in filter_days if d]
elif filter_day:
nd = normalize_day_name(filter_day)
if nd and nd.upper() not in ['ALL', 'TODOS', 'TODOS LOS DÍAS', 'TODOS LOS DIAS', '']:
allowed_days = [nd]
# Normalizar fecha manual para fecha inferida base
manual_dt = None
if manual_date:
try:
if isinstance(manual_date, (datetime, date)):
manual_dt = manual_date if isinstance(manual_date, date) else manual_date.date()
elif isinstance(manual_date, str) and manual_date.strip():
manual_dt = datetime.strptime(manual_date.strip(), '%Y-%m-%d').date()
except Exception:
pass
# Referencia de semana base: lunes 5 de octubre de 2026 (semana activa del 2° cuatrimestre)
ref_monday = date(2026, 10, 5)
if manual_dt:
ref_monday = manual_dt - timedelta(days=manual_dt.weekday())
row_counter = 0
for sheet_cfg in sheets_to_process:
day_name = sheet_cfg['name']
norm_day = normalize_day_name(day_name)
day_idx = sheet_cfg.get('day_idx', 0)
try:
csv_data = self.fetch_sheet_csv(sheet_cfg['gid'])
rows = self.parse_sheet_rows(csv_data, day_name)
@@ -374,10 +408,15 @@ class GoogleSheetsImporter:
days_info.append({
'name': day_name,
'gid': sheet_cfg['gid'],
'day_idx': sheet_cfg['day_idx'],
'day_idx': day_idx,
'rows_count': len(rows),
'status': 'OK'
})
# Si este día no está entre los permitidos, no lo agregamos a preview_rows
if allowed_days and norm_day not in allowed_days:
continue
for r in rows:
carreras = parse_careers_list(r.get('carrera', ''))
for c in carreras:
@@ -394,11 +433,67 @@ class GoogleSheetsImporter:
'careers': carreras,
'day': day_name
}
# Evaluar coincidencia de filtros de Carrera y Materia para la pre-visualización
if filter_career:
fc = filter_career.strip().upper()
match_c = any(fc in c.upper() or c.upper() in fc for c in carreras) or (fc in r.get('carrera', '').upper())
if not match_c:
continue
if filter_subject:
fs = filter_subject.strip().upper()
sub_codes = parse_subject_codes(code)
match_s = (fs in code.upper()) or (fs in name.upper()) or any(fs in sc.upper() for sc in sub_codes)
if not match_s:
continue
row_counter += 1
aula_raw = r.get('aula', '').strip()
room_info = self.normalize_room(aula_raw)
is_virtual = bool(room_info and ('VIRTUAL' in room_info.get('code', '').upper() or room_info.get('building') == 'Campus Virtual'))
# Calcular fecha tentativa
target_date = ref_monday + timedelta(days=day_idx)
if manual_dt and norm_day == normalize_day_name(['Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'][manual_dt.weekday()]):
target_date = manual_dt
# Validaciones
is_valid = True
has_warning = False
warning_msg = None
if not code or not name:
is_valid = False
warning_msg = "Falta código o nombre de asignatura"
elif not room_info or not aula_raw or any(x in aula_raw.upper() for x in ['FERIADO', 'SIN ACTIVIDAD', 'CONSULTAR']):
has_warning = True
warning_msg = f"Aula pendiente o sin asignar ({aula_raw or 'Vacía'})"
preview_rows.append({
'id': row_counter,
'dia': norm_day,
'day_idx': day_idx,
'codigo': code,
'asignatura': name,
'aula': aula_raw or (room_info['code'] if room_info else 'Sin Aula'),
'aula_normalizada': room_info['code'] if room_info else 'Sin Aula',
'is_virtual': is_virtual,
'carrera': r.get('carrera', 'General'),
'carreras_list': carreras,
'horario': r.get('horario', ''),
'turno': r.get('turno', 'Mañana'),
'inferred_date': target_date.strftime('%Y-%m-%d'),
'is_valid': is_valid,
'has_warning': has_warning,
'warning_msg': warning_msg
})
except Exception as e:
days_info.append({
'name': day_name,
'gid': sheet_cfg['gid'],
'day_idx': sheet_cfg.get('day_idx', 0),
'day_idx': day_idx,
'rows_count': 0,
'status': 'Error',
'error': str(e)
@@ -406,19 +501,28 @@ class GoogleSheetsImporter:
return {
'total_rows': total_rows,
'matching_rows': len(preview_rows),
'days': days_info,
'careers': sorted(list(careers_set)),
'subjects': sorted(list(subjects_map.values()), key=lambda x: x['code'])
'subjects': sorted(list(subjects_map.values()), key=lambda x: x['code']),
'preview_rows': preview_rows
}
def sync(self, filter_career=None, filter_subject=None, filter_day=None, manual_date=None, single_date_only=False):
def sync(self, filter_career=None, filter_subject=None, filter_day=None, filter_days=None, manual_date=None, single_date_only=False, import_careers=True, import_subjects=True, import_commissions=True, import_classrooms=True, import_reservations=True, selected_rows=None):
"""
Proceso principal de sincronización con soporte de filtros y fechas manuales:
Proceso principal de sincronización con soporte de filtros, entidades granulares y filas seleccionadas:
:param filter_career: Filtrar por carrera específica (ej. 'DATOS (CCC)')
:param filter_subject: Filtrar por código o nombre de materia (ej. 'ASIG00113')
:param filter_day: Filtrar por día específico ('Lunes'..'Sábado' o 'ALL')
:param filter_days: Lista o cadena de días específicos (ej. ['Lunes', 'Martes'])
:param manual_date: Fecha manual específica (YYYY-MM-DD o objeto date)
:param single_date_only: Si es True y hay manual_date, crea reserva solo para esa fecha puntual
:param import_careers: Booleano para importar/crear carreras
:param import_subjects: Booleano para importar/crear asignaturas
:param import_commissions: Booleano para importar/crear comisiones
:param import_classrooms: Booleano para importar/crear aulas
:param import_reservations: Booleano para importar/crear reservas
:param selected_rows: Lista de filas específicas pre-seleccionadas desde la grilla interactiva
"""
dias_esp = ['Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado', 'Domingo']
@@ -430,19 +534,26 @@ class GoogleSheetsImporter:
manual_dt = manual_date if isinstance(manual_date, date) else manual_date.date()
elif isinstance(manual_date, str) and manual_date.strip():
manual_dt = datetime.strptime(manual_date.strip(), '%Y-%m-%d').date()
except Exception as e:
except Exception:
pass
# Normalizar día filtro si fue provisto
norm_filter_day = normalize_day_name(filter_day) if filter_day else None
if norm_filter_day and norm_filter_day.upper() in ['ALL', 'TODOS', 'TODOS LOS DÍAS', 'TODOS LOS DIAS', '']:
norm_filter_day = None
# Normalizar días permitidos
allowed_days = None
if filter_days:
if isinstance(filter_days, str):
allowed_days = [normalize_day_name(d) for d in filter_days.split(',') if d.strip()]
elif isinstance(filter_days, (list, set)):
allowed_days = [normalize_day_name(d) for d in filter_days if d]
elif filter_day:
nd = normalize_day_name(filter_day)
if nd and nd.upper() not in ['ALL', 'TODOS', 'TODOS LOS DÍAS', 'TODOS LOS DIAS', '']:
allowed_days = [nd]
# Si el usuario no especificó filter_day pero ingresó fecha manual puntual, inferir día
if not norm_filter_day and manual_dt and single_date_only:
# Si el usuario no especificó días pero ingresó fecha manual puntual, inferir día
if not allowed_days and manual_dt and single_date_only:
manual_w = manual_dt.weekday()
if manual_w < 6:
norm_filter_day = dias_esp[manual_w]
allowed_days = [dias_esp[manual_w]]
stats = {
'total_rows_processed': 0,
@@ -461,39 +572,56 @@ class GoogleSheetsImporter:
'filters_applied': {
'career': filter_career.strip() if filter_career else None,
'subject': filter_subject.strip() if filter_subject else None,
'day': norm_filter_day or 'Todos los Días',
'days': allowed_days or ['Todos los Días'],
'manual_date': manual_dt.strftime('%Y-%m-%d') if manual_dt else None,
'single_date_only': bool(single_date_only and manual_dt)
'single_date_only': bool(single_date_only and manual_dt),
'entities': {
'careers': bool(import_careers),
'subjects': bool(import_subjects),
'commissions': bool(import_commissions),
'classrooms': bool(import_classrooms),
'reservations': bool(import_reservations)
}
}
}
all_raw_records = []
sheets_to_process = self.get_sheets_config()
# Filtrar pestañas si se especificó un día específico
if norm_filter_day:
sheets_to_process = [s for s in sheets_to_process if normalize_day_name(s['name']) == norm_filter_day]
if selected_rows and isinstance(selected_rows, list) and len(selected_rows) > 0:
# Modo grilla interactiva: se procesan las filas explícitamente seleccionadas por el usuario
for r in selected_rows:
d_name = r.get('dia') or 'Lunes'
d_idx = map_day_to_idx(d_name)
all_raw_records.append((r, d_idx))
if d_name not in stats['by_day']:
stats['by_day'][d_name] = {'rows_count': 0, 'status': 'OK'}
stats['by_day'][d_name]['rows_count'] += 1
else:
# Modo descarga general desde Google Sheets
sheets_to_process = self.get_sheets_config()
if allowed_days:
sheets_to_process = [s for s in sheets_to_process if normalize_day_name(s['name']) in allowed_days]
for sheet_cfg in sheets_to_process:
day_name = sheet_cfg['name']
try:
csv_data = self.fetch_sheet_csv(sheet_cfg['gid'])
rows = self.parse_sheet_rows(csv_data, day_name)
all_raw_records.extend([(r, sheet_cfg['day_idx']) for r in rows])
stats['by_day'][day_name] = {
'gid': sheet_cfg['gid'],
'rows_count': len(rows),
'status': 'OK'
}
except Exception as e:
err_msg = f"Error al descargar o procesar la hoja de {day_name}: {str(e)}"
stats['errors'].append(err_msg)
stats['by_day'][day_name] = {
'gid': sheet_cfg['gid'],
'rows_count': 0,
'status': 'Error',
'error': str(e)
}
for sheet_cfg in sheets_to_process:
day_name = sheet_cfg['name']
try:
csv_data = self.fetch_sheet_csv(sheet_cfg['gid'])
rows = self.parse_sheet_rows(csv_data, day_name)
all_raw_records.extend([(r, sheet_cfg['day_idx']) for r in rows])
stats['by_day'][day_name] = {
'gid': sheet_cfg['gid'],
'rows_count': len(rows),
'status': 'OK'
}
except Exception as e:
err_msg = f"Error al descargar o procesar la hoja de {day_name}: {str(e)}"
stats['errors'].append(err_msg)
stats['by_day'][day_name] = {
'gid': sheet_cfg['gid'],
'rows_count': 0,
'status': 'Error',
'error': str(e)
}
stats['total_rows_processed'] = len(all_raw_records)
if not all_raw_records:
@@ -581,13 +709,15 @@ class GoogleSheetsImporter:
career = careers_cache.get(c_key)
if not career:
career = Career.query.filter(Career.name.ilike(c_name)).first()
if not career:
if not career and import_careers:
career = Career(name=c_name)
db.session.add(career)
db.session.flush()
stats['careers_created'] += 1
careers_cache[c_key] = career
career_objs.append(career)
if career:
careers_cache[c_key] = career
if career:
career_objs.append(career)
primary_career = career_objs[0] if career_objs else None
@@ -599,23 +729,27 @@ class GoogleSheetsImporter:
classroom = classrooms_cache.get(room_key)
b_entity = buildings_cache.get(room_info['building'].upper())
if not classroom:
classroom = Classroom(
building=room_info['building'],
building_id=b_entity.id if b_entity else None,
floor=room_info['floor'],
capacity=room_info['capacity'],
description=room_info['description'],
is_active=True
)
classroom.code = room_info['code']
db.session.add(classroom)
db.session.flush()
classrooms_cache[room_key] = classroom
stats['classrooms_created'] += 1
if import_classrooms:
classroom = Classroom(
building=room_info['building'],
building_id=b_entity.id if b_entity else None,
floor=room_info['floor'],
capacity=room_info['capacity'],
description=room_info['description'],
is_active=True
)
classroom.code = room_info['code']
db.session.add(classroom)
db.session.flush()
classrooms_cache[room_key] = classroom
stats['classrooms_created'] += 1
else:
classroom = Classroom.query.filter_by(code=room_info['code']).first()
else:
classroom.capacity = max(classroom.capacity, room_info['capacity'])
if not classroom.building_id and b_entity:
classroom.building_id = b_entity.id
if import_classrooms:
classroom.capacity = max(classroom.capacity, room_info['capacity'])
if not classroom.building_id and b_entity:
classroom.building_id = b_entity.id
# 3. Upsert Subject (Soporte Many-to-Many con múltiples carreras y subcódigos)
subject_key = codigo.upper()
@@ -638,76 +772,84 @@ class GoogleSheetsImporter:
department_str = ", ".join([c.name for c in career_objs]) if career_objs else (primary_career.name if primary_career else 'General')
if not subject:
subject = Subject(
code=codigo,
name=asignatura,
career_id=primary_career.id if primary_career else None,
department=department_str,
credits=4,
is_active=True
)
subject.careers = career_objs
db.session.add(subject)
db.session.flush()
stats['subjects_created'] += 1
if import_subjects:
subject = Subject(
code=codigo,
name=asignatura,
career_id=primary_career.id if primary_career else None,
department=department_str,
credits=4,
is_active=True
)
if career_objs:
subject.careers = career_objs
db.session.add(subject)
db.session.flush()
stats['subjects_created'] += 1
else:
if subject.name != asignatura:
subject.name = asignatura
stats['subjects_updated'] += 1
# Vincular todas las carreras a la asignatura sin duplicados
for c_obj in career_objs:
if c_obj not in subject.careers:
subject.careers.append(c_obj)
if not subject.career_id and primary_career:
subject.career_id = primary_career.id
subject.department = ", ".join([c.name for c in subject.careers]) if subject.careers else department_str
if import_subjects:
if subject.name != asignatura:
subject.name = asignatura
stats['subjects_updated'] += 1
# Vincular todas las carreras a la asignatura sin duplicados
for c_obj in career_objs:
if c_obj not in subject.careers:
subject.careers.append(c_obj)
if not subject.career_id and primary_career:
subject.career_id = primary_career.id
subject.department = ", ".join([c.name for c in subject.careers]) if subject.careers else department_str
# Registrar en caché con código completo y con subcódigos individuales
subjects_cache[subject_key] = subject
for sc in sub_codes:
subjects_cache[sc.upper()] = subject
# Registrar en caché con código completo y con subcódigos individuales si existe
if subject:
subjects_cache[subject_key] = subject
for sc in sub_codes:
subjects_cache[sc.upper()] = subject
# 4. Upsert Commission
comm_code = 'C1'
if '-E' in codigo:
comm_code = codigo.split('-')[-1]
elif '-0' in codigo:
comm_code = 'C' + codigo.split('-')[-1]
commission = Commission.query.filter_by(
subject_id=subject.id,
code=comm_code,
semester='2026-2',
year=2026
).first()
if not commission:
commission = Commission(
commission = None
if subject:
comm_code = 'C1'
if '-E' in codigo:
comm_code = codigo.split('-')[-1]
elif '-0' in codigo:
comm_code = 'C' + codigo.split('-')[-1]
commission = Commission.query.filter_by(
subject_id=subject.id,
code=comm_code,
semester='2026-2',
year=2026,
teacher_id=default_user_id,
max_students=classroom.capacity if classroom else 35,
current_students=0,
schedule=f"{dia} {horario_raw}",
shift=turno,
active=True
)
db.session.add(commission)
db.session.flush()
stats['commissions_created'] += 1
else:
commission.schedule = f"{dia} {horario_raw}"
commission.shift = turno
if classroom:
commission.max_students = max(commission.max_students or 0, classroom.capacity)
year=2026
).first()
if not commission:
if import_commissions:
commission = Commission(
subject_id=subject.id,
code=comm_code,
semester='2026-2',
year=2026,
teacher_id=default_user_id,
max_students=classroom.capacity if classroom else 35,
current_students=0,
schedule=f"{dia} {horario_raw}",
shift=turno,
active=True
)
db.session.add(commission)
db.session.flush()
stats['commissions_created'] += 1
else:
if import_commissions:
commission.schedule = f"{dia} {horario_raw}"
commission.shift = turno
if classroom:
commission.max_students = max(commission.max_students or 0, classroom.capacity)
# 5. Generar Reservas de Calendario
t_start, t_end = self.parse_time_range(horario_raw)
effective_day_idx = day_idx if day_idx is not None else (manual_dt.weekday() if manual_dt else 0)
if classroom and t_start and t_end:
if import_reservations and classroom and subject and commission and t_start and t_end:
is_room_virtual = bool(classroom.is_virtual or 'VIRTUAL' in (classroom.code or '').upper())
v_link = None
@@ -718,9 +860,22 @@ class GoogleSheetsImporter:
if not commission.virtual_link:
commission.virtual_link = v_link
# Determinar fechas a generar: puntual o 16 semanas
# Determinar fechas a generar: específica de la fila (datepicker), puntual o 16 semanas
dates_to_generate = []
if single_date_only and manual_dt:
row_specific_date = None
raw_row_date = rec.get('date') or rec.get('inferred_date')
if raw_row_date:
try:
if isinstance(raw_row_date, (datetime, date)):
row_specific_date = raw_row_date if isinstance(raw_row_date, date) else raw_row_date.date()
elif isinstance(raw_row_date, str) and raw_row_date.strip():
row_specific_date = datetime.strptime(raw_row_date.strip(), '%Y-%m-%d').date()
except Exception:
pass
if row_specific_date:
dates_to_generate = [row_specific_date]
elif single_date_only and manual_dt:
dates_to_generate = [manual_dt]
else:
dates_to_generate = [base_monday + timedelta(days=effective_day_idx, weeks=w) for w in range(weeks_to_generate)]