Skip to content

Commit

Permalink
Add org_id to Email Branding Table (#2128)
Browse files Browse the repository at this point in the history
* Add organisation_id to email_branding

* fix format

* fix

* test updates

* fixes
  • Loading branch information
jzbahrai authored Mar 7, 2024
1 parent b33730a commit 2564504
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
13 changes: 12 additions & 1 deletion app/dao/organisation_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

from app import db
from app.dao.dao_utils import transactional, version_class
from app.models import Domain, InvitedOrganisationUser, Organisation, Service, User
from app.models import (
Domain,
EmailBranding,
InvitedOrganisationUser,
Organisation,
Service,
User,
)


def dao_get_organisations():
Expand Down Expand Up @@ -55,6 +62,10 @@ def dao_update_organisation(organisation_id, **kwargs):
domains = kwargs.pop("domains", None)

num_updated = Organisation.query.filter_by(id=organisation_id).update(kwargs)
if "email_branding_id" in kwargs:
email_brand = EmailBranding.query.filter_by(id=kwargs["email_branding_id"]).one()
org = Organisation.query.get(organisation_id)
org.email_branding = email_brand

if isinstance(domains, list):
Domain.query.filter_by(organisation_id=organisation_id).delete()
Expand Down
8 changes: 6 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ class EmailBranding(BaseModel):
nullable=False,
default=BRANDING_ORG_NEW,
)
organisation_id = db.Column(
UUID(as_uuid=True), db.ForeignKey("organisation.id", ondelete="SET NULL"), index=True, nullable=True
)
organisation = db.relationship("Organisation", back_populates="email_branding", foreign_keys=[organisation_id])

def serialize(self) -> dict:
serialized = {
Expand All @@ -285,6 +289,7 @@ def serialize(self) -> dict:
"name": self.name,
"text": self.text,
"brand_type": self.brand_type,
"organisation_id": str(self.organisation_id) if self.organisation_id else "",
}

return serialized
Expand Down Expand Up @@ -449,10 +454,9 @@ class Organisation(BaseModel):
"Domain",
)

email_branding = db.relationship("EmailBranding")
email_branding = db.relationship("EmailBranding", uselist=False)
email_branding_id = db.Column(
UUID(as_uuid=True),
db.ForeignKey("email_branding.id"),
nullable=True,
)

Expand Down
46 changes: 46 additions & 0 deletions migrations/versions/0445_add_org_id_branding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Revision ID: 0445_add_org_id_branding
Revises: 0444_add_index_n_history2.py
Create Date: 2024-02-27
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision = "0445_add_org_id_branding"
down_revision = "0444_add_index_n_history2"


def upgrade():
op.add_column(
"email_branding",
sa.Column("organisation_id", postgresql.UUID(as_uuid=True), nullable=True),
)
op.create_index(
op.f("ix_email_branding_organisation_id"),
"email_branding",
["organisation_id"],
unique=False,
)
op.create_foreign_key(
"fk_email_branding_organisation",
"email_branding",
"organisation",
["organisation_id"],
["id"],
ondelete="SET NULL",
)
op.drop_constraint("fk_organisation_email_branding_id", "organisation", type_="foreignkey")


def downgrade():
op.drop_index(op.f("ix_email_branding_organisation_id"), table_name="email_branding")
op.drop_constraint("fk_email_branding_organisation", "email_branding", type_="foreignkey")
op.drop_column("email_branding", "organisation_id")
op.create_foreign_key(
"fk_organisation_email_branding_id",
"organisation",
"email_branding",
["email_branding_id"],
["id"],
)
7 changes: 5 additions & 2 deletions tests/app/email_branding/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from tests.app.db import create_email_branding


def test_get_email_branding_options(admin_request, notify_db, notify_db_session):
email_branding1 = EmailBranding(colour="#FFFFFF", logo="/path/image.png", name="Org1")
def test_get_email_branding_options(admin_request, notify_db, notify_db_session, sample_organisation):
email_branding1 = EmailBranding(colour="#FFFFFF", logo="/path/image.png", name="Org1", organisation_id=sample_organisation.id)
email_branding2 = EmailBranding(colour="#000000", logo="/path/other.png", name="Org2")
notify_db.session.add_all([email_branding1, email_branding2])
notify_db.session.commit()
Expand All @@ -17,6 +17,8 @@ def test_get_email_branding_options(admin_request, notify_db, notify_db_session)
str(email_branding1.id),
str(email_branding2.id),
}
assert email_branding[0]["organisation_id"] == str(sample_organisation.id)
assert email_branding[1]["organisation_id"] == ""


def test_get_email_branding_by_id(admin_request, notify_db, notify_db_session):
Expand All @@ -37,6 +39,7 @@ def test_get_email_branding_by_id(admin_request, notify_db, notify_db_session):
"id",
"text",
"brand_type",
"organisation_id",
}
assert response["email_branding"]["colour"] == "#FFFFFF"
assert response["email_branding"]["logo"] == "/path/image.png"
Expand Down

0 comments on commit 2564504

Please sign in to comment.