This repository has been archived by the owner on Aug 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·119 lines (103 loc) · 3.53 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3.4
# pylint: disable=C0103,C0111,W0142
# ugh. work around an internal bug with flask and python3.4
import pkgutil
old_loader = pkgutil.get_loader
def override_loader(*args, **kwargs):
try:
return old_loader(*args, **kwargs)
except AttributeError:
return None
pkgutil.get_loader = override_loader
from flask import Flask, render_template, request, redirect, url_for, jsonify
from flask.ext.login import LoginManager, login_required, login_user, \
logout_user, current_user
from werkzeug import secure_filename
from util import slugify, naturaltime, get_gravatar, \
url_for_redirect_back, get_redirect_target, downsample
from models import User, db
import os
import config
UPLOAD_FOLDER = 'uploads'
CONVERT_FOLDER = 'static/movies'
LED_WIDTH=30 # number of LEDs per panel width
LED_HEIGHT=18 # number of LEDs per panel height
PANELS=5 # number of panels
app = Flask('led-movie-visualization')
app.jinja_env.filters['slugify'] = slugify
app.jinja_env.filters['naturaltime'] = naturaltime
app.secret_key = config.secret_key
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(userid):
return User.query.get(userid)
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html', error=error), 404
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
next_target = get_redirect_target()
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
user = User.query.filter_by(login=email).scalar()
if user and user.check_password(password):
login_user(user)
url = url_for('dashboard')
return redirect(url)
else:
error = 'Email and password do not match.'
return render_template('index.html',
next=next_target,
error=error)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/dashboard')
@login_required
def dashboard():
movies = [x for x in os.listdir(CONVERT_FOLDER) if x.startswith('led')]
return render_template('dashboard.html',
movies=movies,
led_width=LED_WIDTH,
led_height=LED_HEIGHT,
panels=PANELS)
@app.route('/upload', methods=['POST'])
@login_required
def upload():
if not request.files:
return jsonify(error='No file uploaded.')
movie = request.files['movie']
filename = secure_filename(movie.filename)
path = os.path.join(UPLOAD_FOLDER, filename)
movie.save(path)
success, msg = downsample(UPLOAD_FOLDER,
CONVERT_FOLDER,
filename,
LED_WIDTH*PANELS,
LED_HEIGHT*PANELS)
if not success:
return jsonify(error=msg)
return redirect(url_for('dashboard'))
@app.route('/<path:path>')
def static_proxy(path):
return app.send_static_file(path)
# only call if you are running this for the first time.
def initialize(username, password):
db.create_all()
return User(username, password).save()
if __name__ == '__main__':
import sys
#app.debug = True
port = 5000
if len(sys.argv) > 1:
port = int(sys.argv[1])
app.run(host='0.0.0.0', port=port)