mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-04-25 11:40:58 -04:00
This should make the ‘All organisations’ page load a lil’ bit quicker. Still worth caching the domains separately so the response is smaller when we only care about domains. This is because the code that uses the domains is part of the sign up flow, so it’s really important that it’s snappy.
81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
from itertools import chain
|
|
|
|
from notifications_python_client.errors import HTTPError
|
|
|
|
from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache
|
|
|
|
|
|
class OrganisationsClient(NotifyAdminAPIClient):
|
|
|
|
@cache.set('organisations')
|
|
def get_organisations(self):
|
|
return self.get(url='/organisations')
|
|
|
|
@cache.set('domains')
|
|
def get_domains(self):
|
|
return list(chain.from_iterable(
|
|
organisation['domains']
|
|
for organisation in self.get_organisations()
|
|
))
|
|
|
|
def get_organisation(self, org_id):
|
|
return self.get(url='/organisations/{}'.format(org_id))
|
|
|
|
def get_organisation_by_domain(self, domain):
|
|
try:
|
|
return self.get(
|
|
url='/organisations/by-domain?domain={}'.format(domain),
|
|
)
|
|
except HTTPError as error:
|
|
if error.status_code == 404:
|
|
return None
|
|
raise error
|
|
|
|
@cache.delete('organisations')
|
|
def create_organisation(self, name):
|
|
data = {
|
|
"name": name
|
|
}
|
|
return self.post(url="/organisations", data=data)
|
|
|
|
@cache.delete('domains')
|
|
@cache.delete('organisations')
|
|
def update_organisation(self, org_id, **kwargs):
|
|
return self.post(url="/organisations/{}".format(org_id), data=kwargs)
|
|
|
|
def update_organisation_name(self, org_id, name):
|
|
return self.update_organisation(org_id, name=name)
|
|
|
|
def get_service_organisation(self, service_id):
|
|
return self.get(url="/service/{}/organisation".format(service_id))
|
|
|
|
@cache.delete('service-{service_id}')
|
|
@cache.delete('live-service-and-organisation-counts')
|
|
def update_service_organisation(self, service_id, org_id):
|
|
data = {
|
|
'service_id': service_id
|
|
}
|
|
return self.post(
|
|
url="/organisations/{}/service".format(org_id),
|
|
data=data
|
|
)
|
|
|
|
def get_organisation_services(self, org_id):
|
|
return self.get(url="/organisations/{}/services".format(org_id))
|
|
|
|
def remove_user_from_organisation(self, org_id, user_id):
|
|
endpoint = '/organisations/{}/users/{}'.format(
|
|
org_id=org_id,
|
|
user_id=user_id)
|
|
data = _attach_current_user({})
|
|
return self.delete(endpoint, data)
|
|
|
|
def is_organisation_name_unique(self, org_id, name):
|
|
return self.get(
|
|
url="/organisations/unique",
|
|
params={"org_id": org_id, "name": name}
|
|
)["result"]
|
|
|
|
|
|
organisations_client = OrganisationsClient()
|