def test_dao_update_template_reply_to_some_to_some(sample_service, sample_user):
    letter_contact = create_letter_contact(sample_service, 'Edinburgh, ED1 1AA')
    letter_contact_2 = create_letter_contact(sample_service, 'London, N1 1DE')

    data = {
        'name': 'Sample Template',
        'template_type': "letter",
        'content': "Template content",
        'service': sample_service,
        'created_by': sample_user,
        'service_letter_contact_id': letter_contact.id,
        'postage': 'second',
    }
    template = Template(**data)
    dao_create_template(template)
    created = Template.query.get(template.id)
    dao_update_template_reply_to(template_id=created.id, reply_to=letter_contact_2.id)
    updated = Template.query.get(template.id)
    assert updated.reply_to == letter_contact_2.id
    assert updated.version == 2
    assert updated.updated_at

    updated_history = TemplateHistory.query.filter_by(id=created.id, version=2).one()
    assert updated_history.service_letter_contact_id == letter_contact_2.id
    assert updated_history.updated_at == updated_history.updated_at
Esempio n. 2
0
def test_dao_update_template_reply_to_some_to_none(sample_service,
                                                   sample_user):
    letter_contact = create_letter_contact(sample_service,
                                           "Edinburgh, ED1 1AA")
    data = {
        "name": "Sample Template",
        "template_type": "letter",
        "content": "Template content",
        "service": sample_service,
        "created_by": sample_user,
        "service_letter_contact_id": letter_contact.id,
        "postage": "second",
    }
    template = Template(**data)
    dao_create_template(template)
    created = Template.query.get(template.id)
    dao_update_template_reply_to(template_id=created.id, reply_to=None)
    updated = Template.query.get(template.id)
    assert updated.reply_to is None
    assert updated.version == 2
    assert updated.updated_at

    history = TemplateHistory.query.filter_by(id=created.id, version=2).one()
    assert history.service_letter_contact_id is None
    assert history.updated_at == updated.updated_at
Esempio n. 3
0
def update_template(service_id, template_id):
    fetched_template = dao_get_template_by_id_and_service_id(
        template_id=template_id, service_id=service_id)

    if not service_has_permission(
            fetched_template.template_type,
        [p.permission for p in fetched_template.service.permissions]):
        message = "Updating {} templates is not allowed".format(
            get_public_notify_type_text(fetched_template.template_type))
        errors = {'template_type': [message]}

        raise InvalidRequest(errors, 403)

    data = request.get_json()
    validate(data, post_update_template_schema)

    # if redacting, don't update anything else
    if data.get('redact_personalisation') is True:
        return redact_template(fetched_template, data)

    if "reply_to" in data:
        check_reply_to(service_id, data.get("reply_to"),
                       fetched_template.template_type)
        updated = dao_update_template_reply_to(template_id=template_id,
                                               reply_to=data.get("reply_to"))
        return jsonify(data=template_schema.dump(updated).data), 200

    current_data = dict(template_schema.dump(fetched_template).data.items())
    updated_template = dict(
        template_schema.dump(fetched_template).data.items())
    updated_template.update(data)

    # Check if there is a change to make.
    if _template_has_not_changed(current_data, updated_template):
        return jsonify(data=updated_template), 200

    over_limit = _content_count_greater_than_limit(
        updated_template['content'], fetched_template.template_type)
    if over_limit:
        message = 'Content has a character count greater than the limit of {}'.format(
            SMS_CHAR_COUNT_LIMIT)
        errors = {'content': [message]}
        raise InvalidRequest(errors, status_code=400)

    update_dict = template_schema.load(updated_template).data

    dao_update_template(update_dict)
    return jsonify(data=template_schema.dump(update_dict).data), 200