Add Some functions to manage db

This commit is contained in:
Michel V 2020-06-25 11:57:19 +02:00
parent fe965840d7
commit 533ab8e8e1
3 changed files with 39 additions and 3 deletions

28
app/database.py Normal file
View File

@ -0,0 +1,28 @@
import os
from os import listdir
from app import app, db
from app.models import File, User
def cleardata():
files = File.query.all()
for f in files:
db.session.delete(f)
db.session.commit()
def updatedata(username):
user = User.query.filter_by(username=username).first()
if not user is None:
folder = os.path.join(app.config['FILES_DIR'], user.username)
files = listdir(folder)
for f in files:
filetodb = File(name = f, user = user)
db.session.add(filetodb)
db.session.commit()
def updatealldata():
for user in User.query.all():
updatedata(user.username)
def recreatealldata():
cleardata()
updatealldata()

View File

@ -114,6 +114,14 @@ def viewUser():
return redirect(url_for('home'))
if current_user.isAdmin:
return redirect(url_for('home'))
files = []
for filefromdb in File.query.all():
if filefromdb.user == current_user:
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

View File

@ -1,9 +1,9 @@
{% extends 'base.html' %}
{% block content %}
{% for file in files %}
<div>
<a href="{{ url_for('download', id=file.id)}}">| {{ file.name }}, {{ file.user.username }} |</a>
</div>
{% for file in files %}
<p>| <a href="{{ url_for('download', id=file.id)}}">{{ file.name }}</a>, {{ file.user.username }} |</p>
{% endfor %}
</div>
{% endblock %}