Files
notifications-api/app/service/utils.py
Chris Hill-Scott 74be99e7c7 Let team key send to whitelist
There is an overlap between team key/trial mode/whitelist. But it’s not
a complete overlap. So it’s hard to understand all the different
permutations of which key lets you send to which people when.

This commit tries to reduce the differences between these concepts. So
for a user of the API

**In trial mode**

- You can send to anyone in your team or whitelist, using the team key
- You can simulate sending to anyone, using the simulate key

**When you’re live**

- You can send to anyone in your team or whitelist, using the team key
- You can simulate sending to anyone, using the simulate key
- You can send to anyone with the live key

So doing a `git diff` on that list, the only difference between being in
trial mode and live mode is now:

`+` You can send to anyone with the live key

**(How trial mode used to work)**
- You can send to anyone in your team or whitelist, using the normal key
- You can simulate sending to anyone, using the simulate key
- You can send to _just_ people in your team using the team key
2016-10-07 15:38:36 +01:00

54 lines
1.5 KiB
Python

import itertools
from app.models import (
ServiceWhitelist,
MOBILE_TYPE, EMAIL_TYPE,
KEY_TYPE_TEST, KEY_TYPE_TEAM, KEY_TYPE_NORMAL)
from notifications_utils.recipients import allowed_to_send_to
def get_recipients_from_request(request_json, key, type):
return [(type, recipient) for recipient in request_json.get(key)]
def get_whitelist_objects(service_id, request_json):
return [
ServiceWhitelist.from_string(service_id, type, recipient)
for type, recipient in (
get_recipients_from_request(request_json,
'phone_numbers',
MOBILE_TYPE) +
get_recipients_from_request(request_json,
'email_addresses',
EMAIL_TYPE)
)
]
def service_allowed_to_send_to(recipient, service, key_type):
if key_type == KEY_TYPE_TEST:
return True
if key_type == KEY_TYPE_NORMAL and not service.restricted:
return True
team_members = itertools.chain.from_iterable(
[user.mobile_number, user.email_address] for user in service.users
)
whitelist_members = [
member.recipient for member in service.whitelist
]
if (
(key_type == KEY_TYPE_NORMAL and service.restricted) or
(key_type == KEY_TYPE_TEAM)
):
return allowed_to_send_to(
recipient,
itertools.chain(
team_members,
whitelist_members
)
)