mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-24 16:23:44 -04:00
Merge pull request #2368 from alphagov/richer-orgs
Use ‘organisations‘ table to store info about organisations (not YAML)
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
from sqlalchemy.sql.expression import func
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.models import (
|
||||
Organisation,
|
||||
Domain,
|
||||
InvitedOrganisationUser,
|
||||
User
|
||||
)
|
||||
@@ -23,6 +26,21 @@ def dao_get_organisation_by_id(organisation_id):
|
||||
return Organisation.query.filter_by(id=organisation_id).one()
|
||||
|
||||
|
||||
def dao_get_organisation_by_email_address(email_address):
|
||||
|
||||
email_address = email_address.lower()
|
||||
|
||||
for domain in Domain.query.order_by(func.char_length(Domain.domain).desc()).all():
|
||||
|
||||
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
|
||||
|
||||
|
||||
def dao_get_organisation_by_service_id(service_id):
|
||||
return Organisation.query.join(Organisation.services).filter_by(id=service_id).first()
|
||||
|
||||
@@ -34,10 +52,26 @@ def dao_create_organisation(organisation):
|
||||
|
||||
@transactional
|
||||
def dao_update_organisation(organisation_id, **kwargs):
|
||||
return Organisation.query.filter_by(id=organisation_id).update(
|
||||
|
||||
domains = kwargs.pop('domains', [])
|
||||
|
||||
organisation = Organisation.query.filter_by(id=organisation_id).update(
|
||||
kwargs
|
||||
)
|
||||
|
||||
if isinstance(domains, list):
|
||||
|
||||
Domain.query.filter_by(organisation_id=organisation_id).delete()
|
||||
|
||||
db.session.bulk_save_objects([
|
||||
Domain(domain=domain.lower(), organisation_id=organisation_id)
|
||||
for domain in domains
|
||||
])
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return organisation
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_add_service_to_organisation(service, organisation_id):
|
||||
|
||||
@@ -11,6 +11,7 @@ from app.dao.dao_utils import (
|
||||
transactional,
|
||||
version_class
|
||||
)
|
||||
from app.dao.organisation_dao import dao_get_organisation_by_email_address
|
||||
from app.dao.service_sms_sender_dao import insert_service_sms_sender
|
||||
from app.dao.service_user_dao import dao_get_service_user
|
||||
from app.models import (
|
||||
@@ -151,14 +152,25 @@ def dao_fetch_service_by_id_and_user(service_id, user_id):
|
||||
|
||||
@transactional
|
||||
@version_class(Service)
|
||||
def dao_create_service(service, user, service_id=None, service_permissions=None, letter_branding=None):
|
||||
def dao_create_service(
|
||||
service,
|
||||
user,
|
||||
service_id=None,
|
||||
service_permissions=None,
|
||||
letter_branding=None,
|
||||
):
|
||||
# the default property does not appear to work when there is a difference between the sqlalchemy schema and the
|
||||
# db schema (ie: during a migration), so we have to set sms_sender manually here. After the GOVUK sms_sender
|
||||
# migration is completed, this code should be able to be removed.
|
||||
|
||||
if not user:
|
||||
raise ValueError("Can't create a service without a user")
|
||||
|
||||
if service_permissions is None:
|
||||
service_permissions = DEFAULT_SERVICE_PERMISSIONS
|
||||
|
||||
organisation = dao_get_organisation_by_email_address(user.email_address)
|
||||
|
||||
from app.dao.permissions_dao import permission_dao
|
||||
service.users.append(user)
|
||||
permission_dao.add_default_service_permissions_for_user(user, service)
|
||||
@@ -173,8 +185,20 @@ def dao_create_service(service, user, service_id=None, service_permissions=None,
|
||||
|
||||
# do we just add the default - or will we get a value from FE?
|
||||
insert_service_sms_sender(service, current_app.config['FROM_NUMBER'])
|
||||
|
||||
if letter_branding:
|
||||
service.letter_branding = letter_branding
|
||||
|
||||
if organisation:
|
||||
|
||||
service.organisation = organisation
|
||||
|
||||
if organisation.email_branding_id:
|
||||
service.email_branding = organisation.email_branding_id
|
||||
|
||||
if organisation.letter_branding_id and not service.letter_branding:
|
||||
service.letter_branding = organisation.letter_branding_id
|
||||
|
||||
db.session.add(service)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user