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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
if __name__ == '__main__':
|
|
|
|
|
|
|
| 14 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, session, flash, send_from_directory
|
| 2 |
+
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
+
from werkzeug.utils import secure_filename
|
| 4 |
import os
|
| 5 |
+
os.system("python dummy_user.py")
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
|
| 8 |
+
app.config['SECRET_KEY'] = 'your_secret_key'
|
| 9 |
+
app.config['UPLOAD_FOLDER'] = 'uploads'
|
| 10 |
|
| 11 |
+
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
| 12 |
+
os.makedirs(app.config['UPLOAD_FOLDER'])
|
| 13 |
|
| 14 |
+
db = SQLAlchemy(app)
|
| 15 |
+
|
| 16 |
+
# User model
|
| 17 |
+
class User(db.Model):
|
| 18 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 19 |
+
role = db.Column(db.String(10)) # student, staff, admin
|
| 20 |
+
username = db.Column(db.String(50), unique=True, nullable=False)
|
| 21 |
+
dob = db.Column(db.String(10))
|
| 22 |
+
|
| 23 |
+
# File model
|
| 24 |
+
class File(db.Model):
|
| 25 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 26 |
+
file_name = db.Column(db.String(200))
|
| 27 |
+
subject_name = db.Column(db.String(100))
|
| 28 |
+
category = db.Column(db.String(50))
|
| 29 |
+
subject_code = db.Column(db.String(20))
|
| 30 |
+
year = db.Column(db.String(10))
|
| 31 |
+
author_name = db.Column(db.String(100))
|
| 32 |
+
uploaded_by = db.Column(db.Integer, db.ForeignKey('user.id'))
|
| 33 |
+
|
| 34 |
+
# Bookmark model
|
| 35 |
+
class Bookmark(db.Model):
|
| 36 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 37 |
+
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
| 38 |
+
file_id = db.Column(db.Integer, db.ForeignKey('file.id'))
|
| 39 |
|
| 40 |
@app.route('/')
|
| 41 |
def index():
|
| 42 |
+
files = File.query.all()
|
| 43 |
+
return render_template('index.html', files=files)
|
| 44 |
+
|
| 45 |
+
@app.route('/login', methods=['GET', 'POST'])
|
| 46 |
+
def login():
|
| 47 |
+
if request.method == 'POST':
|
| 48 |
+
username = request.form['username']
|
| 49 |
+
dob = request.form['dob']
|
| 50 |
+
user = User.query.filter_by(username=username, dob=dob).first()
|
| 51 |
+
if user:
|
| 52 |
+
session['user_id'] = user.id
|
| 53 |
+
session['role'] = user.role
|
| 54 |
+
if user.role == 'admin':
|
| 55 |
+
return redirect(url_for('admin_dashboard'))
|
| 56 |
+
elif user.role == 'student':
|
| 57 |
+
return redirect(url_for('student_dashboard'))
|
| 58 |
+
elif user.role == 'staff':
|
| 59 |
+
return redirect(url_for('staff_dashboard'))
|
| 60 |
+
else:
|
| 61 |
+
flash("Invalid credentials!")
|
| 62 |
+
return render_template('login.html')
|
| 63 |
+
|
| 64 |
+
@app.route('/student_dashboard')
|
| 65 |
+
def student_dashboard():
|
| 66 |
+
if 'user_id' in session and session['role'] == 'student':
|
| 67 |
+
user_id = session['user_id']
|
| 68 |
+
all_files = File.query.all()
|
| 69 |
+
user_uploaded_files = File.query.filter_by(uploaded_by=user_id).all()
|
| 70 |
+
bookmarked_files = File.query.join(Bookmark).filter(Bookmark.user_id == user_id).all()
|
| 71 |
+
return render_template(
|
| 72 |
+
'student_dashboard.html',
|
| 73 |
+
all_files=all_files,
|
| 74 |
+
user_uploaded_files=user_uploaded_files,
|
| 75 |
+
bookmarked_files=bookmarked_files
|
| 76 |
+
)
|
| 77 |
+
return redirect(url_for('login'))
|
| 78 |
+
|
| 79 |
+
@app.route('/staff_dashboard')
|
| 80 |
+
def staff_dashboard():
|
| 81 |
+
if 'user_id' in session and session['role'] == 'staff':
|
| 82 |
+
user_id = session['user_id']
|
| 83 |
+
all_files = File.query.all()
|
| 84 |
+
user_uploaded_files = File.query.filter_by(uploaded_by=user_id).all()
|
| 85 |
+
bookmarked_files = File.query.join(Bookmark).filter(Bookmark.user_id == user_id).all()
|
| 86 |
+
return render_template(
|
| 87 |
+
'staff_dashboard.html',
|
| 88 |
+
all_files=all_files,
|
| 89 |
+
user_uploaded_files=user_uploaded_files,
|
| 90 |
+
bookmarked_files=bookmarked_files
|
| 91 |
+
)
|
| 92 |
+
return redirect(url_for('login'))
|
| 93 |
+
|
| 94 |
+
@app.route('/admin_dashboard', methods=['GET', 'POST'])
|
| 95 |
+
def admin_dashboard():
|
| 96 |
+
if 'user_id' in session and session['role'] == 'admin':
|
| 97 |
+
users = User.query.all()
|
| 98 |
+
files = File.query.all()
|
| 99 |
+
return render_template('admin_dashboard.html', users=users, files=files)
|
| 100 |
+
return redirect(url_for('login'))
|
| 101 |
+
|
| 102 |
+
@app.route('/upload', methods=['GET', 'POST'])
|
| 103 |
+
def upload():
|
| 104 |
+
if 'user_id' in session and session['role'] in ['student', 'staff']:
|
| 105 |
+
if request.method == 'POST':
|
| 106 |
+
file = request.files['file']
|
| 107 |
+
subject_name = request.form['subject_name']
|
| 108 |
+
category = request.form['category']
|
| 109 |
+
subject_code = request.form['subject_code']
|
| 110 |
+
year = request.form['year']
|
| 111 |
+
author_name = request.form['author_name']
|
| 112 |
+
|
| 113 |
+
if file and subject_name and category and subject_code and year and author_name:
|
| 114 |
+
filename = secure_filename(file.filename)
|
| 115 |
+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
| 116 |
+
|
| 117 |
+
new_file = File(
|
| 118 |
+
file_name=filename,
|
| 119 |
+
subject_name=subject_name,
|
| 120 |
+
category=category,
|
| 121 |
+
subject_code=subject_code,
|
| 122 |
+
year=year,
|
| 123 |
+
author_name=author_name,
|
| 124 |
+
uploaded_by=session['user_id']
|
| 125 |
+
)
|
| 126 |
+
db.session.add(new_file)
|
| 127 |
+
db.session.commit()
|
| 128 |
+
flash("File uploaded successfully!")
|
| 129 |
+
return redirect(url_for('student_dashboard' if session['role'] == 'student' else 'staff_dashboard'))
|
| 130 |
+
else:
|
| 131 |
+
flash("All fields are required!")
|
| 132 |
+
return render_template('upload.html')
|
| 133 |
+
return redirect(url_for('login'))
|
| 134 |
+
|
| 135 |
+
@app.route('/delete/<int:file_id>')
|
| 136 |
+
def delete_file(file_id):
|
| 137 |
+
if 'user_id' in session:
|
| 138 |
+
file = File.query.get(file_id)
|
| 139 |
+
if not file:
|
| 140 |
+
flash("File not found!")
|
| 141 |
+
elif file.uploaded_by == session['user_id'] or session['role'] == 'admin':
|
| 142 |
+
try:
|
| 143 |
+
# Remove associated bookmarks
|
| 144 |
+
Bookmark.query.filter_by(file_id=file_id).delete()
|
| 145 |
+
|
| 146 |
+
# Delete the file from the file system
|
| 147 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.file_name)
|
| 148 |
+
if os.path.exists(file_path):
|
| 149 |
+
os.remove(file_path)
|
| 150 |
+
|
| 151 |
+
# Delete the file record from the database
|
| 152 |
+
db.session.delete(file)
|
| 153 |
+
db.session.commit()
|
| 154 |
+
flash("File deleted successfully!")
|
| 155 |
+
except Exception as e:
|
| 156 |
+
flash(f"An error occurred while deleting the file: {str(e)}")
|
| 157 |
+
else:
|
| 158 |
+
flash("You do not have permission to delete this file!")
|
| 159 |
+
return redirect(url_for('student_dashboard' if session['role'] == 'student' else 'staff_dashboard'))
|
| 160 |
+
return redirect(url_for('login'))
|
| 161 |
+
|
| 162 |
+
@app.route('/admin/delete_user/<int:user_id>', methods=['GET'])
|
| 163 |
+
def delete_user(user_id):
|
| 164 |
+
if 'user_id' in session and session['role'] == 'admin':
|
| 165 |
+
user = User.query.get(user_id)
|
| 166 |
+
if not user:
|
| 167 |
+
flash("User not found!")
|
| 168 |
+
elif user.role == 'admin':
|
| 169 |
+
flash("You cannot delete another admin!")
|
| 170 |
+
else:
|
| 171 |
+
try:
|
| 172 |
+
# Check if the user has uploaded any files
|
| 173 |
+
files = File.query.filter_by(uploaded_by=user_id).all()
|
| 174 |
+
for file in files:
|
| 175 |
+
# Remove associated bookmarks
|
| 176 |
+
Bookmark.query.filter_by(file_id=file.id).delete()
|
| 177 |
+
|
| 178 |
+
# Delete file from the file system
|
| 179 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.file_name)
|
| 180 |
+
if os.path.exists(file_path):
|
| 181 |
+
os.remove(file_path)
|
| 182 |
+
|
| 183 |
+
# Delete the file record
|
| 184 |
+
db.session.delete(file)
|
| 185 |
+
|
| 186 |
+
# Delete the user record
|
| 187 |
+
db.session.delete(user)
|
| 188 |
+
db.session.commit()
|
| 189 |
+
flash("User and their uploaded files deleted successfully!")
|
| 190 |
+
except Exception as e:
|
| 191 |
+
flash(f"An error occurred while deleting the user: {str(e)}")
|
| 192 |
+
return redirect(url_for('admin_dashboard'))
|
| 193 |
+
flash("You do not have permission to perform this action!")
|
| 194 |
+
return redirect(url_for('login'))
|
| 195 |
+
|
| 196 |
+
@app.route('/bookmark/<int:file_id>')
|
| 197 |
+
def bookmark(file_id):
|
| 198 |
+
if 'user_id' in session:
|
| 199 |
+
user_id = session['user_id']
|
| 200 |
+
bookmark = Bookmark.query.filter_by(user_id=user_id, file_id=file_id).first()
|
| 201 |
+
if bookmark:
|
| 202 |
+
db.session.delete(bookmark)
|
| 203 |
+
db.session.commit()
|
| 204 |
+
flash("Bookmark removed!")
|
| 205 |
+
else:
|
| 206 |
+
new_bookmark = Bookmark(user_id=user_id, file_id=file_id)
|
| 207 |
+
db.session.add(new_bookmark)
|
| 208 |
+
db.session.commit()
|
| 209 |
+
flash("Bookmark added!")
|
| 210 |
+
return redirect(url_for('student_dashboard' if session['role'] == 'student' else 'staff_dashboard'))
|
| 211 |
+
return redirect(url_for('login'))
|
| 212 |
+
|
| 213 |
+
@app.route('/uploads/<filename>')
|
| 214 |
+
def uploaded_file(filename):
|
| 215 |
+
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
| 216 |
+
|
| 217 |
+
@app.route('/logout')
|
| 218 |
+
def logout():
|
| 219 |
+
session.clear()
|
| 220 |
+
return redirect(url_for('login'))
|
| 221 |
|
| 222 |
if __name__ == '__main__':
|
| 223 |
+
with app.app_context():
|
| 224 |
+
db.create_all()
|
| 225 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|