forked from fossasia/meilix-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
96 lines (74 loc) · 2.97 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
import base64 # for encoding the script for variable
import os
import re
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename
# These are the extension that we are accepting to be uploaded
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
UPLOAD_FOLDER = 'uploads/'
# Initialize the Flask application
app = Flask(__name__)
# This is the path to the upload directory
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
def allowed_file(filename):
# Check for allowed file extension
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def urlify(s):
"""Remove all non-word characters (everything except numbers and letters)"""
s = re.sub(r"[^\w\s]", '', s).strip()
# Replace all runs of whitespace with a single dash
s = re.sub(r"\s+", '-', s)
return s
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
email = request.form['email']
TRAVIS_TAG = request.form['TRAVIS_TAG']
event_url = request.form['event_url']
variables = {}
for name, value in request.form.items():
if name.startswith("GENERATOR_"):
variables[name] = value
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
os.rename(UPLOAD_FOLDER + filename, UPLOAD_FOLDER + 'wallpaper')
filename = 'wallpaper'
if email != '' and TRAVIS_TAG != '':
os.environ["email"] = email
TRAVIS_TAG = urlify(TRAVIS_TAG) # this will fix url issue
os.environ["TRAVIS_TAG"] = TRAVIS_TAG
os.environ["event_url"] = event_url
with open('travis_script_1.sh', 'rb') as f:
os.environ["TRAVIS_SCRIPT"] = str(base64.b64encode(f.read()))[1:]
return redirect(url_for('output'))
return render_template('index.html')
@app.route('/output')
def output():
if os.environ['TRAVIS_TAG']: # if TRAVIS_TAG have value it will proceed
os.system('./script.sh')
print ('/build called')
return render_template('build.html')
else:
return redirect(url_for('index'))
# Function to call meilix script on clicking the build button
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
@app.route('/about')
def about():
# About page
return render_template("about.html")
# Return a custom 404 error.
@app.errorhandler(404)
def page_not_found(e):
return 'Sorry, unexpected error: {}'.format(e), 404
@app.errorhandler(500)
def application_error(e):
# Return a custom 500 error.
return 'Sorry, unexpected error: {}'.format(e), 500
if __name__ == '__main__':
app.run()