Spaces:
Paused
Paused
Fix backend application
Browse files4 unmodified lines5678910114 unmodified lines16171819202122231 unmodified line2526272829303116 unmodified lines48495051525354551 unmodified line57585960616263646566679 unmodified lines77787980818283848 unmodified lines9394959697989924 unmodified lines1241251261271281291301318 unmodified lines1401411421434 unmodified linesfrom werkzeug.utils import secure_filenameapp = Flask(__name__)app.secret_key = "noteshub_secret_key_2026"DATABASE = "noteshub.db"4 unmodified linesos.makedirs(UPLOAD_FOLDER, exist_ok=True)# ---------------- DATABASE ----------------def get_db():conn = sqlite3.connect(DATABASE)conn.row_factory = sqlite3.Row1 unmodified linedef init_db():conn = get_db()conn.execute("""16 unmodified linessubject TEXT NOT NULL,unit TEXT NOT NULL,filename TEXT NOT NULL,uploaded_by INTEGER,FOREIGN KEY(uploaded_by) REFERENCES students(id))""")1 unmodified lineconn.close()# ---------------- HOME ----------------@app.route("/")def home():conn = get_db()notes = conn.execute("""9 unmodified linesreturn render_template("index.html", notes=notes)# ---------------- REGISTER ----------------@app.route("/register", methods=["GET", "POST"])def register():8 unmodified lineshashed_password = generate_password_hash(password)try:conn = get_db()conn.execute("""24 unmodified linesreturn render_template("register.html")# ---------------- LOGIN ----------------@app.route("/login", methods=["GET", "POST"])def login():8 unmodified linesSELECT *FROM studentsWHERE email = ?""", (email,)).4 unmodified lines56789104 unmodified lines151617181920211 unmodified line23242526272816 unmodified lines454647484950511 unmodified line5354555657585960619 unmodified lines717273747576778 unmodified lines86878889909124 unmodified lines1161171181191201211228 unmodified lines1311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503514 unmodified linesfrom werkzeug.utils import secure_filenameapp = Flask(__name__)app.secret_key = "noteshub_secret_key_2026"DATABASE = "noteshub.db"4 unmodified linesos.makedirs(UPLOAD_FOLDER, exist_ok=True)# DATABASEdef get_db():conn = sqlite3.connect(DATABASE)conn.row_factory = sqlite3.Row1 unmodified linedef init_db():conn = get_db()conn.execute("""16 unmodified linessubject TEXT NOT NULL,unit TEXT NOT NULL,filename TEXT NOT NULL,uploaded_by INTEGER)""")1 unmodified lineconn.close()# HOME@app.route("/")def home():conn = get_db()notes = conn.execute("""9 unmodified linesreturn render_template("index.html", notes=notes)# REGISTER@app.route("/register", methods=["GET", "POST"])def register():8 unmodified lineshashed_password = generate_password_hash(password)try:conn = get_db()conn.execute("""24 unmodified linesreturn render_template("register.html")# LOGIN@app.route("/login", methods=["GET", "POST"])def login():8 unmodified linesSELECT *FROM studentsWHERE email = ?""", (email,)).fetchone()conn.close()if student and check_password_hash(student["password"],password):session["student_id"] = student["id"]session["student_name"] = student["name"]return redirect(url_for("dashboard"))flash("Invalid email or password.")return render_template("login.html")# LOGOUT@app.route("/logout")def logout():session.clear()return redirect(url_for("home"))# DASHBOARD@app.route("/dashboard")def dashboard():if "student_id" not in session:return redirect(url_for("login"))conn = get_db()notes = conn.execute("""SELECT *FROM notesWHERE uploaded_by = ?ORDER BY id DESC""", (session["student_id"],)).fetchall()conn.close()return render_template("dashboard.html",notes=notes)# UPLOAD NOTES@app.route("/upload", methods=["GET", "POST"])def upload():if "student_id" not in session:return redirect(url_for("login"))if request.method == "POST":title = request.form["title"]department = request.form["department"]semester = request.form["semester"]subject = request.form["subject"]unit = request.form["unit"]file = request.files["file"]if file and file.filename:filename = secure_filename(file.filename)if not filename.lower().endswith(".pdf"):flash("Only PDF files are allowed.")return redirect(url_for("upload"))file.save(os.path.join(app.config["UPLOAD_FOLDER"],filename))conn = get_db()conn.execute("""INSERT INTO notes(title, department, semester, subject,unit, filename, uploaded_by)VALUES (?, ?, ?, ?, ?, ?, ?)""", (title,department,semester,subject,unit,filename,session["student_id"]))conn.commit()conn.close()flash("Notes uploaded successfully!")return redirect(url_for("dashboard"))return render_template("upload.html")# SEARCH NOTES@app.route("/search")def search():subject = request.args.get("subject", "")unit = request.args.get("unit", "")conn = get_db()query = """SELECT notes.*, students.name AS uploaderFROM notesLEFT JOIN studentsON notes.uploaded_by = students.idWHERE 1=1"""values = []if subject:query += " AND subject LIKE ?"values.append("%" + subject + "%")if unit:query += " AND unit = ?"values.append(unit)query += " ORDER BY notes.id DESC"notes = conn.execute(query,values).fetchall()conn.close()return render_template("search.html",notes=notes,subject=subject,unit=unit)# DOWNLOAD@app.route("/download/<filename>")def download(filename):return send_from_directory(app.config["UPLOAD_FOLDER"],filename,as_attachment=True)# DELETE NOTE@app.route("/delete/<int:note_id>")def delete_note(note_id):if "student_id" not in session:return redirect(url_for("login"))conn = get_db()note = conn.execute("""SELECT *FROM notesWHERE id = ?AND uploaded_by = ?""", (note_id,session["student_id"])).fetchone()if note:file_path = os.path.join(UPLOAD_FOLDER,note["filename"])if os.path.exists(file_path):os.remove(file_path)conn.execute("""DELETE FROM notesWHERE id = ?""", (note_id,))conn.commit()conn.close()return redirect(url_for("dashboard"))# START APPLICATIONinit_db()if __name__ == "__main__":app.run(host="0.0.0.0",port=7860,debug=False)
app.py+223-15