2020-01-21 16:50:44 +00:00
|
|
|
from flask import abort, current_app
|
2018-10-26 15:58:44 +01:00
|
|
|
from werkzeug.utils import cached_property
|
|
|
|
|
|
2019-04-04 11:18:22 +01:00
|
|
|
from app.models import JSONModel
|
2020-03-16 10:14:11 +00:00
|
|
|
from app.models.contact_list import ContactLists
|
2020-01-08 14:29:56 +00:00
|
|
|
from app.models.job import (
|
|
|
|
|
ImmediateJobs,
|
|
|
|
|
PaginatedJobs,
|
|
|
|
|
PaginatedUploads,
|
|
|
|
|
ScheduledJobs,
|
|
|
|
|
)
|
2019-04-04 11:18:22 +01:00
|
|
|
from app.models.organisation import Organisation
|
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
|
|
|
from app.models.user import InvitedUsers, User, Users
|
2018-11-07 11:35:24 +00:00
|
|
|
from app.notify_client.api_key_api_client import api_key_api_client
|
2018-10-26 17:31:49 +01:00
|
|
|
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
|
2018-12-03 10:59:36 +00:00
|
|
|
from app.notify_client.invite_api_client import invite_api_client
|
2018-10-26 16:09:59 +01:00
|
|
|
from app.notify_client.job_api_client import job_api_client
|
2019-01-23 12:27:14 +00:00
|
|
|
from app.notify_client.letter_branding_client import letter_branding_client
|
2020-03-20 08:42:33 +00:00
|
|
|
from app.notify_client.organisations_api_client import organisations_client
|
2018-10-26 16:09:59 +01:00
|
|
|
from app.notify_client.service_api_client import service_api_client
|
2018-11-01 17:11:58 +00:00
|
|
|
from app.notify_client.template_folder_api_client import (
|
|
|
|
|
template_folder_api_client,
|
|
|
|
|
)
|
2018-10-26 15:58:44 +01:00
|
|
|
from app.utils import get_default_sms_sender
|
|
|
|
|
|
|
|
|
|
|
2019-04-04 11:18:22 +01:00
|
|
|
class Service(JSONModel):
|
2018-10-26 15:58:44 +01:00
|
|
|
|
|
|
|
|
ALLOWED_PROPERTIES = {
|
|
|
|
|
'active',
|
|
|
|
|
'contact_link',
|
|
|
|
|
'email_branding',
|
|
|
|
|
'email_from',
|
|
|
|
|
'id',
|
|
|
|
|
'inbound_api',
|
2019-02-01 16:34:54 +00:00
|
|
|
'letter_branding',
|
2018-10-26 15:58:44 +01:00
|
|
|
'letter_contact_block',
|
|
|
|
|
'message_limit',
|
|
|
|
|
'name',
|
|
|
|
|
'permissions',
|
|
|
|
|
'prefix_sms',
|
|
|
|
|
'research_mode',
|
|
|
|
|
'service_callback_api',
|
2019-02-15 11:03:19 +00:00
|
|
|
'volume_email',
|
|
|
|
|
'volume_sms',
|
|
|
|
|
'volume_letter',
|
|
|
|
|
'consent_to_research',
|
2019-03-25 14:46:42 +00:00
|
|
|
'count_as_live',
|
2019-04-16 15:01:54 +01:00
|
|
|
'go_live_user',
|
|
|
|
|
'go_live_at'
|
2018-10-26 15:58:44 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-08 11:54:57 +00:00
|
|
|
TEMPLATE_TYPES = (
|
2018-11-12 09:16:14 +00:00
|
|
|
'email',
|
2019-02-22 17:11:12 +00:00
|
|
|
'sms',
|
2018-11-12 09:16:14 +00:00
|
|
|
'letter',
|
2018-11-08 11:54:57 +00:00
|
|
|
)
|
|
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
def __init__(self, _dict):
|
|
|
|
|
|
2019-04-04 11:18:22 +01:00
|
|
|
super().__init__(_dict)
|
|
|
|
|
if 'permissions' not in self._dict:
|
|
|
|
|
self.permissions = {'email', 'sms', 'letter'}
|
2018-11-14 10:16:55 +00:00
|
|
|
|
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
|
|
|
@classmethod
|
|
|
|
|
def from_id(cls, service_id):
|
|
|
|
|
return cls(service_api_client.get_service(service_id)['data'])
|
|
|
|
|
|
2018-11-05 16:03:11 +00:00
|
|
|
def update(self, **kwargs):
|
|
|
|
|
return service_api_client.update_service(self.id, **kwargs)
|
|
|
|
|
|
2019-07-08 14:32:18 +01:00
|
|
|
def update_count_as_live(self, count_as_live):
|
|
|
|
|
return service_api_client.update_count_as_live(self.id, count_as_live=count_as_live)
|
|
|
|
|
|
2019-04-10 17:20:51 +01:00
|
|
|
def update_status(self, live):
|
|
|
|
|
return service_api_client.update_status(self.id, live=live)
|
|
|
|
|
|
2018-11-05 17:03:37 +00:00
|
|
|
def switch_permission(self, permission):
|
2018-11-05 16:53:03 +00:00
|
|
|
return self.force_permission(
|
|
|
|
|
permission,
|
|
|
|
|
on=not self.has_permission(permission),
|
|
|
|
|
)
|
|
|
|
|
|
2018-11-05 17:03:37 +00:00
|
|
|
def force_permission(self, permission, on=False):
|
2018-11-05 16:53:03 +00:00
|
|
|
|
|
|
|
|
permissions, permission = set(self.permissions), {permission}
|
|
|
|
|
|
|
|
|
|
return self.update_permissions(
|
|
|
|
|
permissions | permission if on else permissions - permission,
|
|
|
|
|
)
|
|
|
|
|
|
2018-11-05 17:03:37 +00:00
|
|
|
def update_permissions(self, permissions):
|
2018-11-05 17:37:50 +00:00
|
|
|
return self.update(permissions=list(permissions))
|
2018-11-05 16:53:03 +00:00
|
|
|
|
2018-11-05 16:17:44 +00:00
|
|
|
def toggle_research_mode(self):
|
2018-11-05 17:37:50 +00:00
|
|
|
self.update(research_mode=not self.research_mode)
|
2018-11-05 16:17:44 +00:00
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
@property
|
|
|
|
|
def trial_mode(self):
|
|
|
|
|
return self._dict['restricted']
|
|
|
|
|
|
2019-06-07 12:38:48 +01:00
|
|
|
@property
|
|
|
|
|
def live(self):
|
|
|
|
|
return not self.trial_mode
|
|
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
def has_permission(self, permission):
|
|
|
|
|
return permission in self.permissions
|
|
|
|
|
|
2020-01-08 14:29:56 +00:00
|
|
|
def get_page_of_jobs(self, page):
|
|
|
|
|
return PaginatedJobs(self.id, page=page)
|
|
|
|
|
|
|
|
|
|
def get_page_of_uploads(self, page):
|
|
|
|
|
return PaginatedUploads(self.id, page=page)
|
|
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
@cached_property
|
|
|
|
|
def has_jobs(self):
|
|
|
|
|
return job_api_client.has_jobs(self.id)
|
|
|
|
|
|
2020-01-08 14:29:56 +00:00
|
|
|
@cached_property
|
|
|
|
|
def immediate_jobs(self):
|
|
|
|
|
if not self.has_jobs:
|
|
|
|
|
return []
|
|
|
|
|
return ImmediateJobs(self.id)
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def scheduled_jobs(self):
|
|
|
|
|
if not self.has_jobs:
|
|
|
|
|
return []
|
|
|
|
|
return ScheduledJobs(self.id)
|
|
|
|
|
|
2019-02-25 16:51:37 +00:00
|
|
|
@cached_property
|
|
|
|
|
def invited_users(self):
|
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
|
|
|
return InvitedUsers(self.id)
|
2019-02-25 16:51:37 +00:00
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def active_users(self):
|
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
|
|
|
return Users(self.id)
|
2019-02-25 16:51:37 +00:00
|
|
|
|
2018-12-03 10:59:36 +00:00
|
|
|
@cached_property
|
|
|
|
|
def team_members(self):
|
2018-12-05 16:39:15 +00:00
|
|
|
return sorted(
|
2019-02-25 16:51:37 +00:00
|
|
|
self.invited_users + self.active_users,
|
2019-02-01 13:09:02 +00:00
|
|
|
key=lambda user: user.email_address.lower(),
|
2018-12-05 16:39:15 +00:00
|
|
|
)
|
2018-12-03 10:59:36 +00:00
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
@cached_property
|
|
|
|
|
def has_team_members(self):
|
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
|
|
|
return len([
|
|
|
|
|
user for user in self.team_members
|
|
|
|
|
if user.has_permission_for_service(self.id, 'manage_service')
|
|
|
|
|
]) > 1
|
2018-10-26 15:58:44 +01:00
|
|
|
|
2019-02-25 16:51:37 +00:00
|
|
|
def cancel_invite(self, invited_user_id):
|
|
|
|
|
if str(invited_user_id) not in {user.id for user in self.invited_users}:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
return invite_api_client.cancel_invited_user(
|
|
|
|
|
service_id=self.id,
|
|
|
|
|
invited_user_id=str(invited_user_id),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def get_team_member(self, user_id):
|
|
|
|
|
|
|
|
|
|
if str(user_id) not in {user.id for user in self.active_users}:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
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
|
|
|
return User.from_id(user_id)
|
2019-02-25 16:51:37 +00:00
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
@cached_property
|
2018-11-05 15:26:59 +00:00
|
|
|
def all_templates(self):
|
2018-10-26 15:58:44 +01:00
|
|
|
|
|
|
|
|
templates = service_api_client.get_service_templates(self.id)['data']
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
template for template in templates
|
|
|
|
|
if template['template_type'] in self.available_template_types
|
|
|
|
|
]
|
|
|
|
|
|
2018-11-08 11:42:01 +00:00
|
|
|
@cached_property
|
|
|
|
|
def all_template_ids(self):
|
|
|
|
|
return {template['id'] for template in self.all_templates}
|
|
|
|
|
|
2019-03-11 14:13:56 +00:00
|
|
|
def get_templates(self, template_type='all', template_folder_id=None, user=None):
|
2019-04-02 15:23:46 +01:00
|
|
|
if user and template_folder_id:
|
2019-03-07 11:05:44 +00:00
|
|
|
folder = self.get_template_folder(template_folder_id)
|
2019-03-11 14:13:56 +00:00
|
|
|
if not user.has_template_folder_permission(folder):
|
2019-03-07 11:05:44 +00:00
|
|
|
return []
|
|
|
|
|
|
2018-10-26 16:30:37 +01:00
|
|
|
if isinstance(template_type, str):
|
|
|
|
|
template_type = [template_type]
|
2019-01-07 14:49:33 +00:00
|
|
|
if template_folder_id:
|
|
|
|
|
template_folder_id = str(template_folder_id)
|
2018-10-26 15:58:44 +01:00
|
|
|
return [
|
2018-11-05 15:26:59 +00:00
|
|
|
template for template in self.all_templates
|
|
|
|
|
if (set(template_type) & {'all', template['template_type']})
|
2018-11-08 15:53:33 +00:00
|
|
|
and template.get('folder') == template_folder_id
|
2018-10-26 15:58:44 +01:00
|
|
|
]
|
|
|
|
|
|
2019-03-19 15:59:59 +00:00
|
|
|
def get_template(self, template_id, version=None):
|
2019-11-04 12:20:09 +00:00
|
|
|
return service_api_client.get_service_template(self.id, template_id, version)['data']
|
2019-03-19 15:59:59 +00:00
|
|
|
|
2019-03-20 17:26:51 +00:00
|
|
|
def get_template_folder_with_user_permission_or_403(self, folder_id, user):
|
|
|
|
|
template_folder = self.get_template_folder(folder_id)
|
2019-03-19 15:59:59 +00:00
|
|
|
|
|
|
|
|
if not user.has_template_folder_permission(template_folder):
|
|
|
|
|
abort(403)
|
|
|
|
|
|
2019-03-20 17:26:51 +00:00
|
|
|
return template_folder
|
|
|
|
|
|
|
|
|
|
def get_template_with_user_permission_or_403(self, template_id, user):
|
|
|
|
|
template = self.get_template(template_id)
|
|
|
|
|
|
|
|
|
|
self.get_template_folder_with_user_permission_or_403(template['folder'], user)
|
|
|
|
|
|
2019-03-19 15:59:59 +00:00
|
|
|
return template
|
|
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
@property
|
|
|
|
|
def available_template_types(self):
|
2018-11-12 09:16:14 +00:00
|
|
|
return list(filter(self.has_permission, self.TEMPLATE_TYPES))
|
2018-11-08 11:54:57 +00:00
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
@property
|
|
|
|
|
def has_templates(self):
|
2018-11-19 16:52:21 +00:00
|
|
|
return bool(self.all_templates)
|
|
|
|
|
|
|
|
|
|
def has_folders(self):
|
|
|
|
|
return bool(self.all_template_folders)
|
2018-10-26 15:58:44 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def has_multiple_template_types(self):
|
|
|
|
|
return len({
|
2018-11-05 15:26:59 +00:00
|
|
|
template['template_type'] for template in self.all_templates
|
2018-10-26 15:58:44 +01:00
|
|
|
}) > 1
|
|
|
|
|
|
2019-02-15 11:03:19 +00:00
|
|
|
@property
|
|
|
|
|
def has_estimated_usage(self):
|
|
|
|
|
return (
|
|
|
|
|
self.consent_to_research is not None and any((
|
|
|
|
|
self.volume_email,
|
|
|
|
|
self.volume_sms,
|
|
|
|
|
self.volume_letter,
|
|
|
|
|
))
|
|
|
|
|
)
|
|
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
@property
|
|
|
|
|
def has_email_templates(self):
|
2018-11-05 15:26:59 +00:00
|
|
|
return len(self.get_templates('email')) > 0
|
2018-10-26 15:58:44 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def has_sms_templates(self):
|
2018-11-05 15:26:59 +00:00
|
|
|
return len(self.get_templates('sms')) > 0
|
2018-10-26 15:58:44 +01:00
|
|
|
|
2019-03-08 14:26:39 +00:00
|
|
|
@property
|
|
|
|
|
def intending_to_send_email(self):
|
|
|
|
|
if self.volume_email is None:
|
|
|
|
|
return self.has_email_templates
|
|
|
|
|
return self.volume_email > 0
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def intending_to_send_sms(self):
|
|
|
|
|
if self.volume_sms is None:
|
|
|
|
|
return self.has_sms_templates
|
|
|
|
|
return self.volume_sms > 0
|
|
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
@cached_property
|
2018-10-26 17:31:49 +01:00
|
|
|
def email_reply_to_addresses(self):
|
|
|
|
|
return service_api_client.get_reply_to_email_addresses(self.id)
|
|
|
|
|
|
|
|
|
|
@property
|
2018-10-26 15:58:44 +01:00
|
|
|
def has_email_reply_to_address(self):
|
2018-10-26 17:31:49 +01:00
|
|
|
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)
|
2018-10-26 15:58:44 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def needs_to_add_email_reply_to_address(self):
|
2019-03-08 14:26:39 +00:00
|
|
|
return self.intending_to_send_email and not self.has_email_reply_to_address
|
2018-10-26 15:58:44 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def shouldnt_use_govuk_as_sms_sender(self):
|
2019-09-12 15:03:32 +01:00
|
|
|
return self.organisation_type != Organisation.TYPE_CENTRAL
|
2018-10-26 15:58:44 +01:00
|
|
|
|
|
|
|
|
@cached_property
|
2018-10-26 17:31:49 +01:00
|
|
|
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
|
2018-10-26 15:58:44 +01:00
|
|
|
def sms_sender_is_govuk(self):
|
2018-10-26 17:31:49 +01:00
|
|
|
return self.default_sms_sender in {'GOVUK', 'None'}
|
|
|
|
|
|
|
|
|
|
def get_sms_sender(self, id):
|
|
|
|
|
return service_api_client.get_sms_sender(self.id, id)
|
2018-10-26 15:58:44 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def needs_to_change_sms_sender(self):
|
|
|
|
|
return all((
|
2019-03-08 14:26:39 +00:00
|
|
|
self.intending_to_send_sms,
|
2018-10-26 15:58:44 +01:00
|
|
|
self.shouldnt_use_govuk_as_sms_sender,
|
|
|
|
|
self.sms_sender_is_govuk,
|
|
|
|
|
))
|
|
|
|
|
|
2018-10-26 17:31:49 +01:00
|
|
|
@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(
|
|
|
|
|
(
|
2019-07-05 13:17:48 +01:00
|
|
|
letter_contact_block
|
|
|
|
|
for letter_contact_block in self.letter_contact_details
|
|
|
|
|
if letter_contact_block['is_default']
|
2018-10-26 17:31:49 +01:00
|
|
|
), None
|
|
|
|
|
)
|
|
|
|
|
|
2019-07-05 13:17:48 +01:00
|
|
|
@property
|
|
|
|
|
def default_letter_contact_block_html(self):
|
2020-01-21 16:50:44 +00:00
|
|
|
# import in the function to prevent cyclical imports
|
|
|
|
|
from app import nl2br
|
|
|
|
|
|
2019-07-05 13:17:48 +01:00
|
|
|
if self.default_letter_contact_block:
|
2020-01-21 16:50:44 +00:00
|
|
|
return nl2br(self.default_letter_contact_block['contact_block'])
|
2019-07-05 13:17:48 +01:00
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
def edit_letter_contact_block(self, id, contact_block, is_default):
|
|
|
|
|
service_api_client.update_letter_contact(
|
|
|
|
|
self.id, letter_contact_id=id, contact_block=contact_block, is_default=is_default,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def remove_default_letter_contact_block(self):
|
|
|
|
|
if self.default_letter_contact_block:
|
|
|
|
|
self.edit_letter_contact_block(
|
|
|
|
|
self.default_letter_contact_block['id'],
|
|
|
|
|
self.default_letter_contact_block['contact_block'],
|
|
|
|
|
is_default=False,
|
|
|
|
|
)
|
|
|
|
|
|
2018-10-26 17:31:49 +01:00
|
|
|
def get_letter_contact_block(self, id):
|
|
|
|
|
return service_api_client.get_letter_contact(self.id, id)
|
|
|
|
|
|
2019-02-27 17:15:09 +00:00
|
|
|
@property
|
|
|
|
|
def volumes(self):
|
|
|
|
|
return sum(filter(None, (
|
|
|
|
|
self.volume_email,
|
|
|
|
|
self.volume_sms,
|
|
|
|
|
self.volume_letter,
|
|
|
|
|
)))
|
|
|
|
|
|
2018-10-26 15:58:44 +01:00
|
|
|
@property
|
|
|
|
|
def go_live_checklist_completed(self):
|
|
|
|
|
return all((
|
2019-02-27 17:15:09 +00:00
|
|
|
bool(self.volumes),
|
2018-10-26 15:58:44 +01:00
|
|
|
self.has_team_members,
|
|
|
|
|
self.has_templates,
|
|
|
|
|
not self.needs_to_add_email_reply_to_address,
|
|
|
|
|
not self.needs_to_change_sms_sender,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def go_live_checklist_completed_as_yes_no(self):
|
|
|
|
|
return 'Yes' if self.go_live_checklist_completed else 'No'
|
2018-10-26 17:31:49 +01:00
|
|
|
|
|
|
|
|
@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):
|
2018-12-03 17:19:38 +00:00
|
|
|
return next(
|
|
|
|
|
(dr for dr in self.data_retention if dr['id'] == id),
|
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
|
2018-12-03 17:57:11 +00:00
|
|
|
def get_days_of_retention(self, notification_type):
|
2018-12-03 17:19:38 +00:00
|
|
|
return next(
|
|
|
|
|
(dr for dr in self.data_retention if dr['notification_type'] == notification_type),
|
|
|
|
|
{}
|
2018-12-03 17:57:11 +00:00
|
|
|
).get('days_of_retention', current_app.config['ACTIVITY_STATS_LIMIT_DAYS'])
|
2018-10-26 17:31:49 +01:00
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
2019-03-06 15:27:43 +00:00
|
|
|
@cached_property
|
|
|
|
|
def email_branding_name(self):
|
|
|
|
|
if self.email_branding is None:
|
|
|
|
|
return 'GOV.UK'
|
|
|
|
|
return self.email_branding['name']
|
|
|
|
|
|
2020-01-17 17:03:07 +00:00
|
|
|
@cached_property
|
|
|
|
|
def letter_branding_name(self):
|
|
|
|
|
if self.letter_branding is None:
|
|
|
|
|
return 'no'
|
|
|
|
|
return self.letter_branding['name']
|
|
|
|
|
|
2019-09-12 14:53:40 +01:00
|
|
|
@property
|
|
|
|
|
def needs_to_change_email_branding(self):
|
2019-09-12 15:03:32 +01:00
|
|
|
return self.email_branding_id is None and self.organisation_type != Organisation.TYPE_CENTRAL
|
2019-09-12 14:53:40 +01:00
|
|
|
|
2019-02-01 16:34:54 +00:00
|
|
|
@property
|
|
|
|
|
def letter_branding_id(self):
|
|
|
|
|
return self._dict['letter_branding']
|
|
|
|
|
|
2018-10-26 17:31:49 +01:00
|
|
|
@cached_property
|
|
|
|
|
def letter_branding(self):
|
2019-02-01 16:34:54 +00:00
|
|
|
if self.letter_branding_id:
|
|
|
|
|
return letter_branding_client.get_letter_branding(self.letter_branding_id)
|
|
|
|
|
return None
|
2018-10-26 17:31:49 +01:00
|
|
|
|
|
|
|
|
@cached_property
|
2019-04-04 11:18:22 +01:00
|
|
|
def organisation(self):
|
2020-03-20 08:42:33 +00:00
|
|
|
return Organisation.from_id(self.organisation_id)
|
2018-10-26 17:31:49 +01:00
|
|
|
|
2019-06-12 12:09:26 +01:00
|
|
|
@property
|
|
|
|
|
def organisation_id(self):
|
|
|
|
|
return self._dict['organisation']
|
|
|
|
|
|
2019-07-09 10:38:07 +01:00
|
|
|
@property
|
|
|
|
|
def organisation_type(self):
|
|
|
|
|
return self.organisation.organisation_type or self._dict['organisation_type']
|
|
|
|
|
|
2020-03-20 08:42:33 +00:00
|
|
|
@property
|
|
|
|
|
def organisation_name(self):
|
|
|
|
|
if not self.organisation:
|
|
|
|
|
return None
|
|
|
|
|
return organisations_client.get_organisation_name(self.organisation_id)
|
|
|
|
|
|
2019-08-27 16:32:30 +01:00
|
|
|
@property
|
|
|
|
|
def organisation_type_label(self):
|
|
|
|
|
return dict(Organisation.TYPES).get(self.organisation_type)
|
|
|
|
|
|
2018-10-26 17:31:49 +01:00
|
|
|
@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)
|
2018-11-01 15:33:09 +00:00
|
|
|
|
2020-02-12 14:03:51 +00:00
|
|
|
@cached_property
|
|
|
|
|
def inbound_sms_summary(self):
|
|
|
|
|
if not self.has_permission('inbound_sms'):
|
|
|
|
|
return None
|
|
|
|
|
return service_api_client.get_inbound_sms_summary(self.id)
|
|
|
|
|
|
2018-11-01 15:33:09 +00:00
|
|
|
@cached_property
|
2018-11-05 16:25:19 +00:00
|
|
|
def all_template_folders(self):
|
2018-11-09 16:04:49 +00:00
|
|
|
return sorted(
|
|
|
|
|
template_folder_api_client.get_template_folders(self.id),
|
|
|
|
|
key=lambda folder: folder['name'].lower(),
|
|
|
|
|
)
|
2018-11-05 16:25:19 +00:00
|
|
|
|
2018-11-08 11:42:01 +00:00
|
|
|
@cached_property
|
|
|
|
|
def all_template_folder_ids(self):
|
|
|
|
|
return {folder['id'] for folder in self.all_template_folders}
|
|
|
|
|
|
2019-03-11 14:13:56 +00:00
|
|
|
def get_user_template_folders(self, user):
|
2019-03-07 17:09:21 +00:00
|
|
|
"""Returns a modified list of folders a user has permission to view
|
|
|
|
|
|
|
|
|
|
For each folder, we do the following:
|
|
|
|
|
- if user has no permission to view the folder, skip it
|
|
|
|
|
- if folder is visible and its parent is visible, we add it to the list of folders
|
|
|
|
|
we later return without modifying anything
|
|
|
|
|
- if folder is visible, but the parent is not, we iterate through the parent until we
|
|
|
|
|
either find a visible parent or reach root folder. On each iteration we concatenate
|
|
|
|
|
invisible parent folder name to the front of our folder name, modifying the name, and we
|
|
|
|
|
change parent_folder_id attribute to a higher level parent. This flattens the path to the
|
|
|
|
|
folder making sure it displays in the closest visible parent.
|
|
|
|
|
|
|
|
|
|
"""
|
2019-03-01 16:25:15 +00:00
|
|
|
user_folders = []
|
|
|
|
|
for folder in self.all_template_folders:
|
2019-03-29 16:44:57 +00:00
|
|
|
if not user.has_template_folder_permission(folder, service=self):
|
2019-03-01 16:25:15 +00:00
|
|
|
continue
|
|
|
|
|
parent = self.get_template_folder(folder["parent_id"])
|
2019-03-29 16:44:57 +00:00
|
|
|
if user.has_template_folder_permission(parent, service=self):
|
2019-03-01 16:25:15 +00:00
|
|
|
user_folders.append(folder)
|
|
|
|
|
else:
|
|
|
|
|
folder_attrs = {
|
|
|
|
|
"id": folder["id"], "name": folder["name"], "parent_id": folder["parent_id"],
|
|
|
|
|
"users_with_permission": folder["users_with_permission"]
|
|
|
|
|
}
|
2019-03-01 17:50:36 +00:00
|
|
|
while folder_attrs["parent_id"] is not None:
|
2019-06-24 16:02:49 +01:00
|
|
|
folder_attrs["name"] = [
|
|
|
|
|
parent["name"],
|
|
|
|
|
folder_attrs["name"],
|
|
|
|
|
]
|
2019-03-01 17:50:36 +00:00
|
|
|
if parent["parent_id"] is None:
|
|
|
|
|
folder_attrs["parent_id"] = None
|
|
|
|
|
else:
|
|
|
|
|
parent = self.get_template_folder(parent["parent_id"])
|
|
|
|
|
folder_attrs["parent_id"] = parent.get("id", None)
|
2019-03-29 16:44:57 +00:00
|
|
|
if user.has_template_folder_permission(parent, service=self):
|
2019-03-01 17:50:36 +00:00
|
|
|
break
|
2019-03-01 16:25:15 +00:00
|
|
|
user_folders.append(folder_attrs)
|
|
|
|
|
return user_folders
|
|
|
|
|
|
2019-03-11 14:13:56 +00:00
|
|
|
def get_template_folders(self, template_type='all', parent_folder_id=None, user=None):
|
|
|
|
|
if user:
|
|
|
|
|
folders = self.get_user_template_folders(user)
|
2019-03-01 16:25:15 +00:00
|
|
|
else:
|
|
|
|
|
folders = self.all_template_folders
|
2019-01-07 14:49:33 +00:00
|
|
|
if parent_folder_id:
|
|
|
|
|
parent_folder_id = str(parent_folder_id)
|
|
|
|
|
|
2018-11-05 16:25:19 +00:00
|
|
|
return [
|
2019-03-01 16:25:15 +00:00
|
|
|
folder for folder in folders
|
2018-11-12 10:17:57 +00:00
|
|
|
if (
|
2019-03-01 16:25:15 +00:00
|
|
|
folder['parent_id'] == parent_folder_id
|
2019-03-11 14:13:56 +00:00
|
|
|
and self.is_folder_visible(folder['id'], template_type, user)
|
2018-11-12 10:17:57 +00:00
|
|
|
)
|
2018-11-05 16:25:19 +00:00
|
|
|
]
|
|
|
|
|
|
2018-11-13 16:05:05 +00:00
|
|
|
def get_template_folder(self, folder_id):
|
2018-11-16 13:41:55 +00:00
|
|
|
if folder_id is None:
|
|
|
|
|
return {
|
|
|
|
|
'id': None,
|
|
|
|
|
'name': 'Templates',
|
|
|
|
|
'parent_id': None,
|
|
|
|
|
}
|
2018-11-14 10:16:55 +00:00
|
|
|
return self._get_by_id(self.all_template_folders, folder_id)
|
2018-11-13 16:05:05 +00:00
|
|
|
|
2019-03-11 14:13:56 +00:00
|
|
|
def is_folder_visible(self, template_folder_id, template_type='all', user=None):
|
2018-11-12 10:17:57 +00:00
|
|
|
|
|
|
|
|
if template_type == 'all':
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
if self.get_templates(template_type, template_folder_id):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
if any(
|
2019-03-11 14:13:56 +00:00
|
|
|
self.is_folder_visible(child_folder['id'], template_type, user)
|
|
|
|
|
for child_folder in self.get_template_folders(template_type, template_folder_id, user)
|
2018-11-12 10:17:57 +00:00
|
|
|
):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
2018-11-05 16:25:19 +00:00
|
|
|
def get_template_folder_path(self, template_folder_id):
|
|
|
|
|
|
2018-11-16 13:41:55 +00:00
|
|
|
folder = self.get_template_folder(template_folder_id)
|
2018-11-05 16:25:19 +00:00
|
|
|
|
2018-11-16 13:41:55 +00:00
|
|
|
if folder['id'] is None:
|
|
|
|
|
return [folder]
|
2018-11-05 16:25:19 +00:00
|
|
|
|
2019-01-30 10:09:04 +00:00
|
|
|
return self.get_template_folder_path(folder['parent_id']) + [
|
|
|
|
|
self.get_template_folder(folder['id'])
|
2018-11-16 13:41:55 +00:00
|
|
|
]
|
2018-11-08 11:56:29 +00:00
|
|
|
|
2018-11-16 14:09:14 +00:00
|
|
|
def get_template_path(self, template):
|
2019-01-30 10:09:04 +00:00
|
|
|
return self.get_template_folder_path(template['folder']) + [
|
2018-11-16 14:09:14 +00:00
|
|
|
template,
|
|
|
|
|
]
|
|
|
|
|
|
2018-11-08 11:56:29 +00:00
|
|
|
def get_template_folders_and_templates(self, template_type, template_folder_id):
|
|
|
|
|
return (
|
2019-03-01 16:25:15 +00:00
|
|
|
self.get_templates(template_type, template_folder_id)
|
|
|
|
|
+ self.get_template_folders(template_type, template_folder_id)
|
2018-11-08 11:56:29 +00:00
|
|
|
)
|
2018-11-08 14:46:18 +00:00
|
|
|
|
2018-11-20 12:18:18 +00:00
|
|
|
@property
|
|
|
|
|
def count_of_templates_and_folders(self):
|
2018-11-20 16:10:14 +00:00
|
|
|
return len(self.all_templates + self.all_template_folders)
|
2018-11-20 12:18:18 +00:00
|
|
|
|
2018-11-08 14:46:18 +00:00
|
|
|
def move_to_folder(self, ids_to_move, move_to):
|
|
|
|
|
|
|
|
|
|
ids_to_move = set(ids_to_move)
|
|
|
|
|
|
|
|
|
|
template_folder_api_client.move_to_folder(
|
|
|
|
|
service_id=self.id,
|
|
|
|
|
folder_id=move_to,
|
|
|
|
|
template_ids=ids_to_move & self.all_template_ids,
|
|
|
|
|
folder_ids=ids_to_move & self.all_template_folder_ids,
|
|
|
|
|
)
|
2018-11-07 11:35:24 +00:00
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def api_keys(self):
|
2018-11-07 12:05:11 +00:00
|
|
|
return sorted(
|
|
|
|
|
api_key_api_client.get_api_keys(self.id)['apiKeys'],
|
|
|
|
|
key=lambda key: key['name'].lower(),
|
|
|
|
|
)
|
2018-11-07 11:35:24 +00:00
|
|
|
|
2018-11-07 11:53:29 +00:00
|
|
|
def get_api_key(self, id):
|
2018-11-14 10:16:55 +00:00
|
|
|
return self._get_by_id(self.api_keys, id)
|
2019-04-04 11:27:05 +01:00
|
|
|
|
2019-09-04 15:55:09 +01:00
|
|
|
@property
|
|
|
|
|
def able_to_accept_agreement(self):
|
|
|
|
|
return (
|
|
|
|
|
self.organisation.agreement_signed is not None
|
2019-09-12 15:03:32 +01:00
|
|
|
or self.organisation_type in {
|
|
|
|
|
Organisation.TYPE_NHS_GP,
|
|
|
|
|
Organisation.TYPE_NHS_LOCAL,
|
|
|
|
|
}
|
2019-09-04 15:55:09 +01:00
|
|
|
)
|
|
|
|
|
|
2019-04-04 11:27:05 +01:00
|
|
|
@property
|
|
|
|
|
def request_to_go_live_tags(self):
|
|
|
|
|
return list(self._get_request_to_go_live_tags())
|
|
|
|
|
|
|
|
|
|
def _get_request_to_go_live_tags(self):
|
|
|
|
|
|
2019-11-19 15:46:29 +00:00
|
|
|
BASE = 'notify_go_live'
|
2019-04-04 11:27:05 +01:00
|
|
|
|
2019-11-19 15:46:29 +00:00
|
|
|
yield 'notify_action'
|
2019-04-04 11:27:05 +01:00
|
|
|
yield BASE
|
|
|
|
|
|
|
|
|
|
if self.go_live_checklist_completed and self.organisation.agreement_signed:
|
|
|
|
|
yield BASE + '_complete'
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for test, tag in (
|
|
|
|
|
(not self.volumes, '_volumes'),
|
|
|
|
|
(not self.go_live_checklist_completed, '_checklist'),
|
|
|
|
|
(not self.organisation.agreement_signed, '_mou'),
|
|
|
|
|
(self.needs_to_add_email_reply_to_address, '_email_reply_to'),
|
|
|
|
|
(not self.has_team_members, '_team_member'),
|
|
|
|
|
(not self.has_templates, '_template_content'),
|
|
|
|
|
(self.needs_to_change_sms_sender, '_sms_sender'),
|
|
|
|
|
):
|
|
|
|
|
if test:
|
|
|
|
|
yield BASE + '_incomplete' + tag
|
2020-02-12 14:03:51 +00:00
|
|
|
|
2020-03-03 17:40:50 +00:00
|
|
|
@cached_property
|
|
|
|
|
def returned_letter_statistics(self):
|
|
|
|
|
return service_api_client.get_returned_letter_statistics(self.id)
|
|
|
|
|
|
2020-02-12 14:03:51 +00:00
|
|
|
@cached_property
|
|
|
|
|
def returned_letter_summary(self):
|
|
|
|
|
return service_api_client.get_returned_letter_summary(self.id)
|
2020-02-12 14:19:21 +00:00
|
|
|
|
|
|
|
|
@property
|
2020-03-03 17:40:50 +00:00
|
|
|
def count_of_returned_letters_in_last_7_days(self):
|
|
|
|
|
return self.returned_letter_statistics['returned_letter_count']
|
2020-02-12 14:19:21 +00:00
|
|
|
|
|
|
|
|
@property
|
2020-03-03 17:40:50 +00:00
|
|
|
def date_of_most_recent_returned_letter_report(self):
|
|
|
|
|
return self.returned_letter_statistics['most_recent_report']
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def has_returned_letters(self):
|
|
|
|
|
return bool(self.date_of_most_recent_returned_letter_report)
|
2020-03-16 10:14:11 +00:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def contact_lists(self):
|
|
|
|
|
return ContactLists(self.id)
|