2022-03-30 13:48:36 +01:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
2019-05-23 17:17:26 +01:00
|
|
|
from werkzeug.utils import cached_property
|
2019-04-04 11:18:22 +01:00
|
|
|
|
2023-08-25 08:57:24 -07:00
|
|
|
from app.models import JSONModel, ModelList, SerialisedModelCollection, SortByNameMixin
|
2023-07-12 12:09:44 -04:00
|
|
|
from app.notify_client.organizations_api_client import organizations_client
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
class Organization(JSONModel, SortByNameMixin):
|
2023-08-25 09:12:23 -07:00
|
|
|
TYPE_FEDERAL = "federal"
|
|
|
|
|
TYPE_STATE = "state"
|
|
|
|
|
TYPE_OTHER = "other"
|
|
|
|
|
|
|
|
|
|
TYPE_LABELS = OrderedDict(
|
|
|
|
|
[
|
|
|
|
|
(TYPE_FEDERAL, "Federal government"),
|
|
|
|
|
(TYPE_STATE, "State government"),
|
|
|
|
|
(TYPE_OTHER, "Other"),
|
|
|
|
|
]
|
|
|
|
|
)
|
2019-08-27 15:52:42 +01:00
|
|
|
|
2019-04-04 11:18:22 +01:00
|
|
|
ALLOWED_PROPERTIES = {
|
2023-08-25 09:12:23 -07:00
|
|
|
"id",
|
|
|
|
|
"name",
|
|
|
|
|
"active",
|
|
|
|
|
"organization_type",
|
|
|
|
|
"domains",
|
|
|
|
|
"count_of_live_services",
|
|
|
|
|
"billing_contact_email_addresses",
|
|
|
|
|
"billing_contact_names",
|
|
|
|
|
"billing_reference",
|
|
|
|
|
"purchase_order_number",
|
|
|
|
|
"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({})
|
2023-07-12 12:09:44 -04:00
|
|
|
return cls(organizations_client.get_organization(org_id))
|
2019-05-23 17:17:26 +01:00
|
|
|
|
2019-06-13 14:23:02 +01:00
|
|
|
@classmethod
|
|
|
|
|
def from_domain(cls, domain):
|
2023-07-12 12:09:44 -04:00
|
|
|
return cls(organizations_client.get_organization_by_domain(domain))
|
2019-06-13 14:23:02 +01:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def from_service(cls, service_id):
|
2023-07-12 12:09:44 -04:00
|
|
|
return cls(organizations_client.get_service_organization(service_id))
|
2019-06-13 14:23:02 +01:00
|
|
|
|
2019-07-02 10:01:29 +01:00
|
|
|
@classmethod
|
|
|
|
|
def create_from_form(cls, form):
|
|
|
|
|
return cls.create(
|
|
|
|
|
name=form.name.data,
|
2023-07-12 12:09:44 -04:00
|
|
|
organization_type=form.organization_type.data,
|
2019-07-02 10:01:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2023-07-12 12:09:44 -04:00
|
|
|
def create(cls, name, organization_type):
|
2023-08-25 09:12:23 -07:00
|
|
|
return cls(
|
|
|
|
|
organizations_client.create_organization(
|
|
|
|
|
name=name,
|
|
|
|
|
organization_type=organization_type,
|
|
|
|
|
)
|
|
|
|
|
)
|
2019-07-02 10:01:29 +01:00
|
|
|
|
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.domains = []
|
2023-07-12 12:09:44 -04:00
|
|
|
self.organization_type = None
|
2019-04-04 11:18:22 +01:00
|
|
|
|
2019-08-27 16:32:30 +01:00
|
|
|
@property
|
2023-07-12 12:09:44 -04:00
|
|
|
def organization_type_label(self):
|
|
|
|
|
return self.TYPE_LABELS.get(self.organization_type)
|
2019-08-27 16:32:30 +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,
|
2023-08-25 09:12:23 -07:00
|
|
|
self.purchase_order_number,
|
2021-02-05 10:57:46 +00:00
|
|
|
]
|
|
|
|
|
if any(billing_details):
|
|
|
|
|
return billing_details
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
2019-05-23 17:17:26 +01:00
|
|
|
@cached_property
|
|
|
|
|
def services(self):
|
2023-07-12 12:09:44 -04:00
|
|
|
return organizations_client.get_organization_services(self.id)
|
2019-05-23 17:17:26 +01:00
|
|
|
|
2019-10-03 11:57:24 +01:00
|
|
|
@cached_property
|
|
|
|
|
def service_ids(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
return [s["id"] for s in self.services]
|
2019-10-03 11:57:24 +01:00
|
|
|
|
2019-05-23 17:17:26 +01:00
|
|
|
@property
|
|
|
|
|
def live_services(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
return [s for s in self.services if s["active"] and not s["restricted"]]
|
2019-05-23 17:17:26 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def trial_services(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
return [s for s in self.services if not s["active"] or s["restricted"]]
|
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 invited_users(self):
|
2023-07-12 12:09:44 -04:00
|
|
|
from app.models.user import OrganizationInvitedUsers
|
2023-08-25 09:12:23 -07:00
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
return OrganizationInvitedUsers(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):
|
2023-07-12 12:09:44 -04:00
|
|
|
from app.models.user import OrganizationUsers
|
2023-08-25 09:12:23 -07:00
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
return OrganizationUsers(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-10-03 11:57:24 +01:00
|
|
|
def update(self, delete_services_cache=False, **kwargs):
|
2023-07-12 12:09:44 -04:00
|
|
|
response = organizations_client.update_organization(
|
2019-10-03 11:57:24 +01:00
|
|
|
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):
|
2023-08-25 09:12:23 -07:00
|
|
|
organizations_client.update_service_organization(service_id, self.id)
|
2019-09-04 16:27:25 +01:00
|
|
|
|
2020-02-28 12:32:47 +00:00
|
|
|
def services_and_usage(self, financial_year):
|
2023-07-12 12:09:44 -04:00
|
|
|
return organizations_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
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
class Organizations(SerialisedModelCollection):
|
|
|
|
|
model = Organization
|
2021-09-28 09:04:21 +01:00
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
class AllOrganizations(ModelList, Organizations):
|
|
|
|
|
client_method = organizations_client.get_organizations
|