-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Template_Categories table (#2193)
* Draft migration to add TemplateCategories table - Initial work on the models * Fix prop logic for template_process_type * Add indexes, unique constraints - Fix typos - Update sqlalchemy model * Add CRUD methods for TemplateCategories - Added template_category_id to the template_history table - Adjusted model class name TemplateCategories -> TemplateCategory - WIP: Added some basic tests for the TemplateCategory dao * Insert low, med, high default categories during migration * Fix prop logic for template_process_type again * WIP: Add API endpoints for interacting with TemplateCategories - Added a schema for TemplateCategory - Added from_json and serialize to the TemplateCategory model - Added the template/category Blueprint - Add some initial CRUD api endpoints * Implement dao and api to update process_type * Address PR comments - 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 * Finish adding needed api endpoints - Moved the default categories into our config file - Added dao_update_template_category - Added dao_delete_template_category_by_id - Added routes to: get and delete a template category - Added a route to get all template categories - Updated the migration file to use default category uuid's from the config file * Chore: logic cleanup * First batch of unit tests & bug fixes * Implement filtering when fetching all categories - Add tests * Add lazy join on TemplateCategory * Clean up dao tests - Squash more bugs - Fix formatting - Added a couple more tests for the filters on dao_get_all_template_categories * Add tests for deleting a template category - Excluded the template_category table from deletion in notify_db_session to preserve the 3 generic template categories between test runs - Fixed inserts in the migration, apparently alembic / sqlalchemy doesn't like multi-line f-strings - Made a few tests shorter by excluding the description_en and description_fr columns as they are optional * Add API tests, squash bugs - Allow passing of a uuid to dao_create_template_category - Fixed issues with get_template_categories and delete_template_category filters / flags - Added a fixture to re-populate the template_category table with generic categories and removed template_categories from the list of tables that are excluded from the post-test db clear * Fix pre-existing tests - formatting * Misc. fixes - Fix incorrectly named prop call in dao - Added category as a declared attribute in TemplateBase model - Added a proper route to get a category by template id - Added a foreign key to templates_history for the category id - Fix a couple more tests * We definitely didn't want that FK on templatehistory... * Logic cleanups - Tweak tests - No longer excluding template_category_id in template_category_schema - Updated migration revision as 0453 was added for pinpoint work * Rename migration - Remove NOT NULL constraint on templates.process_type * Add tests for models - Simplify the dao delete logic * Add tests that were missed for template rest and dao * Rename /template/category to /template-category - Adjust tests - formatting * various fixes * Re-word dao_delete_template_category_by_id Clarify that new category assignments aren't done at random but are chosen from one of the default categories. * Add created_at and updated_at fields - Set a default category id if a category is not passed to create_sample_template * Add default value for created at, fix tests * Quick fix * Fix column defaults - Create a template category when creating a sample template and do not pass in a category as we cannot rely on the defaults existing in the test db * Fix silly typo.. * Remove unneeded assert --------- Co-authored-by: Jumana B <[email protected]>
- Loading branch information
Showing
16 changed files
with
1,125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import uuid | ||
from datetime import datetime | ||
|
||
from flask import current_app | ||
|
||
from app import db | ||
from app.dao.dao_utils import transactional | ||
from app.models import Template, TemplateCategory | ||
|
||
|
||
@transactional | ||
def dao_create_template_category(template_category: TemplateCategory): | ||
if template_category.id is None: | ||
template_category.id = uuid.uuid4() | ||
db.session.add(template_category) | ||
|
||
|
||
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) -> TemplateCategory: | ||
return Template.query.filter_by(id=template_id).one().category | ||
|
||
|
||
# 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=None): | ||
query = TemplateCategory.query | ||
|
||
if template_type is not None: | ||
query = query.join(Template).filter(Template.template_type == template_type) | ||
|
||
if hidden is not None: | ||
query = query.filter(TemplateCategory.hidden == hidden) | ||
|
||
return query.all() | ||
|
||
|
||
@transactional | ||
def dao_update_template_category(template_category: TemplateCategory): | ||
db.session.add(template_category) | ||
db.session.commit() | ||
|
||
|
||
@transactional | ||
def dao_delete_template_category_by_id(template_category_id, cascade=False): | ||
""" | ||
Deletes a `TemplateCategory`. By default, if the `TemplateCategory` is associated with any `Template`, it will not be deleted. | ||
If the `cascade` option is specified then the category will be forcible removed: | ||
1. The `Category` will be dissociated from templates that use it | ||
2. The `Template` is assigned to one of the default categories that matches the priority of the deleted category | ||
3. Finally the `Category` will be deleted | ||
Args: | ||
template_category_id (str): The id of the template_category to delete | ||
cascade (bool, optional): Specify whether to dissociate the category from templates that use it to force removal. Defaults to 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: | ||
# 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.category = dao_get_template_category_by_id(default_category_id) | ||
template.updated_at = datetime.utcnow() | ||
db.session.add(template) | ||
|
||
db.session.delete(template_category) | ||
db.session.commit() | ||
|
||
|
||
def _get_default_category_id(process_type): | ||
default_categories = { | ||
"bulk": current_app.config["DEFAULT_TEMPLATE_CATEGORY_LOW"], | ||
"normal": current_app.config["DEFAULT_TEMPLATE_CATEGORY_MEDIUM"], | ||
"priority": current_app.config["DEFAULT_TEMPLATE_CATEGORY_HIGH"], | ||
} | ||
return default_categories.get(process_type, current_app.config["DEFAULT_TEMPLATE_CATEGORY_LOW"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.