Commit 7fcac306 authored by Mathias Lund Ahrn's avatar Mathias Lund Ahrn
Browse files

Backend support for creat and update exercises with files

parent 399e227b
......@@ -228,12 +228,10 @@ class ExerciseSerializer(serializers.HyperlinkedModelSerializer):
instances: Associated exercise instances with this Exercise type. Hyperlinks.
"""
owner_username = serializers.SerializerMethodField()
print(f"USERNAME {owner_username}")
instances = serializers.HyperlinkedRelatedField(
many=True, view_name="exerciseinstance-detail", read_only=True
)
files = ExerciseFileSerializer(many=True, required=False)
print("INSIDE SERIALIZER!!!!!!!")
class Meta:
model = Exercise
fields = ["url", "id", "owner", "owner_username", "name", "description", "unit", "instances", "files"]
......@@ -241,7 +239,47 @@ class ExerciseSerializer(serializers.HyperlinkedModelSerializer):
def get_owner_username(self, obj):
return obj.owner.username
def create(self, validated_data):
files_data = []
if "files" in validated_data:
files_data = validated_data.pop("files")
exercise = Exercise.objects.create(**validated_data)
for file_data in files_data:
ExerciseFile.objects.create(
exercise=exercise, owner=exercise.owner, file=file_data.get("file")
)
return exercise
def update(self, instance, validated_data):
instance.name = validated_data.get("name", instance.name)
instance.description = validated_data.get("description", instance.description)
instance.unit = validated_data.get("unit", instance.unit)
instance.save()
if "files" in validated_data:
files_data = validated_data.pop("files")
files = instance.files
for file, file_data in zip(files.all(), files_data):
file.file = file_data.get("file", file.file)
# If new files have been added, creating new WorkoutFiles
if len(files_data) > len(files.all()):
for i in range(len(files.all()), len(files_data)):
ExerciseFile.objects.create(
exercise=instance,
owner=instance.owner,
file=files_data[i].get("file"),
)
# Else if files have been removed, delete WorkoutFiles
elif len(files_data) < len(files.all()):
for i in range(len(files_data), len(files.all())):
files.all()[i].delete()
return instance
class RememberMeSerializer(serializers.HyperlinkedModelSerializer):
"""Serializer for an RememberMe. Hyperlinks are used for relationships by default.
......
......@@ -22,6 +22,11 @@ urlpatterns = format_suffix_patterns(
views.ExerciseDetail.as_view(),
name="exercise-detail",
),
path(
"api/exercise-files/<int:pk>/",
views.ExerciseFileDetail.as_view(),
name="exercisefile-detail",
),
path(
"api/exercise-instances/",
views.ExerciseInstanceList.as_view(),
......
......@@ -20,6 +20,7 @@ from workouts.permissions import (
IsReadOnly,
IsPublic,
IsWorkoutPublic,
IsOwnerOfExercise
)
from workouts.mixins import CreateListModelMixin
from workouts.models import Workout, Exercise, ExerciseInstance, WorkoutFile, ExerciseFile
......@@ -48,6 +49,9 @@ def api_root(request, format=None):
"workout-files": reverse(
"workout-file-list", request=request, format=format
),
"exercise-files": reverse(
"exercise-file-list", request=request, format=format
),
"comments": reverse("comment-list", request=request, format=format),
"likes": reverse("like-list", request=request, format=format),
}
......@@ -195,11 +199,9 @@ class ExerciseList(
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
print("POST part in VIEW")
return self.create(request, *args, **kwargs)
def perform_create(self, serializer):
print("COmming here?")
serializer.save(owner=self.request.user)
class ExerciseDetail(
......@@ -238,7 +240,7 @@ class ExerciseFileDetail(
queryset = ExerciseFile.objects.all()
serializer_class = ExerciseFileSerializer
permission_classes = [permissions.IsAuthenticated]
permission_classes = [permissions.IsAuthenticated & IsOwnerOfExercise]
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment