Self hosted FLOSS fitness/workout, nutrition and weight tracker
restructure api
Tom Bowyerdfcbe46fd68b
diff --git a/wger/exercises/templates/history/list3.html b/wger/exercises/templates/history/list3.htmlindex f9d4cd023..e9809033f 100644--- a/wger/exercises/templates/history/list3.html+++ b/wger/exercises/templates/history/list3.html@@ -21,8 +21,51 @@Main Content-->{% block content %}-<h1>TODO</h1>-+<div class="accordion" id="accordionExample">+ {% for entry in history %}+ <div class="card">+ <div class="card-header" id="heading{{ entry.record.history_id }}">+ <button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse"+ data-target="#collapse{{ entry.record.history_id }}" aria-expanded="true"+ aria-controls="collapse{{ entry.record.history_id }}">+ History entry for {{ entry.record.instance.name }}+ <small>{{ entry.record.history_date }}</small>+ </button>+ </div>+ <div id="collapse{{ entry.record.history_id }}" class="collapse"+ aria-labelledby="heading{{ entry.record.history_id }}" data-parent="#accordionExample">+ <div class="card-body">+ {% if entry.record.history_type == '~' %}+ <table class="table">+ <thead>+ <tr>+ <td>Field</td>+ <td>Old</td>+ <td>New</td>+ </tr>+ </thead>+ <tbody>+ {% if entry.delta %}+ {% for change in entry.delta.changes %}+ <tr>+ <td>{{ change.field }}</td>+ <td>{{ change.old }}</td>+ <td>{{ change.new }}</td>+ </tr>+ {% endfor %}+ {% endif %}+ </tbody>+ </table>+ {% elif entry.record.history_type == '-' %}+ <p>Entry deleted</p>+ {% elif entry.record.history_type == '+' %}+ <p>Entry created</p>+ {% endif %}+ </div>+ </div>+ </div>+ {% endfor %}+</div>{% endblock %}diff --git a/wger/exercises/views/history.py b/wger/exercises/views/history.pyindex 733534eb7..eed9dfb19 100644--- a/wger/exercises/views/history.py+++ b/wger/exercises/views/history.py@@ -14,10 +14,12 @@# You should have received a copy of the GNU Affero General Public License# Standard Library+from datetime import datetime, timedeltaimport logging# Djangofrom django.contrib.auth.decorators import permission_required+from django.contrib.contenttypes.models import ContentTypefrom django.shortcuts import render# Third Party@@ -36,7 +38,13 @@ def overview(request):"""Generic view to list the history of the exercises"""- context = fetch_exercise_stream()+ context = {+ 'stream': Action.objects.get(),++ # We can't pass the enum to the template, so we have to do this+ # https://stackoverflow.com/questions/35953132/+ 'modes': StreamVerbs.__members__+ }return render(request, 'history/list.html', context)@@ -46,9 +54,13 @@ def overview2(request):"""Generic view to list the history of the exercises"""- history = fetch_exercise_history()+ out = []+ for entry in Exercise.history.all():+ if entry.prev_record:+ out.append(+ {'record': entry, 'delta': entry.diff_against(entry.prev_record)})- return render(request, 'history/list2.html', {'history': history})+ return render(request, 'history/list2.html', {'history': out})@permission_required('exercises.change_exercise')@@ -56,32 +68,46 @@ def control(request):"""Admin view of the history of the exercises"""- context = fetch_exercise_stream()- history = fetch_exercise_history()-- print(history)- print(context)+ objectContentTypeID = ContentType.objects.get_for_model(Exercise).id- return render(request, 'history/list3.html', {- 'context': context,- 'history': history,- })--def fetch_exercise_history():history = []for entry in Exercise.history.all():+ stream = fetch_exercise_stream_for_object_id_content_type_id_and_timestamp(+ entry.id,+ objectContentTypeID,+ entry.history_date+ )if entry.prev_record:- history.append(- {'record': entry, 'delta': entry.diff_against(entry.prev_record)})- return history+ history.append({+ 'record': entry,+ 'delta': entry.diff_against(entry.prev_record),+ 'stream': stream+ })+ else:+ history.append({+ 'record': entry,+ 'delta': None,+ 'stream': stream+ })+ print(history)-def fetch_exercise_stream():- context = {- 'stream': Action.objects.all(),+ return render(request, 'history/list3.html', {+ 'history': history,+ })- # We can't pass the enum to the template, so we have to do this- # https://stackoverflow.com/questions/35953132/- 'modes': StreamVerbs.__members__- }- return context+def fetch_exercise_stream_for_object_id_content_type_id_and_timestamp(+ object_id,+ content_type_id,+ timestamp+):+ end_range = timestamp + timedelta(seconds=0.5)+ stream = Action.objects.filter(+ action_object_object_id=object_id,+ action_object_content_type_id=content_type_id,+ timestamp__range=(timestamp, end_range)+ )++ if len(stream) >= 1:+ return stream[0]+ return None