[hamster-applet] putting back totals in report. fix by Jerome Lacoste and klingger-at-great.sk



commit 77475faa9938d0b542e66902d641202bc8015576
Author: jerome.lacoste <jerome lacoste gmail com>
Date:   Sat Nov 6 01:35:39 2010 +0000

    putting back totals in report. fix by Jerome Lacoste and klingger-at-great.sk

 data/report_template.html |  184 ++++++++++++++++++++++++--------------------
 src/hamster/reports.py    |   44 +++++++++++-
 2 files changed, 143 insertions(+), 85 deletions(-)
---
diff --git a/data/report_template.html b/data/report_template.html
index 44ff60d..0485ed4 100644
--- a/data/report_template.html
+++ b/data/report_template.html
@@ -88,100 +88,116 @@
 
 <div id="tabs">
 	<ul>
-            <li><a href="#tabs-1">$totals_by_day_title</a></li>
+            <li><a href="#tabs-1">$totals_title</a></li>
             <li><a href="#tabs-2">$activity_log_title</a></li>
 	</ul>
 	<div id="tabs-1">
-            <div class="by_day_controls">
-                $show_prompt
-                <input type="checkbox" id="show_details" checked="checked"></input>
-                <label for="show_details">$activity_totals_heading</label>
-
-                <input type="checkbox" id="show_totals" checked="checked"></input>
-                <label for="show_totals">$category_totals_heading</label>
-            </div>
-
-            <table>
-                <tr>
-                    <th>Date</th>
-                    <th class="by_date_row">$activity_totals_heading</th>
-                    <th class="by_date_total_row">$category_totals_heading</th>
+        <h2>$totals_title</h2>
+        <table>
+            <tr>
+                <th>$header_category</th>
+                <th>$header_activity</th>
+                <th>$header_duration</th>
+            </tr>
+            <by_activity_category>
+                <tr><td>$category</td><td>$activity</td><td>$duration</td></tr>
+            </by_activity_category>
+            <totals_activity>
+                <tr><th colspan="2" style="text-align:right;">$total_row</th><th>$total_duration</th></tr>
+            </totals_activity>
+        </table>
+
+        <h2>$totals_by_day_title</h2>
+
+        <div class="by_day_controls">
+            $show_prompt
+            <input type="checkbox" id="show_details" checked="checked"></input>
+            <label for="show_details">$activity_totals_heading</label>
+
+            <input type="checkbox" id="show_totals" checked="checked"></input>
+            <label for="show_totals">$category_totals_heading</label>
+        </div>
+
+        <table>
+            <tr>
+                <th>Date</th>
+                <th class="by_date_row">$activity_totals_heading</th>
+                <th class="by_date_total_row">$category_totals_heading</th>
+            </tr>
+
+            <by_date>
+                <tr valign="top">
+                    <td>$date</td>
+                    <td class="by_date_row">
+                        <table width="100%">
+                            <by_date_activity>
+                                <tr>
+                                    <td>$activity ($category)</td>
+                                    <td style="text-align: right">
+                                        <!-- there is also duration_decimal and duration_minutes -->
+                                        $duration
+                                    </td>
+                                </tr>
+                            </by_date_activity>
+                        </table>
+                    </td>
+                    <td class="by_date_total_row">
+                        <table width="100%">
+                            <by_date_category>
+                                <tr>
+                                    <td>$category</td>
+                                    <td style="text-align: right">
+                                        <!-- there is also duration_decimal and duration_minutes -->
+                                        $duration
+                                    </td>
+                                </tr>
+                            </by_date_category>
+                        </table>
+                    </td>
                 </tr>
-
-                <by_date>
-                    <tr valign="top">
-                        <td>$date</td>
-                        <td class="by_date_row">
-                            <table width="100%">
-                                <by_date_activity>
-                                    <tr>
-                                        <td>$activity ($category)</td>
-                                        <td style="text-align: right">
-                                            <!-- there is also duration_decimal and duration_minutes -->
-                                            $duration
-                                        </td>
-                                    </tr>
-                                </by_date_activity>
-                            </table>
-                        </td>
-                        <td class="by_date_total_row">
-                            <table width="100%">
-                                <by_date_category>
-                                    <tr>
-                                        <td>$category</td>
-                                        <td style="text-align: right">
-                                            <!-- there is also duration_decimal and duration_minutes -->
-                                            $duration
-                                        </td>
-                                    </tr>
-                                </by_date_category>
-                            </table>
-                        </td>
-                    </tr>
-                    <tr><td colspan="3">&nbsp;</td></tr>
-                </by_date>
-            </table>
+                <tr><td colspan="3">&nbsp;</td></tr>
+            </by_date>
+        </table>
 	</div>
 	<div id="tabs-2">
-            <table>
+        <table>
+            <tr>
+                <th>$header_date</th>
+                <th>$header_activity</th>
+                <th>$header_category</th>
+                <th>$header_tags</th>
+                <th>$header_start</th>
+                <th>$header_end</th>
+                <th>$header_duration</th>
+                <th>$header_description</th>
+            </tr>
+            <all_activities>
                 <tr>
