Edgewall Software
Modify

Opened 5 years ago

Closed 4 years ago

#13225 closed defect (fixed)

[notification] ticket_subject_template cannot begin with # character

Reported by: Ryan J Ollos Owned by: Ryan J Ollos
Priority: normal Milestone: 1.4.2
Component: general Version:
Severity: normal Keywords:
Cc: Branch:
Release Notes:

Fixed [notification] ticket_subject_template and batch_subject_template cannot begin with # character.

API Changes:
Internal Changes:

Description

Reported in th:comment:8:ticket:13654. The issue is seen with [notification] ticket_subject_template, and likely [notification] batch_subject_template.

[notification]
ticket_subject_template = #${ticket.id}: ${summary}

Need to check if the issue occurs prior to the Jinja2 transition.

Attachments (0)

Change History (12)

comment:1 by Ryan J Ollos, 5 years ago

Summary: ticket_subject_template cannot begin with # character[notification] ticket_subject_template cannot begin with # character

comment:2 by anonymous, 5 years ago

In trac.ticket.notification.TicketFormatter._format_subj and ._format_subj_batchmodify the calls to trac.util.text.jinja2template use line_statement_prefix='#'.

The default line_statement_prefix=None (and similar for line_comment_prefix) is probably better for such single-line templates.

comment:3 by Jun Omae, 5 years ago

I think line_comment_prefix=None should be used by the same reason.

>>> from trac.util.text import jinja2env
>>> data = {'ticket': {'id': 42}}
>>> def jinja2template(template, text=False):
...     return jinja2env(autoescape=not text,
...                      line_statement_prefix=None).from_string(template)
...
>>> jinja2template('${ticket.id}', text=True).render(**data)
u'42'
>>>
>>> jinja2template('#${ticket.id}', text=True).render(**data)
u'#42'
>>> jinja2template('##${ticket.id}', text=True).render(**data)
u''
>>> 
>>> def jinja2template(template, text=False):
...     return jinja2env(autoescape=not text,
...                      line_statement_prefix=None,
...                      line_comment_prefix=None).from_string(template)
...
>>> jinja2template('#${ticket.id}', text=True).render(**data)
u'#42'
>>> jinja2template('##${ticket.id}', text=True).render(**data)
u'##42'

comment:4 by Ryan J Ollos, 5 years ago

Milestone: next-stable-1.4.x1.4.1

comment:5 by Ryan J Ollos, 5 years ago

Milestone: 1.4.11.4.2

comment:6 by Ryan J Ollos, 5 years ago

Owner: set to Ryan J Ollos
Status: newassigned

comment:7 by Ryan J Ollos, 5 years ago

Release Notes: modified (diff)

Added test coverage to anonymous comment:1 change and Jun's comment:2 change: log:rjollos.git:t13325_ticket_subject_template.

comment:8 by Peter Suter, 5 years ago

This could break plugins that need multiline templates with comments or statements, right? (I'm not aware of any such plugins, so maybe not a problem.)

Would a jinja2template(..., oneliner=True) parameter be useful?

For example a (untested) Jinja2TemplateTestCase:

            def test_multiline_text_template(self):
                self.assertEqual("""
## This is a comment in a multiline template with the following statement:
# if True:
<h1>Hell&O</h1>
# endif
""",
                                 jinja2template("<h1>${hell}O</h1>", text=True, oneliner=False).render(
                                     {'hell': 'Hell&'}))

Or is jinja2template inherently intended only for oneliners, and multiline templates should always use chrome.render_template_string instead? Is it limited to text=True?

comment:9 by Ryan J Ollos, 5 years ago

It's a good question. Probably better to add the oneliner=True option unless someone has a good answer otherwise.

comment:10 by Ryan J Ollos, 5 years ago

I'll add oneliner argument to be safe. Thanks for reviewing!

in reply to:  8 comment:11 by Ryan J Ollos, 4 years ago

Replying to Peter Suter:

Or is jinja2template inherently intended only for oneliners, and multiline templates should always use chrome.render_template_string instead?

I think this is the right question. The notification body is rendered using text=True but chrome.render_template_string is used.

A plugin implementing notification might use inline source for the notification body, and that inline source might have statements. The inline source code might be plain text (text=True) or HTML (text=False).

Instead, added a utility function in trac.ticket.notification that avoids explicitly adding an argument to jinja2template. Instead, jinja2template supports pass-through of arguments to jinjaenv: [59709a6e8/rjollos.git]

comment:12 by Ryan J Ollos, 4 years ago

Resolution: fixed
Status: assignedclosed

Committed to 1.4-stable in r17391, merged to trunk in r17392.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Ryan J Ollos.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Ryan J Ollos to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.