Self hosted FLOSS fitness/workout, nutrition and weight tracker
Refactor calendar template code
Roland Geiderb027df603472
diff --git a/wger/manager/templates/calendar/day.html b/wger/manager/templates/calendar/day.htmlindex 20f2e48cd..436fac99f 100644--- a/wger/manager/templates/calendar/day.html+++ b/wger/manager/templates/calendar/day.html@@ -36,88 +36,45 @@{# #}{% block content %}{% if logs %}- {% with first_log=logs|first %}- {% with session=first_log.get_workout_session %}- <div class="row">- <div class="col-sm-4">- <a href="{{first_log.workout.get_absolute_url}}" class="btn btn-default btn-block">{{ first_log.workout }}</a>- </div>-- <div class="col-sm-4">- <a href="{% url 'manager:log:log' first_log.workout.id %}" class="btn btn-default btn-block">{% trans "Log overview" %}</a>- </div>-- {% if is_owner %}- <div class="col-sm-4">- {% if session %}- <a href="{% url 'manager:session:edit' session.pk %}" class="btn btn-default btn-block wger-modal-dialog">{% trans "Edit workout session" %}</a>- {% else %}- <a href="{% url 'manager:session:add' first_log.workout_id date|date:'Y' date|date:'m' date|date:'d' %}" class="btn btn-default btn-block wger-modal-dialog">{% trans "Add workout impression" %}</a>- {% endif %}- </div>- {% endif %}- </div>- {% if session %}- <table class="table" style="margin-top: 2em;">+ {% for date, value in logs.items %}++ {% include 'calendar/partial_overview_table.html' %}+++ {% for exercise, logs in value.logs.items %}+ <table class="table table-hover table-bordered" style="margin-top: 2em;">+ <tr class="active">+ <th>{{ exercise }}</th>+ </tr>+ {% for log in logs %}<tr>- <th style="width: 30%">{% trans "Time" %}</th><td>- {% if session.time_start %}- {{session.time_start|time:"H:i"}} - {{session.time_end|time:"H:i"}}- {% else %}- -/-+ {{log.reps}} × {{log.weight}} {% trans_weight_unit 'kg' owner_user %}++ {% if is_owner %}+ <span class="editoptions">+ <a href="{% url 'manager:log:delete' log.pk %}"+ title="{% trans 'Delete' %}"+ class="wger-modal-dialog">+ <img src="{% static 'images/icons/trash.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Delete' %}"></a>+ <a href="{% url 'manager:log:edit' log.pk %}"+ title="{% trans 'Edit' %}"+ class="wger-modal-dialog">+ <img src="{% static 'images/icons/edit.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Edit' %}"></a>+ </span>{% endif %}</td></tr>- <tr>- <th>{% trans "General impression" %}</th>- <td>{% trans session.get_impression_display %}</td>- </tr>- <tr>- <th>{% trans "Notes" %}</th>- <td>{{session.notes|default:'-/-'}}</td>- </tr>- </table>- {% endif %}- {% endwith %}- {% endwith %}--+ {% endfor %}+ </table>- {% regroup logs by exercise as grouped_logs %}- {% for i in grouped_logs %}-- <table class="table table-hover table-bordered" style="margin-top: 2em;">- <tr class="active">- <th>{{ i.grouper }}</th>- </tr>- {% for log in i.list %}- <tr>- <td>- {{log.reps}} × {{log.weight}} {% trans_weight_unit 'kg' owner_user %}-- {% if is_owner %}- <span class="editoptions">- <a href="{% url 'manager:log:delete' log.pk %}"- title="{% trans 'Delete' %}"- class="wger-modal-dialog">- <img src="{% static 'images/icons/trash.svg' %}"- width="16"- height="16"- alt="{% trans 'Delete' %}"></a>- <a href="{% url 'manager:log:edit' log.pk %}"- title="{% trans 'Edit' %}"- class="wger-modal-dialog">- <img src="{% static 'images/icons/edit.svg' %}"- width="16"- height="16"- alt="{% trans 'Edit' %}"></a>- </span>- {% endif %}- </td>- </tr>{% endfor %}- </table>{% endfor %}{% else %}{% trans "Nothing found" %}diff --git a/wger/manager/templates/calendar/month.html b/wger/manager/templates/calendar/month.htmlindex f40faf220..8281df529 100644--- a/wger/manager/templates/calendar/month.html+++ b/wger/manager/templates/calendar/month.html@@ -1,8 +1,5 @@{% extends "base.html" %}-{% load i18n %}-{% load staticfiles %}-{% load wger_extras %}-{% load cache %}+{% load i18n staticfiles wger_extras cache %}{# #}@@ -48,73 +45,24 @@ $(document).ready(function() {{% cache cache_timeout workout-log-full is_owner owner_user.id current_year current_month %}{{calendar|safe}}-{% for date in logs %}+{% for date, value in logs.items %}<div id="log-{{date|date:'Y-m-d'}}" class="calendar-log-data">+<h3>{{date}}<small><a href="{% url 'manager:workout:calendar-day' owner_user date.year date.month date.day %}">{% trans 'Detail page' %}</a></small></h3>- {% regroup logs|get_item:date by exercise as grouped_logs %}-- {# TODO: what if some of the entries don't belong to a workout? #}- {% with tmp=grouped_logs|first %}- {% with first_log=tmp.list|first %}- {% with session=first_log.get_workout_session %}- <div class="row">- <div class="col-sm-4">- <a href="{{first_log.workout.get_absolute_url}}" class="btn btn-default btn-block">{{ first_log.workout }}</a>- </div>-- <div class="col-sm-4">- <a href="{% url 'manager:log:log' first_log.workout.id %}" class="btn btn-default btn-block">{% trans "Log overview" %}</a>- </div>-- <div class="col-sm-4">- {% if is_owner %}- {% if session %}- <a href="{% url 'manager:session:edit' session.pk %}" class="btn btn-default btn-block wger-modal-dialog">{% trans "Edit workout session" %}</a>- {% else %}- <a href="{% url 'manager:session:add' first_log.workout_id date|date:'Y' date|date:'m' date|date:'d' %}" class="btn btn-default btn-block wger-modal-dialog">{% trans "Add workout impression" %}</a>- {% endif %}- {% endif %}- </div>- </div>- {% if session %}- <table class="table" style="margin-top: 2em;">- <tr>- <th style="width: 30%">{% trans "Time" %}</th>- <td>- {% if session.time_start %}- {{session.time_start|time:"H:i"}} - {{session.time_end|time:"H:i"}}- {% else %}- -/-- {% endif %}- </td>- </tr>- <tr>- <th>{% trans "General impression" %}</th>- <td>{% trans session.get_impression_display %}</td>- </tr>- <tr>- <th>{% trans "Notes" %}</th>- <td>{{session.notes|default:'-/-'}}</td>- </tr>- </table>- {% endif %}- {% endwith %}- {% endwith %}- {% endwith %}-+ {% include 'calendar/partial_overview_table.html' %}<div class="row" style="margin-top: 2em;">- {% for i in grouped_logs %}+ {% for exercise, logs in value.logs.items %}<div class="col-sm-4"><div class="panel panel-default">- <div class="panel-heading">{{ i.grouper }}</div>+ <div class="panel-heading">{{ exercise }}</div><div class="panel-body" style="min-height: 8em;"><ul>- {% for log in i.list %}+ {% for log in logs %}<li>{{log.reps}} × {{log.weight}}{% if is_owner %}diff --git a/wger/manager/templates/calendar/partial_overview_table.html b/wger/manager/templates/calendar/partial_overview_table.htmlnew file mode 100644index 000000000..a67f35e0b--- /dev/null+++ b/wger/manager/templates/calendar/partial_overview_table.html@@ -0,0 +1,43 @@+{% load i18n %}++<div class="row">+ <div class="col-sm-4">+ <a href="{{value.workout.get_absolute_url}}" class="btn btn-default btn-block">{{ value.workout }}</a>+ </div>++ <div class="col-sm-4">+ <a href="{% url 'manager:log:log' value.workout.id %}" class="btn btn-default btn-block">{% trans "Log overview" %}</a>+ </div>++ {% if is_owner %}+ <div class="col-sm-4">+ {% if value.session %}+ <a href="{% url 'manager:session:edit' value.session.pk %}" class="btn btn-default btn-block wger-modal-dialog">{% trans "Edit workout session" %}</a>+ {% else %}+ <a href="{% url 'manager:session:add' value.workout.id date|date:'Y' date|date:'m' date|date:'d' %}" class="btn btn-default btn-block wger-modal-dialog">{% trans "Add workout impression" %}</a>+ {% endif %}+ </div>+ {% endif %}+ </div>+{% if value.session %}+<table class="table" style="margin-top: 2em;">+ <tr>+ <th style="width: 30%">{% trans "Time" %}</th>+ <td>+ {% if value.session.time_start %}+ {{value.session.time_start|time:"H:i"}} - {{value.session.time_end|time:"H:i"}}+ {% else %}+ -/-+ {% endif %}+ </td>+ </tr>+ <tr>+ <th>{% trans "General impression" %}</th>+ <td>{% trans value.session.get_impression_display %}</td>+ </tr>+ <tr>+ <th>{% trans "Notes" %}</th>+ <td>{{value.session.notes|default:'-/-'}}</td>+ </tr>+</table>+{% endif %}\ No newline at end of filediff --git a/wger/manager/templates/mobile/calendar/month.html b/wger/manager/templates/mobile/calendar/month.htmlindex 9cb8e3d47..232c139ad 100644--- a/wger/manager/templates/mobile/calendar/month.html+++ b/wger/manager/templates/mobile/calendar/month.html@@ -1,8 +1,5 @@{% extends "base.html" %}-{% load i18n %}-{% load staticfiles %}-{% load wger_extras %}-{% load cache %}+{% load i18n staticfiles wger_extras cache %}{# #}@@ -58,8 +55,9 @@ $(document).ready(function() {{# Popups to edit or delete the logs #}{# #}{% if is_owner %}-{% for i in grouped_logs %}-{% for log in i.list %}+{% for date, value in logs.items %}+{% for exercise, logs in value.logs.items %}+{% for log in logs %}<div class="modal fade" id="log-popup-{{log.pk}}"><div class="modal-dialog"><div class="modal-content">@@ -92,71 +90,34 @@ $(document).ready(function() {</div>{% endfor %}{% endfor %}+{% endfor %}{% endif %}-<div id="log-{{date|date:'Y-m-d'}}" class="calendar-log-data">- <h3>{{date}}</h3>-+{% for date, value in logs.items %}+ <div id="log-{{date|date:'Y-m-d'}}" class="calendar-log-data">- {# TODO: what if some of the entries don't belong to a workout? #}- {% with tmp=grouped_logs|first %}- {% with first_log=tmp.list|first %}- {% with session=first_log.get_workout_session %}+ <h3>+ {{date}}+ <small><a href="{% url 'manager:workout:calendar-day' owner_user date.year date.month date.day %}">{% trans 'Detail page' %}</a></small>+ </h3>- <a href="{{first_log.workout.get_absolute_url}}"- class="btn btn-default btn-block">{{ first_log.workout }}</a>+ {% include 'calendar/partial_overview_table.html' %}- <a href="{% url 'manager:log:log' first_log.workout.id %}"- class="btn btn-default btn-block">{% trans "Log overview" %}</a>-- {% if is_owner %}- {% if session %}- <a href="{% url 'manager:session:edit' session.pk %}"- class="btn btn-default btn-block wger-modal-dialog">{% trans "Edit workout session" %}</a>- {% else %}- <a href="{% url 'manager:session:add' first_log.workout_id date|date:'Y' date|date:'m' date|date:'d' %}"- class="btn btn-default btn-block wger-modal-dialog">{% trans "Add workout impression" %}</a>- {% endif %}- {% endif %}- {% if session %}- <table class="table" style="margin-top: 2em;">- <tr>- <th style="width: 30%">{% trans "Time" %}</th>- <td>- {% if session.time_start %}- {{session.time_start|time:"H:i"}} - {{session.time_end|time:"H:i"}}- {% else %}- -/-- {% endif %}- </td>- </tr>- <tr>- <th>{% trans "General impression" %}</th>- <td>{% trans session.get_impression_display %}</td>- </tr>- <tr>- <th>{% trans "Notes" %}</th>- <td>{{session.notes|default:'-/-'}}</td>- </tr>- </table>- {% endif %}- {% endwith %}- {% endwith %}- {% endwith %}-{# #}{# Logs #}{# #}<div class="row" style="margin-top: 2em;">- {% for i in grouped_logs %}++ {% for exercise, logs in value.logs.items %}+<div class="col-sm-4"><div class="panel panel-default">- <div class="panel-heading">{{ i.grouper }}</div>+ <div class="panel-heading">{{ exercise }}</div><div class="panel-body" style="min-height: 8em;"><ul>- {% for log in i.list %}+ {% for log in logs %}<li>{% if is_owner %}<a href="#log-popup-{{log.pk}}" data-toggle="modal">@@ -175,6 +136,7 @@ $(document).ready(function() {</div></div>{% endfor %}+{% endfor %}{% endcache %}{% endblock %}diff --git a/wger/manager/views/log.py b/wger/manager/views/log.pyindex 34bee8c31..83ee0c3e4 100644--- a/wger/manager/views/log.py+++ b/wger/manager/views/log.py@@ -49,7 +49,7 @@ from wger.utils.generic_views import WgerFormMixinfrom wger.utils.generic_views import WgerDeleteMixinfrom wger.utils.generic_views import WgerPermissionMixinfrom wger.utils.helpers import check_access, make_token-from wger.weight.helpers import process_log_entries+from wger.weight.helpers import process_log_entries, group_log_entrieslogger = logging.getLogger(__name__)@@ -413,8 +413,9 @@ def calendar(request, username=None, year=None, month=None):cache.set(cache_mapper.get_workout_log(user.pk, year, month), logs_filtered)(current_workout, schedule) = Schedule.objects.get_current_workout(user)+context['calendar'] = WorkoutCalendar(logs_filtered).formatmonth(year, month)- context['logs'] = process_log_entries(logs)[0]+ context['logs'] = group_log_entries(logs)context['current_year'] = yearcontext['current_month'] = monthcontext['current_workout'] = current_workout@@ -440,7 +441,7 @@ def day(request, username, year, month, day):logs = WorkoutLog.objects.filter(user=user,date=date).order_by('id', 'exercise')context = {}- context['logs'] = logs+ context['logs'] = group_log_entries(logs)context['date'] = datecontext['owner_user'] = usercontext['is_owner'] = is_ownerdiff --git a/wger/weight/helpers.py b/wger/weight/helpers.pyindex 9dc00ab6d..8d129a08b 100644--- a/wger/weight/helpers.py+++ b/wger/weight/helpers.py@@ -56,6 +56,30 @@ def parse_weight_csv(request, cleaned_data):return (weight_list, error_list)+def group_log_entries(logs):+ '''+ Processes and regroups a list of log entries so they can be more easily+ used in the different calendar pages++ :param logs: a list of logs+ :return: a dictionary with grouped logs by date and exercise+ '''+ out = {}++ for entry in logs:+ if not out.get(entry.date):+ out[entry.date] = {'date': entry.date,+ 'workout': entry.workout,+ 'session': entry.get_workout_session(),+ 'logs': {}}++ if not out[entry.date]['logs'].get(entry.exercise):+ out[entry.date]['logs'][entry.exercise] = []++ out[entry.date]['logs'][entry.exercise].append(entry)+ return out++def process_log_entries(logs):'''Processes and regroups a list of log entries so they can be rendered