Compare commits
10 Commits
25a50c452e
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc29fb09f2 | ||
|
|
64e04bcc78 | ||
|
|
8a42c5854a | ||
|
|
c08b31d07e | ||
|
|
3df5ec6acc | ||
|
|
67372cc8ab | ||
|
|
6d68c59ac3 | ||
|
|
9d9d6200dc | ||
|
|
afaa8edb6e | ||
|
|
6273ae9222 |
BIN
db copy.sqlite3
BIN
db copy.sqlite3
Binary file not shown.
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
@@ -12,10 +12,17 @@ from rest_framework.authentication import TokenAuthentication
|
|||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.decorators import authentication_classes
|
from rest_framework.decorators import authentication_classes
|
||||||
from rest_framework.decorators import permission_classes
|
from rest_framework.decorators import permission_classes
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def all_equipment(request):
|
def all_equipment(request):
|
||||||
|
if 'all_equipment' in cache:
|
||||||
|
data = cache.get('all_equipment')
|
||||||
|
return Response(data=data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
users = Equipment.objects.all()
|
users = Equipment.objects.all()
|
||||||
serializer = EquipmentSerializer(users, many=True)
|
serializer = EquipmentSerializer(users, many=True)
|
||||||
return Response(data=serializer.data, status=status.HTTP_200_OK)
|
data = serializer.data
|
||||||
|
cache.set('all_equipment', data, timeout=None)
|
||||||
|
return Response(data=data, status=status.HTTP_200_OK)
|
||||||
18
exercise/migrations/0007_exercise_estimated_rep_duration.py
Normal file
18
exercise/migrations/0007_exercise_estimated_rep_duration.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 4.2.2 on 2023-07-25 15:22
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('exercise', '0006_alter_exercise_name'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='exercise',
|
||||||
|
name='estimated_rep_duration',
|
||||||
|
field=models.FloatField(blank=True, max_length=255, null=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
18
exercise/migrations/0008_exercise_video_override.py
Normal file
18
exercise/migrations/0008_exercise_video_override.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 4.2.2 on 2023-07-26 22:47
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('exercise', '0007_exercise_estimated_rep_duration'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='exercise',
|
||||||
|
name='video_override',
|
||||||
|
field=models.CharField(blank=True, max_length=255, null=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -21,6 +21,8 @@ class Exercise(models.Model):
|
|||||||
equipment_required = models.CharField(null=True, blank=True, max_length=255)
|
equipment_required = models.CharField(null=True, blank=True, max_length=255)
|
||||||
muscle_groups = models.CharField(null=True, blank=True, max_length=255)
|
muscle_groups = models.CharField(null=True, blank=True, max_length=255)
|
||||||
synonyms = models.CharField(null=True, blank=True, max_length=255)
|
synonyms = models.CharField(null=True, blank=True, max_length=255)
|
||||||
|
estimated_rep_duration = models.FloatField(null=True, blank=True, max_length=255)
|
||||||
|
video_override = models.CharField(null=True, blank=True, max_length=255)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ('name',)
|
ordering = ('name',)
|
||||||
@@ -29,7 +31,10 @@ class Exercise(models.Model):
|
|||||||
return self.name + " --------- " + self.description
|
return self.name + " --------- " + self.description
|
||||||
|
|
||||||
def video_url(self):
|
def video_url(self):
|
||||||
return "exercise_videos/" + self.name.replace(" ", "_") + ".mp4"
|
if self.video_override is not None and len(self.video_override) > 0:
|
||||||
|
return "exercise_videos/" + self.video_override
|
||||||
|
else:
|
||||||
|
return "exercise_videos/" + self.name.replace(" ", "_") + ".mp4"
|
||||||
|
|
||||||
def audio_url(self):
|
def audio_url(self):
|
||||||
return "exercise_audio/" + self.name.replace(" ", "_") + ".m4a"
|
return "exercise_audio/" + self.name.replace(" ", "_") + ".m4a"
|
||||||
@@ -12,10 +12,17 @@ from rest_framework.authentication import TokenAuthentication
|
|||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.decorators import authentication_classes
|
from rest_framework.decorators import authentication_classes
|
||||||
from rest_framework.decorators import permission_classes
|
from rest_framework.decorators import permission_classes
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def all_exercises(request):
|
def all_exercises(request):
|
||||||
|
if 'all_exercises' in cache:
|
||||||
|
data = cache.get('all_exercises')
|
||||||
|
return Response(data=data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
users = Exercise.objects.all()
|
users = Exercise.objects.all()
|
||||||
serializer = ExerciseSerializer(users, many=True)
|
serializer = ExerciseSerializer(users, many=True)
|
||||||
return Response(data=serializer.data, status=status.HTTP_200_OK)
|
data = serializer.data
|
||||||
|
cache.set('all_exercises', data, timeout=None)
|
||||||
|
return Response(data=data, status=status.HTTP_200_OK)
|
||||||
@@ -12,10 +12,17 @@ from rest_framework.authentication import TokenAuthentication
|
|||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.decorators import authentication_classes
|
from rest_framework.decorators import authentication_classes
|
||||||
from rest_framework.decorators import permission_classes
|
from rest_framework.decorators import permission_classes
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def all_muscles(request):
|
def all_muscles(request):
|
||||||
|
if 'all_muscles' in cache:
|
||||||
|
data = cache.get('all_muscles')
|
||||||
|
return Response(data=data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
users = Muscle.objects.all()
|
users = Muscle.objects.all()
|
||||||
serializer = MuscleSerializer(users, many=True)
|
serializer = MuscleSerializer(users, many=True)
|
||||||
return Response(data=serializer.data, status=status.HTTP_200_OK)
|
data = serializer.data
|
||||||
|
cache.set('all_muscles', data, timeout=None)
|
||||||
|
return Response(data=data, status=status.HTTP_200_OK)
|
||||||
@@ -13,6 +13,7 @@ Django==4.2.2
|
|||||||
django-debug-toolbar==4.1.0
|
django-debug-toolbar==4.1.0
|
||||||
django-import-export==3.2.0
|
django-import-export==3.2.0
|
||||||
django-push-notifications==3.0.0
|
django-push-notifications==3.0.0
|
||||||
|
django-redis==5.3.0
|
||||||
djangorestframework==3.14.0
|
djangorestframework==3.14.0
|
||||||
et-xmlfile==1.1.0
|
et-xmlfile==1.1.0
|
||||||
gevent==22.10.1
|
gevent==22.10.1
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ from . import views
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# path('sync_equipment/', views.sync_equipment, name='sync_equipment'),
|
# path('sync_equipment/', views.sync_equipment, name='sync_equipment'),
|
||||||
# path('sync_muscle_groups/', views.sync_muscle_groups, name='sync_equipment'),
|
# path('sync_muscle_groups/', views.sync_muscle_groups, name='sync_equipment'),
|
||||||
|
path('clear_redis/', views.clear_redis, name='clear_redis'),
|
||||||
]
|
]
|
||||||
@@ -5,6 +5,7 @@ from equipment.models import Equipment, WorkoutEquipment
|
|||||||
from rest_framework.decorators import api_view
|
from rest_framework.decorators import api_view
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
@@ -15,7 +16,7 @@ def sync_equipment(request):
|
|||||||
for equipment in all_equipment:
|
for equipment in all_equipment:
|
||||||
if len(equipment) > 0:
|
if len(equipment) > 0:
|
||||||
try:
|
try:
|
||||||
equipment_obj = Equipment.objects.get(name=equipment)
|
equipment_obj = Equipment.objects.get(name=equipment.lower())
|
||||||
WorkoutEquipment.objects.create(exercise=exercise, equipment=equipment_obj).save()
|
WorkoutEquipment.objects.create(exercise=exercise, equipment=equipment_obj).save()
|
||||||
except Equipment.DoesNotExist:
|
except Equipment.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
@@ -32,7 +33,7 @@ def sync_muscle_groups(request):
|
|||||||
if len(muscle_group) > 0:
|
if len(muscle_group) > 0:
|
||||||
try:
|
try:
|
||||||
print(muscle_group)
|
print(muscle_group)
|
||||||
muscle_obj = Muscle.objects.get(name=muscle_group)
|
muscle_obj = Muscle.objects.get(name=muscle_group.lower())
|
||||||
print(muscle_obj)
|
print(muscle_obj)
|
||||||
ExerciseMuscle.objects.create(exercise=exercise, muscle=muscle_obj).save()
|
ExerciseMuscle.objects.create(exercise=exercise, muscle=muscle_obj).save()
|
||||||
except MuscleGroup.DoesNotExist:
|
except MuscleGroup.DoesNotExist:
|
||||||
@@ -40,3 +41,8 @@ def sync_muscle_groups(request):
|
|||||||
|
|
||||||
|
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
@api_view(['GET'])
|
||||||
|
def clear_redis(request):
|
||||||
|
cache.clear()
|
||||||
|
return Response(status=status.HTTP_200_OK)
|
||||||
@@ -15,7 +15,7 @@ class SupersetExerciseInline(admin.StackedInline):
|
|||||||
|
|
||||||
@admin.register(Superset)
|
@admin.register(Superset)
|
||||||
class SupersetAdmin(ImportExportModelAdmin):
|
class SupersetAdmin(ImportExportModelAdmin):
|
||||||
list_display = ("name", "workout", "order", "rounds", "get_workout_id",)
|
list_display = ("name", "workout", "order", "rounds", "get_workout_id", "estimated_time",)
|
||||||
ordering = ("order",)
|
ordering = ("order",)
|
||||||
inlines = [
|
inlines = [
|
||||||
SupersetExerciseInline,
|
SupersetExerciseInline,
|
||||||
|
|||||||
20
superset/migrations/0006_alter_superset_workout.py
Normal file
20
superset/migrations/0006_alter_superset_workout.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# Generated by Django 4.2.2 on 2023-07-25 15:55
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('workout', '0011_alter_completedworkout_notes'),
|
||||||
|
('superset', '0005_supersetexercise_order'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='superset',
|
||||||
|
name='workout',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='superset_workout', to='workout.workout'),
|
||||||
|
),
|
||||||
|
]
|
||||||
18
superset/migrations/0007_superset_estimated_time.py
Normal file
18
superset/migrations/0007_superset_estimated_time.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 4.2.2 on 2023-07-25 16:01
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('superset', '0006_alter_superset_workout'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='superset',
|
||||||
|
name='estimated_time',
|
||||||
|
field=models.FloatField(blank=True, max_length=255, null=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -10,11 +10,13 @@ class Superset(models.Model):
|
|||||||
|
|
||||||
workout = models.ForeignKey(
|
workout = models.ForeignKey(
|
||||||
Workout,
|
Workout,
|
||||||
on_delete=models.CASCADE
|
on_delete=models.CASCADE,
|
||||||
|
related_name='superset_workout'
|
||||||
)
|
)
|
||||||
|
|
||||||
rounds = models.IntegerField(max_length=3, blank=False, null=False)
|
rounds = models.IntegerField(max_length=3, blank=False, null=False)
|
||||||
order = models.IntegerField(max_length=3, blank=False, null=False)
|
order = models.IntegerField(max_length=3, blank=False, null=False)
|
||||||
|
estimated_time = models.FloatField(max_length=255, blank=True, null=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name #+ " : " + self.description + " | by: " + self.registered_user.nick_name
|
return self.name #+ " : " + self.description + " | by: " + self.registered_user.nick_name
|
||||||
|
|||||||
@@ -58,6 +58,16 @@ MIDDLEWARE = [
|
|||||||
|
|
||||||
ROOT_URLCONF = 'werkout_api.urls'
|
ROOT_URLCONF = 'werkout_api.urls'
|
||||||
|
|
||||||
|
CACHES = {
|
||||||
|
"default": {
|
||||||
|
"BACKEND": "django_redis.cache.RedisCache",
|
||||||
|
"LOCATION": "redis://redis:6379/",
|
||||||
|
"OPTIONS": {
|
||||||
|
"CLIENT_CLASS": "django_redis.client.DefaultClient"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
@@ -159,6 +169,17 @@ if os.environ.get("DATABASE_URL"):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CACHES = {
|
||||||
|
"default": {
|
||||||
|
"BACKEND": "django_redis.cache.RedisCache",
|
||||||
|
"LOCATION": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
|
||||||
|
"OPTIONS": {
|
||||||
|
"CLIENT_CLASS": "django_redis.client.DefaultClient"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
CELERY_BROKER_URL = os.environ.get("REDIS_URL", "") + "/1"
|
CELERY_BROKER_URL = os.environ.get("REDIS_URL", "") + "/1"
|
||||||
CELERY_RESULT_BACKEND = os.environ.get("REDIS_URL", "") + "/1"
|
CELERY_RESULT_BACKEND = os.environ.get("REDIS_URL", "") + "/1"
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ urlpatterns = [
|
|||||||
path('muscle/', include('muscle.urls')),
|
path('muscle/', include('muscle.urls')),
|
||||||
path('equipment/', include('equipment.urls')),
|
path('equipment/', include('equipment.urls')),
|
||||||
path('registered_user/', include('registered_user.urls')),
|
path('registered_user/', include('registered_user.urls')),
|
||||||
path('.well-known/apple-app-site-association', TemplateView.as_view(template_name='frontend/apple-app-site-association', content_type='application/json',))
|
path('.well-known/apple-app-site-association', TemplateView.as_view(template_name='frontend/apple-app-site-association', content_type='application/json',)),
|
||||||
# path('scripts/', include('scripts.urls')),
|
path('scripts/', include('scripts.urls')),
|
||||||
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
|
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class SupersetInline(admin.StackedInline):
|
|||||||
|
|
||||||
@admin.register(Workout)
|
@admin.register(Workout)
|
||||||
class WorkoutAdmin(ImportExportModelAdmin):
|
class WorkoutAdmin(ImportExportModelAdmin):
|
||||||
list_display = ("name", "description", "registered_user", "created_at", "updated_at")
|
list_display = ("name", "description", "estimated_time", "registered_user", "created_at", "updated_at")
|
||||||
inlines = [
|
inlines = [
|
||||||
SupersetInline,
|
SupersetInline,
|
||||||
]
|
]
|
||||||
@@ -33,3 +33,5 @@ class CompletedWorkoutAdmin(admin.ModelAdmin):
|
|||||||
@admin.register(PlannedWorkout)
|
@admin.register(PlannedWorkout)
|
||||||
class PlannedWorkoutAdmin(admin.ModelAdmin):
|
class PlannedWorkoutAdmin(admin.ModelAdmin):
|
||||||
list_display = ("registered_user", "workout", "on_date",)
|
list_display = ("registered_user", "workout", "on_date",)
|
||||||
|
|
||||||
|
ordering = ['workout__created_at']
|
||||||
18
workout/migrations/0012_workout_estimated_time.py
Normal file
18
workout/migrations/0012_workout_estimated_time.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 4.2.2 on 2023-07-25 16:43
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('workout', '0011_alter_completedworkout_notes'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='workout',
|
||||||
|
name='estimated_time',
|
||||||
|
field=models.FloatField(blank=True, max_length=255, null=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -20,9 +20,10 @@ class Workout(models.Model):
|
|||||||
RegisteredUser,
|
RegisteredUser,
|
||||||
on_delete=models.CASCADE
|
on_delete=models.CASCADE
|
||||||
)
|
)
|
||||||
|
estimated_time = models.FloatField(max_length=255, blank=True, null=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name #+ " : " + self.description + " | by: " + self.registered_user.nick_name
|
return self.name + " : " + self.description + " | by: " + str(self.created_at)
|
||||||
|
|
||||||
class WorkoutExercise(models.Model):
|
class WorkoutExercise(models.Model):
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|||||||
53228
workout/sample_workout.json
Normal file
53228
workout/sample_workout.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -6,7 +6,7 @@ from muscle.models import ExerciseMuscle
|
|||||||
from equipment.models import WorkoutEquipment
|
from equipment.models import WorkoutEquipment
|
||||||
from video.models import Video
|
from video.models import Video
|
||||||
from video.serializers import VideoSerializer
|
from video.serializers import VideoSerializer
|
||||||
from superset.serializers import SupersetSerializer
|
from superset.serializers import SupersetSerializer, SupersetExerciseSerializer
|
||||||
from superset.models import Superset, SupersetExercise
|
from superset.models import Superset, SupersetExercise
|
||||||
|
|
||||||
class WorkoutExerciseSerializer(serializers.ModelSerializer):
|
class WorkoutExerciseSerializer(serializers.ModelSerializer):
|
||||||
@@ -79,12 +79,23 @@ class WorkoutDetailSerializer(serializers.ModelSerializer):
|
|||||||
male_videos = serializers.SerializerMethodField()
|
male_videos = serializers.SerializerMethodField()
|
||||||
female_videos = serializers.SerializerMethodField()
|
female_videos = serializers.SerializerMethodField()
|
||||||
both_videos = serializers.SerializerMethodField()
|
both_videos = serializers.SerializerMethodField()
|
||||||
|
all_superset_exercise = serializers.SerializerMethodField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Workout
|
model = Workout
|
||||||
fields = ('id', 'name', 'description', 'supersets', 'registered_user', 'male_videos', 'female_videos', 'both_videos')
|
fields = ('id', 'name', 'description', 'supersets', 'registered_user', 'male_videos', 'female_videos', 'both_videos', 'estimated_time', 'all_superset_exercise', )
|
||||||
depth = 1
|
depth = 1
|
||||||
|
|
||||||
|
def get_all_superset_exercise(self, obj):
|
||||||
|
all_superset_exercise = []
|
||||||
|
supersets = Superset.objects.filter(workout=obj).order_by('order')
|
||||||
|
for superset in supersets:
|
||||||
|
supersetExercises = SupersetExercise.objects.filter(superset=superset).order_by('order')
|
||||||
|
for x in range(0, superset.rounds):
|
||||||
|
all_superset_exercise += supersetExercises
|
||||||
|
data = SupersetExerciseSerializer(all_superset_exercise, many=True).data
|
||||||
|
return data
|
||||||
|
|
||||||
def get_supersets(self, obj):
|
def get_supersets(self, obj):
|
||||||
objs = Superset.objects.filter(workout=obj).order_by('order')
|
objs = Superset.objects.filter(workout=obj).order_by('order')
|
||||||
data = SupersetSerializer(objs, many=True).data
|
data = SupersetSerializer(objs, many=True).data
|
||||||
|
|||||||
@@ -10,4 +10,6 @@ urlpatterns = [
|
|||||||
path('create/', views.add_workout, name='create new workout'),
|
path('create/', views.add_workout, name='create new workout'),
|
||||||
path('planned_workouts/', views.workouts_planned_by_logged_in_user, name='planned workout for user'),
|
path('planned_workouts/', views.workouts_planned_by_logged_in_user, name='planned workout for user'),
|
||||||
path('plan_workout/', views.plan_workout, name='plan workout'),
|
path('plan_workout/', views.plan_workout, name='plan workout'),
|
||||||
|
|
||||||
|
path('add_from_files/', views.add_from_files, name='plan workout'),
|
||||||
]
|
]
|
||||||
125
workout/views.py
125
workout/views.py
@@ -15,20 +15,35 @@ from rest_framework.decorators import permission_classes
|
|||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def all_workouts(request):
|
def all_workouts(request):
|
||||||
|
if 'all_workouts' in cache:
|
||||||
|
data = cache.get('all_workouts')
|
||||||
|
return Response(data=data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
users = Workout.objects.all()
|
users = Workout.objects.all()
|
||||||
serializer = WorkoutSerializer(users, many=True)
|
serializer = WorkoutSerializer(users, many=True)
|
||||||
return Response(data=serializer.data, status=status.HTTP_200_OK)
|
data = serializer.data
|
||||||
|
cache.set('all_workouts', data, timeout=None)
|
||||||
|
return Response(data=data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def workout_details(request, workout_id):
|
def workout_details(request, workout_id):
|
||||||
users = Workout.objects.get(pk=workout_id)
|
cache_name = "wk"+str(workout_id)
|
||||||
serializer = WorkoutDetailSerializer(users, many=False)
|
if cache_name in cache:
|
||||||
return Response(data=serializer.data, status=status.HTTP_200_OK)
|
data = cache.get(cache_name)
|
||||||
|
return Response(data=data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
workout = Workout.objects.get(pk=workout_id)
|
||||||
|
serializer = WorkoutDetailSerializer(workout, many=False)
|
||||||
|
data = serializer.data
|
||||||
|
cache.set(cache_name, data, timeout=300)
|
||||||
|
return Response(data = data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
@authentication_classes([TokenAuthentication])
|
@authentication_classes([TokenAuthentication])
|
||||||
@@ -73,6 +88,7 @@ def add_workout(request):
|
|||||||
workout = serializer.save(registered_user=registered_user)
|
workout = serializer.save(registered_user=registered_user)
|
||||||
workout.save()
|
workout.save()
|
||||||
|
|
||||||
|
workout_total_time = 0
|
||||||
for superset in exercise_data:
|
for superset in exercise_data:
|
||||||
name = superset["name"]
|
name = superset["name"]
|
||||||
rounds = superset["rounds"]
|
rounds = superset["rounds"]
|
||||||
@@ -87,6 +103,7 @@ def add_workout(request):
|
|||||||
)
|
)
|
||||||
superset.save()
|
superset.save()
|
||||||
|
|
||||||
|
superset_total_time = 0
|
||||||
for exercise in exercises:
|
for exercise in exercises:
|
||||||
exercise_id = exercise["id"]
|
exercise_id = exercise["id"]
|
||||||
exercise_obj = Exercise.objects.get(pk=exercise_id)
|
exercise_obj = Exercise.objects.get(pk=exercise_id)
|
||||||
@@ -102,11 +119,23 @@ def add_workout(request):
|
|||||||
supersetExercise.weight = exercise["weight"]
|
supersetExercise.weight = exercise["weight"]
|
||||||
if "reps" in exercise:
|
if "reps" in exercise:
|
||||||
supersetExercise.reps = exercise["reps"]
|
supersetExercise.reps = exercise["reps"]
|
||||||
|
superset_total_time += exercise["reps"] * exercise_obj.estimated_rep_duration
|
||||||
if "duration" in exercise:
|
if "duration" in exercise:
|
||||||
supersetExercise.duration = exercise["duration"]
|
supersetExercise.duration = exercise["duration"]
|
||||||
|
superset_total_time += exercise["duration"]
|
||||||
|
|
||||||
supersetExercise.save()
|
supersetExercise.save()
|
||||||
|
|
||||||
|
superset.estimated_time = superset_total_time
|
||||||
|
superset.save()
|
||||||
|
|
||||||
|
workout_total_time += (superset_total_time * rounds)
|
||||||
|
|
||||||
superset_order += 1
|
superset_order += 1
|
||||||
|
workout.estimated_time = workout_total_time
|
||||||
|
workout.save()
|
||||||
|
|
||||||
|
cache.delete('all_workouts')
|
||||||
|
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
@@ -132,3 +161,91 @@ def plan_workout(request):
|
|||||||
serializer.save()
|
serializer.save()
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
@api_view(['GET'])
|
||||||
|
def add_from_files(request):
|
||||||
|
sample_url = os.getcwd() + "/workout/sample_workout.json"
|
||||||
|
print(sample_url)
|
||||||
|
with open(sample_url) as user_file:
|
||||||
|
file_contents = user_file.read()
|
||||||
|
parsed_json = json.loads(file_contents)
|
||||||
|
|
||||||
|
for item in parsed_json:
|
||||||
|
workout_name = item["name"]
|
||||||
|
workout_description = item["description"]
|
||||||
|
workout_created = item["created"]
|
||||||
|
|
||||||
|
workout_obj = Workout.objects.create(
|
||||||
|
registered_user = RegisteredUser.objects.get(pk=2),
|
||||||
|
description = workout_description,
|
||||||
|
name = workout_name,
|
||||||
|
created_at = workout_created
|
||||||
|
)
|
||||||
|
|
||||||
|
workout_obj.save()
|
||||||
|
workout_obj.created_at = workout_created
|
||||||
|
workout_obj.save(update_fields=['created_at'])
|
||||||
|
workout_total_time = 0
|
||||||
|
|
||||||
|
supersets = item["supersets"]
|
||||||
|
superset_order = 1
|
||||||
|
for superset in supersets:
|
||||||
|
superset_name = superset["name"]
|
||||||
|
superset_rounds = superset["rounds"]
|
||||||
|
|
||||||
|
superset_obj = Superset.objects.create(
|
||||||
|
workout=workout_obj,
|
||||||
|
name=superset_name,
|
||||||
|
rounds=superset_rounds,
|
||||||
|
order=superset_order
|
||||||
|
)
|
||||||
|
|
||||||
|
superset_obj.save()
|
||||||
|
|
||||||
|
superset_order += 1
|
||||||
|
|
||||||
|
exercises = superset["exercises"]
|
||||||
|
exercise_order = 1
|
||||||
|
|
||||||
|
superset_total_time = 0
|
||||||
|
for exercise in exercises:
|
||||||
|
side = exercise["side"]
|
||||||
|
name = exercise["name"]
|
||||||
|
|
||||||
|
duration = exercise["duration"]
|
||||||
|
reps = exercise["reps"]
|
||||||
|
side = exercise["side"]
|
||||||
|
|
||||||
|
exercise_obj = None
|
||||||
|
if len(side) > 0:
|
||||||
|
exercise_obj = Exercise.objects.get(name=name, side=side)
|
||||||
|
else:
|
||||||
|
exercise_obj = Exercise.objects.get(name=name, side="")
|
||||||
|
|
||||||
|
supersetExercise = SupersetExercise.objects.create(
|
||||||
|
superset=superset_obj,
|
||||||
|
exercise=exercise_obj,
|
||||||
|
order=exercise_order
|
||||||
|
)
|
||||||
|
|
||||||
|
if reps != 0:
|
||||||
|
supersetExercise.reps = reps
|
||||||
|
superset_total_time += reps * exercise_obj.estimated_rep_duration
|
||||||
|
if reps == 0 and duration != 0:
|
||||||
|
supersetExercise.duration = duration
|
||||||
|
superset_total_time += exercise["duration"]
|
||||||
|
supersetExercise.save()
|
||||||
|
|
||||||
|
exercise_order += 1
|
||||||
|
|
||||||
|
superset_obj.estimated_time = superset_total_time
|
||||||
|
superset_obj.save()
|
||||||
|
|
||||||
|
workout_total_time += (superset_total_time * superset_rounds)
|
||||||
|
|
||||||
|
workout_obj.estimated_time = workout_total_time
|
||||||
|
workout_obj.save()
|
||||||
|
|
||||||
|
cache.delete('all_workouts')
|
||||||
|
|
||||||
|
return Response(status=status.HTTP_200_OK)
|
||||||
Reference in New Issue
Block a user