add dao_reduce_sms_provider_priority function

retrive the sms providers from the DB, and decrease the chosen
provider's priority by 10, while increasing the other by 10.

add a check in to ensure we never decrease below 0 or increase above 100
- this is per provider, we don't check that the two add up to 100 or
  anything. If the values are outside of this range (eg: set via the UI)
then they'll probably* fix themselves at some point - we've added tests
to document these cases.

Use with_for_update to ensure that the method can only run once at a
time - other invocations of the function will be held on that line until
the currently running one ends and commits the transaction. This doesn't
affect anyone doing things from the UI.
This commit is contained in:
Leo Hemsted
2019-11-11 14:31:58 +00:00
parent 6f38cbbcf1
commit fa7e0a1e84
4 changed files with 61 additions and 28 deletions

View File

@@ -22,13 +22,11 @@ def get_provider_details_by_identifier(identifier):
def get_alternative_sms_provider(identifier):
alternate_provider = None
if identifier == 'firetext':
alternate_provider = 'mmg'
return 'mmg'
elif identifier == 'mmg':
alternate_provider = 'firetext'
return ProviderDetails.query.filter_by(identifier=alternate_provider).one()
return 'firetext'
raise ValueError('Unrecognised sms provider {}'.format(identifier))
def get_current_provider(notification_type):
@@ -49,9 +47,23 @@ def dao_get_provider_versions(provider_id):
@transactional
def dao_toggle_sms_provider(identifier):
alternate_provider = get_alternative_sms_provider(identifier)
dao_switch_sms_provider_to_provider_with_identifier(alternate_provider.identifier)
def dao_reduce_sms_provider_priority(identifier):
# get current priority of both providers
q = ProviderDetails.query.filter(
ProviderDetails.notification_type == 'sms',
ProviderDetails.active
).with_for_update()
providers = {provider.identifier: provider for provider in q}
other = get_alternative_sms_provider(identifier)
# always keep values between 0 and 100
providers[identifier].priority = max(0, providers[identifier].priority - 10)
providers[other].priority = min(100, providers[other].priority + 10)
def dao_toggle_sms_provider(*args, **kwargs):
raise NotImplementedError
@transactional