Get info about organisations from database table

This is the first step of replacing the `domains.yml` file.

In order to replicate the same functionality we get from the
`domains.yml` file and its associated code this commit adds a
`Organisation` model. This model copies a lot of methods from the
`AgreementInfo` class which wrapped the `domains.yml` file.

It factors out some stuff that would otherwise be duplicated between the
`Organisation` and `Service` model, in such a way that could be reused
for making other models in the future.

This commit doesn’t change other parts of the code to make use of this
new model yet – that will come in subsequent commits.
This commit is contained in:
Chris Hill-Scott
2019-04-04 11:18:22 +01:00
parent df1a1b5b66
commit 718f440720
5 changed files with 209 additions and 18 deletions

View File

@@ -2,7 +2,10 @@ from itertools import chain
from flask import abort, request, session
from flask_login import AnonymousUserMixin, UserMixin
from werkzeug.utils import cached_property
from app.models.organisation import Organisation
from app.notify_client.organisations_api_client import organisations_client
from app.utils import is_gov_user
roles = {
@@ -192,6 +195,16 @@ class User(UserMixin):
def is_locked(self):
return self.failed_login_count >= self.max_failed_login_count
@property
def email_domain(self):
return self.email_address.split('@')[-1]
@cached_property
def default_organisation(self):
return Organisation(
organisations_client.get_organisation_by_domain(self.email_domain)
)
def serialize(self):
dct = {
"id": self.id,
@@ -322,3 +335,7 @@ class AnonymousUser(AnonymousUserMixin):
# set the anonymous user so that if a new browser hits us we don't error http://stackoverflow.com/a/19275188
def logged_in_elsewhere(self):
return False
@property
def default_organisation(self):
return Organisation(None)