first commit!

Marek Baczynski 6 years ago
parent 009f5cfe63
commit 603f157c9e

@ -0,0 +1,5 @@


@ -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: <a href="{0}"{0}</a>.'.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': ''})
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)
return notfound(path)
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. <a href="/create?code={}">Click here to create a link for it</a>'.format(path)
def invalid(path):
return "Your code: {} is not valid. Please go away.".format(path)