2017-01-20 16:13:13 +00:00
|
|
|
from flask import current_app
|
|
|
|
|
|
2017-03-02 18:03:53 +00:00
|
|
|
from app.dao.users_dao import get_user_by_id
|
2017-01-20 16:13:13 +00:00
|
|
|
|
|
|
|
|
|
2017-03-02 18:03:53 +00:00
|
|
|
def provider_is_inactive(new_provider):
|
|
|
|
|
if not new_provider.active:
|
|
|
|
|
current_app.logger.warning('Cancelling switch to {} as they are inactive'.format(
|
2017-01-20 16:13:13 +00:00
|
|
|
new_provider.identifier,
|
|
|
|
|
))
|
|
|
|
|
return True
|
|
|
|
|
|
2017-03-02 18:03:53 +00:00
|
|
|
|
|
|
|
|
def provider_is_primary(current_provider, new_provider, identifier):
|
|
|
|
|
if current_provider.identifier == identifier:
|
|
|
|
|
current_app.logger.warning('Provider {} is already activated'.format(current_provider.display_name))
|
|
|
|
|
return True
|
|
|
|
|
|
2017-01-20 16:13:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2017-03-02 18:03:53 +00:00
|
|
|
def switch_providers(current_provider, new_provider):
|
|
|
|
|
# Automatic update so set as notify user
|
|
|
|
|
notify_user = get_user_by_id(current_app.config['NOTIFY_USER_ID'])
|
|
|
|
|
current_provider.created_by_id = new_provider.created_by_id = notify_user.id
|
|
|
|
|
|
2017-01-20 16:13:13 +00:00
|
|
|
# Swap priority to change primary provider
|
|
|
|
|
if new_provider.priority > current_provider.priority:
|
|
|
|
|
new_provider.priority, current_provider.priority = current_provider.priority, new_provider.priority
|
|
|
|
|
|
2017-03-02 18:03:53 +00:00
|
|
|
# Increase other provider priority if equal
|
2017-01-20 16:13:13 +00:00
|
|
|
elif new_provider.priority == current_provider.priority:
|
|
|
|
|
current_provider.priority += 10
|
2017-01-23 13:36:04 +00:00
|
|
|
|
|
|
|
|
_print_provider_switch_logs(current_provider, new_provider)
|
2017-03-02 18:03:53 +00:00
|
|
|
return current_provider, new_provider
|
2017-01-20 16:13:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _print_provider_switch_logs(current_provider, new_provider):
|
|
|
|
|
current_app.logger.warning('Switching provider from {} to {}'.format(
|
|
|
|
|
current_provider.identifier,
|
|
|
|
|
new_provider.identifier
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
current_app.logger.warning('Provider {} now updated with priority of {}'.format(
|
|
|
|
|
current_provider.identifier,
|
|
|
|
|
current_provider.priority
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
current_app.logger.warning('Provider {} now updated with priority of {}'.format(
|
|
|
|
|
new_provider.identifier,
|
|
|
|
|
new_provider.priority
|
|
|
|
|
))
|