-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (64 loc) · 2.03 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Blueprint, send_from_directory, render_template, flash, request, redirect, url_for, render_template
from flask_login import login_required, current_user
from . import db, APP_ROOT
from .models import User, Data
import os
main = Blueprint('main', __name__)
def make_tree(path):
tree = dict(name=os.path.basename(path), children=[])
try:
lst = os.listdir(path)
lst.sort()
except OSError as e:
print(e.errno)
print(e.filename)
print(e.strerror) #ignore errors
else:
for name in lst:
fn = os.path.join(path, name)
if os.path.isdir(fn):
tree['children'].append(make_tree(fn))
else:
tree['children'].append(dict(name=name))
return tree
@main.route('/')
def index():
return render_template('index.html')
@main.route('/profile')
@login_required
def profilelist():
global listid, listnama, listuser
listid = []
listnama = []
# try :
rows = Data.query.all()
# print(rows)
for row in rows:
listid.append(row.id)
listnama.append(row.nama)
# except :
# return redirect('/')
# print(data)
return render_template('ProfileList.html', len=len(listid), listid=listid, listnama=listnama)
@main.route('/profile/<userid>')
@login_required
def userprofile(userid):
global nama, gender, usia, asal
try:
res = Data.query.get(userid)
nama = res.nama
gender = res.gender
usia = res.usia
asal = res.asal
tree = make_tree(os.path.join(APP_ROOT,'uploads',nama))
print(tree)
except:
return redirect(url_for('main.profilelist')) # 404 page
# print(data)
return render_template('ProfilePage.html', tree=tree, userid=userid, nama=nama, gender=gender, usia=usia, asal=asal)
@main.route('/static/<userid>/<path:filename>')
@login_required
def profilefiles(userid,filename):
nama = Data.query.get(userid).nama
print(nama)
return send_from_directory(os.path.join('uploads',nama), filename)