2019-03-07 11:23:42 +00:00
|
|
|
from sqlalchemy.sql.expression import func
|
|
|
|
|
|
2018-02-07 14:43:09 +00:00
|
|
|
from app import db
|
2021-04-14 07:11:01 +01:00
|
|
|
from app.dao.dao_utils import VersionOptions, autocommit, version_class
|
2021-03-12 12:50:07 +00:00
|
|
|
from app.models import Domain, Organisation, Service, User
|
2018-02-07 14:43:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_organisations():
|
|
|
|
|
return Organisation.query.order_by(
|
|
|
|
|
Organisation.active.desc(), Organisation.name.asc()
|
|
|
|
|
).all()
|
|
|
|
|
|
|
|
|
|
|
2020-02-17 11:12:54 +00:00
|
|
|
def dao_count_organisations_with_live_services():
|
2019-04-11 13:38:21 +01:00
|
|
|
return db.session.query(Organisation.id).join(Organisation.services).filter(
|
|
|
|
|
Service.active.is_(True),
|
|
|
|
|
Service.restricted.is_(False),
|
|
|
|
|
Service.count_as_live.is_(True),
|
|
|
|
|
).distinct().count()
|
|
|
|
|
|
|
|
|
|
|
2018-02-10 01:34:37 +00:00
|
|
|
def dao_get_organisation_services(organisation_id):
|
|
|
|
|
return Organisation.query.filter_by(
|
|
|
|
|
id=organisation_id
|
|
|
|
|
).one().services
|
|
|
|
|
|
|
|
|
|
|
2020-02-24 14:19:12 +00:00
|
|
|
def dao_get_organisation_live_services(organisation_id):
|
|
|
|
|
return Service.query.filter_by(
|
|
|
|
|
organisation_id=organisation_id,
|
|
|
|
|
restricted=False
|
|
|
|
|
).all()
|
|
|
|
|
|
|
|
|
|
|
2018-02-07 14:43:09 +00:00
|
|
|
def dao_get_organisation_by_id(organisation_id):
|
|
|
|
|
return Organisation.query.filter_by(id=organisation_id).one()
|
|
|
|
|
|
|
|
|
|
|
2019-02-19 12:02:18 +00:00
|
|
|
def dao_get_organisation_by_email_address(email_address):
|
|
|
|
|
|
2019-04-05 15:47:16 +01:00
|
|
|
email_address = email_address.lower().replace('.gsi.gov.uk', '.gov.uk')
|
2019-02-19 12:02:18 +00:00
|
|
|
|
2019-03-07 11:23:42 +00:00
|
|
|
for domain in Domain.query.order_by(func.char_length(Domain.domain).desc()).all():
|
|
|
|
|
|
2019-02-19 12:02:18 +00:00
|
|
|
if (
|
|
|
|
|
email_address.endswith("@{}".format(domain.domain)) or
|
|
|
|
|
email_address.endswith(".{}".format(domain.domain))
|
|
|
|
|
):
|
|
|
|
|
return Organisation.query.filter_by(id=domain.organisation_id).one()
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2018-02-10 01:34:37 +00:00
|
|
|
def dao_get_organisation_by_service_id(service_id):
|
2018-02-12 18:26:51 +00:00
|
|
|
return Organisation.query.join(Organisation.services).filter_by(id=service_id).first()
|
2018-02-10 01:34:37 +00:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2018-02-07 14:43:09 +00:00
|
|
|
def dao_create_organisation(organisation):
|
|
|
|
|
db.session.add(organisation)
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2018-02-09 11:17:13 +00:00
|
|
|
def dao_update_organisation(organisation_id, **kwargs):
|
2019-02-19 11:47:30 +00:00
|
|
|
|
2019-03-20 12:21:25 +00:00
|
|
|
domains = kwargs.pop('domains', None)
|
2019-02-19 11:47:30 +00:00
|
|
|
|
2019-07-04 15:12:08 +01:00
|
|
|
num_updated = Organisation.query.filter_by(id=organisation_id).update(
|
2018-02-09 11:17:13 +00:00
|
|
|
kwargs
|
|
|
|
|
)
|
2018-02-10 01:34:37 +00:00
|
|
|
|
2019-03-08 14:38:49 +00:00
|
|
|
if isinstance(domains, list):
|
2019-02-19 11:47:30 +00:00
|
|
|
|
|
|
|
|
Domain.query.filter_by(organisation_id=organisation_id).delete()
|
|
|
|
|
|
|
|
|
|
db.session.bulk_save_objects([
|
2019-03-08 14:38:49 +00:00
|
|
|
Domain(domain=domain.lower(), organisation_id=organisation_id)
|
2019-02-19 11:47:30 +00:00
|
|
|
for domain in domains
|
|
|
|
|
])
|
|
|
|
|
|
2019-09-25 13:43:05 +01:00
|
|
|
organisation = Organisation.query.get(organisation_id)
|
|
|
|
|
|
2019-07-04 14:48:16 +01:00
|
|
|
if 'organisation_type' in kwargs:
|
2019-09-25 13:57:06 +01:00
|
|
|
_update_organisation_services(organisation, 'organisation_type', only_where_none=False)
|
2019-09-25 13:43:05 +01:00
|
|
|
|
2020-09-09 10:43:16 +01:00
|
|
|
if 'crown' in kwargs:
|
|
|
|
|
_update_organisation_services(organisation, 'crown', only_where_none=False)
|
|
|
|
|
|
2019-09-25 13:43:05 +01:00
|
|
|
if 'email_branding_id' in kwargs:
|
2019-09-25 13:57:06 +01:00
|
|
|
_update_organisation_services(organisation, 'email_branding')
|
2019-09-25 13:43:05 +01:00
|
|
|
|
|
|
|
|
if 'letter_branding_id' in kwargs:
|
2019-09-25 13:57:06 +01:00
|
|
|
_update_organisation_services(organisation, 'letter_branding')
|
2019-02-19 11:47:30 +00:00
|
|
|
|
2019-07-04 15:12:08 +01:00
|
|
|
return num_updated
|
2019-02-19 11:47:30 +00:00
|
|
|
|
2018-02-10 01:34:37 +00:00
|
|
|
|
2019-09-25 13:43:05 +01:00
|
|
|
@version_class(
|
2019-09-25 13:57:06 +01:00
|
|
|
VersionOptions(Service, must_write_history=False),
|
2019-09-25 13:43:05 +01:00
|
|
|
)
|
2019-09-25 13:57:06 +01:00
|
|
|
def _update_organisation_services(organisation, attribute, only_where_none=True):
|
2019-07-04 14:48:16 +01:00
|
|
|
for service in organisation.services:
|
2019-09-25 13:57:06 +01:00
|
|
|
if getattr(service, attribute) is None or not only_where_none:
|
|
|
|
|
setattr(service, attribute, getattr(organisation, attribute))
|
2019-09-25 13:43:05 +01:00
|
|
|
db.session.add(service)
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2019-07-04 14:48:16 +01:00
|
|
|
@version_class(Service)
|
2018-02-10 01:34:37 +00:00
|
|
|
def dao_add_service_to_organisation(service, organisation_id):
|
|
|
|
|
organisation = Organisation.query.filter_by(
|
|
|
|
|
id=organisation_id
|
|
|
|
|
).one()
|
|
|
|
|
|
2019-08-12 15:59:27 +01:00
|
|
|
service.organisation_id = organisation_id
|
2019-07-04 14:48:16 +01:00
|
|
|
service.organisation_type = organisation.organisation_type
|
2019-07-10 18:17:33 +01:00
|
|
|
service.crown = organisation.crown
|
|
|
|
|
|
2019-07-04 14:48:16 +01:00
|
|
|
db.session.add(service)
|
2018-02-20 17:09:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_users_for_organisation(organisation_id):
|
2020-03-30 17:47:15 +01:00
|
|
|
return db.session.query(
|
|
|
|
|
User
|
|
|
|
|
).join(
|
|
|
|
|
User.organisations
|
|
|
|
|
).filter(
|
|
|
|
|
Organisation.id == organisation_id,
|
2018-02-20 17:09:16 +00:00
|
|
|
User.state == 'active'
|
2018-02-21 11:49:21 +00:00
|
|
|
).order_by(User.created_at).all()
|
2018-02-20 17:09:16 +00:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2018-02-20 17:09:16 +00:00
|
|
|
def dao_add_user_to_organisation(organisation_id, user_id):
|
|
|
|
|
organisation = dao_get_organisation_by_id(organisation_id)
|
2018-02-21 17:03:32 +00:00
|
|
|
user = User.query.filter_by(id=user_id).one()
|
2018-02-23 13:36:42 +00:00
|
|
|
user.organisations.append(organisation)
|
2018-02-20 17:09:16 +00:00
|
|
|
db.session.add(organisation)
|
|
|
|
|
return user
|
2022-01-05 13:30:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@autocommit
|
|
|
|
|
def dao_remove_user_from_organisation(organisation, user):
|
|
|
|
|
organisation.users.remove(user)
|