From 4bab29168ca349526e7bc3297948455e072cca63 Mon Sep 17 00:00:00 2001 From: wbanks Date: Tue, 11 Jun 2024 16:04:02 -0400 Subject: [PATCH] Draft migration to add TemplateCategories table - Initial work on the models --- app/models.py | 28 ++++++++++ .../versions/0454_add_template_category.py | 55 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 migrations/versions/0454_add_template_category.py diff --git a/app/models.py b/app/models.py index f79867918e..920f218de1 100644 --- a/app/models.py +++ b/app/models.py @@ -24,6 +24,7 @@ convert_local_timezone_to_utc, convert_utc_to_local_timezone, ) +from pkg_resources import declare_namespace from sqlalchemy import CheckConstraint, Index, UniqueConstraint from sqlalchemy.dialects.postgresql import JSON, JSONB, UUID from sqlalchemy.ext.associationproxy import association_proxy @@ -1032,6 +1033,18 @@ def get_users_with_permission(self): PRECOMPILED_TEMPLATE_NAME = "Pre-compiled PDF" +class TemplateCategories(BaseModel): + __tablename__ = "template_category" + + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + name_en = db.Column(db.String(255), nullable=False) + name_fr = db.Column(db.String(255), nullable=False) + description_en = db.Column(db.String(200), nullable=True) + description_fr = db.Column(db.String(200), nullable=True) + sms_process_type = db.Column(db.String(200), nullable=False) + email_process_type = db.Column(db.String(200), nullable=False) + hidden = db.Column(db.Boolean, nullable=False, default=False) + class TemplateBase(BaseModel): __abstract__ = True @@ -1078,6 +1091,10 @@ def service_id(cls): def created_by_id(cls): return db.Column(UUID(as_uuid=True), db.ForeignKey("users.id"), index=True, nullable=False) + @declared_attr + def template_category_id(cls): + return db.Column(UUID(as_uuid=True), db.ForeignKey("template_categories.id"), index=True, nullable=True) + @declared_attr def created_by(cls): return db.relationship("User") @@ -1179,6 +1196,8 @@ class Template(TemplateBase): service = db.relationship("Service", backref="templates") version = db.Column(db.Integer, default=0, nullable=False) + template_categories = db.relationship("TemplateCategories", backref="templates") + folder = db.relationship( "TemplateFolder", @@ -1198,6 +1217,15 @@ def get_link(self): _external=True, ) + @property + def template_process_type(self): + 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: + return self.process_type if self.process_type else self.template_categories.email_process_type + return self.process_type + + @classmethod def from_json(cls, data, folder=None): """ diff --git a/migrations/versions/0454_add_template_category.py b/migrations/versions/0454_add_template_category.py new file mode 100644 index 0000000000..c6a0be975a --- /dev/null +++ b/migrations/versions/0454_add_template_category.py @@ -0,0 +1,55 @@ +""" + +Revision ID: 0454_add_template_category +Revises: 0453_add_callback_failure_email +Create Date: 2024-06-11 13:32:00 +""" + +from datetime import datetime + +import sqlalchemy as sa +from alembic import op +from sqlalchemy.dialects import postgresql + + +def upgrade(): + op.create_table( + "template_categories", + sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("name_en", sa.String(length=255), nullable=False), + sa.Column("name_fr", sa.String(length=255), nullable=False), + sa.Column("description_en", sa.String(length=255), nullable=True), + sa.Column("description_fr", sa.String(length=255), nullable=True), + sa.Column("sms_process_type", sa.String(length=255), nullable=False), + sa.Column("email_process_type", sa.String(length=255), nullable=False), + sa.Column("hidden", sa.Boolean(), nullable=False), + ) + + op.add_column( + "templates", + sa.Colum("template_category_id", postgresql.UUID(as_uuid=True), nullable=True) + ) + op.create_index( + op.f("ix_template_category_id", "templates", ["template_category_id"], unique=False) + ) + op.create_foreign_key( + "fk_template_template_categories", + "template", + "template_category", + ["template_category_id"], + ["id"] + ) + op.get_bind() + + # Insert the generic Low priority (bulk) category + # op.execute(""" + # INSERT INTO template_category (id, name_en, name_fr, sms_process_type, email_process_type, hidden) + # VALUES ('00000000-0000-0000-0000-000000000000', 'Low Category (Bulk)', 'Catégorie Basse (En Vrac)', true + # """ + # ) + +def downgrade(): + op.drop_constraint("fk_template_template_category", "templates", type_="foreignkey") + op.drop_index(op.f("ix_template_category_id"), table_name="templates") + op.drop_column("templates", "template_category_id") + op.drop_table("template_category_id") \ No newline at end of file