Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
- Changed the category relationship in Template from TemplateCategory to template_category
- dao_get_template_category_by_template_id now simply selects the template, and returns the associated template_category instead of using an expensive join
  • Loading branch information
whabanks committed Jun 17, 2024
1 parent e34dc4a commit 6a291fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 6 additions & 5 deletions app/dao/template_categories_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
5 changes: 4 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:
Expand Down

0 comments on commit 6a291fc

Please sign in to comment.