Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Move logic to helpers file

Roland Geider3f5363b770eb
diff --git a/wger/weight/helpers.py b/wger/weight/helpers.py
index 0d238bf0b..21fb5ded9 100644
--- a/wger/weight/helpers.py
+++ b/wger/weight/helpers.py
@@ -198,3 +198,33 @@ def process_log_entries(logs):
chart_data.append(entry_list[rep]['list'])
return entry_log, json.dumps(chart_data, cls=DecimalJsonEncoder)
+
+
+def get_last_entries(user, amount=5):
+ '''
+ Get the last weight entries as well as the difference to the last
+
+ This can be used e.g. to present a list where the last entries and
+ their changes are presented.
+ '''
+
+ last_entries = WeightEntry.objects.filter(user=user).order_by('-date')[:5]
+ last_entries_details = []
+
+ for index, entry in enumerate(last_entries):
+ curr_entry = entry
+ prev_entry_index = index + 1
+
+ if prev_entry_index < len(last_entries):
+ prev_entry = last_entries[prev_entry_index]
+ else:
+ prev_entry = None
+
+ if prev_entry and curr_entry:
+ weight_diff = curr_entry.weight - prev_entry.weight
+ day_diff = (curr_entry.date - prev_entry.date).days
+ else:
+ weight_diff = day_diff = None
+ last_entries_details.append((curr_entry, weight_diff, day_diff))
+
+ return last_entries_details
diff --git a/wger/weight/views.py b/wger/weight/views.py
index 163e2c54e..3e63aabe1 100644
--- a/wger/weight/views.py
+++ b/wger/weight/views.py
@@ -152,29 +152,12 @@ def overview(request, username=None):
'month': max_date.month,
'day': max_date.day}
- last_five_weight_entries = WeightEntry.objects.filter(user=user).order_by('-date')[:5]
- last_five_weight_entries_details = []
-
- for index, entry in enumerate(last_five_weight_entries):
- curr_entry = entry
- prev_entry_index = index + 1
-
- if prev_entry_index < len(last_five_weight_entries):
- prev_entry = last_five_weight_entries[prev_entry_index]
- else:
- prev_entry = None
-
- if prev_entry and curr_entry:
- weight_diff = curr_entry.weight - prev_entry.weight
- day_diff = (curr_entry.date - prev_entry.date).days
- else:
- weight_diff = day_diff = None
- last_five_weight_entries_details.append((curr_entry, weight_diff, day_diff))
+ last_weight_entries = helpers.get_last_entries(user)
template_data['is_owner'] = is_owner
template_data['owner_user'] = user
template_data['show_shariff'] = is_owner
- template_data['last_five_weight_entries_details'] = last_five_weight_entries_details
+ template_data['last_five_weight_entries_details'] = last_weight_entries
return render(request, 'overview.html', template_data)