pyceo/ceo/cli/positions.py

45 lines
1.4 KiB
Python
Raw Normal View History

2021-08-27 22:57:50 -04:00
import click
from zope import component
from ..utils import http_get, http_post
from .utils import handle_sync_response, handle_stream_response, print_colon_kv
2021-08-27 22:57:50 -04:00
from ceo_common.interfaces import IConfig
from ceod.transactions.members import UpdateMemberPositionsTransaction
2021-08-27 23:01:36 -04:00
2021-08-27 22:57:50 -04:00
@click.group(short_help='List or change exec positions')
def positions():
update_commands()
2021-08-27 22:57:50 -04:00
@positions.command(short_help='Get current positions')
def get():
resp = http_get('/api/positions')
result = handle_sync_response(resp)
print_colon_kv(result.items())
@positions.command(short_help='Update positions')
2021-08-30 16:55:46 -04:00
def set(**kwargs):
body = {k.replace('_', '-'): v for k, v in kwargs.items()}
print_body = {k: v or '' for k, v in body.items()}
2021-08-27 22:57:50 -04:00
click.echo('The positions will be updated:')
print_colon_kv(print_body.items())
2021-08-27 22:57:50 -04:00
click.confirm('Do you want to continue?', abort=True)
resp = http_post('/api/positions', json=body)
2021-08-27 22:57:50 -04:00
handle_stream_response(resp, UpdateMemberPositionsTransaction.operations)
# Provides dynamic parameters for `set' command using config file
def update_commands():
2021-08-30 16:55:46 -04:00
global set
2021-08-27 22:57:50 -04:00
cfg = component.getUtility(IConfig)
2021-08-27 22:57:50 -04:00
avail = cfg.get('positions_available')
required = cfg.get('positions_required')
for pos in avail:
2021-08-30 16:55:46 -04:00
r = pos in required
set = click.option(f'--{pos}', metavar='USERNAME', required=r, prompt=r)(set)