Self hosted FLOSS fitness/workout, nutrition and weight tracker
Handle create history / events
Tom Bowyera8109c4fd8da
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex 4a0fa7d82..c3204c063 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -148,9 +148,10 @@ class ExerciseTranslationViewSet(CreateUpdateModelViewSet):updated_object = Exercise.objects.get(id=obj_id)# Create a default history to diff against if there is none- most_recent_history = updated_object.history.order_by('history_date').last()- if most_recent_history is None:- updated_object.save()+ if updated_object:+ most_recent_history = updated_object.history.order_by('history_date').last()+ if most_recent_history is None:+ updated_object.save()super().perform_create(serializer)diff --git a/wger/exercises/templates/history/list3.html b/wger/exercises/templates/history/list3.htmlindex dbd541f89..89125d163 100644--- a/wger/exercises/templates/history/list3.html+++ b/wger/exercises/templates/history/list3.html@@ -25,6 +25,7 @@<thead><tr><th scope="col">{% translate 'User' %}</th>+ <th scope="col">{% translate 'Action Type' %}</th><th scope="col">{% translate 'Object' %}</th><th scope="col">{% translate 'Time' %}</th><th scope="col">{% translate 'Changes' %}</th>@@ -35,6 +36,7 @@{% for event in history %}<tr><td>{{ event.stream.actor }}</td>+ <td>{{ event.stream.verb }}</td><td>{% if event.stream.action_object.get_absolute_url %}<a href="{{ event.stream.action_object.get_absolute_url }}">{{ event.stream.action_object }}</a>@@ -43,51 +45,64 @@{% endif %}</td><td>{{ event.stream.timesince }}</td>- <td>- <a href="#" id="view-changes" data-toggle="modal" data-target="#changes-modal-{{ event.stream.id }}"- class="btn btn-success btn-sm btn-block">- {% translate "View changes" %}- </a>- <div class="modal fade" id="changes-modal-{{ event.stream.id }}">- <div class="modal-dialog">- <div class="modal-content">- <div class="modal-header">- <h4 class="modal-title">{% translate 'Exercise changes' %}</h4>- </div>- <div class="modal-body">- <table>- <thead>- <tr>- <td>Field</td>- <td>Old</td>- <td>New</td>- </tr>- </thead>- <tbody>- {% if event.history.delta %}- {% for change in event.history.delta.changes %}- <tr>- <td>{{ change.field }}</td>- <td>{{ change.old }}</td>- <td>{{ change.new }}</td>- </tr>- {% endfor %}- {% endif %}- </tbody>- </table>- </div>- <div class="modal-footer">- <button type="button" class="btn btn-light" data-dismiss="modal">{% translate "Close" %}</button>+ {% if event.stream.verb == "created" %}+ <td>+ <a href="{{ event.stream.action_object.get_absolute_url }}" id="view-changes" class="btn btn-success btn-sm btn-block">+ {% translate "View exercise" %}+ </a>+ </td>+ <td>+ <a href="{% url 'exercise:exercise:delete' event.stream.action_object.id %}" id="view-changes" class="btn btn-danger btn-sm btn-block">+ {% translate "Delete exercise" %}+ </a>+ </td>+ {% elif event.stream.verb == "updated" %}+ <td>+ <a href="#" id="view-changes" data-toggle="modal" data-target="#changes-modal-{{ event.stream.id }}"+ class="btn btn-success btn-sm btn-block">+ {% translate "View changes" %}+ </a>+ <div class="modal fade" id="changes-modal-{{ event.stream.id }}">+ <div class="modal-dialog">+ <div class="modal-content">+ <div class="modal-header">+ <h4 class="modal-title">{% translate 'Exercise changes' %}</h4>+ </div>+ <div class="modal-body">+ <table>+ <thead>+ <tr>+ <td>Field</td>+ <td>Old</td>+ <td>New</td>+ </tr>+ </thead>+ <tbody>+ {% if event.history.delta %}+ {% for change in event.history.delta.changes %}+ <tr>+ <td>{{ change.field }}</td>+ <td>{{ change.old }}</td>+ <td>{{ change.new }}</td>+ </tr>+ {% endfor %}+ {% endif %}+ </tbody>+ </table>+ </div>+ <div class="modal-footer">+ <button type="button" class="btn btn-light" data-dismiss="modal">{% translate "Close" %}</button>+ </div></div></div></div>- </div>- </td>- <td>- <a href="revert/{{ event.history.record.history_id }}" id="view-changes" class="btn btn-danger btn-sm btn-block">- {% translate "Revert changes" %}- </a>- </td>+ </td>+ <td>+ <a href="revert/{{ event.history.record.history_id }}" id="view-changes" class="btn btn-danger btn-sm btn-block">+ {% translate "Revert changes" %}+ </a>+ </td>+ {% endif %}</tr>{% endfor %}</tbody>diff --git a/wger/exercises/views/history.py b/wger/exercises/views/history.pyindex 2742bba6c..708ce4712 100644--- a/wger/exercises/views/history.py+++ b/wger/exercises/views/history.py@@ -14,7 +14,6 @@# You should have received a copy of the GNU Affero General Public License# Standard Library-from datetime import datetime, timedeltaimport logging# Django@@ -24,7 +23,6 @@ from django.shortcuts import renderfrom django.http import HttpResponseRedirectfrom django.urls import (reverse,- reverse_lazy,)# Third Party@@ -60,6 +58,7 @@ def overview2(request):"""Generic view to list the history of the exercises"""+out = []for entry in Exercise.history.all():if entry.prev_record:@@ -81,16 +80,33 @@ def control(request):out = []for entry in stream:- # Fetch history- hist_id = entry.data['data']['history_id']- hist = Exercise.history.filter(history_id=hist_id).first()-- if hist.prev_record:+ if entry.verb == "created":out.append({- 'history': {'record': hist, 'delta': hist.diff_against(hist.prev_record)},+ 'history': {'record': None, 'delta': None},'stream': entry})-+ continue+ elif entry.verb == "updated":+ # Fetch history+ entry_data = entry.data+ if entry_data is None:+ continue++ entry_dict = entry_data.get('data', {})+ history_id = entry_dict.get('history_id', 0)+ if history_id == 0:+ continue++ hist = Exercise.history.get(history_id=history_id)+ if hist.prev_record:+ out.append({+ 'history': {'record': hist, 'delta': hist.diff_against(hist.prev_record)},+ 'stream': entry+ })+ elif entry.verb == "deleted":+ print("TODO")++ print(out)return render(request, 'history/list3.html', {'history': out,})@@ -101,7 +117,9 @@ def history_revert(request, pk):"""Used to revert objects"""- hist = Exercise.history.filter(history_id=pk).first()+ hist = Exercise.history.get(history_id=pk)+ if hist is None:+ return HttpResponseRedirect(reverse('exercise:history:admin-control'))# exit early if there's no prev_recordrevert_obj = hist.prev_record@@ -109,7 +127,10 @@ def history_revert(request, pk):return HttpResponseRedirect(reverse('exercise:history:admin-control'))diff = hist.diff_against(hist.prev_record)+current_obj = Exercise.objects.get(id=revert_obj.instance.id)+ if current_obj is None:+ return HttpResponseRedirect(reverse('exercise:history:admin-control'))for change in diff.changes:setattr(current_obj, change.field, change.old)