2019-07-12 09:03:35 +01:00
|
|
|
|
from flask import abort
|
2019-05-23 17:17:26 +01:00
|
|
|
|
from werkzeug.utils import cached_property
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
2021-11-11 14:35:55 +00:00
|
|
|
|
from app.models import (
|
|
|
|
|
|
JSONModel,
|
|
|
|
|
|
ModelList,
|
|
|
|
|
|
SerialisedModelCollection,
|
|
|
|
|
|
SortByNameMixin,
|
|
|
|
|
|
)
|
2019-09-12 11:49:31 +01:00
|
|
|
|
from app.notify_client.email_branding_client import email_branding_client
|
|
|
|
|
|
from app.notify_client.letter_branding_client import letter_branding_client
|
2019-05-23 17:17:26 +01:00
|
|
|
|
from app.notify_client.organisations_api_client import organisations_client
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
2021-11-11 14:35:55 +00:00
|
|
|
|
class Organisation(JSONModel, SortByNameMixin):
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
2019-09-12 15:03:32 +01:00
|
|
|
|
TYPE_CENTRAL = 'central'
|
|
|
|
|
|
TYPE_LOCAL = 'local'
|
|
|
|
|
|
TYPE_NHS_CENTRAL = 'nhs_central'
|
|
|
|
|
|
TYPE_NHS_LOCAL = 'nhs_local'
|
|
|
|
|
|
TYPE_NHS_GP = 'nhs_gp'
|
|
|
|
|
|
TYPE_EMERGENCY_SERVICE = 'emergency_service'
|
|
|
|
|
|
TYPE_SCHOOL_OR_COLLEGE = 'school_or_college'
|
|
|
|
|
|
TYPE_OTHER = 'other'
|
|
|
|
|
|
|
2022-03-22 17:01:54 +00:00
|
|
|
|
NHS_TYPES = (
|
2019-09-12 15:03:32 +01:00
|
|
|
|
(TYPE_NHS_CENTRAL, 'NHS – central government agency or public body'),
|
|
|
|
|
|
(TYPE_NHS_LOCAL, 'NHS Trust or Clinical Commissioning Group'),
|
|
|
|
|
|
(TYPE_NHS_GP, 'GP practice'),
|
2022-03-22 17:01:54 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
TYPES = (
|
|
|
|
|
|
(TYPE_CENTRAL, 'Central government'),
|
|
|
|
|
|
(TYPE_LOCAL, 'Local government'),
|
|
|
|
|
|
) + NHS_TYPES + (
|
2019-09-12 15:03:32 +01:00
|
|
|
|
(TYPE_EMERGENCY_SERVICE, 'Emergency service'),
|
|
|
|
|
|
(TYPE_SCHOOL_OR_COLLEGE, 'School or college'),
|
|
|
|
|
|
(TYPE_OTHER, 'Other'),
|
2019-08-27 15:52:42 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2019-04-04 11:18:22 +01:00
|
|
|
|
ALLOWED_PROPERTIES = {
|
|
|
|
|
|
'id',
|
|
|
|
|
|
'name',
|
|
|
|
|
|
'active',
|
|
|
|
|
|
'crown',
|
|
|
|
|
|
'organisation_type',
|
|
|
|
|
|
'letter_branding_id',
|
|
|
|
|
|
'email_branding_id',
|
|
|
|
|
|
'agreement_signed',
|
|
|
|
|
|
'agreement_signed_at',
|
|
|
|
|
|
'agreement_signed_by_id',
|
|
|
|
|
|
'agreement_signed_version',
|
2019-06-18 14:24:29 +01:00
|
|
|
|
'agreement_signed_on_behalf_of_name',
|
|
|
|
|
|
'agreement_signed_on_behalf_of_email_address',
|
2019-04-04 11:18:22 +01:00
|
|
|
|
'domains',
|
2019-05-13 14:50:40 +01:00
|
|
|
|
'request_to_go_live_notes',
|
2019-06-12 12:09:26 +01:00
|
|
|
|
'count_of_live_services',
|
2021-02-05 10:57:46 +00:00
|
|
|
|
'billing_contact_email_addresses',
|
|
|
|
|
|
'billing_contact_names',
|
|
|
|
|
|
'billing_reference',
|
|
|
|
|
|
'purchase_order_number',
|
2021-02-04 18:00:41 +00:00
|
|
|
|
'notes',
|
2019-04-04 11:18:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-23 17:17:26 +01:00
|
|
|
|
@classmethod
|
|
|
|
|
|
def from_id(cls, org_id):
|
2020-03-20 08:42:33 +00:00
|
|
|
|
if not org_id:
|
|
|
|
|
|
return cls({})
|
2019-05-23 17:17:26 +01:00
|
|
|
|
return cls(organisations_client.get_organisation(org_id))
|
|
|
|
|
|
|
2019-06-13 14:23:02 +01:00
|
|
|
|
@classmethod
|
|
|
|
|
|
def from_domain(cls, domain):
|
|
|
|
|
|
return cls(organisations_client.get_organisation_by_domain(domain))
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def from_service(cls, service_id):
|
|
|
|
|
|
return cls(organisations_client.get_service_organisation(service_id))
|
|
|
|
|
|
|
2019-07-02 10:01:29 +01:00
|
|
|
|
@classmethod
|
|
|
|
|
|
def create_from_form(cls, form):
|
|
|
|
|
|
return cls.create(
|
|
|
|
|
|
name=form.name.data,
|
|
|
|
|
|
crown={
|
|
|
|
|
|
'crown': True,
|
|
|
|
|
|
'non-crown': False,
|
|
|
|
|
|
'unknown': None,
|
|
|
|
|
|
}.get(form.crown_status.data),
|
|
|
|
|
|
organisation_type=form.organisation_type.data,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def create(cls, name, crown, organisation_type, agreement_signed=False):
|
|
|
|
|
|
return cls(organisations_client.create_organisation(
|
|
|
|
|
|
name=name,
|
|
|
|
|
|
crown=crown,
|
|
|
|
|
|
organisation_type=organisation_type,
|
|
|
|
|
|
agreement_signed=agreement_signed,
|
|
|
|
|
|
))
|
|
|
|
|
|
|
2019-04-04 11:18:22 +01:00
|
|
|
|
def __init__(self, _dict):
|
|
|
|
|
|
|
|
|
|
|
|
super().__init__(_dict)
|
|
|
|
|
|
|
|
|
|
|
|
if self._dict == {}:
|
2019-05-03 13:35:52 +01:00
|
|
|
|
self.name = None
|
|
|
|
|
|
self.crown = None
|
|
|
|
|
|
self.agreement_signed = None
|
|
|
|
|
|
self.domains = []
|
|
|
|
|
|
self.organisation_type = None
|
2019-05-13 14:50:40 +01:00
|
|
|
|
self.request_to_go_live_notes = None
|
2019-09-12 13:45:35 +01:00
|
|
|
|
self.email_branding_id = None
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
2019-08-27 16:32:30 +01:00
|
|
|
|
@property
|
|
|
|
|
|
def organisation_type_label(self):
|
|
|
|
|
|
return dict(self.TYPES).get(self.organisation_type)
|
|
|
|
|
|
|
2019-04-04 11:18:22 +01:00
|
|
|
|
@property
|
|
|
|
|
|
def crown_status_or_404(self):
|
2019-04-04 15:02:30 +01:00
|
|
|
|
if self.crown is None:
|
2019-04-04 11:18:22 +01:00
|
|
|
|
abort(404)
|
2019-04-04 15:02:30 +01:00
|
|
|
|
return self.crown
|
2019-05-23 17:17:26 +01:00
|
|
|
|
|
2021-02-05 10:57:46 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def billing_details(self):
|
|
|
|
|
|
billing_details = [
|
|
|
|
|
|
self.billing_contact_email_addresses,
|
|
|
|
|
|
self.billing_contact_names,
|
|
|
|
|
|
self.billing_reference,
|
|
|
|
|
|
self.purchase_order_number
|
|
|
|
|
|
]
|
|
|
|
|
|
if any(billing_details):
|
|
|
|
|
|
return billing_details
|
|
|
|
|
|
else:
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
2019-05-23 17:17:26 +01:00
|
|
|
|
@cached_property
|
|
|
|
|
|
def services(self):
|
|
|
|
|
|
return organisations_client.get_organisation_services(self.id)
|
|
|
|
|
|
|
2019-10-03 11:57:24 +01:00
|
|
|
|
@cached_property
|
|
|
|
|
|
def service_ids(self):
|
|
|
|
|
|
return [s['id'] for s in self.services]
|
|
|
|
|
|
|
2019-05-23 17:17:26 +01:00
|
|
|
|
@property
|
|
|
|
|
|
def live_services(self):
|
|
|
|
|
|
return [s for s in self.services if s['active'] and not s['restricted']]
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def trial_services(self):
|
|
|
|
|
|
return [s for s in self.services if not s['active'] or s['restricted']]
|
|
|
|
|
|
|
|
|
|
|
|
@cached_property
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
def invited_users(self):
|
|
|
|
|
|
from app.models.user import OrganisationInvitedUsers
|
|
|
|
|
|
return OrganisationInvitedUsers(self.id)
|
2019-05-23 17:17:26 +01:00
|
|
|
|
|
|
|
|
|
|
@cached_property
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
def active_users(self):
|
|
|
|
|
|
from app.models.user import OrganisationUsers
|
|
|
|
|
|
return OrganisationUsers(self.id)
|
2019-05-23 17:17:26 +01:00
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
|
def team_members(self):
|
|
|
|
|
|
return sorted(
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
self.invited_users + self.active_users,
|
|
|
|
|
|
key=lambda user: user.email_address.lower(),
|
2019-05-23 17:17:26 +01:00
|
|
|
|
)
|
2019-06-12 13:40:57 +01:00
|
|
|
|
|
2019-09-12 11:49:31 +01:00
|
|
|
|
@cached_property
|
|
|
|
|
|
def email_branding(self):
|
|
|
|
|
|
if self.email_branding_id:
|
|
|
|
|
|
return email_branding_client.get_email_branding(
|
|
|
|
|
|
self.email_branding_id
|
|
|
|
|
|
)['email_branding']
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def email_branding_name(self):
|
|
|
|
|
|
if self.email_branding_id:
|
|
|
|
|
|
return self.email_branding['name']
|
|
|
|
|
|
return 'GOV.UK'
|
|
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
|
def letter_branding(self):
|
|
|
|
|
|
if self.letter_branding_id:
|
|
|
|
|
|
return letter_branding_client.get_letter_branding(
|
|
|
|
|
|
self.letter_branding_id
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-10-11 14:58:28 +01:00
|
|
|
|
@cached_property
|
|
|
|
|
|
def agreement_signed_by(self):
|
|
|
|
|
|
if self.agreement_signed_by_id:
|
|
|
|
|
|
from app.models.user import User
|
|
|
|
|
|
return User.from_id(self.agreement_signed_by_id)
|
|
|
|
|
|
|
2019-10-03 11:57:24 +01:00
|
|
|
|
def update(self, delete_services_cache=False, **kwargs):
|
|
|
|
|
|
response = organisations_client.update_organisation(
|
|
|
|
|
|
self.id,
|
|
|
|
|
|
cached_service_ids=self.service_ids if delete_services_cache else None,
|
|
|
|
|
|
**kwargs
|
|
|
|
|
|
)
|
2019-06-18 14:24:29 +01:00
|
|
|
|
self.__init__(response)
|
|
|
|
|
|
|
2019-09-04 16:27:25 +01:00
|
|
|
|
def associate_service(self, service_id):
|
|
|
|
|
|
organisations_client.update_service_organisation(
|
2019-11-04 12:20:09 +00:00
|
|
|
|
service_id,
|
2019-09-04 16:27:25 +01:00
|
|
|
|
self.id
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2020-02-28 12:32:47 +00:00
|
|
|
|
def services_and_usage(self, financial_year):
|
|
|
|
|
|
return organisations_client.get_services_and_usage(self.id, financial_year)
|
2020-02-25 17:49:55 +00:00
|
|
|
|
|
2019-06-12 13:40:57 +01:00
|
|
|
|
|
2021-09-28 09:04:21 +01:00
|
|
|
|
class Organisations(SerialisedModelCollection):
|
2019-06-12 13:40:57 +01:00
|
|
|
|
model = Organisation
|
2021-09-28 09:04:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AllOrganisations(ModelList, Organisations):
|
|
|
|
|
|
client_method = organisations_client.get_organisations
|