-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
89 lines (74 loc) · 3 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
import os
import re
import time
import argparse
import json
import uuid
from flask import Flask, request, redirect, url_for
from flask import render_template
from werkzeug.utils import secure_filename
from aslite.db import get_first_project, save_project_to_db, get_project_with_name
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Server')
parser.add_argument('-d', '--debug', action="store_true", help='up to how many papers to fetch')
args = parser.parse_args()
print(args)
# -----------------------------------------------------------------------------
# inits and globals
app = Flask(__name__)
# set the secret key so we can cryptographically sign cookies and maintain sessions
if os.path.isfile('secret_key.txt'):
# example of generating a good key on your system is:
# import secrets; secrets.token_urlsafe(16)
sk = open('secret_key.txt').read().strip()
else:
print("WARNING: no secret key found, using default devkey")
sk = 'devkey'
app.secret_key = sk
app.config['UPLOAD_FOLDER'] = "static/save/"
if args.debug:
app.config["ENV"] = "development"
app.config["DEBUG"] = True
ALLOWED_EXTENSIONS = set(['svg'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# -----------------------------------------------------------------------------
# routes
@app.route("/")
def index():
return redirect('/project')
@app.route("/project/")
def project():
firstProject = get_first_project()
return render_template('index.html', project=firstProject)
@app.route("/project/<name>")
def projectName(name = None):
project = get_project_with_name(name)
return render_template('index.html', project=project)
@app.route("/edit")
def edit_page():
_fields = request.args.to_dict()
filename = request.args['projectname'] if 'projectname' in _fields else None
project = None
if filename != None:
project = get_project_with_name(filename)
return render_template('edit.html', project=project)
@app.route('/edit', methods=['POST'])
def upload_image():
print("upload_image..")
if 'file' not in request.files:
return render_template('edit.html')
file = request.files['file']
if file.filename == '':
return render_template('edit.html')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
data = json.loads(request.form['hdata'])
data['id'] = str(uuid.uuid1()) if data['id'] == None else data['id']
data['name'] = request.form['name']
save_project_to_db(data, data['id'])
return render_template('edit.html', project=None)
else:
return redirect(request.url)
app.run()