Skip to content

Commit 229b7d6

Browse files
committed
another one
1 parent b46a8f9 commit 229b7d6

7 files changed

Lines changed: 156 additions & 18 deletions

File tree

indico/modules/email_templates/blueprint.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
# LICENSE file for more details.
77

88
from indico.modules.email_templates.controllers import (RHAddCategoryEmailTemplate, RHAddEventEmailTemplate,
9-
RHListCategoryEmailTemplates, RHListEventEmailTemplates,
10-
RHEditEmailTemplate, RHDeleteEmailTemplate,
11-
RHCloneEventEmailTemplate, RHCloneCategoryEmailTemplate,
12-
RHCloneEventSystemEmailTemplate,
13-
RHCloneCategorySystemEmailTemplate, RHViewEmailTemplate)
9+
RHCloneCategoryEmailTemplate,
10+
RHCloneCategorySystemEmailTemplate, RHCloneEventEmailTemplate,
11+
RHCloneEventSystemEmailTemplate, RHDeleteEmailTemplate,
12+
RHEditEmailTemplate, RHListCategoryEmailTemplates,
13+
RHListEventEmailTemplates, RHViewCategoryEmailTemplate,
14+
RHViewEmailTemplate, RHViewEventEmailTemplate)
1415
from indico.util.caching import memoize
1516
from indico.web.flask.util import make_view_func
1617
from indico.web.flask.wrappers import IndicoBlueprint
1718

19+
1820
_bp = IndicoBlueprint('email_templates', __name__, template_folder='templates',
1921
virtual_template_folder='email_templates')
2022

@@ -46,7 +48,8 @@ def view_func(**kwargs):
4648
_dispatch(RHCloneEventEmailTemplate, RHCloneCategoryEmailTemplate),
4749
defaults={'object_type': object_type}, methods=('GET', 'POST'))
4850
_bp.add_url_rule(f'{prefix}/<email_template_name>/view-system-template', 'view_system_email_template',
49-
RHViewEmailTemplate, defaults={'object_type': object_type}, methods=('GET', 'POST'))
51+
_dispatch(RHViewEventEmailTemplate, RHViewCategoryEmailTemplate),
52+
defaults={'object_type': object_type}, methods=('GET', 'POST'))
5053
_bp.add_url_rule(f'{prefix}/<email_template_name>/system-template/clone', 'clone_system_email_template',
5154
_dispatch(RHCloneEventSystemEmailTemplate, RHCloneCategorySystemEmailTemplate),
5255
defaults={'object_type': object_type}, methods=('GET', 'POST'))

indico/modules/email_templates/controllers.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
from copy import deepcopy
99

10-
from flask import flash, request, session
10+
from flask import flash, request, session, jsonify, render_template
1111
from markupsafe import Markup
1212
from werkzeug.exceptions import Forbidden, NotFound
1313

1414
from indico.core.db import db
1515
from indico.modules.categories import Category
1616
from indico.modules.categories.controllers.base import RHManageCategoryBase
1717
from indico.modules.designer.forms import CloneTemplateForm
18+
from indico.modules.email_templates.dummy import get_dummy_registration
1819
from indico.modules.email_templates.forms import CreateEmailTemplatesForm, EditEmailTemplatesForm
1920
from indico.modules.email_templates.models.email_templates import EmailTemplate
2021
from indico.modules.email_templates.util import get_inherited_templates, get_system_templates
@@ -123,19 +124,26 @@ def _process(self):
123124
return jsonify_form(form, disabled_until_change=False)
124125

125126

126-
class RHViewEmailTemplate(AddEmailTemplateMixin, RHProtected):
127+
class RHViewEmailTemplate(AddEmailTemplateMixin):
127128
def _process_args(self):
129+
super()._process_args()
128130
self.email_template_name = request.view_args['email_template_name']
129131

