~homeworkprod/byceps

ref: 3beaa96d910b36c05dfa41af8f22073a10dcab53 byceps/scripts/create_initial_admin_user.py -rwxr-xr-x 1.3 KiB
3beaa96d — Jochen Kupperschmidt PostgreSQL v9.6 is only available as of Ubuntu Trusty, but not yet on Ubuntu Precise 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Create an initial user with admin privileges to begin BYCEPS setup.

:Copyright: 2006-2017 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

import click

from byceps.database import db
from byceps.services.authentication.password import service as password_service
from byceps.services.user import service as user_service
from byceps.util.system import get_config_filename_from_env_or_exit

from bootstrap.util import app_context


@click.command()
@click.option('--screen_name', prompt=True)
@click.option('--email_address', prompt=True)
@click.option('--password', prompt=True, hide_input=True)
def execute(screen_name, email_address, password):
    click.echo('Creating user "{}" ... '.format(screen_name), nl=False)

    user = _create_user(screen_name, email_address)
    password_service.create_password_hash(user.id, password)

    click.secho('done.', fg='green')


def _create_user(screen_name, email_address):
    try:
        user = user_service.build_user(screen_name, email_address)
    except ValueError as e:
        raise click.UsageError(e)

    user.enabled = True
    db.session.add(user)
    db.session.commit()

    return user


if __name__ == '__main__':
    config_filename = get_config_filename_from_env_or_exit()
    with app_context(config_filename):
        execute()