Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Save entries to activity stream when adding exercises

Roland Geider12577334694a
diff --git a/wger/exercises/api/permissions.py b/wger/exercises/api/permissions.py
index 2bd92ddaa..919eadef0 100644
--- a/wger/exercises/api/permissions.py
+++ b/wger/exercises/api/permissions.py
@@ -32,6 +32,10 @@ class CanEditExercises(BasePermission):
if request.method in self.SAFE_METHODS:
return True
+ # Only logged-in users can edit
+ if not request.user.is_authenticated:
+ return False
+
# Only "trustworthy" users can edit
return (
request.user.userprofile.is_trustworthy
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index 5e680ac02..c68490b80 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -111,6 +111,8 @@ class ExerciseCommentSerializer(serializers.ModelSerializer):
ExerciseComment serializer
"""
+ id = serializers.IntegerField(required=False, read_only=True)
+
class Meta:
model = ExerciseComment
fields = [
@@ -217,12 +219,16 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):
Exercise translation serializer
"""
+ id = serializers.IntegerField(required=False, read_only=True)
+ uuid = serializers.UUIDField(required=False, read_only=True)
+
class Meta:
model = Exercise
fields = (
"id",
"uuid",
"name",
+ "exercise_base",
"description",
"creation_date",
"language",
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index ab9a2308f..2264f108e 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -35,10 +35,12 @@ from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.response import Response
# wger
+from exercises.views.helper import HistoryModes
from wger.config.models import LanguageConfig
from wger.core.api.viewsets import CreateUpdateModelViewSet
from wger.exercises.api.permissions import CanEditExercises
from wger.exercises.api.serializers import (
+ ExerciseTranslationSerializer,
EquipmentSerializer,
ExerciseAliasSerializer,
ExerciseBaseInfoSerializer,
@@ -90,18 +92,57 @@ class ExerciseBaseViewSet(CreateUpdateModelViewSet):
"""
Save entry to activity stream
"""
- super(ExerciseBaseViewSet, self).perform_create(serializer)
+ super().perform_create(serializer)
actstream_action.send(self.request.user, verb='created', action_object=serializer.instance)
def perform_update(self, serializer):
"""
Save entry to activity stream
"""
- super(ExerciseBaseViewSet, self).perform_update(serializer)
+ super().perform_create(serializer)
actstream_action.send(self.request.user, verb='updated', action_object=serializer.instance)
-class ExerciseViewSet(viewsets.ReadOnlyModelViewSet): #viewsets.ReadOnlyModelViewSet
+class ExerciseTranslationViewSet(CreateUpdateModelViewSet):
+ """
+ API endpoint for editing or adding exercise objects.
+ """
+ queryset = Exercise.objects.accepted()
+ permission_classes = (CanEditExercises, )
+ serializer_class = ExerciseTranslationSerializer
+ ordering_fields = '__all__'
+ filterset_fields = (
+ 'uuid',
+ 'creation_date',
+ 'exercise_base',
+ 'description',
+ 'name',
+ )
+
+ def perform_create(self, serializer):
+ """
+ Save entry to activity stream
+ """
+ super().perform_create(serializer)
+ actstream_action.send(
+ self.request.user,
+ verb=HistoryModes.ADDED.value,
+ action_object=serializer.instance,
+ )
+
+ def perform_update(self, serializer):
+ """
+ Save entry to activity stream
+ """
+ super().perform_create(serializer)
+ actstream_action.send(
+ self.request.user,
+ verb=HistoryModes.UPDATED.value,
+ action_object=serializer.instance,
+ )
+
+
+class ExerciseViewSet(CreateUpdateModelViewSet):
"""
API endpoint for exercise objects. For a single read-only endpoint with all
the information of an exercise, see /api/v2/exerciseinfo/
@@ -352,6 +393,28 @@ class ExerciseCommentViewSet(CreateUpdateModelViewSet):
qs = ExerciseComment.objects.filter(exercise__in=exercises)
return qs
+ def perform_create(self, serializer):
+ """
+ Save entry to activity stream
+ """
+ super().perform_create(serializer)
+ actstream_action.send(
+ self.request.user,
+ verb=HistoryModes.ADDED.value,
+ action_object=serializer.instance,
+ )
+
+ def perform_update(self, serializer):
+ """
+ Save entry to activity stream
+ """
+ super().perform_create(serializer)
+ actstream_action.send(
+ self.request.user,
+ verb=HistoryModes.UPDATED.value,
+ action_object=serializer.instance,
+ )
+
class ExerciseAliasViewSet(CreateUpdateModelViewSet):
"""
diff --git a/wger/exercises/apps.py b/wger/exercises/apps.py
index d8360a235..44f7743a2 100644
--- a/wger/exercises/apps.py
+++ b/wger/exercises/apps.py
@@ -27,4 +27,5 @@ class ExerciseConfig(AppConfig):
from actstream import registry
registry.register(self.get_model('Exercise'))
registry.register(self.get_model('ExerciseBase'))
+ registry.register(self.get_model('ExerciseComment'))
registry.register(self.get_model('ExerciseImage'))
diff --git a/wger/exercises/management/commands/read-exercise-cleanup.py b/wger/exercises/management/commands/read-exercise-cleanup.py
index 9539d2647..6d093fc2b 100644
--- a/wger/exercises/management/commands/read-exercise-cleanup.py
+++ b/wger/exercises/management/commands/read-exercise-cleanup.py
@@ -182,7 +182,6 @@ class Command(BaseCommand):
# Set the nane and description, if there is any
if exercise_name:
exercise.name = exercise_name
- exercise.name_original = exercise_name
exercise.description = exercise_description
else:
continue
diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.py
index fe069e9f7..17a0c3b1c 100644
--- a/wger/exercises/models/base.py
+++ b/wger/exercises/models/base.py
@@ -98,6 +98,12 @@ class ExerciseBase(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
history = HistoricalRecords()
"""Edit history"""
+ def __str__(self):
+ """
+ Return a more human-readable representation
+ """
+ return f"base {self.uuid}"
+
#
# Own methods
#
diff --git a/wger/exercises/models/comment.py b/wger/exercises/models/comment.py
index 728ccf5c2..bb7bba978 100644
--- a/wger/exercises/models/comment.py
+++ b/wger/exercises/models/comment.py
@@ -35,7 +35,6 @@ class ExerciseComment(models.Model):
exercise = models.ForeignKey(
Exercise,
verbose_name=_('Exercise'),
- editable=False,
on_delete=models.CASCADE,
)
comment = models.CharField(
diff --git a/wger/exercises/models/exercise.py b/wger/exercises/models/exercise.py
index 6a652636b..308d782bb 100644
--- a/wger/exercises/models/exercise.py
+++ b/wger/exercises/models/exercise.py
@@ -64,7 +64,10 @@ class Exercise(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
)
"""Description on how to perform the exercise"""
- name = models.CharField(max_length=200, verbose_name=_('Name'))
+ name = models.CharField(
+ max_length=200,
+ verbose_name=_('Name'),
+ )
"""The exercise's name"""
creation_date = models.DateField(
diff --git a/wger/exercises/templates/history/list.html b/wger/exercises/templates/history/list.html
index 1e4da4e71..e47ff97f3 100644
--- a/wger/exercises/templates/history/list.html
+++ b/wger/exercises/templates/history/list.html
@@ -1,9 +1,5 @@
{% extends "base.html" %}
-{% load i18n %}
-{% load static %}
-{% load wger_extras %}
-{% load cache %}
-{% load thumbnail %}
+{% load i18n static wger_extras activity_tags %}
<!--
Title
@@ -21,58 +17,38 @@
Main Content
-->
{% block content %}
- <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>
+ <table class="table">
+ <thead>
+ <tr>
+ <th scope="col">user</th>
+ <th scope="col">action</th>
+ <th scope="col">Object</th>
+ <th scope="col">History changes</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for event in stream %}
+ <tr>
+ <th>{{ event.actor }}</th>
+ <td>
+ {% if event.verb == modes.ADDED.value %}
+ <span class="{% fa_class 'plus' %}"></span>
+ {% elif event.verb == modes.UPDATED.value %}
+ <span class="{% fa_class 'asterisk' %}"></span>
+ {% endif %}
+ </td>
+ <td>
+ {% if event.action_object.get_absolute_url %}
+ <a href="{{ event.action_object.get_absolute_url }}">{{ event.action_object }}</a>
+ {% else %}
+ {{ event.action_object }}
+ {% endif %}
+ </td>
+ <td> {{ event.timesince }}</td>
+ </tr>
{% endfor %}
-
- </div>
-
+ </tbody>
+ </table>
{% endblock %}
diff --git a/wger/exercises/tests/test_corrected_exercise.py b/wger/exercises/tests/test_corrected_exercise.py
index 346030139..87de3a928 100644
--- a/wger/exercises/tests/test_corrected_exercise.py
+++ b/wger/exercises/tests/test_corrected_exercise.py
@@ -33,7 +33,7 @@ class ExercisesCorrectionTestCase(WgerTestCase):
description = 'a nice, long and accurate description for the exercise'
response = self.client.post(
reverse('exercise:exercise:correct', kwargs={'pk': 1}), {
- 'name_original': 'my test exercise',
+ 'name': 'my test exercise',
'license': 2,
'description': description,
'muscles': [3],
diff --git a/wger/exercises/views/helper.py b/wger/exercises/views/helper.py
new file mode 100644
index 000000000..6687d8169
--- /dev/null
+++ b/wger/exercises/views/helper.py
@@ -0,0 +1,19 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU Affero General Public License
+
+import enum
+
+
+class HistoryModes(enum.Enum):
+ ADDED = 'created'
+ UPDATED = 'updated'
diff --git a/wger/exercises/views/history.py b/wger/exercises/views/history.py
index b5e8c45bb..e70ac6b0f 100644
--- a/wger/exercises/views/history.py
+++ b/wger/exercises/views/history.py
@@ -21,8 +21,10 @@ from django.contrib.auth.decorators import permission_required
from django.shortcuts import render
# wger
+from exercises.views.helper import HistoryModes
from wger.exercises.models import Exercise
+from actstream.models import Action
logger = logging.getLogger(__name__)
@@ -32,6 +34,23 @@ def overview(request):
"""
Generic view to list the history of the exercises
"""
+
+ print(HistoryModes.ADDED.value)
+ context = {
+ 'stream': Action.objects.all(),
+
+ # We can't pass the enmu to the template, so we have to do this
+ # https://stackoverflow.com/questions/35953132/
+ 'modes': HistoryModes.__members__
+ }
+ return render(request, 'history/list.html', context)
+
+
+@permission_required('exercises.change_exercise')
+def overview2(request):
+ """
+ Generic view to list the history of the exercises
+ """
context = {}
out = []