|
| 1 | +"""Brandify transactional email templates (subjects + masthead + body) |
| 2 | +
|
| 3 | +Revision ID: f3b1c2d4a5e6 |
| 4 | +Revises: e9a1c3b7d2f4 |
| 5 | +Create Date: 2026-05-02 14:00:00.000000 |
| 6 | +
|
| 7 | +Refreshes the four seeded email_templates rows in place so they read brand |
| 8 | +from the render context with a tenant.name fallback. The render path now |
| 9 | +populates `brand` whenever a transactional email is triggered through a |
| 10 | +Brand's host. Without this update, production keeps the original Fief seed |
| 11 | +(masthead links to https://example.com, subjects say "OpenSensor.io") |
| 12 | +because EmailTemplateInitializer only seeds rows when missing. |
| 13 | +
|
| 14 | +NOTE: this overwrites any customization made via the admin UI for these |
| 15 | +four template types. Current DB content matches the unmodified Fief seed, |
| 16 | +so this is safe at the time of writing. |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +import sqlalchemy as sa |
| 23 | +from alembic import op |
| 24 | + |
| 25 | +# revision identifiers, used by Alembic. |
| 26 | +revision = "f3b1c2d4a5e6" |
| 27 | +down_revision = "e9a1c3b7d2f4" |
| 28 | +branch_labels = None |
| 29 | +depends_on = None |
| 30 | + |
| 31 | + |
| 32 | +# Templates dir relative to this migration: fief/alembic/versions/X.py |
| 33 | +# parents[2] resolves to the `fief` package root. |
| 34 | +_TEMPLATES_DIR = ( |
| 35 | + Path(__file__).resolve().parents[2] / "services" / "email_template" / "templates" |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +_NEW_SUBJECTS = { |
| 40 | + "WELCOME": "Welcome to {{ brand.name if brand else tenant.name }}", |
| 41 | + "VERIFY_EMAIL": ( |
| 42 | + "Verify your email for your {{ brand.name if brand else tenant.name }} account" |
| 43 | + ), |
| 44 | + "FORGOT_PASSWORD": "Reset your {{ brand.name if brand else tenant.name }} password", |
| 45 | +} |
| 46 | + |
| 47 | +_PRIOR_SUBJECTS = { |
| 48 | + "WELCOME": "Welcome to {{ tenant.name }}", |
| 49 | + "VERIFY_EMAIL": "Verify your email for your {{ tenant.name }}'s account", |
| 50 | + "FORGOT_PASSWORD": "Reset your {{ tenant.name }}'s password", |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +def _read_template(filename: str) -> str: |
| 55 | + return (_TEMPLATES_DIR / filename).read_text() |
| 56 | + |
| 57 | + |
| 58 | +def upgrade(): |
| 59 | + table_prefix = op.get_context().opts["table_prefix"] |
| 60 | + table = f"{table_prefix}email_templates" |
| 61 | + |
| 62 | + op.execute( |
| 63 | + sa.text(f"UPDATE {table} SET content = :c WHERE type = 'BASE'").bindparams( |
| 64 | + c=_read_template("base.html") |
| 65 | + ) |
| 66 | + ) |
| 67 | + op.execute( |
| 68 | + sa.text( |
| 69 | + f"UPDATE {table} SET subject = :s, content = :c WHERE type = 'WELCOME'" |
| 70 | + ).bindparams(s=_NEW_SUBJECTS["WELCOME"], c=_read_template("welcome.html")) |
| 71 | + ) |
| 72 | + op.execute( |
| 73 | + sa.text( |
| 74 | + f"UPDATE {table} SET subject = :s, content = :c WHERE type = 'VERIFY_EMAIL'" |
| 75 | + ).bindparams( |
| 76 | + s=_NEW_SUBJECTS["VERIFY_EMAIL"], c=_read_template("verify_email.html") |
| 77 | + ) |
| 78 | + ) |
| 79 | + op.execute( |
| 80 | + sa.text( |
| 81 | + f"UPDATE {table} " |
| 82 | + "SET subject = :s, content = :c WHERE type = 'FORGOT_PASSWORD'" |
| 83 | + ).bindparams( |
| 84 | + s=_NEW_SUBJECTS["FORGOT_PASSWORD"], |
| 85 | + c=_read_template("forgot_password.html"), |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def downgrade(): |
| 91 | + # Best-effort downgrade: restore the prior subject lines. We cannot |
| 92 | + # restore the prior content bodies precisely without bundling them here, |
| 93 | + # so a roll-back leaves the brand-aware content in place — harmless |
| 94 | + # because a None brand falls back to tenant.name. |
| 95 | + table_prefix = op.get_context().opts["table_prefix"] |
| 96 | + table = f"{table_prefix}email_templates" |
| 97 | + for type_, subject in _PRIOR_SUBJECTS.items(): |
| 98 | + op.execute( |
| 99 | + sa.text(f"UPDATE {table} SET subject = :s WHERE type = :t").bindparams( |
| 100 | + s=subject, t=type_ |
| 101 | + ) |
| 102 | + ) |
0 commit comments