Commits

Andy Mikhailenko committed 02a12ee

web: refactored templates to reuse fact listing code.

  • Participants
  • Parent commits 0d36d7d

Comments (0)

Files changed (5)

File timetra/web/__init__.py

 @blueprint.route('/')
 def dashboard():
     # можно storage.get_facts_for_today(), но тогда в 00:00 обрезается в ноль
-    facts = list(reversed(storage.hamster_storage.get_todays_facts()))
+    facts = storage.hamster_storage.get_todays_facts()
     stats = get_stats(facts)
     sleep_drift = drift.collect_drift_data(activity='sleeping', span_days=7)
 

File timetra/web/templates/_helpers.html

 {% macro render_delta(fact) %}
     +{{ fact.delta }}{% if not fact.end_time %}...{% endif %}
 {% endmacro %}
+
+{% macro render_facts(facts, hide_date=False, hide_activity=False, hide_category=False,
+                      hide_tags=False, hide_actions=False) %}
+    <table class="table table-striped ">
+        <thead>
+            <tr>
+                <th>Start</th>
+                <th>Duration</th>
+                {% if not hide_category %}
+                    <th>Category</th>
+                {% endif %}
+                {% if not hide_activity %}
+                    <th>Activity</th>
+                {% endif %}
+                <th>Description</th>
+                {% if not hide_tags %}
+                    <th>Tags</th>
+                {% endif %}
+                {% if not hide_actions %}
+                    <th>Actions</th>
+                {% endif %}
+            </tr>
+        </thead>
+        <tbody>
+            {% for fact in facts %}
+                <tr>
+                    <td>
+                        {% if hide_date %}
+                            {{ fact.start_time.strftime('%H:%M') }}
+                        {% else %}
+                            {{ fact.start_time.strftime('%d.%m.%Y %H:%M') }}
+                        {% endif %}
+                    </td>
+
+                    <td>{{ render_delta(fact) }}</td>
+
+                    {% if not hide_category %}
+                        <td>{{ fact.category }}</td>
+                    {% endif %}
+
+                    {% if not hide_activity %}
+                        <td><a href="{{ url_for('timetra.activity', activity=fact.activity) }}" class="{# label label-{{ appraise_category(fact.category) }} #}">{{ fact.activity }}</a></td>
+                    {% endif %}
+
+                    <td>{% if fact.description %}{{ fact.description|replace('\n','<br>')|safe|urlize }}{% endif %}</td>
+                    {% if not hide_category %}
+                        <td>{{ render_tags(fact.tags) }}</td>
+                    {% endif %}
+
+                    {% if not hide_actions %}
+                        <td><a href="{{ url_for('timetra.edit_fact', fact_id=fact.id) }}"><i class="icon-pencil"></i></a></td>
+                    {% endif %}
+
+                </tr>
+            {% endfor %}
+        <tbody>
+    </table>
+{% endmacro %}

File timetra/web/templates/activity.html

 {% extends 'base.html' %}
 
-{% from "_helpers.html" import render_tags, render_delta %}
+{% from "_helpers.html" import render_tags, render_delta, render_facts %}
 
 {% block heading %} {{ activity.capitalize() }} Log {% endblock %}
 {% block content %}
-    <table class="table table-striped ">
-        <thead>
-            <tr>
-                <th>Start Time</th>
-                <th>Duration</th>
-                <th>Description</th>
-                <th>Tags</th>
-            </tr>
-        </thead>
-        <tbody>
-            {% for fact in facts %}
-                <tr>
-                    <td>{{ fact.start_time.strftime('%d.%m.%Y %H:%M') }}</td>
-                    <td>{{ render_delta(fact) }}</td>
-                    <td>{{ fact.description or '' }}</td>
-                    <td>{{ render_tags(fact.tags) }}</td>
-                </tr>
-            {% endfor %}
-        </tbody>
-    </table>
+    <p>Found {{ facts|count }} facts.</p>
+    {{ render_facts(facts, hide_activity=True, hide_category=True) }}
 {% endblock %}

File timetra/web/templates/dashboard.html

 {% extends 'base.html' %}
 
-{% from "_helpers.html" import render_tags, render_delta %}
+{% from "_helpers.html" import render_tags, render_facts %}
 
 {% block heading %} Dashboard: What's Up Today {% endblock %}
 {% block content %}
     </div>
 
     <h2>History</h2>
-    <table class="table table-striped ">
-        <thead>
-            <tr>
-                <th>Time</th>
-                <th>Duration</th>
-                <th>Activity</th>
-                <th>Description</th>
-                <th>Tags</th>
-                <th>Actions</th>
-            </tr>
-        </thead>
-        <tbody>
-            {% for fact in facts %}
-                <tr>
-                    <td>{{ fact.start_time.strftime('%H:%M') }}</td>
-                    <td>{{ render_delta(fact) }}</td>
-                    <td><a href="{{ url_for('timetra.activity', activity=fact.activity) }}" class="label label-{{ appraise_category(fact.category) }}">{{ fact.activity }}</a></td>
-                    <td>{% if fact.description %}{{ fact.description|replace('\n','<br>')|safe|urlize }}{% endif %}</td>
-                    <td>{{ render_tags(fact.tags) }}</td>
-                    <td><a href="{{ url_for('timetra.edit_fact', fact_id=fact.id) }}"><i class="icon-pencil"></i></a></td>
 
-                </tr>
-            {% endfor %}
-        <tbody>
-    </table>
+    {{ render_facts(facts|reverse, hide_date=True) }}
+
 {% endblock %}

File timetra/web/templates/search.html

 {% extends 'base.html' %}
 
-{% from "_helpers.html" import render_tags, render_delta %}
+{% from "_helpers.html" import render_tags, render_delta, render_facts %}
 
 {% block heading %} Search results {% endblock %}
 {% block content %}
-    <table class="table table-striped ">
-        <thead>
-            <tr>
-                <th>Start Time</th>
-                <th>Duration</th>
-                <th>Activity</th>
-                <th>Description</th>
-                <th>Tags</th>
-            </tr>
-        </thead>
-        <tbody>
-            {% for fact in facts %}
-                <tr>
-                    <td>{{ fact.start_time.strftime('%d.%m.%Y %H:%M') }}</td>
-                    <td>{{ render_delta(fact) }}</td>
-                    <td><a href="{{ url_for('timetra.activity', activity=fact.activity) }}">{{ fact.activity }}</a>@{{ fact.category }}</td>
-                    <td>{% if fact.description %}{{ fact.description|urlize }}{% endif %}</td>
-                    <td>{{ render_tags(fact.tags) }}</td>
-                </tr>
-            {% endfor %}
-        </tbody>
-    </table>
+    <p>Found {{ facts|count }} facts.</p>
+    {{ render_facts(facts) }}
 {% endblock %}