2019-04-04 11:18:22 +01:00
|
|
|
|
from flask import Markup, abort
|
2019-05-23 17:17:26 +01:00
|
|
|
|
from werkzeug.utils import cached_property
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
|
|
|
|
|
from app.models import JSONModel
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Organisation(JSONModel):
|
|
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
|
'domains',
|
2019-05-13 14:50:40 +01:00
|
|
|
|
'request_to_go_live_notes',
|
2019-04-04 11:18:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-23 17:17:26 +01:00
|
|
|
|
@classmethod
|
|
|
|
|
|
def from_id(cls, org_id):
|
|
|
|
|
|
return cls(organisations_client.get_organisation(org_id))
|
|
|
|
|
|
|
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-04-04 11:18:22 +01:00
|
|
|
|
|
2019-04-04 11:23:40 +01:00
|
|
|
|
def as_human_readable(self, fallback_domain):
|
2019-04-04 11:18:22 +01:00
|
|
|
|
if self.agreement_signed:
|
2019-05-13 14:50:40 +01:00
|
|
|
|
agreement_statement = 'Yes, on behalf of {}.'.format(self.name)
|
2019-04-04 11:23:40 +01:00
|
|
|
|
elif self.name:
|
2019-05-13 14:50:40 +01:00
|
|
|
|
agreement_statement = '{} (organisation is {}, {}).'.format(
|
2019-04-04 11:18:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
False: 'No',
|
|
|
|
|
|
None: 'Can’t tell',
|
|
|
|
|
|
}.get(self.agreement_signed),
|
|
|
|
|
|
self.name,
|
|
|
|
|
|
{
|
|
|
|
|
|
True: 'a crown body',
|
|
|
|
|
|
False: 'a non-crown body',
|
|
|
|
|
|
None: 'crown status unknown',
|
2019-04-04 15:02:30 +01:00
|
|
|
|
}.get(self.crown),
|
2019-04-04 11:18:22 +01:00
|
|
|
|
)
|
|
|
|
|
|
else:
|
2019-05-13 14:50:40 +01:00
|
|
|
|
agreement_statement = 'Can’t tell (domain is {}).'.format(fallback_domain)
|
|
|
|
|
|
|
|
|
|
|
|
if self.request_to_go_live_notes:
|
|
|
|
|
|
agreement_statement = agreement_statement + ' ' + self.request_to_go_live_notes
|
|
|
|
|
|
|
|
|
|
|
|
return agreement_statement
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
2019-04-04 11:23:40 +01:00
|
|
|
|
def as_info_for_branding_request(self, fallback_domain):
|
|
|
|
|
|
return self.name or 'Can’t tell (domain is {})'.format(fallback_domain)
|
2019-04-04 11:18:22 +01:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def as_jinja_template(self):
|
2019-04-04 15:02:30 +01:00
|
|
|
|
if self.crown is None:
|
2019-04-04 11:18:22 +01:00
|
|
|
|
return 'agreement-choose'
|
|
|
|
|
|
if self.agreement_signed:
|
|
|
|
|
|
return 'agreement-signed'
|
|
|
|
|
|
return 'agreement'
|
|
|
|
|
|
|
|
|
|
|
|
def as_terms_of_use_paragraph(self, **kwargs):
|
|
|
|
|
|
return Markup(self._as_terms_of_use_paragraph(**kwargs))
|
|
|
|
|
|
|
|
|
|
|
|
def _as_terms_of_use_paragraph(self, terms_link, download_link, support_link, signed_in):
|
|
|
|
|
|
|
|
|
|
|
|
if not signed_in:
|
|
|
|
|
|
return ((
|
|
|
|
|
|
'{} <a href="{}">Sign in</a> to download a copy '
|
|
|
|
|
|
'or find out if one is already in place.'
|
|
|
|
|
|
).format(self._acceptance_required, terms_link))
|
|
|
|
|
|
|
|
|
|
|
|
if self.agreement_signed is None:
|
|
|
|
|
|
return ((
|
|
|
|
|
|
'{} <a href="{}">Download the agreement</a> or '
|
|
|
|
|
|
'<a href="{}">contact us</a> to find out if we already '
|
|
|
|
|
|
'have one in place with your organisation.'
|
|
|
|
|
|
).format(self._acceptance_required, download_link, support_link))
|
|
|
|
|
|
|
|
|
|
|
|
if self.agreement_signed is False:
|
|
|
|
|
|
return ((
|
|
|
|
|
|
'{} <a href="{}">Download a copy</a>.'
|
|
|
|
|
|
).format(self._acceptance_required, download_link))
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
'Your organisation ({}) has already accepted the '
|
|
|
|
|
|
'GOV.UK Notify data sharing and financial '
|
|
|
|
|
|
'agreement.'.format(self.name)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def _acceptance_required(self):
|
|
|
|
|
|
return (
|
|
|
|
|
|
'Your organisation {} must also accept our data sharing '
|
|
|
|
|
|
'and financial agreement.'.format(
|
|
|
|
|
|
'({})'.format(self.name) if self.name else '',
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
|
def services(self):
|
|
|
|
|
|
return organisations_client.get_organisation_services(self.id)
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
def active_users(self):
|
|
|
|
|
|
# need to put this here to prevent cyclical import
|
|
|
|
|
|
from app.notify_client.user_api_client import user_api_client
|
|
|
|
|
|
return user_api_client.get_users_for_organisation(org_id=self.id)
|
|
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
|
def invited_users(self):
|
|
|
|
|
|
# need to put this here to prevent cyclical import
|
|
|
|
|
|
from app.notify_client.org_invite_api_client import org_invite_api_client
|
|
|
|
|
|
return org_invite_api_client.get_invites_for_organisation(org_id=self.id)
|
|
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
|
def team_members(self):
|
|
|
|
|
|
return sorted(
|
|
|
|
|
|
self.active_users + [i for i in self.invited_users if i.status != 'accepted'],
|
|
|
|
|
|
key=lambda user: user.email_address,
|
|
|
|
|
|
)
|