-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
81 lines (72 loc) · 2.91 KB
/
app.py
File metadata and controls
81 lines (72 loc) · 2.91 KB
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
#import zip
import os
from flask import Flask, redirect, request, url_for, flash, send_file, Response
from jinja2 import Environment, PackageLoader
from werkzeug.utils import secure_filename
from PIL import Image,ImageDraw,ImageFont
import re
import shutil
UPLOAD_FOLDER = '/Users/itstimetogetout/Desktop/Projects/FranciscanLads/MiniProjects/certi_generator/uploads/'
ALLOWED_EXTENSIONS = {'txt', 'png', 'jpg', 'jpeg', 'docx', 'css'}
app = Flask(__name__)
get = Environment(loader=PackageLoader(__name__, 'templates')).get_template
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
names = request.form.get('names', '')
font = request.form.get('font', '')
height = request.form.get('height', '')
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
create_certificate(os.path.join(app.config['UPLOAD_FOLDER'], filename), names, int(font), int(height))
return redirect(url_for('downloads'))
return get('login.html').render()
@app.route('/downloads/')
def downloads():
return get('download.html').render()
@app.route('/return-files/')
def return_files_tut():
try:
result = send_file('/Users/itstimetogetout/Desktop/Projects/FranciscanLads/MiniProjects/certi_generator/outputzip.zip', attachment_filename='outputzip.zip')
os.remove('outputzip.zip')
return result
except Exception as e:
return str(e)
def create_certificate(filename, names, fontsize, height):
names=names.split("\n")
font = ImageFont.truetype("arial.ttf", fontsize)
img = Image.open(filename)
W, H = img.size
for name in names:
name = name.replace(chr(13), '')
img = Image.open(filename)
draw = ImageDraw.Draw(img)
w, h = draw.textsize(name, font=font)
draw.text(((W-w)/2, height), name, (0, 0, 0), font=font, align="center")
name = re.sub('\W+',' ', name)
s = name+'.png'
print(s)
if s != ".png":
img.save("/Users/itstimetogetout/Desktop/Projects/FranciscanLads/MiniProjects/certi_generator/output/"+s, 'PNG')
shutil.make_archive('outputzip', 'zip', 'output')
shutil.rmtree('output')
shutil.rmtree('uploads')
os.mkdir('output')
os.mkdir('uploads')
if __name__ == '__main__':
app.debug = True
app.run()