130132
def _process(self):
131-
tpl = get_template_module(f'events/registration/emails/{self.email_template_name}', registration=None,
133+
title = self.email_template_name.replace('_', ' ').title()
134+
filename = get_system_templates(self.email_template_name)
135+
dummy = get_dummy_registration()
136+
tpl = get_template_module(f'events/registration/emails/{filename}', registration=dummy,
132137
attach_rejection_reason='{rejection_reason}', diff=None, old_price=None,
133138
receipt=None)
134-
form = EditEmailTemplatesForm()
135-
if form.validate_on_submit():
136-
flash(_("Added new email template '{}'").format(), 'success')
137-
return jsonify_data(html=_render_template_list(self.target, event=self.event_or_none))
138-
return jsonify_form(form, disabled_until_change=False)
139+
# email_body = replace_placeholders('accreditation-email', request.form['body'], accreditation=accreditation)
140+
# email_subject = replace_placeholders('accreditation-email', request.form['subject'],
141+
# accreditation=accreditation)
142+
# tpl = get_template_module('events/registration/emails/custom_email.html', email_subject=email_subject,
143+
# email_body=email_body)
144+
html = render_template('email_templates/preview.html', title=title, type='Registration',
145+
subject=tpl.get_subject(), body=tpl.get_body())
146+
return jsonify(html=html)
139147

140148

141149
class RHListEventEmailTemplates(EmailTemplateListMixin, RHManageEventBase):
@@ -154,6 +162,14 @@ class RHAddCategoryEmailTemplate(AddEmailTemplateMixin, RHManageCategoryBase):
154162
pass
155163

156164

165+
class RHViewEventEmailTemplate(RHViewEmailTemplate, RHManageEventBase):
166+
pass
167+
168+
169+
class RHViewCategoryEmailTemplate(RHViewEmailTemplate, RHManageCategoryBase):
170+
pass
171+
172+
157173
class RHModifyEmailTemplateBase(SpecificEmailTemplateMixin, RHProtected):
158174
def _check_access(self):
159175
RHProtected._check_access(self)
@@ -187,7 +203,6 @@ def _check_access(self):
187203
elif isinstance(self.target, Event):
188204
check_event_locked(self, self.target)
189205

190-
191206
def _process_args(self):
192207
self.email_tpl = EmailTemplate.get_or_404(request.view_args['email_template_id'])
193208
if self.target.is_deleted:
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# This file is part of Indico.
2+
# Copyright (C) 2002 - 2025 CERN
3+
#
4+
# Indico is free software; you can redistribute it and/or
5+
# modify it under the terms of the MIT License; see the
6+
# LICENSE file for more details.
7+
from datetime import datetime
8+
9+
from _decimal import Decimal
10+
from pytz import timezone
11+
12+
from indico.util.date_time import format_currency
13+
from indico.util.locators import locator_property
14+
15+
16+
class Dummy:
17+
"""Lightweight dummy object used in many Indico tests."""
18+
pass
19+
20+
def get_data(self):
21+
first_name = Dummy()
22+
first_name.html_field_name = 'first_name'
23+
last_name = Dummy()
24+
last_name.html_field_name = 'last_name'
25+
date_of_birth = Dummy()
26+
date_of_birth.html_field_name = 'date_of_birth'
27+
sex = Dummy()
28+
country = Dummy()
29+
email_address = Dummy()
30+
31+
def get_summary_data(self, hide_empty=False):
32+
# personal_data_section
33+
personal_data_section = Dummy()
34+
personal_data_section.title = 'Personal Data'
35+
personal_data_section.is_manager_only = False
36+
return {}
37+
38+
def render_price(self):
39+
return format_currency(self.price, self.currency)
40+
41+
def get_locator_dict(self):
42+
return dict(reg_form_id=self.registration_form.id, registration_id=self.id, event_id=self.event.id)
43+
44+
@locator_property
45+
def locator(self):
46+
return self.get_locator_dict()
47+
48+
@locator.registrant
49+
def locator(self):
50+
locator = self.get_locator_dict()
51+
del locator['registration_id']
52+
locator['token'] = 'd8343'
53+
return locator
54+
55+
56+
def get_dummy_registration():
57+
"""Return a full dummy registration object using Dummy class."""
58+
reg = Dummy()
59+
60+
# Basic fields
61+
reg.id = 123
62+
reg.first_name = 'John'
63+
reg.last_name = 'Doe'
64+
reg.full_name = f'{reg.first_name} {reg.last_name}'
65+
reg.email = 'john.doe@example.com'
66+
reg.price = 150
67+
reg.friendly_id = 123
68+
69+
# state object
70+
reg.state = Dummy()
71+
reg.state.title = 'Complete'
72+
73+
reg.data = []
74+
75+
reg.summary_data = {}
76+
77+
# Methods
78+
reg.get_full_name = lambda: reg.full_name
79+
reg.get_base_price = lambda: reg.price
80+
81+
# Registration Form
82+
reg.registration_form = Dummy()
83+
reg.registration_form.id = 123
84+
reg.registration_form.title = 'Demo Registration Form'
85+
reg.registration_form.currency = 'CHF'
86+
reg.price = Decimal('0')
87+
reg.currency = 'EUR'
88+
89+
90+
# Event
91+
reg.event = Dummy()
92+
reg.event.id = 123
93+
reg.event.title = 'Demo Conference 2025'
94+
reg.event.start_dt = datetime.fromisoformat('2025-02-15 09:00')
95+
reg.event.end_dt = datetime.fromisoformat('2025-02-17 17:00')
96+
reg.event.timezone = 'Europe/Zurich'
97+
reg.event.external_url = 'www.example.com'
98+
reg.event.start_dt_local = reg.event.start_dt
99+
reg.event.end_dt_local = reg.event.end_dt
100+
reg.event.tzinfo = timezone(reg.event.timezone)
101+
reg.registration_form.event = reg.event
102+
return reg

indico/modules/email_templates/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from indico.util.i18n import _
1313
from indico.util.placeholders import render_placeholder_info
1414
from indico.web.forms.base import IndicoForm
15-
from indico.web.forms.validators import NoRelativeURLs, HiddenUnless
15+
from indico.web.forms.validators import HiddenUnless, NoRelativeURLs
1616
from indico.web.forms.widgets import TinyMCEWidget
1717

1818

indico/modules/email_templates/models/email_templates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sqlalchemy.ext.hybrid import Comparator, hybrid_property
1010

1111
from indico.core.db import db
12-
from indico.modules.logs import EventLogRealm, CategoryLogRealm
12+
from indico.modules.logs import CategoryLogRealm, EventLogRealm
1313
from indico.util.locators import locator_property
1414
from indico.util.string import format_repr
1515

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% from 'message_box.html' import message_box %}
2+
3+
{% call message_box('highlight', fixed_width=true) %}
4+
{% trans -%}
5+
This preview is only shown for a dummy registration.<br>
6+
When sending the emails, registrant will receive an email
7+
customized with the data of their registration.
8+
{%- endtrans %}
9+
{% endcall %}
10+
<span class="ui label basic blue">{{ type }}</span>
11+
<h2>{{ title }}</h2>
12+
<strong>{% trans %}Subject{% endtrans %}</strong>
13+
<pre class="mono">{{ subject | forceescape }}</pre>
14+
<br>
15+
<strong>{% trans %}Body{% endtrans %}</strong>
16+
<div>{{ body }}</div>

indico/modules/email_templates/util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ def get_inherited_templates(obj):
2121
return get_all_templates(obj) - set(obj.email_templates)
2222

2323

24-
def get_system_templates():
24+
def get_system_templates(email_template_name=None):
2525
"""Get all templates hardcoded in the registration module system."""
2626
registration_templates = {
2727
'registration_creation': 'registration_creation_to_registrant.html',
2828
'registration_modification': 'registration_modification_to_registrant.html',
2929
'registration_status_update': 'registration_state_update_to_registrant.html',
3030
'registration_receipt': 'registration_receipt_created_to_registrant.html'
3131
}
32+
if email_template_name:
33+
return registration_templates.get(email_template_name)
3234
return registration_templates

0 commit comments

Comments
 (0)