-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_interface.py
110 lines (88 loc) · 4.37 KB
/
simple_interface.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
import os
import time
import requests
from flask import render_template, request, flash, redirect, url_for, abort
from jinja2.utils import markupsafe
from app import app, PLOTS_FOLDER, UPLOAD_FOLDER
from functions import dir_listing, process_images
from backend.pix_plot_creater import PixPlotCreater
def url_for_wrapper(path, **kwargs):
if app.config['SERVER_HTTPS']:
return url_for(path, _external=True, _scheme='https', **kwargs)
else:
return url_for(path, **kwargs)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/uploads/', defaults={'req_path': ''})
@app.route('/uploads/<path:req_path>')
def uploads(req_path):
return dir_listing(UPLOAD_FOLDER, req_path, 'images.html')
@app.route('/plots/', defaults={'req_path': ''})
@app.route('/plots/<path:req_path>')
def plots(req_path):
return dir_listing(PLOTS_FOLDER, req_path, 'plots.html')
@app.route('/upload/', methods=['GET', 'POST'])
def upload_form():
if request.method == 'POST':
result_response, status_code = process_images(request)
if status_code != 200:
message = 'There was an error uploading photos: ' + str(result_response['reason'])
flash(message)
return redirect(url_for_wrapper('upload_form'))
else:
return redirect(url_for_wrapper('uploads', req_path=result_response['folder_name']))
else:
return render_template('upload_form.html')
@app.route('/create/', methods=['GET', 'POST'])
def create():
folders = os.listdir(UPLOAD_FOLDER)
if request.method == 'GET':
return render_template('create.html', folders=folders)
elif request.method == 'POST':
form_data = request.form
image_location = os.path.join(UPLOAD_FOLDER, form_data['folder_name'])
destination = os.path.join(PLOTS_FOLDER, form_data['folder_name'])
app.logger.debug('Create request for %s' % form_data['folder_name'])
create_url = request.url_root + "api/pixplot"
# TODO: use ProxyFix
create_url = create_url.replace('http://', 'https://')
app.logger.debug('Create request url: ' + str(create_url))
creater = PixPlotCreater(image_location, destination)
creater.create_new(metadata=True)
app.logger.info('Creation started for %s' % form_data['folder_name'])
start = time.time()
while True:
check = creater.check_complete()
if check:
# Finished
creation_log = creater.read_log()
last_line = [line for line in creation_log.split('\n') if line.strip()][-1]
app.logger.debug('Last line of log: ' + last_line)
if 'Error' in last_line:
message = markupsafe.Markup('<a href="/plots/%s/pixplot_creation.log" class="alert-link">Error creating PixPlot; download log</a>' % form_data['folder_name'])
log_message = f"Creation Error ({form_data['folder_name']}): {last_line}"
else:
message = markupsafe.Markup('<a href="/plots/%s/index.html" class="alert-link">Finished! Your PixPlot is uploaded here.</a>' %
form_data['folder_name'])
log_message = 'Creation completed for %s' % form_data['folder_name']
break
elif check is None:
# Error; no process
message = markupsafe.Markup('<a href="/plots/%s/pixplot_creation.log" class="alert-link">Error creating PixPlot; download log</a>' % form_data['folder_name'])
log_message = 'Error creating %s' % form_data['folder_name']
app.logger.error(f"{form_data['folder_name']} Error ({check}): check log {str(creater.plot_path) + 'pixplot_creation.log'}")
break
else:
# Running
time.sleep(10)
if time.time() - start > 600:
# Taking a long time
message = markupsafe.Markup('<a href="/plots/%s/index.html" class="alert-link">Taking longer than 10 minutes; check back later at this link</a>' % form_data['folder_name'])
log_message = 'Took longer than 10 minutes to create %s' % form_data['folder_name']
break
app.logger.info(log_message)
flash(message)
return redirect(url_for_wrapper('create'))
else:
return abort(404)