diff --git a/app/dao/template_categories_dao.py b/app/dao/template_categories_dao.py index 03d4851498..dd9cd4a7f7 100644 --- a/app/dao/template_categories_dao.py +++ b/app/dao/template_categories_dao.py @@ -4,7 +4,7 @@ from app import db from app.dao.dao_utils import transactional -from app.models import TemplateCategory +from app.models import Template, TemplateCategory @transactional @@ -18,13 +18,14 @@ def dao_update_template_category(template_category: TemplateCategory): db.session.add(template_category) -def dao_get_template_category_by_id(template_category_id): +def dao_get_template_category_by_id(template_category_id) -> TemplateCategory: return TemplateCategory.query.filter_by(id=template_category_id).one() -def dao_get_template_category_by_template_id(template_id): - return TemplateCategory.query.join(TemplateCategory.templates).filter_by(id=template_id).one() +def dao_get_template_category_by_template_id(template_id) -> TemplateCategory: + return Template.query.filter_by(id=template_id).one().template_category -def dao_get_all_template_categories(): +# TODO: Add filters: Select all template categories used by at least 1 sms/email template +def dao_get_all_template_categories(template_type=None, hidden=False): return TemplateCategory.query.order_by(asc(TemplateCategory.name_en)).all() diff --git a/app/models.py b/app/models.py index a9abcc0277..b79da6d5ad 100644 --- a/app/models.py +++ b/app/models.py @@ -1213,7 +1213,7 @@ class Template(TemplateBase): service = db.relationship("Service", backref="templates") version = db.Column(db.Integer, default=0, nullable=False) - category = db.relationship("TemplateCategory", backref="templates") + category = db.relationship("template_category", backref="templates") folder = db.relationship( "TemplateFolder", @@ -1235,6 +1235,9 @@ def get_link(self): @property def template_process_type(self): + """By default we use the process_type from TemplateCategory, but allow admins to override it on a per-template basis. + Only when overriden do we use the process_type from the template itself. + """ if self.template_type == SMS_TYPE: return self.process_type if self.process_type else self.template_categories.sms_process_type elif self.template_type == EMAIL_TYPE: