|
| 1 | +""" |
| 2 | +
|
| 3 | +Revision ID: 0496_newsletter_templates |
| 4 | +Revises: 0494_update_some_templates |
| 5 | +Create Date: 2025-10-21 00:00:00 |
| 6 | +
|
| 7 | +""" |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +from alembic import op |
| 11 | +from flask import current_app |
| 12 | + |
| 13 | +revision = "0496_newsletter_templates" |
| 14 | +down_revision = "0495_newsletter_conf_templates" |
| 15 | + |
| 16 | +newsletter_template_en_id = current_app.config["NEWSLETTER_EMAIL_TEMPLATE_ID_EN"] |
| 17 | +newsletter_template_fr_id = current_app.config["NEWSLETTER_EMAIL_TEMPLATE_ID_FR"] |
| 18 | +template_ids = [newsletter_template_en_id, newsletter_template_fr_id] |
| 19 | + |
| 20 | +def upgrade(): |
| 21 | + template_insert = """ |
| 22 | + INSERT INTO templates (id, name, template_type, created_at, content, archived, service_id, subject, |
| 23 | + created_by_id, version, process_type, hidden) |
| 24 | + VALUES ('{}', '{}', '{}', '{}', '{}', False, '{}', '{}', '{}', 1, '{}', false) |
| 25 | + """ |
| 26 | + template_history_insert = """ |
| 27 | + INSERT INTO templates_history (id, name, template_type, created_at, content, archived, service_id, subject, |
| 28 | + created_by_id, version, process_type, hidden) |
| 29 | + VALUES ('{}', '{}', '{}', '{}', '{}', False, '{}', '{}', '{}', 1, '{}', false) |
| 30 | + """ |
| 31 | + |
| 32 | + template_content_en = "\n".join( |
| 33 | + [ |
| 34 | + "((newsletter_content))", |
| 35 | + "", |
| 36 | + "[Unsubscribe](((unsubscribe_link))) / [Change language](((change_language_link)))" |
| 37 | + ] |
| 38 | + ) |
| 39 | + |
| 40 | + template_content_fr = "\n".join( |
| 41 | + [ |
| 42 | + "((newsletter_content))", |
| 43 | + "", |
| 44 | + "[Se désabonner](((unsubscribe_link))) / [Changer la langue](((change_language_link)))" |
| 45 | + ] |
| 46 | + ) |
| 47 | + |
| 48 | + templates = [ |
| 49 | + { |
| 50 | + "id": newsletter_template_en_id, |
| 51 | + "name": "EN Notify Newsletter", |
| 52 | + "subject": f"Newsletter ((newsletter_number))", |
| 53 | + "content": template_content_en, |
| 54 | + "template_type": "email", |
| 55 | + "process_type": "normal", |
| 56 | + }, |
| 57 | + { |
| 58 | + "id": newsletter_template_fr_id, |
| 59 | + "name": "FR Notify Newsletter", |
| 60 | + "subject": "Infolettre ((newsletter_number))", |
| 61 | + "content": template_content_fr, |
| 62 | + "template_type": "email", |
| 63 | + "process_type": "normal", |
| 64 | + }, |
| 65 | + ] |
| 66 | + |
| 67 | + for template in templates: |
| 68 | + op.execute( |
| 69 | + template_insert.format( |
| 70 | + template["id"], |
| 71 | + template["name"], |
| 72 | + template["template_type"], |
| 73 | + datetime.utcnow(), |
| 74 | + template["content"], |
| 75 | + current_app.config["NOTIFY_SERVICE_ID"], |
| 76 | + template["subject"], |
| 77 | + current_app.config["NOTIFY_USER_ID"], |
| 78 | + template["process_type"], |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + op.execute( |
| 83 | + template_history_insert.format( |
| 84 | + template["id"], |
| 85 | + template["name"], |
| 86 | + template["template_type"], |
| 87 | + datetime.utcnow(), |
| 88 | + template["content"], |
| 89 | + current_app.config["NOTIFY_SERVICE_ID"], |
| 90 | + template["subject"], |
| 91 | + current_app.config["NOTIFY_USER_ID"], |
| 92 | + template["process_type"], |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | +def downgrade(): |
| 97 | + for template_id in template_ids: |
| 98 | + op.execute("DELETE FROM notifications WHERE template_id = '{}'".format(template_id)) |
| 99 | + op.execute("DELETE FROM notification_history WHERE template_id = '{}'".format(template_id)) |
| 100 | + op.execute("DELETE FROM template_redacted WHERE template_id = '{}'".format(template_id)) |
| 101 | + op.execute("DELETE FROM templates_history WHERE id = '{}'".format(template_id)) |
| 102 | + op.execute("DELETE FROM templates WHERE id = '{}'".format(template_id)) |
| 103 | + |
0 commit comments