def main()

in pysteve/cli/load_election.py [0:0]


def main(y_fname):
    cfg = easydict.EasyDict(yaml.safe_load(open(y_fname)))

    s = requests.Session()

    eid = cfg.election.eid
    LOGGER.info(f'ELECTION: {eid}')
    issues = ensure_election(s, cfg)

    # Remove all the issues, so we can load the new/current set.
    for issue in issues:
        iid = issue['id']
        print(f'DELETING: {iid}')
        r = s.get(f'{cfg.config.endpoint}/delete/{eid}/{iid}')
        if r.status_code != 200:
            # Something is wrong.
            LOGGER.error(f'UNKNOWN: {r.status_code}')
            LOGGER.debug(f'BODY: {r.text}')
            raise Exception

    # Load all the defined issues.
    for idx, issue in enumerate(cfg['issues']):
        iid = f'issue-{idx}'
        print(f'CREATING: {iid}: {issue}')

        payload = {
            'title': issue['title'],
            'description': issue['description'],
            'type': issue['type'],
            ### not needed for YNA. fix as needed
            #'candidates': [ ],
            }
        r = s.post(f'{cfg.config.endpoint}/create/{eid}/{iid}',
                   data=payload)
        if r.status_code != 201:
            # Something is wrong.
            LOGGER.error(f'UNKNOWN: {r.status_code}')
            LOGGER.debug(f'BODY: {r.text}')
            raise Exception

        LOGGER.debug(r.json())