from flask import Flask, render_template, request, jsonify, redirect, url_for, session import json import os from datetime import datetime app = Flask(__name__) app.secret_key = "recom_secret_2024" DATA_FILE = "students.json" ADMIN_PASSWORD = "Sugar220307" def load_students(): if not os.path.exists(DATA_FILE): return [] with open(DATA_FILE, "r") as f: return json.load(f) def save_student(student): students = load_students() student["id"] = len(students) + 1 student["registered_at"] = datetime.now().strftime("%d-%m-%Y %H:%M") students.append(student) with open(DATA_FILE, "w") as f: json.dump(students, f, indent=2) return student["id"] @app.route("/") def index(): return render_template("index.html") @app.route("/instructions") def instructions(): name = request.args.get("name", "") cls = request.args.get("class", "") section = request.args.get("section", "") if not name: return redirect(url_for("index")) return render_template("instructions.html", name=name, cls=cls, section=section) @app.route("/contact") def contact(): name = request.args.get("name", "") cls = request.args.get("class", "") section = request.args.get("section", "") if not name: return redirect(url_for("index")) return render_template("contact.html", name=name, cls=cls, section=section) @app.route("/submit", methods=["POST"]) def submit(): data = request.get_json() student_id = save_student(data) return jsonify({"success": True, "id": student_id}) @app.route("/success") def success(): return render_template("success.html") @app.route("/admin", methods=["GET", "POST"]) def admin(): if request.method == "POST": pwd = request.form.get("password", "") if pwd == ADMIN_PASSWORD: session["admin"] = True return redirect(url_for("admin")) else: return render_template("admin_login.html", error="Wrong password. Try again.") if not session.get("admin"): return render_template("admin_login.html", error=None) students = load_students() return render_template("admin_dashboard.html", students=students) @app.route("/admin/delete/", methods=["POST"]) def delete_student(student_id): students = load_students() students = [s for s in students if s["id"] != student_id] with open(DATA_FILE, "w") as f: json.dump(students, f, indent=2) return jsonify({"success": True, "total": len(students)}) @app.route("/admin/logout") def admin_logout(): session.pop("admin", None) return redirect(url_for("admin")) if __name__ == "__main__": import sys if len(sys.argv) > 1 and sys.argv[1] == "list": students = load_students() if not students: print("No students registered yet.") else: print(f"\n{'─'*60}") print(f" RECOM — Registered Students ({len(students)} total)") print(f"{'─'*60}") for s in students: print(f"\n #{s['id']} {s['name']} | Class {s['class']} - {s['section']}") print(f" 📞 {s.get('phone','—')} 💬 WhatsApp: {s.get('whatsapp','—')}") print(f" 📍 {s.get('address','—')}") print(f" 🕐 Registered: {s.get('registered_at','—')}") print(f"\n{'─'*60}\n") else: app.run(host="0.0.0.0", port=7860, debug=False)