Commit 069238a4 authored by Sondre Westby Liestøl's avatar Sondre Westby Liestøl
Browse files

Changes added to workout.html and backend.

parent 897fe465
Pipeline #159136 passed with stage
in 23 seconds
# Generated by Django 3.1 on 2022-03-08 15:13
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('workouts', '0005_exercise_difficulty'),
]
operations = [
migrations.AddField(
model_name='workout',
name='difficulty',
field=models.IntegerField(default=0),
),
]
......@@ -46,6 +46,7 @@ class Workout(models.Model):
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, related_name="workouts"
)
difficulty = models.IntegerField(default=1)
# Visibility levels
PUBLIC = "PU" # Visible to all authenticated users
......
......@@ -78,6 +78,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
"visibility",
"exercise_instances",
"files",
"difficulty"
]
extra_kwargs = {"owner": {"read_only": True}}
......@@ -99,9 +100,8 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
files_data = validated_data.pop("files")
workout = Workout.objects.create(**validated_data)
for exercise_instance_data in exercise_instances_data:
ExerciseInstance.objects.create(workout=workout, **exercise_instance_data)
exercise = ExerciseInstance.objects.create(workout=workout, **exercise_instance_data)
for file_data in files_data:
WorkoutFile.objects.create(
workout=workout, owner=workout.owner, file=file_data.get("file")
......
......@@ -119,7 +119,7 @@ class WorkoutList(
JSONParser,
] # For parsing JSON and Multi-part requests
filter_backends = [filters.OrderingFilter]
ordering_fields = ["name", "date", "owner__username"]
ordering_fields = ["name", "date", "owner__username", "difficulty"]
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
......
......@@ -26,7 +26,7 @@ async function fetchWorkouts(ordering) {
rows[1].querySelectorAll("td")[1].textContent = localDate.toLocaleTimeString(); // Time
rows[2].querySelectorAll("td")[1].textContent = workout.owner_username; //Owner
rows[3].querySelectorAll("td")[1].textContent = workout.exercise_instances.length; // Exercises
rows[4].querySelectorAll("td")[1].textContent = "5"; // Exercises
rows[4].querySelectorAll("td")[1].textContent = workout.; // Difficulty
container.appendChild(aWorkout);
});
......@@ -47,7 +47,7 @@ window.addEventListener("DOMContentLoaded", async () => {
if (urlParams.has('ordering')) {
let aSort = null;
ordering = urlParams.get('ordering');
if (ordering == "name" || ordering == "owner" || ordering == "date") {
if (ordering == "name" || ordering == "owner" || ordering == "date" || ordering == "difficulty") {
let aSort = document.querySelector(`a[href="?ordering=${ordering}"`);
aSort.href = `?ordering=-${ordering}`;
}
......@@ -61,6 +61,7 @@ window.addEventListener("DOMContentLoaded", async () => {
if (ordering.includes("owner")) {
ordering += "__username";
}
let workouts = await fetchWorkouts(ordering);
let tabEls = document.querySelectorAll('a[data-bs-toggle="list"]');
......
......@@ -30,7 +30,7 @@
<a class="list-group-item list-group-item-action" id="list-athlete-workouts-list" data-bs-toggle="list" href="#list-athlete-workouts" role="tab" aria-controls="athlete">Athlete Workouts</a>
<a class="list-group-item list-group-item-action" id="list-public-workouts-list" data-bs-toggle="list" href="#list-public-workouts" role="tab" aria-controls="public">Public Workouts</a>
</div>
<div class="mt-1">Sort by: <a href="?ordering=date">Date</a> <a href="?ordering=owner">Owner</a> <a href="?ordering=name">Name</a>
<div class="mt-1">Sort by: <a href="?ordering=date">Date</a> <a href="?ordering=owner">Owner</a> <a href="?ordering=name">Name</a> <a href="?ordering=difficulty">Difficulty</a>
<br>Currently sorting by: <span id="current-sort"></span>
</div>
<div class="list-group mt-1" id="div-content"></div>
......
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