import os from app import app, words, db from flask import render_template, redirect, url_for, flash, send_from_directory from app.forms import UploadForm, LoginForm, RegisterForm from werkzeug.utils import secure_filename from flask_login import current_user, login_user, logout_user from app.models import User, File # =================================================== # How to use the render_template in this application: # =================================================== # return render_template(parameters) # Parameters: # 'page.html' # dico=words (Usefull to translate all the site with one dictionary) # userlogged=current_user (Usefull to know if the user is logged in, and if yes, his name) # title='title' (Optional: If you want to change the title of the page, You can also use words['pageTitle'] to change faster when you want to translate) # Home page # The title page @app.route('/') @app.route('/home') def home(): return render_template('home.html', title=words['nameHome'], dico=words, userlogged=current_user) # Login page # Use it to login # Redirect automatically to the home page if the user is already logged in @app.route('/login', methods=['GET', 'POST']) def login(): if current_user.is_authenticated: return redirect(url_for('home')) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user is None or not user.check_password(form.password.data): flash(words['ErrorInvalid']) return redirect(url_for('login')) login_user(user, remember=form.remember_me.data) return redirect(url_for('home')) return render_template('login.html', title=words['nameLogin'], dico=words, form=form, userlogged=current_user) # Logout page # Use it to logout # Redirect automatically to the home page if the user isn't logged in @app.route('/logout') def logout(): logout_user() return redirect(url_for('home')) # Register page # Use it to create a new user # Redirect automatically to the home page if the user isn't logged in or isn't an Admin @app.route('/register', methods=['GET', 'POST']) def register(): if not current_user.is_authenticated: flash(words['ErrorLogged']) return redirect(url_for('home')) if current_user.isAdmin == False: flash(words['ErrorPermission']) return redirect(url_for('home')) form = RegisterForm() if form.validate_on_submit(): user = User(username = form.username.data, isAdmin = form.isAdmin.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() os.mkdir(os.path.join(app.config['FILES_DIR'], form.username.data)) flash(words['termRegistered'] + form.username.data) return redirect(url_for('home')) return render_template('register.html', title=words['nameRegister'], dico=words, form=form, userlogged=current_user) # Upload page # Use it to upload a file # Redirect automatically to the home page if the user isn't logged in # If the user isn't an Admin, it will upload directly to the right folder # If the user is an Admin, it will ask to the user to choose the right folder @app.route('/upload', methods=['GET', 'POST']) def upload(): if not current_user.is_authenticated: flash(words['ErrorLogged']) return redirect(url_for('home')) form = UploadForm() if form.validate_on_submit(): f = form.uploadfile.data if form.filename_choose.data: original = f.filename.split('.') filename = secure_filename(form.filename_choose.data + '.' + original[-1]) else: filename = secure_filename(f.filename) if current_user.isAdmin: folder=form.folder.data else: folder = current_user.username f.save(os.path.join(app.config['FILES_DIR'], folder, filename)) filetodb = File(name=filename, user=current_user, folder=folder) db.session.add(filetodb) db.session.commit() return redirect(url_for('home')) return render_template('upload.html', title=words['nameUpload'], dico=words, form=form, userlogged=current_user) # Download pages # Use it to download a file # Redirect automatically to the home page if the user isn't logged in # If the id is'nt reconized, redirect automatically to the home page # If the file don't belong to the requested user, redirect automatically to the home page @app.route('/download/') def download(id): if not current_user.is_authenticated: flash(words['ErrorLogged']) return redirect(url_for('home')) filetodb = File.query.filter_by(id=id).first() if not filetodb: flash(words['ErrorId']) return redirect(url_for('home')) if filetodb.user == current_user or current_user.isAdmin == True: return send_from_directory(os.path.join(app.config['FILES_DIR'], filetodb.user.username), filetodb.name, as_attachment=True) else: flash(words['ErrorPermission']) return redirect(url_for('home')) # View Page # It will show all files which belong to the requested user # If the user is an Admin, if will show a page with all users folders # Redirect automatically to the home page if the user isn't logged in @app.route('/view') def view(): if not current_user.is_authenticated: flash(words['ErrorLogged']) return redirect(url_for('home')) if current_user.isAdmin: return render_template('view.html', title=words['nameView'], dico=words, userlogged=current_user, users = User.query.all()) files = [] for filefromdb in File.query.all(): if filefromdb.folder == current_user.username: files.append(filefromdb) if not len(files): flash(words['ErrorNoFiles']) return redirect(url_for('home')) return render_template('view.html', title=words['nameView'], dico=words, userlogged=current_user, files=files) # Admins' View Page # It will show all files which belong to the requested user # Redirect automatically to the home page if the user isn't logged in or isn't an Admin # If the id is'nt reconized, redirect automatically to the home page @app.route('/view/') def viewAdmin(id): if not current_user.is_authenticated: flash(words['ErrorLogged']) return redirect(url_for('home')) if not current_user.isAdmin: flash(words['ErrorPermission']) return redirect(url_for('home')) user = User.query.filter_by(id=id).first() if not user: flash(words['ErrorId']) return redirect(url_for('home')) files = [] for filefromdb in File.query.all(): if filefromdb.folder == user.username: files.append(filefromdb) if not len(files): flash(words['ErrorNoFiles']) return redirect(url_for('home')) return render_template('view.html', title=words['nameView'] + ' - ' + user.username, dico=words, userlogged=current_user, files=files)