Пример #1
0
    logger.debug('Updating contact')
    data = format_contact(real_contact)

    should_update = False
    for k, v in data.items():
        test_v = getattr(ipbx_contact, k)
        logger.debug('Testing key {0}: '.format(k) +
                     'Freshdesk: {0} '.format(v) + '3CX: {0}'.format(test_v))
        if test_v != v:
            logger.info('Updating {0}: {1} => {2}'.format(k, test_v, v))
            setattr(ipbx_contact, k, v)
            should_update = True

    if should_update:
        if DRY:
            logger.info('DRY: would have update the contact with data')
        else:
            session = ipbx_client.get_session()
            session.add(ipbx_contact)
            session.commit()
            logger.info('Contact {0} updated'.format(real_contact.name))
    else:
        logger.info('No need to update')


if __name__ == "__main__":
    try:
        main(sys.argv[1:])
    except Exception as exception:
        logger.exception(exception)
    client = ovh.Client()

    section = config.sections()[0]
    logger.new_loop_logger()
    for key in config[section]:
        logger.new_iteration(prefix=key)
        url = '/me/task/contactChange/{0}'.format(key)
        contact = client.get(url)
        logger.debug('current request', contact)
        if contact['dateDone'] is not None:
            logger.info('Already accepted')
        else:
            params = {'token': config[section][key]}
            accept_url = '{0}/accept'.format(url)
            logger.debug('Going to post to {0} with params'.format(accept_url),
                         params)
            try:
                client.post(accept_url, **params)
            except APIError:
                logger.info('Already accepted')
                continue
    logger.del_loop_logger()


if __name__ == '__main__':
    try:
        process()
    except Exception as e:
        logger.exception(e)