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
2020-06-23 19:23:57 +02:00
from app . models import User , File
2020-06-23 16:11:01 +02:00
# ===================================================
# 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)
2020-06-24 18:03:41 +02:00
# userlogged=current_user (Usefull to know if the user is logged in, and if yes, his name)
2020-06-23 16:11:01 +02:00
# 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 ( ) :
2020-06-24 18:03:41 +02:00
return render_template ( ' home.html ' , title = words [ ' nameHome ' ] , dico = words , userlogged = current_user )
2020-06-23 16:11:01 +02:00
# 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 ' ) )
2020-06-24 18:03:41 +02:00
return render_template ( ' login.html ' , title = words [ ' nameLogin ' ] , dico = words , form = form , userlogged = current_user )
2020-06-23 16:11:01 +02:00
# 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 :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorLogged ' ] )
2020-06-23 16:11:01 +02:00
return redirect ( url_for ( ' home ' ) )
if current_user . isAdmin == False :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorPermission ' ] )
2020-06-23 16:11:01 +02:00
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 ( )
2020-06-23 19:23:57 +02:00
os . mkdir ( os . path . join ( app . config [ ' FILES_DIR ' ] , form . username . data ) )
2020-06-23 16:11:01 +02:00
flash ( words [ ' termRegistered ' ] + form . username . data )
return redirect ( url_for ( ' home ' ) )
2020-06-24 18:03:41 +02:00
return render_template ( ' register.html ' , title = words [ ' nameRegister ' ] , dico = words , form = form , userlogged = current_user )
2020-06-23 16:11:01 +02:00
# 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 :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorLogged ' ] )
2020-06-23 16:11:01 +02:00
return redirect ( url_for ( ' home ' ) )
form = UploadForm ( )
if form . validate_on_submit ( ) :
f = form . uploadfile . data
2020-06-27 12:34:40 +02:00
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 )
2020-06-23 19:23:57 +02:00
db . session . add ( filetodb )
db . session . commit ( )
2020-06-23 16:11:01 +02:00
return redirect ( url_for ( ' home ' ) )
2020-06-24 18:03:41 +02:00
return render_template ( ' upload.html ' , title = words [ ' nameUpload ' ] , dico = words , form = form , userlogged = current_user )
2020-06-23 16:11:01 +02:00
2020-06-24 18:03:41 +02:00
# Download pages
2020-06-23 16:11:01 +02:00
# Use it to download a file
2020-06-23 19:23:57 +02:00
# Redirect automatically to the home page if the user isn't logged in
2020-06-23 16:11:01 +02:00
# If the id is'nt reconized, redirect automatically to the home page
2020-06-23 19:23:57 +02:00
# If the file don't belong to the requested user, redirect automatically to the home page
@app.route ( ' /download/<id> ' )
def download ( id ) :
2020-06-23 16:11:01 +02:00
if not current_user . is_authenticated :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorLogged ' ] )
2020-06-23 16:11:01 +02:00
return redirect ( url_for ( ' home ' ) )
2020-06-23 19:23:57 +02:00
filetodb = File . query . filter_by ( id = id ) . first ( )
if not filetodb :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorId ' ] )
2020-06-23 19:23:57 +02:00
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 :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorPermission ' ] )
2020-06-24 18:03:41 +02:00
return redirect ( url_for ( ' home ' ) )
2020-06-25 13:08:48 +02:00
# View Page
2020-06-24 18:03:41 +02:00
# It will show all files which belong to the requested user
2020-06-25 13:08:48 +02:00
# 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
2020-06-24 18:03:41 +02:00
@app.route ( ' /view ' )
2020-06-25 13:08:48 +02:00
def view ( ) :
2020-06-24 18:03:41 +02:00
if not current_user . is_authenticated :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorLogged ' ] )
2020-06-24 18:03:41 +02:00
return redirect ( url_for ( ' home ' ) )
if current_user . isAdmin :
2020-06-25 13:08:48 +02:00
return render_template ( ' view.html ' , title = words [ ' nameView ' ] , dico = words , userlogged = current_user , users = User . query . all ( ) )
2020-06-25 11:57:19 +02:00
files = [ ]
for filefromdb in File . query . all ( ) :
2020-06-29 14:01:25 +02:00
if filefromdb . folder == current_user . username :
2020-06-25 11:57:19 +02:00
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 )
2020-06-24 18:03:41 +02:00
# 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/<id> ' )
def viewAdmin ( id ) :
if not current_user . is_authenticated :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorLogged ' ] )
2020-06-24 18:03:41 +02:00
return redirect ( url_for ( ' home ' ) )
if not current_user . isAdmin :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorPermission ' ] )
2020-06-24 18:03:41 +02:00
return redirect ( url_for ( ' home ' ) )
user = User . query . filter_by ( id = id ) . first ( )
if not user :
2020-06-25 13:08:48 +02:00
flash ( words [ ' ErrorId ' ] )
2020-06-24 18:03:41 +02:00
return redirect ( url_for ( ' home ' ) )
files = [ ]
for filefromdb in File . query . all ( ) :
2020-06-29 14:01:25 +02:00
if filefromdb . folder == user . username :
2020-06-24 18:03:41 +02:00
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 )