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)