Skip to content

Commit 6eb0bfa

Browse files
author
Richard Jones
committed
added ability to implement new templating utility methods
1 parent 5fe04e3 commit 6eb0bfa

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ are given with the most recent entry first.
2020
- fixed history to display username instead of userid
2121
- shipped templates didn't import all hyperdb types in dbinit.py
2222
- fixed bug in Interval serialisation
23+
- handle "unset" status in status auditor (sf bug 621250)
24+
- issues in 'done-cbb' are now also moved to 'chatting' on new messages
25+
- implemented the missing Interval.__add__
26+
- added ability to implement new templating utility methods
2327

2428

2529
2002-10-02 0.5.0

doc/customizing.txt

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Customising Roundup
33
===================
44

5-
:Version: $Revision: 1.56 $
5+
:Version: $Revision: 1.57 $
66

77
.. This document borrows from the ZopeBook section on ZPT. The original is at:
88
http://www.zope.org/Documentation/Books/ZopeBook/current/ZPT.stx
@@ -1334,14 +1334,21 @@ or the python expression::
13341334
The utils variable
13351335
~~~~~~~~~~~~~~~~~~
13361336

1337-
Note: this is implemented by the roundup.cgi.templating.TemplatingUtils class.
1337+
Note: this is implemented by the roundup.cgi.templating.TemplatingUtils class,
1338+
but it may be extended as described below.
13381339

13391340
=============== =============================================================
13401341
Method Description
13411342
=============== =============================================================
13421343
Batch return a batch object using the supplied list
13431344
=============== =============================================================
13441345

1346+
You may add additional utility methods by writing them in your tracker
1347+
``interfaces.py`` module's ``TemplatingUtils`` class. See `adding a time log
1348+
to your issues`_ for an example. The TemplatingUtils class itself will have a
1349+
single attribute, ``client``, which may be used to access the ``client.db``
1350+
when you need to perform arbitrary database queries.
1351+
13451352
Batching
13461353
::::::::
13471354

@@ -2520,10 +2527,35 @@ able to give a summary of the total time spent on a particular issue.
25202527
isn't it handy? The important change is setting the action to
25212528
"edit_with_timelog" for edit operations (where the item exists)
25222529

2523-
6. Display the time log for an issue::
2530+
6. We want to display a total of the time log times that have been accumulated
2531+
for an issue. To do this, we'll need to actually write some Python code,
2532+
since it's beyond the scope of PageTemplates to perform such calculations.
2533+
We do this by adding a method to the TemplatingUtils class in our tracker
2534+
``interfaces.py`` module::
2535+
2536+
2537+
class TemplatingUtils:
2538+
''' Methods implemented on this class will be available to HTML
2539+
templates through the 'utils' variable.
2540+
'''
2541+
def totalTimeSpent(self, times):
2542+
''' Call me with a list of timelog items (which have an Interval
2543+
"period" property)
2544+
'''
2545+
total = Interval('')
2546+
for time in times:
2547+
total += time.period._value
2548+
return total
2549+
2550+
As indicated in the docstrings, we will be able to access the
2551+
``totalTimeSpent`` method via the ``utils`` variable in our templates. See
2552+
2553+
7. Display the time log for an issue::
25242554

25252555
<table class="otherinfo" tal:condition="context/times">
2526-
<tr><th colspan="3" class="header">Time Log</th></tr>
2556+
<tr><th colspan="3" class="header">Time Log
2557+
<tal:block tal:replace="python:utils.totalTimeSpent(context.times)" />
2558+
</th></tr>
25272559
<tr><th>Date</th><th>Period</th><th>Logged By</th></tr>
25282560
<tr tal:repeat="time context/times">
25292561
<td tal:content="time/creation"></td>
@@ -2532,9 +2564,12 @@ able to give a summary of the total time spent on a particular issue.
25322564
</tr>
25332565
</table>
25342566

2535-
I put this just above the Messages log in my issue display.
2567+
I put this just above the Messages log in my issue display. Note our use
2568+
of the ``totalTimeSpent`` method which will total up the times for the
2569+
issue and return a new Interval. That will be automatically displayed in
2570+
the template as text like "+ 1y 2:40" (1 year, 2 hours and 40 minutes).
25362571

2537-
6. If you're using a persistent web server - roundup-server or mod_python for
2572+
8. If you're using a persistent web server - roundup-server or mod_python for
25382573
example - then you'll need to restart that to pick up the code changes.
25392574
When that's done, you'll be able to use the new time logging interface.
25402575

roundup/cgi/templating.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,27 @@ class RoundupPageTemplate(PageTemplate.PageTemplate):
130130
The current tracker config.
131131
*db*
132132
The current database, used to access arbitrary database items.
133+
*utils*
134+
This is a special class that has its base in the TemplatingUtils
135+
class in this file. If the tracker interfaces module defines a
136+
TemplatingUtils class then it is mixed in, overriding the methods
137+
in the base class.
133138
'''
134139
def getContext(self, client, classname, request):
140+
# construct the TemplatingUtils class
141+
utils = TemplatingUtils
142+
if hasattr(client.instance.interfaces, 'TemplatingUtils'):
143+
class utils(client.instance.interfaces.TemplatingUtils, utils):
144+
pass
145+
135146
c = {
136147
'options': {},
137148
'nothing': None,
138149
'request': request,
139150
'db': HTMLDatabase(client),
140151
'config': client.instance.config,
141152
'tracker': client.instance,
142-
'utils': TemplatingUtils(client),
153+
'utils': utils(client),
143154
'templates': Templates(client.instance.config.TEMPLATES),
144155
}
145156
# add in the item if there is one

roundup/templates/classic/interfaces.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: interfaces.py,v 1.15 2002-09-09 23:55:19 richard Exp $
18+
# $Id: interfaces.py,v 1.16 2002-10-11 01:26:43 richard Exp $
1919

2020
from roundup import mailgw
2121
from roundup.cgi import client
@@ -26,6 +26,12 @@ class Client(client.Client):
2626
'''
2727
pass
2828

29+
class TemplatingUtils:
30+
''' Methods implemented on this class will be available to HTML templates
31+
through the 'utils' variable.
32+
'''
33+
pass
34+
2935
class MailGW(mailgw.MailGW):
3036
''' derives basic mail gateway implementation from the standard module,
3137
with any specific extensions

roundup/templates/minimal/interfaces.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: interfaces.py,v 1.1 2002-09-26 04:15:07 richard Exp $
18+
# $Id: interfaces.py,v 1.2 2002-10-11 01:26:43 richard Exp $
1919

2020
from roundup import mailgw
2121
from roundup.cgi import client
@@ -26,6 +26,12 @@ class Client(client.Client):
2626
'''
2727
pass
2828

29+
class TemplatingUtils:
30+
''' Methods implemented on this class will be available to HTML templates
31+
through the 'utils' variable.
32+
'''
33+
pass
34+
2935
class MailGW(mailgw.MailGW):
3036
''' derives basic mail gateway implementation from the standard module,
3137
with any specific extensions

0 commit comments

Comments
 (0)