diff --git a/app.py b/app.py index c4c151c..a196de2 100644 --- a/app.py +++ b/app.py @@ -1,12 +1,15 @@ -from dotenv import main -from qrcode_generator import create_qrcode, IMG_OUTPUT_PATH -from flask import Flask, request -from flask_bootstrap import Bootstrap +import os + +from dotenv import load_dotenv +from flask import Flask, request +from flask_bootstrap import Bootstrap from flask.templating import render_template -from dotenv import load_dotenv -from forms import QRCodeForm +from forms import QRCodeForm +from qrcode_generator import create_qrcode, IMG_OUTPUT_PATH -import os +# +# Setup +# load_dotenv() @@ -17,14 +20,32 @@ if not os.path.exists(IMG_OUTPUT_PATH): os.mkdir(IMG_OUTPUT_PATH) + +# +# App routes +# + @app.route('/', methods=['POST', 'GET']) def homepage(): - return render_template('index.html', title='QRCode Generator', form=QRCodeForm()) + return render_template( + 'index.html', + title='QRCode Generator', + form=QRCodeForm()) + @app.route('/generate_qrcode', methods=['POST']) def generate_qrcode(): img_path = create_qrcode(request.form['content']) - return render_template('generate_qrcode.html', title='generated QRCode', img = img_path, content = request.form['content']) + return render_template( + 'generate_qrcode.html', + title='generated QRCode', + img=img_path, + content=request.form['content']) + + +# +# Run! +# if __name__ == "__main__": app.run(threaded=True, port=9691, host='0.0.0.0') diff --git a/qrcode_generator.py b/qrcode_generator.py index b620c5d..d129224 100644 --- a/qrcode_generator.py +++ b/qrcode_generator.py @@ -1,13 +1,24 @@ import hashlib import segno + from datetime import datetime IMG_OUTPUT_PATH = 'static/qrcodes/' + def generate_qrcode_image(content, err_lvl): - return segno.make(content, error = err_lvl) + return segno.make(content, error=err_lvl) + + +def create_qrcode(content, error='H'): + filename = IMG_OUTPUT_PATH \ + + hashlib.sha256( + f'{content}_{datetime.now()}'.encode('utf-8')).hexdigest() \ + + '.svg' -def create_qrcode(content, error = 'H'): - filename = IMG_OUTPUT_PATH + hashlib.sha256(f'{content}_{datetime.now()}'.encode('utf-8')).hexdigest() + '.svg' - generate_qrcode_image(content, error).save(filename, scale=10, light='white', dark='black') + generate_qrcode_image(content, error).save( + filename, + scale=10, + light='white', + dark='black') return filename diff --git a/templates/base.html b/templates/base.html index baa7e75..4174780 100644 --- a/templates/base.html +++ b/templates/base.html @@ -10,3 +10,7 @@ {% endblock %} {% block html_atrib %}lang="en"{% endblock %} +{% block styles %} +{{ super() }} + +{% endblock %} \ No newline at end of file diff --git a/templates/generate_qrcode.html b/templates/generate_qrcode.html index b1972e8..a56230f 100644 --- a/templates/generate_qrcode.html +++ b/templates/generate_qrcode.html @@ -1,10 +1,14 @@ {% extends "base.html" %} {% block content %} -