Merge pull request #2620 from alphagov/update-service-branding-when-updating-org

Update the branding of services branding when updating their organisation’s branding
This commit is contained in:
Chris Hill-Scott
2019-10-03 10:53:34 +01:00
committed by GitHub
2 changed files with 73 additions and 8 deletions

View File

@@ -1,7 +1,7 @@
from sqlalchemy.sql.expression import func
from app import db
from app.dao.dao_utils import transactional, version_class
from app.dao.dao_utils import VersionOptions, transactional, version_class
from app.models import (
Organisation,
Domain,
@@ -77,18 +77,27 @@ def dao_update_organisation(organisation_id, **kwargs):
for domain in domains
])
organisation = Organisation.query.get(organisation_id)
if 'organisation_type' in kwargs:
organisation = Organisation.query.get(organisation_id)
if organisation.services:
_update_org_type_for_organisation_services(organisation)
_update_organisation_services(organisation, 'organisation_type', only_where_none=False)
if 'email_branding_id' in kwargs:
_update_organisation_services(organisation, 'email_branding')
if 'letter_branding_id' in kwargs:
_update_organisation_services(organisation, 'letter_branding')
return num_updated
@version_class(Service)
def _update_org_type_for_organisation_services(organisation):
@version_class(
VersionOptions(Service, must_write_history=False),
)
def _update_organisation_services(organisation, attribute, only_where_none=True):
for service in organisation.services:
service.organisation_type = organisation.organisation_type
if getattr(service, attribute) is None or not only_where_none:
setattr(service, attribute, getattr(organisation, attribute))
db.session.add(service)