Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

add ability to revert

Tom Bowyer8df789dd4bbe
diff --git a/wger/exercises/templates/history/list3.html b/wger/exercises/templates/history/list3.html
index c2c81871d..a4b69e931 100644
--- a/wger/exercises/templates/history/list3.html
+++ b/wger/exercises/templates/history/list3.html
@@ -84,7 +84,7 @@
</div>
</td>
<td>
- <a href="#" id="view-changes" class="btn btn-danger btn-sm btn-block">
+ <a href="revert/{{ event.history.record.history_id }}" id="view-changes" class="btn btn-danger btn-sm btn-block">
{% translate "Revert changes" %}
</a>
</td>
diff --git a/wger/exercises/urls.py b/wger/exercises/urls.py
index 5137e5dc5..f47398824 100644
--- a/wger/exercises/urls.py
+++ b/wger/exercises/urls.py
@@ -41,6 +41,8 @@ patterns_history = [
path('overview/', history.overview, name='overview'),
path('overview-history/', history.overview2, name='overview-history'),
path('admin-control/', history.control, name='admin-control'),
+ path('admin-control/revert/<int:pk>/',
+ history.history_revert, name='history-revert'),
]
# sub patterns for muscles
diff --git a/wger/exercises/views/history.py b/wger/exercises/views/history.py
index a1a94369d..98060ac71 100644
--- a/wger/exercises/views/history.py
+++ b/wger/exercises/views/history.py
@@ -21,9 +21,15 @@ import logging
from django.contrib.auth.decorators import permission_required
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import render
+from django.http import HttpResponseRedirect
+from django.urls import (
+ reverse,
+ reverse_lazy,
+)
# Third Party
from actstream.models import Action
+from actstream import action as actstream_action
# wger
from wger.exercises.models import Exercise
@@ -73,6 +79,8 @@ def control(request):
action_object_content_type_id=object_content_type_ID,
)
+ print(stream)
+
out = []
for entry in stream:
# Fetch history
@@ -95,3 +103,30 @@ def control(request):
return render(request, 'history/list3.html', {
'history': out,
})
+
+
+@permission_required('exercises.change_exercise')
+def history_revert(request, pk):
+ """
+ Used to revert objects
+ """
+
+ hist = Exercise.history.filter(history_id=pk).first()
+
+ revert_obj = hist.prev_record
+
+ revert_obj.instance.save()
+
+ updated_object = Exercise.objects.get(id=revert_obj.id)
+ most_recent_history = updated_object.history.order_by('history_date').last()
+
+ actstream_action.send(
+ request.user,
+ verb=StreamVerbs.UPDATED.value,
+ action_object=revert_obj.instance,
+ data={
+ 'history_id': most_recent_history.history_id,
+ }
+ )
+
+ return HttpResponseRedirect(reverse('exercise:history:admin-control'))