Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Refactor group_log_entries helper

Roland Geider0822c18e67d6
diff --git a/wger/manager/views/log.py b/wger/manager/views/log.py
index 22d6acfe6..278409505 100644
--- a/wger/manager/views/log.py
+++ b/wger/manager/views/log.py
@@ -319,17 +319,13 @@ def calendar(request, username=None, year=None, month=None):
'''
Show a calendar with all the workout logs
'''
+ context = {}
is_owner, user = check_access(request.user, username)
year = int(year) if year else datetime.date.today().year
month = int(month) if month else datetime.date.today().month
- context = {}
- logs = WorkoutLog.objects.filter(user=user,
- date__year=year,
- date__month=month).order_by('exercise')
-
(current_workout, schedule) = Schedule.objects.get_current_workout(user)
- grouped_log_entries = group_log_entries(logs, (user.pk, year, month))
+ grouped_log_entries = group_log_entries(user, year, month)
context['calendar'] = WorkoutCalendar(grouped_log_entries).formatmonth(year, month)
context['logs'] = grouped_log_entries
@@ -348,6 +344,7 @@ def day(request, username, year, month, day):
'''
Show the logs for a single day
'''
+ context = {}
is_owner, user = check_access(request.user, username)
try:
@@ -355,9 +352,7 @@ def day(request, username, year, month, day):
except ValueError as e:
logger.error("Error on date: {0}".format(e))
return HttpResponseForbidden()
- logs = WorkoutLog.objects.filter(user=user, date=date).order_by('id', 'exercise')
- context = {}
- context['logs'] = group_log_entries(logs, (user.pk, date.year, date.month, date.day))
+ context['logs'] = group_log_entries(user, date.year, date.month, date.day)
context['date'] = date
context['owner_user'] = user
context['is_owner'] = is_owner
diff --git a/wger/software/templates/changelog.html b/wger/software/templates/changelog.html
index e55fda7d0..2d117fcc7 100644
--- a/wger/software/templates/changelog.html
+++ b/wger/software/templates/changelog.html
@@ -40,6 +40,7 @@
<a href="https://github.com/rolandgeider/wger/issues/108">#100</a>
<a href="https://github.com/rolandgeider/wger/issues/108">#108</a>
<a href="https://github.com/rolandgeider/wger/issues/117">#117</a>
+ <a href="https://github.com/rolandgeider/wger/issues/131">#131</a>
</li>
</ul>
diff --git a/wger/weight/helpers.py b/wger/weight/helpers.py
index c58ba648f..10af8efaa 100644
--- a/wger/weight/helpers.py
+++ b/wger/weight/helpers.py
@@ -27,6 +27,8 @@ from django.core.cache import cache
from wger.utils.helpers import DecimalJsonEncoder
from wger.utils.cache import cache_mapper
from wger.weight.models import WeightEntry
+from wger.manager.models import WorkoutSession
+from wger.manager.models import WorkoutLog
logger = logging.getLogger(__name__)
@@ -59,22 +61,42 @@ def parse_weight_csv(request, cleaned_data):
return (weight_list, error_list)
-def group_log_entries(logs, hash_dict):
+def group_log_entries(user, year, month, day=None):
'''
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
- :param hash_dict: a dictionary in the form (username, year, month)
+ :param user: the user to filter the logs for
+ :param year: year
+ :param month: month
+ :param day: optional, day
+
:return: a dictionary with grouped logs by date and exercise
'''
+ log_hash = hash(frozenset([user.pk, year, month, day]))
+
+ # There can be workout sessions without any associated log entries, so it is
+ # not enough so simply iterate through the logs
+ if day:
+ filter_date = datetime.date(year, month, day)
+ logs = WorkoutLog.objects.filter(user=user, date=filter_date)
+ sessions = WorkoutSession.objects.filter(user=user, date=filter_date)
+
+ else:
+ logs = WorkoutLog.objects.filter(user=user,
+ date__year=year,
+ date__month=month)
+
+ sessions = WorkoutSession.objects.filter(user=user,
+ date__year=year,
+ date__month=month)
- log_hash = hash(hash_dict)
out = cache.get(cache_mapper.get_workout_log_list(log_hash))
if not out:
-
out = {}
+
+ # Logs
for entry in logs:
if not out.get(entry.date):
out[entry.date] = {'date': entry.date,
@@ -86,6 +108,15 @@ def group_log_entries(logs, hash_dict):
out[entry.date]['logs'][entry.exercise] = []
out[entry.date]['logs'][entry.exercise].append(entry)
+
+ # Sessions
+ for entry in sessions:
+ if not out.get(entry.date):
+ out[entry.date] = {'date': entry.date,
+ 'workout': entry.workout,
+ 'session': entry,
+ 'logs': {}}
+
cache.set(cache_mapper.get_workout_log_list(log_hash), out)
return out