can create a workout
This commit is contained in:
17
exercise/migrations/0002_alter_exercise_options.py
Normal file
17
exercise/migrations/0002_alter_exercise_options.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 4.2.2 on 2023-06-12 01:26
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('exercise', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='exercise',
|
||||
options={'ordering': ('name',)},
|
||||
),
|
||||
]
|
||||
@@ -10,7 +10,7 @@ class WorkoutExerciseInline(admin.StackedInline):
|
||||
|
||||
@admin.register(Workout)
|
||||
class WorkoutAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "description", "user", "created_at", "updated_at")
|
||||
list_display = ("name", "description", "registered_user", "created_at", "updated_at")
|
||||
inlines = [
|
||||
WorkoutExerciseInline,
|
||||
]
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-06-12 01:26
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('workout', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='workout',
|
||||
old_name='user',
|
||||
new_name='registered_user',
|
||||
),
|
||||
]
|
||||
19
workout/migrations/0003_alter_workout_name.py
Normal file
19
workout/migrations/0003_alter_workout_name.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# Generated by Django 4.2.2 on 2023-06-12 01:49
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('workout', '0002_rename_user_workout_registered_user'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='workout',
|
||||
name='name',
|
||||
field=models.CharField(default='q', max_length=255),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
@@ -14,15 +14,15 @@ WORKOUT_LEVEL = (
|
||||
class Workout(models.Model):
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
name = models.CharField(null=True, blank=True, max_length=255)
|
||||
name = models.CharField(max_length=255)
|
||||
description = models.CharField(null=True, blank=True, max_length=255)
|
||||
user = models.ForeignKey(
|
||||
registered_user = models.ForeignKey(
|
||||
RegisteredUser,
|
||||
on_delete=models.CASCADE
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return self.name + " : " + self.description + " | by: " + self.user.nick_name
|
||||
return self.name #+ " : " + self.description + " | by: " + self.registered_user.nick_name
|
||||
|
||||
class WorkoutExercise(models.Model):
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
@@ -42,4 +42,9 @@ class GetCompleteWorkoutSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = CompletedWorkout
|
||||
exclude = ['registered_user']
|
||||
|
||||
class POSTCompleteWorkoutSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Workout
|
||||
exclude = ['registered_user']
|
||||
@@ -6,5 +6,6 @@ urlpatterns = [
|
||||
path('all/', views.all_workouts, name='all workouts'),
|
||||
path('<workout_id>/details/', views.workout_details, name='single workout details'),
|
||||
path('complete/', views.complete_workout, name='user complete workout'),
|
||||
path('completed/', views.workouts_completed_by_logged_in_user, name='user completed workouts')
|
||||
path('completed/', views.workouts_completed_by_logged_in_user, name='user completed workouts'),
|
||||
path('create/', views.add_workout, name='create new workout')
|
||||
]
|
||||
@@ -48,3 +48,45 @@ def workouts_completed_by_logged_in_user(request):
|
||||
workouts = CompletedWorkout.objects.filter(registered_user=registered_user)
|
||||
serializer = GetCompleteWorkoutSerializer(workouts, many=True)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
@api_view(['POST'])
|
||||
@authentication_classes([TokenAuthentication])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def add_workout(request):
|
||||
registered_user = RegisteredUser.objects.get(user=request.user)
|
||||
if registered_user is None:
|
||||
return Response(status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# exercise_data = dict(request.POST)["exercise_data"]
|
||||
exercise_data = request.data["exercise_data"]
|
||||
print(exercise_data)
|
||||
if exercise_data is None:
|
||||
return Response({"exercise_data": [ "missing" ] }, status=status.HTTP_400_BAD_REQUEST)
|
||||
if len(exercise_data) < 1:
|
||||
return Response({"exercise_data": [ "empty" ] }, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
serializer = POSTCompleteWorkoutSerializer(data=request.data)
|
||||
|
||||
if serializer.is_valid():
|
||||
new_obj = serializer.save(registered_user=registered_user)
|
||||
new_obj.save()
|
||||
|
||||
for data in exercise_data:
|
||||
print(data)
|
||||
id = data["id"]
|
||||
if id is None:
|
||||
continue
|
||||
|
||||
exercise = Exercise.objects.get(pk=id)
|
||||
completed_workout = WorkoutExercise.objects.create(
|
||||
workout=new_obj,
|
||||
exercise=exercise
|
||||
)
|
||||
if "weight" in data:
|
||||
completed_workout.weight = data["weight"]
|
||||
if "reps" in data:
|
||||
completed_workout.reps = data["reps"]
|
||||
completed_workout.save()
|
||||
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
Reference in New Issue
Block a user