-                    <th>$header_date</th>
-                    <th>$header_activity</th>
-                    <th>$header_category</th>
-                    <th>$header_tags</th>
-                    <th>$header_start</th>
-                    <th>$header_end</th>
-                    <th>$header_duration</th>
-                    <th>$header_description</th>
+                    <td>
+                        <!-- there is also date_iso -->
+                        $date
+                    </td>
+                    <td>$activity</td>
+                    <td>$category</td>
+                    <td>$tags</td>
+                    <td>
+                        <!-- there is also start_iso -->
+                        $start
+                    </td>
+                    <td>
+                        <!-- there is also end_iso -->
+                        $end
+                    </td>
+                    <td>
+                        <!-- there is also duration_decimal and duration_minutes -->
+                        $duration
+                    </td>
+                    <td>$description</td>
                 </tr>
-                <all_activities>
-                    <tr>
-                        <td>
-                            <!-- there is also date_iso -->
-                            $date
-                        </td>
-                        <td>$activity</td>
-                        <td>$category</td>
-                        <td>$tags</td>
-                        <td>
-                            <!-- there is also start_iso -->
-                            $start
-                        </td>
-                        <td>
-                            <!-- there is also end_iso -->
-                            $end
-                        </td>
-                        <td>
-                            <!-- there is also duration_decimal and duration_minutes -->
-                            $duration
-                        </td>
-                        <td>$description</td>
-                    </tr>
-                </all_activities>
-            </table>
+            </all_activities>
+        </table>
 	</div>
 </div>
-
 <p class="template-instructions">
     <a href="file:///$data_dir/report_template.html">$show_template</a>.
     $template_instructions.
diff --git a/src/hamster/reports.py b/src/hamster/reports.py
index aac6f0b..f1a2253 100644
--- a/src/hamster/reports.py
+++ b/src/hamster/reports.py
@@ -209,6 +209,8 @@ class HTMLWriter(ReportWriter):
 
         self.by_date_row_template = self._extract_template('by_date_activity')
         self.by_date_total_row_template = self._extract_template("by_date_category")
+        self.by_activity_category_row_template = self._extract_template("by_activity_category")
+        self.totals_row_template = self._extract_template("totals_activity")
 
         self.by_date_template = self._extract_template('by_date')
 
@@ -311,12 +313,50 @@ class HTMLWriter(ReportWriter):
                            ))
             by_date.append(res)
 
+        by_activity_category_rows = []
+        # group by activity
+        name_category = lambda fact: (fact.category)
+        name_activity = lambda fact: (fact.activity)
+
+        total_duration = None
+        by_category = sorted(facts, key=name_category)
+        for category, category_facts in itertools.groupby(by_category, lambda fact:fact.category):
+            by_activity = sorted(category_facts, key=name_activity)
+
+            for (activity), ac_facts in itertools.groupby(by_activity, name_activity):
+                duration = dt.timedelta()
+                for fact in ac_facts:
+                    duration += fact.delta
+
+                by_activity_category_rows.append(Template(self.by_activity_category_row_template).safe_substitute(
+                                        dict(activity = activity,
+                                             category = category,
+                                             duration = stuff.format_duration(duration),
+                                             duration_minutes = "%d" % (stuff.duration_minutes(fact.delta)),
+                                             duration_decimal = "%.2f" % (stuff.duration_minutes(fact.delta) / 60.0),
+                                        )
+                                    ))
+                if (total_duration):
+                    total_duration += duration
+                else:
+                    total_duration = duration
+
+        by_totals_rows = []
+        if (len(by_activity_category_rows) > 0):
+            by_totals_rows.append(Template(self.totals_row_template).safe_substitute(
+                                  dict(total_duration = stuff.format_duration(total_duration),
+                                         total_duration_minutes = "%d" % (stuff.duration_minutes(total_duration)),
+                                         total_duration_decimal = "%.2f" % (stuff.duration_minutes(total_duration) / 60.0),
+                                         total_row = _("Total")
+                                    )
+                                ))
 
 
         data = dict(
             title = self.title,
             totals_by_day_title = _("Totals by Day"),
             activity_log_title = _("Activity Log"),
+            totals_title = _("Totals"),
 
             activity_totals_heading = _("totals by activity"),
             category_totals_heading = _("totals by category"),
@@ -337,7 +377,9 @@ class HTMLWriter(ReportWriter):
             template_instructions = _("You can override it by storing your version in %(home_folder)s") % {'home_folder': runtime.home_data_dir},
 
             all_activities_rows = "\n".join(self.fact_rows),
-            by_date_rows = "\n".join(by_date)
+            by_date_rows = "\n".join(by_date),
+            by_activity_category_rows = "\n".join(by_activity_category_rows),
+            totals_activity_rows = "\n".join(by_totals_rows)
         )
         report.write(Template(self.main_template).safe_substitute(data))
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]