Method for random label tag generation. Fixed the "renaming the most important file" mess.

master
Marek Baczynski 6 years ago
parent 1aaf5bd1b1
commit 444b9ecf9d

@ -1,14 +1,27 @@
from flask import Flask, Response, redirect, url_for, request, session, abort, render_template
from flask import Flask, Response, redirect, url_for, request, session, abort, render_template, send_file
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user, current_user
import json
import re
from PIL import Image, ImageFont, ImageDraw
import qrcode
from io import BytesIO
import json, random, re, string
app = Flask(__name__)
systemURL = '192.168.0.11:5000'
systemURL = 'http://192.168.0.11:5000/'
randomchars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789' #note the lack of zero
with open('links.json', 'r') as infile:
links = json.load(infile)
def saveFile():
with open('links.json', 'w') as outfile:
json.dump(links, outfile)
def randomToken():
code = ""
for i in range(0,6):
code += random.choice(randomchars)
return code
def codeValidation(code):
if len(code) not in range (5,6):
return False
@ -24,6 +37,27 @@ def codeLink(code):
return None
return links[code]['url']
def createNewCode():
code=randomToken()
while code in links: #danger!
code=randomToken()
links[code]={'url':''}
return code
def serveImage(img):
img_io = BytesIO()
img.save(img_io, 'PNG')
img_io.seek(0)
return send_file(img_io, mimetype='image/png')
@app.route('/code/<code>')
def serveCode(code):
code = code[:code.find('.')]
if codeValidation(code) is False:
return send_file('static/invalid.png', mimetype='image/png')
img = qrcode.make(systemURL + code)
return serveImage(img)
@app.route("/edit", methods=['GET', 'POST'])
def edit():
code = request.form.get("code")
@ -35,15 +69,29 @@ def edit():
url = request.form.get("url")
if url is None:
return render_template('editlink.html', code=code)
#Saving the link:
links[code]['url']=url
saveFile()
return render_template('success.html')
@app.route("/admin", methods=['GET', 'POST'])
@app.route("/admin")
def admin():
return render_template('admin.html')
@app.route("/generate", methods=['GET', 'POST'])
def generate():
howmany = request.form.get("howmany")
if howmany is None:
return render_template('generate.html')
return render_template("message.html", message="Totally generated {} many codes right now.".format(howmany))
@app.route("/list")
def list():
return render_template('message.html', message='Comming soon!™')
linklist = []
for key, value in links.items():
if len(value['url'])>0:
linklist.append({'code':key, 'url':value['url']})
return render_template('list.html', linklist=linklist)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')

94
qr.py

@ -1,94 +0,0 @@
from flask import Flask, Response, redirect, url_for, request, session, abort, render_template, send_file
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user, current_user
from PIL import Image, ImageFont, ImageDraw
from io import BytesIO
import qrcode
import json
import re
app = Flask(__name__)
systemURL = 'http://192.168.0.11:5000/'
with open('links.json', 'r') as infile:
links = json.load(infile)
def saveFile():
with open('links.json', 'w') as outfile:
json.dump(links, outfile)
def codeValidation(code):
if len(code) not in range (5,6):
return False
if re.match('^[\w-]+$', code) is None:
return False
return True
def codeLink(code):
global links
if code not in links:
return False
if len(links[code]['url'])==0:
return None
return links[code]['url']
def serveImage(img):
img_io = BytesIO()
img.save(img_io, 'PNG')
img_io.seek(0)
return send_file(img_io, mimetype='image/png')
@app.route('/code/<code>')
def serveCode(code):
code = code[:code.find('.')]
if codeValidation(code) is False:
return send_file('static/invalid.png', mimetype='image/png')
img = qrcode.make(systemURL + code)
return serveImage(img)
@app.route("/edit", methods=['GET', 'POST'])
def edit():
code = request.form.get("code")
print ("code: {}".format(code))
if codeLink(code) is False:
return render_template('invalid.html', code=code)
if codeLink(code) is not None:
return render_template('cantedit.html', url=codeLink(code))
url = request.form.get("url")
if url is None:
return render_template('editlink.html', code=code)
#Saving the link:
links[code]['url']=url
saveFile()
return render_template('success.html')
@app.route("/admin")
def admin():
return render_template('admin.html')
@app.route("/generate", methods=['GET', 'POST'])
def generate():
howmany = request.form.get("howmany")
if howmany is None:
return render_template('generate.html')
return render_template("message.html", message="Totally generated {} many codes right now.".format(howmany))
@app.route("/list")
def list():
linklist = []
for key, value in links.items():
if len(value['url'])>0:
linklist.append({'code':key, 'url':value['url']})
return render_template('list.html', linklist=linklist)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
if len(path)==0:
return render_template('welcome.html')
if path in links:
if codeLink(path) is not None:
return redirect(links[path]['url'], code=302)
else:
return render_template('newcode.html', code=path)
else:
return render_template('invalid.html', code=path)
Loading…
Cancel
Save