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
|
2019-09-25 13:43:05 +01:00
|
|
|
from app.dao.dao_utils import VersionOptions, transactional, version_class
|
2018-02-21 16:39:17 +00:00
|
|
|
from app.models import (
|
|
|
|
|
Organisation,
|
2019-02-19 11:47:30 +00:00
|
|
|
Domain,
|
2018-02-21 16:39:17 +00:00
|
|
|
InvitedOrganisationUser,
|
2019-04-11 13:38:21 +01:00
|
|
|
Service,
|
2018-02-21 16:39:17 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
2019-04-11 13:38:21 +01:00
|
|
|
def dao_count_organsations_with_live_services():
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-02-07 14:43:09 +00:00
|
|
|
@transactional
|
|
|
|
|
def dao_create_organisation(organisation):
|
|
|
|
|
db.session.add(organisation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@transactional
|
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:43:05 +01:00
|
|
|
_update_org_type_for_organisation_services(organisation)
|
|
|
|
|
|
|
|
|
|
if 'email_branding_id' in kwargs:
|
|
|
|
|
_update_email_branding_for_organisation_services(organisation)
|
|
|
|
|
|
|
|
|
|
if 'letter_branding_id' in kwargs:
|
|
|
|
|
_update_letter_branding_for_organisation_services(organisation)
|
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(
|
|
|
|
|
VersionOptions(Service, must_write_history=False)
|
|
|
|
|
)
|
2019-07-04 14:48:16 +01:00
|
|
|
def _update_org_type_for_organisation_services(organisation):
|
|
|
|
|
for service in organisation.services:
|
|
|
|
|
service.organisation_type = organisation.organisation_type
|
|
|
|
|
db.session.add(service)
|
|
|
|
|
|
|
|
|
|
|
2019-09-25 13:43:05 +01:00
|
|
|
@version_class(
|
|
|
|
|
VersionOptions(Service, must_write_history=False)
|
|
|
|
|
)
|
|
|
|
|
def _update_email_branding_for_organisation_services(organisation):
|
|
|
|
|
for service in organisation.services:
|
|
|
|
|
if service.email_branding is None:
|
|
|
|
|
service.email_branding = organisation.email_branding
|
|
|
|
|
db.session.add(service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@version_class(
|
|
|
|
|
VersionOptions(Service, must_write_history=False)
|
|
|
|
|
)
|
|
|
|
|
def _update_letter_branding_for_organisation_services(organisation):
|
|
|
|
|
for service in organisation.services:
|
|
|
|
|
if service.letter_branding is None:
|
|
|
|
|
service.letter_branding = organisation.letter_branding
|
|
|
|
|
db.session.add(service)
|
|
|
|
|
|
|
|
|
|
|
2018-02-10 01:34:37 +00:00
|
|
|
@transactional
|
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_invited_organisation_user(user_id):
|
2018-02-21 11:49:21 +00:00
|
|
|
return InvitedOrganisationUser.query.filter_by(id=user_id).one()
|
2018-02-20 17:09:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_users_for_organisation(organisation_id):
|
|
|
|
|
return User.query.filter(
|
2018-02-23 13:36:42 +00:00
|
|
|
User.organisations.any(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
|
|
|
|
|
|
|
|
|
2018-02-21 16:39:17 +00:00
|
|
|
@transactional
|
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
|