-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
115 lines (93 loc) · 4.47 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
"""python app.py"""
from flask import Flask, render_template, request, url_for, send_from_directory, jsonify, send_file
import os
import PyCertGen
import common_utils
# "templates" this is for plain html files or "Great_Templates" this is for complex css + imgs +js +html+sass
TEMPLATES = "Great_Templates"
app = Flask(__name__, static_folder="assets", template_folder=TEMPLATES)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB Standard File Size
ROOT_DIR = os.getcwd()
# Reloading
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.route('/')
def home():
return render_template('index.html')
@app.route('/gencert')
def base():
return render_template('uploadtemp.html')
# https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/
@app.route('/uploadsuccess', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
# f.save(f.filename)
# Perform Some File Validation so that only DOCX File Can be uploaded
DocxFileSavePath = os.path.join(
ROOT_DIR, "TempSaved", "Generated.docx")
f.save(DocxFileSavePath)
# return str(f.filename) + 'file uploaded successfully'
document = PyCertGen.DocxLoader(DocxFileSavePath)
ParsedResults = PyCertGen.parser(document, v=0)
CleanParsed = PyCertGen.cleanParsed(ParsedResults)
return render_template('uploadsuccess.html', UploadFileMessage=str(f.filename) + ' file uploaded successfully', data=CleanParsed)
@app.route('/reuploadsuccess', methods=['GET', 'POST'])
def reupload_file():
if request.method == 'POST':
f = request.files['reuploadfile']
# f.save(f.filename)
# Perform Some File Validation so that only DOCX File Can be uploaded
DocxFileSavePath = os.path.join(
ROOT_DIR, "TempSaved", "Generated.docx")
f.save(DocxFileSavePath)
# return str(f.filename) + 'file uploaded successfully'
document = PyCertGen.DocxLoader(DocxFileSavePath)
ParsedResults = PyCertGen.parser(document, v=0)
CleanParsed = PyCertGen.cleanParsed(ParsedResults)
return render_template('uploadsuccess.html', UploadFileMessage=str(f.filename) + ' file uploaded successfully', data=CleanParsed)
@app.route('/uploadexcel')
def upload_excel_file():
return render_template('uploadexcel.html')
@app.route('/uploadexcelsuccess', methods=['GET', 'POST'])
def uploadexcelsuccess():
if request.method == 'POST':
f = request.files['ExcelFile']
# f.save(f.filename)
# Perform Some File Validation so that only EXCEL File Can be uploaded
ExcelFileSavePath = os.path.join(ROOT_DIR, "TempSaved", "Data.xlsx")
f.save(ExcelFileSavePath)
DocxFileSavePath = os.path.join(
ROOT_DIR, "TempSaved", "Generated.docx")
# return str(f.filename) + 'file uploaded successfully'
SaveFolder = os.path.join(ROOT_DIR, "TempSaved", "TempFiles")
# Clean First
common_utils.DeleteFolderContents(SaveFolder)
PyCertGen.CertGenEngine(
DocxFileSavePath, ExcelFileSavePath, SaveFolder)
SaveFolderPath = os.path.join("TempSaved", "TempFiles")
# SaveZipFilePath = os.path.join(ROOT_DIR, "TempSaved", "CertGen.zip")
SaveZipFilePath = os.path.join("TempZipSaved", "CertGen.zip")
common_utils.zipper(SaveFolderPath, SaveZipFilePath)
# Processing
return render_template('run.html', value="CertGen.zip")
@app.route('/download/<filename>')
def return_files_tut(filename):
if filename == "CertGen.zip":
ZipFilePath = os.path.join("TempZipSaved", filename)
return send_file(ZipFilePath, as_attachment=True, mimetype='application/zip',
download_name='CertGenResults.zip')
elif filename == "Sample.docx":
SampleDOCXPath = os.path.join("CertTemplateSamples", filename)
return send_file(SampleDOCXPath, as_attachment=True)
elif filename == "Sample.xlsx":
SampleExcelPath = os.path.join("CertTemplateSamples", filename)
return send_file(SampleExcelPath, as_attachment=True)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'assets', 'favicons'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
if __name__ == "__main__":
# app.run(debug=True)
# uvicorn.run("main:app", host="0.0.0.0", port=8080)
app.run(debug=True, port=8086, host='0.0.0.0')