import os import configparser from flask import Flask, request, render_template_string, redirect, url_for app = Flask(__name__) DATA_DIR = "/data" def get_poll_files(): if not os.path.exists(DATA_DIR): return [] return [f for f in os.listdir(DATA_DIR) if f.endswith('.encuesta')] def load_poll(filename): filepath = os.path.join(DATA_DIR, filename) if not os.path.exists(filepath): return None config = configparser.ConfigParser() config.read(filepath, encoding='utf-8') poll_id = filename.replace('.encuesta', '') titulo = config.get('encuesta', 'titulo', fallback=poll_id) pregunta = config.get('encuesta', 'pregunta', fallback='') # Leer el estado de la encuesta (por defecto True si no existe el campo) activa_raw = config.get('encuesta', 'activa', fallback='true').lower() activa = activa_raw in ['true', '1', 'yes', 'si'] options = [] votes = [] if 'opciones' in config and 'votos' in config: for key in config['opciones']: options.append(config['opciones'][key]) votes.append(int(config['votos'].get(key, '0'))) return { "id": poll_id, "filename": filename, "titulo": titulo, "pregunta": pregunta, "activa": activa, "options": options, "votes": votes } def save_vote(filename, option_index): filepath = os.path.join(DATA_DIR, filename) config = configparser.ConfigParser() config.read(filepath, encoding='utf-8') # Seguridad: verificar si sigue activa antes de guardar activa_raw = config.get('encuesta', 'activa', fallback='true').lower() if activa_raw not in ['true', '1', 'yes', 'si']: return False key = str(option_index + 1) current_votes = int(config.get('votos', key, fallback='0')) config.set('votos', key, str(current_votes + 1)) with open(filepath, 'w', encoding='utf-8') as f: config.write(f) return True HTML_LAYOUT = """
No hay encuestas activas en este momento.
" else: for poll in active_polls: content += f''' ''' # BLOQUE: Encuestas Finalizadas content += 'No hay encuestas finalizadas.
" else: for poll in closed_polls: content += f''' ''' return render_template_string(HTML_LAYOUT, content=content) @app.route('/poll/⚠️ Esta encuesta ha finalizado. Solo se pueden consultar los resultados.
' if voted: total = sum(poll["votes"]) content += "Resultados:
" bar_class = "bar" if poll["activa"] else "bar bar-closed" for i, opt in enumerate(poll["options"]): count = poll["votes"][i] percent = round((count / total * 100), 1) if total > 0 else 0 content += f'''Total de votos: {total}
' content += f'Volver al listado' else: content += f'' return render_template_string(HTML_LAYOUT, content=content) @app.route('/vote/