if 'organisation_type' in kwargs:
        _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(
    VersionOptions(Service, must_write_history=False), )
def _update_organisation_services(organisation,
                                  attribute,
                                  only_where_none=True):
    for service in organisation.services:
        if getattr(service, attribute) is None or not only_where_none:
            setattr(service, attribute, getattr(organisation, attribute))
        db.session.add(service)


@transactional
@version_class(Service)
def dao_add_service_to_organisation(service, organisation_id):
    organisation = Organisation.query.filter_by(id=organisation_id).one()

    service.organisation_id = organisation_id
Exemple #2
0
    return query.all()


def dao_fetch_all_services_created_by_user(user_id):
    query = Service.query.filter_by(
        created_by_id=user_id
    ).order_by(
        asc(Service.created_at)
    )

    return query.all()


@transactional
@version_class(
    VersionOptions(ApiKey, must_write_history=False),
    VersionOptions(Service),
    VersionOptions(Template, history_class=TemplateHistory, must_write_history=False),
)
def dao_archive_service(service_id):
    # have to eager load templates and api keys so that we don't flush when we loop through them
    # to ensure that db.session still contains the models when it comes to creating history objects
    service = Service.query.options(
        joinedload('templates'),
        joinedload('templates.template_redacted'),
        joinedload('api_keys'),
    ).filter(Service.id == service_id).one()

    service.active = False
    service.name = get_archived_db_column_value(service.name)
    service.email_from = get_archived_db_column_value(service.email_from)
from flask import current_app
from sqlalchemy import asc, desc

from app import db
from app.models import (LETTER_TYPE, SECOND_CLASS, Template, TemplateHistory,
                        TemplateRedacted)
from app.dao.dao_utils import (
    transactional,
    version_class,
    VersionOptions,
)
from app.dao.users_dao import get_user_by_id


@transactional
@version_class(VersionOptions(Template, history_class=TemplateHistory))
def dao_create_template(template):
    template.id = uuid.uuid4(
    )  # must be set now so version history model can use same id
    template.archived = False

    redacted_dict = {
        "template": template,
        "redact_personalisation": False,
    }
    if template.created_by:
        redacted_dict.update({"updated_by": template.created_by})
    else:
        redacted_dict.update({"updated_by_id": template.created_by_id})

    template.template_redacted = TemplateRedacted(**redacted_dict)