From 603f157c9ee4603d892cb175b6bab145222d6a97 Mon Sep 17 00:00:00 2001 From: Marek Baczynski Date: Mon, 15 Oct 2018 21:11:46 +0200 Subject: [PATCH] first commit! --- links.json | 5 +++++ qr.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 links.json create mode 100644 qr.py diff --git a/links.json b/links.json new file mode 100644 index 0000000..3570ff6 --- /dev/null +++ b/links.json @@ -0,0 +1,5 @@ +{ + "aaaaa":{"url":"https://www.youtube.com/watch?v=CZlfbep2LdU"}, + "dfghj":{"url":"https://www.youtube.com/watch?v=pF80Y28zhqg"}, + "cvbnm":{"url":""} +} \ No newline at end of file diff --git a/qr.py b/qr.py new file mode 100644 index 0000000..f7c5bfb --- /dev/null +++ b/qr.py @@ -0,0 +1,57 @@ +from flask import Flask, Response, redirect, url_for, request, session, abort, render_template +from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user, current_user +import json +import datetime +import re +app = Flask(__name__) + +with open('links.json', 'r') as infile: + links = json.load(infile) + +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 None + if len(links[code]['url'])==0: + return None + return links[code]['url'] + +@app.route("/create", methods=['GET', 'POST']) +def create(): + code = request.args.get("code") + if codeLink(code) is not None: + return 'This link already points here: .'.format(codeLink(code)) + return "Creating a link for {}".format(request.args.get("")) + +@app.route("/list", methods=['GET', 'POST']) +def list(): + return 'Look at all my codes:' + +@app.route('/', defaults={'path': ''}) +@app.route('/') +def catch_all(path): + if len(path)==0: + return landing_page(path) + if path in links: + if codeLink(path) is not None: + return redirect(links[path]['url'], code=302) + else: + return notfound(path) + else: + return invalid(path) + +def landing_page(path): + return 'Hello, QR! You are here: {}'.format(path) + +def notfound(path): + return 'This is a new QR code. Click here to create a link for it'.format(path) + +def invalid(path): + return "Your code: {} is not valid. Please go away.".format(path)