mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-15 17:52:26 -05:00
NotificationStatistics was added as a spike but didn't work out as expected. This is finally removing all that unused code. I'll drop the table in the next PR
66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
import itertools
|
|
|
|
from app.dao.date_util import get_financial_year
|
|
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
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
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
|
|
)
|
|
)
|
|
|
|
|
|
def get_current_financial_year_start_year():
|
|
now = datetime.now()
|
|
financial_year_start = now.year
|
|
start_date, end_date = get_financial_year(now.year)
|
|
if now < start_date:
|
|
financial_year_start = financial_year_start - 1
|
|
return financial_year_start
|