From bab5f150c0e329ef2f63df1407b7e2d4b2ebe76f Mon Sep 17 00:00:00 2001 From: William B <7444334+whabanks@users.noreply.github.com> Date: Mon, 15 Jul 2024 10:46:05 -0400 Subject: [PATCH] Raise exception when deleting cat that is linked to template(s) (#2219) - Refactored dao_delete_template_category_by_id so it's easier to understand --- app/dao/template_categories_dao.py | 33 ++++++++++--------- app/template/template_category_rest.py | 13 ++------ tests/app/dao/test_template_categories_dao.py | 4 ++- .../template/test_template_category_rest.py | 2 +- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/app/dao/template_categories_dao.py b/app/dao/template_categories_dao.py index 1215f1a523..ea584df8ba 100644 --- a/app/dao/template_categories_dao.py +++ b/app/dao/template_categories_dao.py @@ -5,6 +5,7 @@ from app import db from app.dao.dao_utils import transactional +from app.errors import InvalidRequest from app.models import Template, TemplateCategory @@ -58,23 +59,25 @@ def dao_delete_template_category_by_id(template_category_id, cascade=False): template_category = dao_get_template_category_by_id(template_category_id) templates = Template.query.filter_by(template_category_id=template_category_id).all() - if not templates or cascade: + if templates and not cascade: + raise InvalidRequest( + "Cannot delete categories associated with templates. Dissociate the category from templates first.", 400 + ) + + if templates and cascade: # When there are templates and we are cascading, we set the category to a default # that matches the template's previous category's priority - if cascade: - for template in templates: - # Get the a default category that matches the previous priority of the template, based on template type - default_category_id = _get_default_category_id( - template_category.sms_process_type - if template.template_type == "sms" - else template_category.email_process_type - ) - template.template_category_id = default_category_id - template.updated_at = datetime.utcnow() - db.session.add(template) - db.session.commit() - - db.session.delete(template_category) + for template in templates: + # Get the a default category that matches the previous priority of the template, based on template type + default_category_id = _get_default_category_id( + template_category.sms_process_type if template.template_type == "sms" else template_category.email_process_type + ) + template.template_category_id = default_category_id + template.updated_at = datetime.utcnow() + db.session.add(template) + db.session.commit() + + db.session.delete(template_category) def _get_default_category_id(process_type): diff --git a/app/template/template_category_rest.py b/app/template/template_category_rest.py index 84b8f06bd6..dd8ea98088 100644 --- a/app/template/template_category_rest.py +++ b/app/template/template_category_rest.py @@ -9,7 +9,7 @@ dao_update_template_category, ) from app.errors import register_errors -from app.models import Template, TemplateCategory +from app.models import TemplateCategory from app.schemas import template_category_schema template_category_blueprint = Blueprint( @@ -92,13 +92,6 @@ def delete_template_category(template_category_id): Returns: (flask.Response): The response message and http status code. """ - - if request.args.get("cascade") == "True": - dao_delete_template_category_by_id(template_category_id, cascade=True) - return "", 204 - - if Template.query.filter_by(template_category_id=template_category_id).count() > 0: - return jsonify(message="Cannot delete a template category with templates assigned to it."), 400 - else: - dao_delete_template_category_by_id(template_category_id) + cascade = True if request.args.get("cascade") == "True" else False + dao_delete_template_category_by_id(template_category_id, cascade=cascade) return "", 204 diff --git a/tests/app/dao/test_template_categories_dao.py b/tests/app/dao/test_template_categories_dao.py index 07681618ea..3ae33d1561 100644 --- a/tests/app/dao/test_template_categories_dao.py +++ b/tests/app/dao/test_template_categories_dao.py @@ -10,6 +10,7 @@ dao_update_template_category, ) from app.dao.templates_dao import dao_create_template +from app.errors import InvalidRequest from app.models import BULK, NORMAL, Template, TemplateCategory from tests.app.conftest import create_sample_template @@ -375,7 +376,8 @@ def test_dao_delete_template_category_by_id_should_not_allow_deletion_when_assoc ): create_sample_template(notify_db, notify_db_session, template_category=sample_template_category) - dao_delete_template_category_by_id(sample_template_category.id) + with pytest.raises(InvalidRequest): + dao_delete_template_category_by_id(sample_template_category.id) assert TemplateCategory.query.count() == 1 diff --git a/tests/app/template/test_template_category_rest.py b/tests/app/template/test_template_category_rest.py index 37ce63fa74..2669cc5978 100644 --- a/tests/app/template/test_template_category_rest.py +++ b/tests/app/template/test_template_category_rest.py @@ -119,7 +119,7 @@ def test_get_template_categories( "cascade, expected_status_code, expected_msg", [ ("True", 204, ""), - ("False", 400, "Cannot delete a template category with templates assigned to it."), + ("False", 400, "Cannot delete categories associated with templates. Dissociate the category from templates first."), ], ) def test_delete_template_category_cascade(