Add Some functions to manage db
This commit is contained in:
parent
fe965840d7
commit
533ab8e8e1
|
@ -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()
|
|
@ -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
|
||||
|
|
|
@ -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 %}
|
Loading…
Reference in New Issue