Pycloud/app/routes.py

107 lines
4.2 KiB
Python
Raw Normal View History

2020-06-23 16:11:01 +02:00
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
# ===================================================
# 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)
# userloged=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, userloged=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, userloged=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:
return redirect(url_for('home'))
if current_user.isAdmin == False:
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()
flash(words['termRegistered'] + form.username.data)
return redirect(url_for('home'))
return render_template('register.html', title=words['nameRegister'], dico=words, form=form, userloged=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:
return redirect(url_for('home'))
form = UploadForm()
if form.validate_on_submit():
f = form.uploadfile.data
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['FILES_DIR'], filename))
return redirect(url_for('home'))
return render_template('upload.html', title=words['nameUpload'], dico=words, form=form, userloged=current_user)
# Admin's download pages
# Use it to download a file
# Redirect automatically to the home page if the user isn't logged in or isn't an Admin
# If the user or the id is'nt reconized, redirect automatically to the home page
@app.route('/downloadAdmin/<user>/<id>')
def downloadAdmin(user, id):
if not current_user.is_authenticated:
return redirect(url_for('home'))
if current_user.isAdmin == False:
return redirect(url_for('home'))
# Users' download pages
# Use it to download a file
# Redirect automatically to the home page if the user isn't logged in or is an Admin
# If the id is'nt reconized, redirect automatically to the home page
@app.route('/downloadUser/<id>')
def downloadUser(id):
if not current_user.is_authenticated:
return redirect(url_for('home'))
if current_user.isAdmin == True:
return redirect(url_for('home'))