mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-19 22:13:48 -04:00
Refactor settings page to use service model
There’s a lot of code in service settings which: - talks to the API directly through the clients - passes that information through to the Jinja template By encapsulating this logic in the service model: - the Jinja template can access the data directly - the logic can be reused across multiple methods
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
from notifications_utils.field import Field
|
||||
from werkzeug.utils import cached_property
|
||||
|
||||
from app.notify_client.billing_api_client import billing_api_client
|
||||
from app.notify_client.email_branding_client import email_branding_client
|
||||
from app.notify_client.inbound_number_client import inbound_number_client
|
||||
from app.notify_client.job_api_client import job_api_client
|
||||
from app.notify_client.organisations_api_client import organisations_client
|
||||
from app.notify_client.service_api_client import service_api_client
|
||||
from app.notify_client.user_api_client import user_api_client
|
||||
from app.utils import get_default_sms_sender
|
||||
@@ -114,10 +119,28 @@ class Service():
|
||||
return len(self.templates_by_type('sms')) > 0
|
||||
|
||||
@cached_property
|
||||
def email_reply_to_addresses(self):
|
||||
return service_api_client.get_reply_to_email_addresses(self.id)
|
||||
|
||||
@property
|
||||
def has_email_reply_to_address(self):
|
||||
return bool(service_api_client.get_reply_to_email_addresses(
|
||||
self.id
|
||||
))
|
||||
return bool(self.email_reply_to_addresses)
|
||||
|
||||
@property
|
||||
def count_email_reply_to_addresses(self):
|
||||
return len(self.email_reply_to_addresses)
|
||||
|
||||
@property
|
||||
def default_email_reply_to_address(self):
|
||||
return next(
|
||||
(
|
||||
x['email_address']
|
||||
for x in self.email_reply_to_addresses if x['is_default']
|
||||
), None
|
||||
)
|
||||
|
||||
def get_email_reply_to_address(self, id):
|
||||
return service_api_client.get_reply_to_email_address(self.id, id)
|
||||
|
||||
@property
|
||||
def needs_to_add_email_reply_to_address(self):
|
||||
@@ -128,10 +151,38 @@ class Service():
|
||||
return self.organisation_type in {'local', 'nhs'}
|
||||
|
||||
@cached_property
|
||||
def sms_senders(self):
|
||||
return service_api_client.get_sms_senders(self.id)
|
||||
|
||||
@property
|
||||
def sms_senders_with_hints(self):
|
||||
|
||||
def attach_hint(sender):
|
||||
hints = []
|
||||
if sender['is_default']:
|
||||
hints += ["default"]
|
||||
if sender['inbound_number_id']:
|
||||
hints += ["receives replies"]
|
||||
if hints:
|
||||
sender['hint'] = "(" + " and ".join(hints) + ")"
|
||||
return sender
|
||||
|
||||
return [attach_hint(sender) for sender in self.sms_senders]
|
||||
|
||||
@property
|
||||
def default_sms_sender(self):
|
||||
return get_default_sms_sender(self.sms_senders)
|
||||
|
||||
@property
|
||||
def count_sms_senders(self):
|
||||
return len(self.sms_senders)
|
||||
|
||||
@property
|
||||
def sms_sender_is_govuk(self):
|
||||
return get_default_sms_sender(
|
||||
service_api_client.get_sms_senders(self.id)
|
||||
) in {'GOVUK', 'None'}
|
||||
return self.default_sms_sender in {'GOVUK', 'None'}
|
||||
|
||||
def get_sms_sender(self, id):
|
||||
return service_api_client.get_sms_sender(self.id, id)
|
||||
|
||||
@property
|
||||
def needs_to_change_sms_sender(self):
|
||||
@@ -141,6 +192,26 @@ class Service():
|
||||
self.sms_sender_is_govuk,
|
||||
))
|
||||
|
||||
@cached_property
|
||||
def letter_contact_details(self):
|
||||
return service_api_client.get_letter_contacts(self.id)
|
||||
|
||||
@property
|
||||
def count_letter_contact_details(self):
|
||||
return len(self.letter_contact_details)
|
||||
|
||||
@property
|
||||
def default_letter_contact_block(self):
|
||||
return next(
|
||||
(
|
||||
Field(x['contact_block'], html='escape')
|
||||
for x in self.letter_contact_details if x['is_default']
|
||||
), None
|
||||
)
|
||||
|
||||
def get_letter_contact_block(self, id):
|
||||
return service_api_client.get_letter_contact(self.id, id)
|
||||
|
||||
@property
|
||||
def go_live_checklist_completed(self):
|
||||
return all((
|
||||
@@ -153,3 +224,42 @@ class Service():
|
||||
@property
|
||||
def go_live_checklist_completed_as_yes_no(self):
|
||||
return 'Yes' if self.go_live_checklist_completed else 'No'
|
||||
|
||||
@cached_property
|
||||
def free_sms_fragment_limit(self):
|
||||
return billing_api_client.get_free_sms_fragment_limit_for_year(self.id) or 0
|
||||
|
||||
@cached_property
|
||||
def data_retention(self):
|
||||
return service_api_client.get_service_data_retention(self.id)
|
||||
|
||||
def get_data_retention_item(self, id):
|
||||
return service_api_client.get_service_data_retention_by_id(self.id, id)
|
||||
|
||||
@property
|
||||
def email_branding_id(self):
|
||||
return self._dict['email_branding']
|
||||
|
||||
@cached_property
|
||||
def email_branding(self):
|
||||
if self.email_branding_id:
|
||||
return email_branding_client.get_email_branding(self.email_branding_id)['email_branding']
|
||||
return None
|
||||
|
||||
@cached_property
|
||||
def letter_branding(self):
|
||||
return email_branding_client.get_letter_email_branding().get(
|
||||
self.dvla_organisation, '001'
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def organisation_name(self):
|
||||
return organisations_client.get_service_organisation(self.id).get('name', None)
|
||||
|
||||
@cached_property
|
||||
def inbound_number(self):
|
||||
return inbound_number_client.get_inbound_sms_number_for_service(self.id)['data'].get('number', '')
|
||||
|
||||
@property
|
||||
def has_inbound_number(self):
|
||||
return bool(self.inbound_number)
|
||||
|
||||
Reference in New Issue
Block a user