Skip to content

Commit

Permalink
Draft migration to add TemplateCategories table
Browse files Browse the repository at this point in the history
- Initial work on the models
  • Loading branch information
whabanks committed Jun 11, 2024
1 parent f6ed3af commit 4bab291
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
convert_local_timezone_to_utc,
convert_utc_to_local_timezone,
)
from pkg_resources import declare_namespace

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'declare_namespace' is not used.
from sqlalchemy import CheckConstraint, Index, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSON, JSONB, UUID
from sqlalchemy.ext.associationproxy import association_proxy
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand All @@ -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):
"""
Expand Down
55 changes: 55 additions & 0 deletions migrations/versions/0454_add_template_category.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 4bab291

Please sign in to comment.