manage.cmd: add invites blueprint

- schema.sql: remove temporary and expires_at columns
 - migration.scripts: remove emporary and expires_at columns
This commit is contained in:
Luna Mendes 2018-11-19 01:34:04 -03:00
parent 99fab19766
commit 8b305407c2
4 changed files with 101 additions and 8 deletions

99
manage/cmd/invites.py Normal file
View File

@ -0,0 +1,99 @@
import datetime
import string
from random import choice
ALPHABET = string.ascii_lowercase + string.ascii_uppercase + string.digits
async def _gen_inv() -> str:
"""Generate an invite code"""
return ''.join(choice(ALPHABET) for _ in range(10))
async def gen_inv(ctx) -> str:
"""Generate an invite."""
for _ in range(10):
possible_inv = await _gen_inv()
created_at = await ctx.db.fetchval("""
SELECT created_at
FROM instance_invites
WHERE code = $1
""", possible_inv)
if created_at is None:
return possible_inv
return None
async def make_inv(ctx, args):
code = await gen_inv(ctx)
max_uses = args.max_uses
await ctx.db.execute("""
INSERT INTO instance_invites (code, max_uses)
VALUES ($1, $2)
""", code, max_uses)
print(f'invite created with {max_uses} max uses', code)
async def list_invs(ctx, args):
rows = await ctx.db.fetch("""
SELECT code, created_at, uses, max_uses
FROM instance_invites
""")
print(len(rows), 'invites')
for row in rows:
max_uses = row['max_uses']
delta = datetime.datetime.utcnow() - row['created_at']
usage = ('infinite uses' if max_uses == -1
else f'{row["uses"]} / {max_uses}')
print(f'\t{row["code"]}, {usage}, made {delta} ago')
async def delete_inv(ctx, args):
inv = args.invite_code
res = await ctx.db.execute("""
DELETE FROM instance_invites
WHERE code = $1
""", inv)
if res == 'DELETE 0':
print('NOT FOUND')
return
print('OK')
def setup(subparser):
makeinv_parser = subparser.add_parser(
'makeinv',
help='create an invite',
)
makeinv_parser.add_argument(
'max_uses', nargs='?', type=int, default=-1,
help='Maximum amount of uses before the invite is unavailable',
)
makeinv_parser.set_defaults(func=make_inv)
listinv_parser = subparser.add_parser(
'listinv',
help='list all invites',
)
listinv_parser.set_defaults(func=list_invs)
delinv_parser = subparser.add_parser(
'delinv',
help='delete an invite',
)
delinv_parser.add_argument('invite_code')
delinv_parser.set_defaults(func=delete_inv)

View File

@ -3,10 +3,6 @@ CREATE TABLE IF NOT EXISTS instance_invites (
created_at timestamp without time zone default (now() at time zone 'utc'), created_at timestamp without time zone default (now() at time zone 'utc'),
temporary bool DEFAULT false,
expires_at timestamp without time zone,
uses bigint DEFAULT 0, uses bigint DEFAULT 0,
max_uses bigint DEFAULT -1 max_uses bigint DEFAULT -1
); );

View File

@ -7,7 +7,7 @@ from logbook import Logger
from run import init_app_managers, init_app_db from run import init_app_managers, init_app_db
from manage.cmd.migration import migration from manage.cmd.migration import migration
from manage.cmd import users, tests from manage.cmd import users, tests, invites
log = Logger(__name__) log = Logger(__name__)
@ -32,6 +32,7 @@ def init_parser():
migration(subparser) migration(subparser)
users.setup(subparser) users.setup(subparser)
tests.setup(subparser) tests.setup(subparser)
invites.setup(subparser)
return parser return parser

View File

@ -45,9 +45,6 @@ CREATE TABLE IF NOT EXISTS instance_invites (
created_at timestamp without time zone default (now() at time zone 'utc'), created_at timestamp without time zone default (now() at time zone 'utc'),
temporary bool DEFAULT false,
expires_at timestamp without time zone,
uses bigint DEFAULT 0, uses bigint DEFAULT 0,
-- -1 means infinite uses -- -1 means infinite uses