-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from SFTtech/milo/refactorings
major refactorings
- Loading branch information
Showing
288 changed files
with
40,276 additions
and
49,469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,4 @@ | ||
import argparse | ||
import asyncio | ||
from pathlib import Path | ||
|
||
from abrechnung.config import read_config | ||
from abrechnung.util import log_setup | ||
|
||
from abrechnung.mailer import MailerCli | ||
from abrechnung.http.cli import ApiCli | ||
from abrechnung.database.cli import DatabaseCli | ||
from abrechnung.admin import AdminCli | ||
from abrechnung.demo import DemoCli | ||
|
||
|
||
def main(): | ||
""" | ||
main CLI entry point | ||
parses commands and jumps into the subprogram's entry point | ||
""" | ||
cli = argparse.ArgumentParser() | ||
|
||
cli.add_argument( | ||
"-c", | ||
"--config-path", | ||
default="/etc/abrechnung/abrechnung.yaml", | ||
help="config file, default: %(default)s", | ||
) | ||
|
||
cli.add_argument( | ||
"-d", "--debug", action="store_true", help="enable asyncio debugging" | ||
) | ||
cli.add_argument( | ||
"-v", "--verbose", action="count", default=0, help="increase program verbosity" | ||
) | ||
cli.add_argument( | ||
"-q", "--quiet", action="count", default=0, help="decrease program verbosity" | ||
) | ||
|
||
subparsers = cli.add_subparsers() | ||
|
||
def add_subcommand(name, subcommand_class): | ||
subparser = subparsers.add_parser(name) | ||
subparser.set_defaults(subcommand_class=subcommand_class) | ||
subcommand_class.argparse_register(subparser) | ||
|
||
# add all the subcommands here | ||
add_subcommand("mailer", MailerCli) | ||
add_subcommand("api", ApiCli) | ||
add_subcommand("db", DatabaseCli) | ||
add_subcommand("admin", AdminCli) | ||
add_subcommand("demo", DemoCli) | ||
|
||
args = vars(cli.parse_args()) | ||
|
||
# set up log level | ||
log_setup(args["verbose"] - args["quiet"]) | ||
|
||
# enable asyncio debugging | ||
# loop = asyncio.get_event_loop() | ||
# loop.set_debug(args["debug"]) | ||
|
||
config_path = args.pop("config_path") | ||
config = read_config(Path(config_path)) | ||
|
||
try: | ||
subcommand_class = args.pop("subcommand_class") | ||
except KeyError: | ||
cli.error("no subcommand was given") | ||
subcommand_class.argparse_validate(args, cli.error) | ||
subcommand_object = subcommand_class(config=config, **args) | ||
asyncio.run(subcommand_object.run()) | ||
|
||
from abrechnung.cli.main import main | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,26 @@ | ||
import argparse | ||
import logging | ||
from getpass import getpass | ||
|
||
from abrechnung import subcommand | ||
from abrechnung.application.users import UserService | ||
from abrechnung.config import Config | ||
from abrechnung.framework.database import create_db_pool | ||
|
||
|
||
class AdminCli(subcommand.SubCommand): | ||
def __init__(self, config: Config, **args): # pylint: disable=super-init-not-called | ||
self.config = config | ||
self.logger = logging.getLogger(__name__) | ||
|
||
self.command = args.pop("command") | ||
self.args = args | ||
|
||
@staticmethod | ||
def argparse_register(subparser: argparse.ArgumentParser): | ||
subparsers = subparser.add_subparsers(required=True) | ||
|
||
create_user_parser = subparsers.add_parser("create-user") | ||
create_user_parser.set_defaults(command="create_user") | ||
create_user_parser.add_argument("-n", "--name", type=str, required=True) | ||
create_user_parser.add_argument("-e", "--email", type=str, required=True) | ||
create_user_parser.add_argument("--skip-email-check", action="store_true") | ||
|
||
async def handle_create_user_command(self): | ||
self.logger.info( | ||
f"Creating user with email: {self.args['email']} and username: {self.args['name']}" | ||
) | ||
password = getpass(prompt="Input initial password for user:") | ||
repeat_password = getpass(prompt="Repeat password:") | ||
if password != repeat_password: | ||
print("Passwords do not match!") | ||
return | ||
|
||
db_pool = await create_db_pool(self.config.database) | ||
user_service = UserService(db_pool, self.config) | ||
user_service.enable_registration = True | ||
if self.args["skip_email_check"]: | ||
user_service.valid_email_domains = None | ||
await user_service.register_user( # pylint: disable=missing-kwoa | ||
username=self.args["name"], | ||
email=self.args["email"], | ||
password=password, | ||
) | ||
|
||
async def run(self): | ||
if self.command == "create_user": | ||
return await self.handle_create_user_command() | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def create_user(config: Config, name: str, email: str, skip_email_check: bool, no_email_confirmation: bool): | ||
logger.info(f"Creating user with email: {email} and username: {name}") | ||
password = getpass(prompt="Input initial password for user:") | ||
repeat_password = getpass(prompt="Repeat password:") | ||
if password != repeat_password: | ||
print("Passwords do not match!") | ||
return | ||
|
||
db_pool = await create_db_pool(config.database) | ||
user_service = UserService(db_pool, config) | ||
user_service.enable_registration = True | ||
if skip_email_check: | ||
user_service.valid_email_domains = None | ||
await user_service.register_user( # pylint: disable=missing-kwoa | ||
username=name, email=email, password=password, requires_email_confirmation=not no_email_confirmation | ||
) |
Oops, something went wrong.