Spaces:
Paused
Paused
Fix backend application
Browse files4 unmodified lines567889104 unmodified lines1516171920181920211 unmodified line2324252826272816 unmodified lines4546475152484950511 unmodified line5354556061565758645960619 unmodified lines7172738081747576778 unmodified lines8687889689909124 unmodified lines1161171181271281191201211228 unmodified lines1311321331431341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503514 unmodified linesfrom werkzeug.utils import secure_filename
app = Flask(__name__)
app.secret_key = "noteshub_secret_key_2026"
DATABASE = "noteshub.db"4 unmodified linesos.makedirs(UPLOAD_FOLDER, exist_ok=True)
# ---------------- DATABASE ----------------
# DATABASEdef get_db(): conn = sqlite3.connect(DATABASE) conn.row_factory = sqlite3.Row1 unmodified line
def init_db():
conn = get_db()
conn.execute("""16 unmodified lines subject TEXT NOT NULL, unit TEXT NOT NULL, filename TEXT NOT NULL, uploaded_by INTEGER, FOREIGN KEY(uploaded_by) REFERENCES students(id) uploaded_by INTEGER ) """)
1 unmodified line conn.close()
# ---------------- HOME ----------------
# HOME@app.route("/")def home():
conn = get_db()
notes = conn.execute("""9 unmodified lines return render_template("index.html", notes=notes)
# ---------------- REGISTER ----------------
# REGISTER@app.route("/register", methods=["GET", "POST"])def register():
8 unmodified lines hashed_password = generate_password_hash(password)
try:
conn = get_db()
conn.execute("""24 unmodified lines return render_template("register.html")
# ---------------- LOGIN ----------------
# LOGIN@app.route("/login", methods=["GET", "POST"])def login():
8 unmodified lines SELECT * FROM students WHERE email = ? """, (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 notes WHERE 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 uploader FROM notes LEFT JOIN students ON notes.uploaded_by = students.id WHERE 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 notes WHERE 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 notes WHERE 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