Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Automatically update existing workout sessions if available

Roland Geider41720beed1a4
diff --git a/wger/manager/api/resources.py b/wger/manager/api/resources.py
index daae1b8c1..40d68c2a6 100644
--- a/wger/manager/api/resources.py
+++ b/wger/manager/api/resources.py
@@ -16,6 +16,8 @@
from tastypie import fields
from tastypie.authentication import ApiKeyAuthentication
+from tastypie.authentication import MultiAuthentication
+from tastypie.authentication import SessionAuthentication
from tastypie.resources import ModelResource
from tastypie.constants import ALL, ALL_WITH_RELATIONS
@@ -95,7 +97,7 @@ class WorkoutSessionResource(ModelResource):
class Meta:
queryset = WorkoutSession.objects.all()
- authentication = ApiKeyAuthentication()
+ authentication = MultiAuthentication(SessionAuthentication(), ApiKeyAuthentication())
authorization = UserObjectsOnlyAuthorization()
filtering = {'id': ALL,
"date": ALL,
diff --git a/wger/manager/static/js/jquery.ui.datepicker-bg.js b/wger/manager/static/js/jquery.ui.datepicker-bg.js
index 86ab88582..3bd9b0262 100644
--- a/wger/manager/static/js/jquery.ui.datepicker-bg.js
+++ b/wger/manager/static/js/jquery.ui.datepicker-bg.js
@@ -15,7 +15,9 @@ jQuery(function($){
dayNamesShort: ['Нед','Пон','Вто','Сря','Чет','Пет','Съб'],
dayNamesMin: ['Не','По','Вт','Ср','Че','Пе','Съ'],
weekHeader: 'Wk',
- dateFormat: 'dd.mm.yy',
+ //dateFormat: 'dd.mm.yy',
+ // Replacing the default date format to make handling dates with JS easier
+ dateFormat: 'yy-mm-dd',
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
diff --git a/wger/manager/static/js/jquery.ui.datepicker-de.js b/wger/manager/static/js/jquery.ui.datepicker-de.js
index ac2d516aa..fe31742e9 100644
--- a/wger/manager/static/js/jquery.ui.datepicker-de.js
+++ b/wger/manager/static/js/jquery.ui.datepicker-de.js
@@ -14,7 +14,9 @@ jQuery(function($){
dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
weekHeader: 'Wo',
- dateFormat: 'dd.mm.yy',
+ //dateFormat: 'dd.mm.yy',
+ // Replacing the default date format to make handling dates with JS easier
+ dateFormat: 'yy-mm-dd',
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
diff --git a/wger/manager/static/js/jquery.ui.datepicker-en.js b/wger/manager/static/js/jquery.ui.datepicker-en.js
index 50c1f56fd..a2d60909b 100644
--- a/wger/manager/static/js/jquery.ui.datepicker-en.js
+++ b/wger/manager/static/js/jquery.ui.datepicker-en.js
@@ -14,7 +14,9 @@ jQuery(function($){
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
dayNamesMin: ['Su','Mo','Tu','We','Th','Fr','Sa'],
weekHeader: 'Wk',
- dateFormat: 'dd.mm.yy',
+ //dateFormat: 'dd.mm.yy',
+ // Replacing the default date format to make handling dates with JS easier
+ dateFormat: 'yy-mm-dd',
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
diff --git a/wger/manager/static/js/jquery.ui.datepicker-es.js b/wger/manager/static/js/jquery.ui.datepicker-es.js
index 15e85d82b..7b1e74b4d 100644
--- a/wger/manager/static/js/jquery.ui.datepicker-es.js
+++ b/wger/manager/static/js/jquery.ui.datepicker-es.js
@@ -14,7 +14,9 @@ jQuery(function($){
dayNamesShort: ['dom','lun','mar','mié','juv','vie','sáb'],
dayNamesMin: ['D','L','M','X','J','V','S'],
weekHeader: 'Sm',
- dateFormat: 'dd.mm.yy',
+ //dateFormat: 'dd.mm.yy',
+ // Replacing the default date format to make handling dates with JS easier
+ dateFormat: 'yy-mm-dd',
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
diff --git a/wger/manager/static/js/jquery.ui.datepicker-ru.js b/wger/manager/static/js/jquery.ui.datepicker-ru.js
index a51971405..f3c30e021 100644
--- a/wger/manager/static/js/jquery.ui.datepicker-ru.js
+++ b/wger/manager/static/js/jquery.ui.datepicker-ru.js
@@ -14,7 +14,9 @@ jQuery(function($){
dayNamesShort: ['вск','пнд','втр','срд','чтв','птн','сбт'],
dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
weekHeader: 'Нед',
- dateFormat: 'dd.mm.yy',
+ //dateFormat: 'dd.mm.yy',
+ // Replacing the default date format to make handling dates with JS easier
+ dateFormat: 'yy-mm-dd',
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
diff --git a/wger/manager/static/js/workout-manager.js b/wger/manager/static/js/workout-manager.js
index 1e8e53235..921126613 100644
--- a/wger/manager/static/js/workout-manager.js
+++ b/wger/manager/static/js/workout-manager.js
@@ -474,3 +474,29 @@ function prefetch_images(imageArray) {
});
}
+
+/*
+ * Helper function used in the workout log dialog to fetch existing
+ * workout sessions through the REST API
+ */
+ function get_workout_session() {
+ $('#id_date').on('change', function() {
+ var date = $('#id_date').val();
+ if(date) {
+ $.get( '/api/v1/workoutsession/?date=' + date, function( result ) {
+ if (result.objects.length == 1) {
+ $('#id_notes').val(result.objects[0].notes)
+ $('#id_impression').val(result.objects[0].impression)
+ $('#id_time_start').val(result.objects[0].time_start)
+ $('#id_time_end').val(result.objects[0].time_end)
+ }
+ else {
+ $('#id_notes').val('');
+ $('#id_impression').val('2');
+ $('#id_time_start').val('');
+ $('#id_time_end').val('');
+ }
+ });
+ }
+ });
+}
\ No newline at end of file
diff --git a/wger/manager/templates/day/log.html b/wger/manager/templates/day/log.html
index ba6da2114..932405821 100644
--- a/wger/manager/templates/day/log.html
+++ b/wger/manager/templates/day/log.html
@@ -10,6 +10,8 @@
<script type="text/javascript">
$(document).ready(function () {
$( "#id_date" ).datepicker();
+
+ get_workout_session();
});
</script>
diff --git a/wger/manager/templates/mobile/day/log.html b/wger/manager/templates/mobile/day/log.html
index aae2805a8..6cb487043 100644
--- a/wger/manager/templates/mobile/day/log.html
+++ b/wger/manager/templates/mobile/day/log.html
@@ -6,7 +6,14 @@
{% block title %}{% trans "New weight log" %} – {{day.description}}{% endblock %}
-{% block header %}{% endblock %}
+{% block header %}
+<script type="text/javascript">
+$(document).ready(function () {
+ get_workout_session();
+});
+</script>
+
+{% endblock %}
{% block content %}
<form action="{{form_action}}"
diff --git a/wger/manager/views/log.py b/wger/manager/views/log.py
index eb7689182..5c365dce3 100644
--- a/wger/manager/views/log.py
+++ b/wger/manager/views/log.py
@@ -182,13 +182,20 @@ def add(request, pk):
if dateform.is_valid() and session_form.is_valid() and formset.is_valid():
log_date = dateform.cleaned_data['date']
+ if WorkoutSession.objects.filter(user=request.user, date=log_date).exists():
+ session = WorkoutSession.objects.get(user=request.user, date=log_date)
+ session_form = HelperWorkoutSessionForm(data=post_copy, instance=session)
+
# Save the Workout Session only if there is not already one for this date
+ instance = session_form.save(commit=False)
if not WorkoutSession.objects.filter(user=request.user, date=log_date).exists():
- instance = session_form.save(commit=False)
instance.date = log_date
instance.user = request.user
instance.workout = day.training
- instance.save()
+ else:
+ session = WorkoutSession.objects.get(user=request.user, date=log_date)
+ instance.instance = session
+ instance.save()
# Log entries
instances = formset.save(commit=False)
@@ -206,7 +213,14 @@ def add(request, pk):
formset = WorkoutLogFormSet(queryset=WorkoutLog.objects.none())
dateform = HelperDateForm(initial={'date': datetime.date.today()})
- session_form = HelperWorkoutSessionForm()
+ #session_form = HelperWorkoutSessionForm()
+ # Depending on whether there is already a workout session for today, update
+ # the current one or create a new one (this will be the most usual case)
+ if WorkoutSession.objects.filter(user=request.user, date=datetime.date.today()).exists():
+ session = WorkoutSession.objects.get(user=request.user, date=datetime.date.today())
+ session_form = HelperWorkoutSessionForm(instance=session)
+ else:
+ session_form = HelperWorkoutSessionForm()
# Pass the correct forms to the exercise list
for exercise in exercise_list: