Verified Commit fec3b7da authored by Davide Depau's avatar Davide Depau
Browse files

Add management cli

parent 8007c28f
#!/usr/bin/env python3
import flask
from flask_redis import FlaskRedis
from flask_httpauth import HTTPBasicAuth
import re
__all__ = ('app', 'redis_store', 'management_cli')
import os
import crypt
import re
import flask
from flask_httpauth import HTTPBasicAuth
from flask_redis import FlaskRedis
app = flask.Flask(__name__)
auth = HTTPBasicAuth()
......@@ -13,7 +13,18 @@ app.config['REDIS_URL'] = os.environ.get('REDIS_URL', 'redis://'
redis_store = FlaskRedis()
pure_string = re.compile(r'^\w+$')
admin_dir = os.path.join(os.path.dirname(__file__), 'admin')
def redis_key_for_user(user: str) -> str:
return "user.{}".format(user)
def redis_key_for_url(url: str) -> str:
return "url.{}".format(url)
def redis_key_for_count(url: str) -> str:
return "count.{}".format(url)
def handle_redirect(path):
......@@ -27,7 +38,7 @@ def handle_redirect(path):
def handle_login(username, password):
crypt_pw = redis_store.get("user." + username)
crypt_pw = redis_store.get(redis_key_for_user(username))
if crypt_pw is not None:
crypt_pw = crypt_pw.decode()
return crypt_pw == crypt.crypt(password, crypt_pw)
......@@ -51,9 +62,9 @@ def add_url():
long_url = payload['url']
if not pure_string.match(short_url):
return flask.abort(400)
if not redis_store.exists('url.' + short_url):
redis_store.set('count.' + short_url, 0)
redis_store.set('url.' + short_url, long_url)
if not redis_store.exists(redis_key_for_url(short_url)):
redis_store.set(redis_key_for_count(short_url), 0)
redis_store.set(redis_key_for_url(short_url), long_url)
return ''
except KeyError:
return flask.abort(400)
......@@ -67,8 +78,8 @@ def del_url():
payload = flask.request.get_json()
if payload:
redis_store.delete('url.' + payload['name'])
redis_store.delete('count.' + payload['name'])
return ''
except KeyError:
return flask.abort(400)
......@@ -84,12 +95,11 @@ def list_urls():
for key in keys:
real_key = key.replace('url.', '', 1)
return_dict[real_key] = {'url': redis_store.get(key).decode(),
'count': int(redis_store.get('count.' + real_key).decode())}
'count': int(redis_store.get(redis_key_for_count(real_key)).decode())}
return flask.jsonify(return_dict)
@app.route('/<path:path>', strict_slashes=False)
def redirect(path):
if pure_string.match(path):
return handle_redirect(path)
......@@ -97,5 +107,7 @@ def redirect(path):
return flask.abort(404)
from .management_cli import *
if __name__ == "__main__":
import crypt
import click
from . import app, redis_store, redis_key_for_user
@click.option("--passwd", "-p", prompt="Password", help="Password", hide_input=True)
def adduser(username, passwd):
"""Add UUS admin user"""
cryptpwd = crypt.crypt(passwd)
redis_store.set(redis_key_for_user(username), cryptpwd)
@click.option("--passwd", "-p", prompt="Password", help="Password", hide_input=True)
def passwd(username, passwd):
"""Change UUS admin user password"""
adduser(username, passwd)
def deluser(username):
"""Delete UUS admin user"""
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment