diff --git a/app/main/views/platform_admin.py b/app/main/views/platform_admin.py index e2f9b27cd..49e381248 100644 --- a/app/main/views/platform_admin.py +++ b/app/main/views/platform_admin.py @@ -428,6 +428,7 @@ def clear_cache(): 'organisations', 'domains', 'live-service-and-organisation-counts', + 'organisation-????????-????-????-????-????????????-name', ]), ]) diff --git a/app/main/views/service_settings.py b/app/main/views/service_settings.py index 78e371363..73766ac6b 100644 --- a/app/main/views/service_settings.py +++ b/app/main/views/service_settings.py @@ -991,15 +991,14 @@ def service_preview_letter_branding(service_id): def link_service_to_organisation(service_id): all_organisations = organisations_client.get_organisations() - current_linked_organisation = organisations_client.get_service_organisation(service_id).get('id', None) form = LinkOrganisationsForm( choices=convert_dictionary_to_wtforms_choices_format(all_organisations, 'id', 'name'), - organisations=current_linked_organisation + organisations=current_service.organisation_id ) if form.validate_on_submit(): - if form.organisations.data != current_linked_organisation: + if form.organisations.data != current_service.organisation_id: organisations_client.update_service_organisation( service_id, form.organisations.data diff --git a/app/models/organisation.py b/app/models/organisation.py index 8d1390250..4028a0d51 100644 --- a/app/models/organisation.py +++ b/app/models/organisation.py @@ -50,6 +50,8 @@ class Organisation(JSONModel): @classmethod def from_id(cls, org_id): + if not org_id: + return cls({}) return cls(organisations_client.get_organisation(org_id)) @classmethod diff --git a/app/models/service.py b/app/models/service.py index c053438cc..241d325eb 100644 --- a/app/models/service.py +++ b/app/models/service.py @@ -18,6 +18,7 @@ from app.notify_client.inbound_number_client import inbound_number_client from app.notify_client.invite_api_client import invite_api_client from app.notify_client.job_api_client import job_api_client from app.notify_client.letter_branding_client import letter_branding_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.template_folder_api_client import ( template_folder_api_client, @@ -459,7 +460,7 @@ class Service(JSONModel): @cached_property def organisation(self): - return Organisation.from_service(self.id) + return Organisation.from_id(self.organisation_id) @property def organisation_id(self): @@ -469,6 +470,12 @@ class Service(JSONModel): def organisation_type(self): return self.organisation.organisation_type or self._dict['organisation_type'] + @property + def organisation_name(self): + if not self.organisation: + return None + return organisations_client.get_organisation_name(self.organisation_id) + @property def organisation_type_label(self): return dict(Organisation.TYPES).get(self.organisation_type) diff --git a/app/notify_client/organisations_api_client.py b/app/notify_client/organisations_api_client.py index d1c309081..f30fe009b 100644 --- a/app/notify_client/organisations_api_client.py +++ b/app/notify_client/organisations_api_client.py @@ -22,6 +22,10 @@ class OrganisationsClient(NotifyAdminAPIClient): def get_organisation(self, org_id): return self.get(url='/organisations/{}'.format(org_id)) + @cache.set('organisation-{org_id}-name') + def get_organisation_name(self, org_id): + return self.get_organisation(org_id)['name'] + def get_organisation_by_domain(self, domain): try: return self.get( @@ -54,6 +58,7 @@ class OrganisationsClient(NotifyAdminAPIClient): return api_response + @cache.delete('organisation-{org_id}-name') def update_organisation_name(self, org_id, name): return self.update_organisation(org_id, name=name) diff --git a/app/templates/views/service-settings.html b/app/templates/views/service-settings.html index 7b7213e3a..767d611fe 100644 --- a/app/templates/views/service-settings.html +++ b/app/templates/views/service-settings.html @@ -316,7 +316,7 @@ {% call field() %} {% if current_service.organisation_id %} - {{ current_service.organisation.name }} + {{ current_service.organisation_name }} {% else %} Not set diff --git a/app/templates/withnav_template.html b/app/templates/withnav_template.html index 6c24a22fa..114bc8bdf 100644 --- a/app/templates/withnav_template.html +++ b/app/templates/withnav_template.html @@ -12,7 +12,7 @@ {% if current_service.organisation_id %} {% if current_user.platform_admin or (current_user.belongs_to_organisation(current_service.organisation_id) and current_service.live) %} - {{ current_service.organisation.name }} + {{ current_service.organisation_name }} {% endif %} {% endif %}