Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add template system for workouts

Roland Geideracc9219a8514
diff --git a/wger/core/templates/navigation.html b/wger/core/templates/navigation.html
index d59841bf4..0bd02e720 100644
--- a/wger/core/templates/navigation.html
+++ b/wger/core/templates/navigation.html
@@ -36,7 +36,7 @@
{% if user.is_authenticated %}
<li><a class="dropdown-item" href="{% url 'manager:workout:calendar' user.username %}" rel="nofollow">{% translate "Calendar" %}</a></li>
{% endif %}
-
+
<li><a class="dropdown-item" href="{% url 'gallery:images:overview' %}" rel="nofollow">
{% if not user.is_authenticated %}
<span class="{% fa_class 'lock' %}"></span>
@@ -44,6 +44,21 @@
{% translate "Gallery" %}
</a></li>
+ <li class="dropdown-divider"></li>
+ <li class="dropdown-header">{% translate "Workout templates" %}</li>
+ <li><a class="dropdown-item" href="{% url 'manager:templates:overview' %}" rel="nofollow">
+ {% if not user.is_authenticated %}
+ <span class="{% fa_class 'lock' %}"></span>
+ {% endif %}
+ {% translate "Your templates" %}
+ </a></li>
+ <li><a class="dropdown-item" href="{% url 'manager:templates:public' %}" rel="nofollow">
+ {% if not user.is_authenticated %}
+ <span class="{% fa_class 'lock' %}"></span>
+ {% endif %}
+ {% translate "Public templates" %}
+ </a></li>
+
<li class="dropdown-divider"></li>
<li class="dropdown-header">{% translate "Exercises" %}</li>
<li><a class="dropdown-item" href="{% url 'exercise:exercise:overview' %}">{% translate "by category" context "as in sorted by category" %}</a></li>
diff --git a/wger/manager/api/serializers.py b/wger/manager/api/serializers.py
index ef428f41c..cc9a900ad 100644
--- a/wger/manager/api/serializers.py
+++ b/wger/manager/api/serializers.py
@@ -43,7 +43,27 @@ class WorkoutSerializer(serializers.ModelSerializer):
class Meta:
model = Workout
- exclude = ('user', )
+ fields = ('id', 'name', 'creation_date', 'description')
+
+
+class WorkoutTemplateSerializer(serializers.ModelSerializer):
+ """
+ Workout template serializer
+ """
+
+ class Meta:
+ model = Workout
+ fields = ('id', 'name', 'creation_date', 'description', 'is_public')
+
+
+class WorkoutAndTemplateSerializer(serializers.ModelSerializer):
+ """
+ Workout or template serializer
+ """
+
+ class Meta:
+ model = Workout
+ fields = ('id', 'name', 'creation_date', 'description', 'is_template', 'is_public')
class WorkoutSessionSerializer(serializers.ModelSerializer):
diff --git a/wger/manager/migrations/0014_auto_20210717_1858.py b/wger/manager/migrations/0014_auto_20210717_1858.py
new file mode 100644
index 000000000..e3eeeba89
--- /dev/null
+++ b/wger/manager/migrations/0014_auto_20210717_1858.py
@@ -0,0 +1,23 @@
+# Generated by Django 3.2.3 on 2021-07-17 16:58
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('manager', '0013_set_comment'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='workout',
+ name='is_public',
+ field=models.BooleanField(default=False, help_text='', verbose_name='Public Template'),
+ ),
+ migrations.AddField(
+ model_name='workout',
+ name='is_template',
+ field=models.BooleanField(default=False, help_text='', verbose_name='Workout Template'),
+ ),
+ ]
diff --git a/wger/manager/models.py b/wger/manager/models.py
deleted file mode 100644
index 6cf6ddc87..000000000
--- a/wger/manager/models.py
+++ /dev/null
@@ -1,174 +0,0 @@
-# -*- coding: utf-8 -*-
-
-# 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
-# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
-
-# Standard Library
-import datetime
-import logging
-import typing
-
-# Django
-from django.contrib.auth.models import User
-from django.core.cache import cache
-from django.core.exceptions import (
- ObjectDoesNotExist,
- ValidationError,
-)
-from django.core.validators import (
- MaxValueValidator,
- MinValueValidator,
-)
-from django.db import models
-from django.urls import reverse
-from django.utils.translation import gettext_lazy as _
-
-# wger
-from wger.core.models import (
- DaysOfWeek,
- RepetitionUnit,
- WeightUnit,
-)
-from wger.exercises.models import Exercise
-from wger.utils.cache import (
- cache_mapper,
- reset_workout_canonical_form,
- reset_workout_log,
-)
-from wger.utils.fields import Html5DateField
-from wger.utils.helpers import normalize_decimal
-
-
-class WorkoutSession(models.Model):
- """
- Model for a workout session
- """
-
- # Note: values hardcoded in manager.helpers.WorkoutCalendar
- IMPRESSION_BAD = '1'
- IMPRESSION_NEUTRAL = '2'
- IMPRESSION_GOOD = '3'
-
- IMPRESSION = (
- (IMPRESSION_BAD, _("Bad")),
- (IMPRESSION_NEUTRAL, _('Neutral')),
- (IMPRESSION_GOOD, _('Good')),
- )
-
- user = models.ForeignKey(
- User,
- verbose_name=_('User'),
- on_delete=models.CASCADE,
- )
- """
- The user the workout session belongs to
-
- See note in weight.models.WeightEntry about why this is not editable=False
- """
-
- workout = models.ForeignKey(
- Workout,
- verbose_name=_('Workout'),
- on_delete=models.CASCADE,
- )
- """
- The workout the session belongs to
- """
-
- date = Html5DateField(verbose_name=_('Date'))
- """
- The date the workout session was performed
- """
-
- notes = models.TextField(
- verbose_name=_('Notes'),
- null=True,
- blank=True,
- help_text=_('Any notes you might want to save about this workout '
- 'session.')
- )
- """
- User notes about the workout
- """
-
- impression = models.CharField(
- verbose_name=_('General impression'),
- max_length=2,
- choices=IMPRESSION,
- default=IMPRESSION_NEUTRAL,
- help_text=_(
- 'Your impression about this workout session. '
- 'Did you exercise as well as you could?'
- )
- )
- """
- The user's general impression of workout
- """
-
- time_start = models.TimeField(verbose_name=_('Start time'), blank=True, null=True)
- """
- Time the workout session started
- """
-
- time_end = models.TimeField(verbose_name=_('Finish time'), blank=True, null=True)
- """
- Time the workout session ended
- """
-
- def __str__(self):
- """
- Return a more human-readable representation
- """
- return "{0} - {1}".format(self.workout, self.date)
-
- class Meta:
- """
- Set other properties
- """
- ordering = [
- "date",
- ]
- unique_together = ("date", "user")
-
- def clean(self):
- """
- Perform some additional validations
- """
-
- if (not self.time_end and self.time_start) or (self.time_end and not self.time_start):
- raise ValidationError(_("If you enter a time, you must enter both start and end time."))
-
- if self.time_end and self.time_start and self.time_start > self.time_end:
- raise ValidationError(_("The start time cannot be after the end time."))
-
- def get_owner_object(self):
- """
- Returns the object that has owner information
- """
- return self
-
- def save(self, *args, **kwargs):
- """
- Reset cache
- """
- reset_workout_log(self.user_id, self.date.year, self.date.month)
- super(WorkoutSession, self).save(*args, **kwargs)
-
- def delete(self, *args, **kwargs):
- """
- Reset cache
- """
- reset_workout_log(self.user_id, self.date.year, self.date.month)
- super(WorkoutSession, self).delete(*args, **kwargs)
diff --git a/wger/manager/models/workout.py b/wger/manager/models/workout.py
index ba384624c..5e52cf4b5 100644
--- a/wger/manager/models/workout.py
+++ b/wger/manager/models/workout.py
@@ -22,6 +22,7 @@ from django.urls import reverse
from django.utils.translation import gettext_lazy as _
# wger
+from wger.manager.managers import WorkoutManager, WorkoutTemplateManager, WorkoutAndTemplateManager
from wger.utils.cache import (
cache_mapper,
reset_workout_canonical_form,
@@ -33,6 +34,10 @@ class Workout(models.Model):
Model for a training schedule
"""
+ objects = WorkoutManager()
+ templates = WorkoutTemplateManager()
+ both = WorkoutAndTemplateManager()
+
class Meta:
"""
Meta class to set some other properties
diff --git a/wger/manager/templates/workout/overview.html b/wger/manager/templates/workout/overview.html
index dc0754417..7b3790824 100644
--- a/wger/manager/templates/workout/overview.html
+++ b/wger/manager/templates/workout/overview.html
@@ -2,7 +2,7 @@
{% load i18n %}
{% load static %}
-{% block title %}{% translate "Your workouts" %}{% endblock %}
+{% block title %}{{ title }}{% endblock %}
{% block content %}
@@ -21,16 +21,22 @@
<p class="list-group-item-text">{{ workout.creation_date }}</p>
</a>
{% empty %}
- <a href="{% url 'manager:workout:add' %}" class="list-group-item">
- {% translate "No workouts found." %}<br>{% translate "Add one now." %}
- </a>
+ {% if template_overview %}
+ {% translate "No templates found." %}
+ {% else %}
+ <a href="{% url 'manager:workout:add' %}" class="list-group-item">
+ {% translate "No workouts found." %}<br>{% translate "Add one now." %}
+ </a>
+ {% endif %}
{% endfor %}
</div>
{% endblock %}
{% block options %}
+{% if not template_overview %}
<a href="{% url 'manager:workout:add' %}" class="btn btn-success btn-sm">
{% translate "Add workout" %}
</a>
+{% endif %}
{% endblock %}
diff --git a/wger/manager/templates/workout/template_view.html b/wger/manager/templates/workout/template_view.html
new file mode 100644
index 000000000..d53984cbb
--- /dev/null
+++ b/wger/manager/templates/workout/template_view.html
@@ -0,0 +1,88 @@
+{% extends "base.html" %}
+{% load i18n %}
+{% load static %}
+{% load wger_extras %}
+
+{# #}
+{# Opengraph #}
+{# #}
+{% block opengraph %}
+ {{ block.super }}
+ <meta property="og:title" content="{% translate 'Workout template' %}">
+ <meta property="og:description" content="{{ workout }} / {{owner_user.username}}">
+{% endblock %}
+
+
+{# #}
+{# Title #}
+{# #}
+{% block title %}{% translate 'Workout template' %}{% endblock %}
+
+
+{# #}
+{# Header #}
+{# #}
+{% block header %}
+
+{% endblock %}
+
+
+{# #}
+{# Content #}
+{# #}
+{% block content %}
+
+
+{% if workout.name %}
+ <h4>{{workout.name }}</h4>
+{% endif %}
+
+{% if workout.description %}
+ <p>{{ workout.description }}</p>
+{% endif %}
+
+{% for day in workout.canonical_representation.day_list %}
+ <div id="div-day-{{ day.obj.id }}">
+ {% render_day day.obj is_owner %}
+ </div>
+{% endfor %}
+
+<a href="{% url 'manager:templates:make-workout' workout.id %}" class="btn btn-outline-info">
+ {% translate "Make workout" %}
+</a>
+{% endblock %}
+
+
+
+{# #}
+{# Side bar #}
+{# #}
+{% block sidebar %}
+<h4>{% translate "Muscles trained" %}</h4>
+
+<p>
+ {% translate "Images show combined muscles as trained by the exercises on the workout." %}
+</p>
+
+<div class="row">
+ <div class="col-6">
+ {% render_muscles muscles.front muscles.frontsecondary %}
+ </div>
+ <div class="col-6">
+ {% render_muscles muscles.back muscles.backsecondary %}
+ </div>
+</div>
+{% endblock %}
+
+
+
+{# #}
+{# Options #}
+{# #}
+{% block options %}
+<a href="{% url 'manager:workout:copy' workout.id %}" class="btn btn-success">
+ <span class="{% fa_class 'clone' %}"></span>
+ {% translate "Use this template" %}
+</a>
+
+{% endblock %}
diff --git a/wger/manager/urls.py b/wger/manager/urls.py
index 19d8708ef..16c68f249 100644
--- a/wger/manager/urls.py
+++ b/wger/manager/urls.py
@@ -56,6 +56,30 @@ patterns_log = [
)
]
+# sub patterns for templates
+patterns_templates = [
+ path(
+ 'overview',
+ workout.template_overview,
+ name='overview',
+ ),
+ path(
+ 'public',
+ workout.public_template_overview,
+ name='public',
+ ),
+ path(
+ '<int:pk>/view/',
+ workout.template_view,
+ name='view',
+ ),
+ path(
+ '<int:pk>/make-workout/',
+ workout.make_workout,
+ name='make-workout',
+ ),
+]
+
# sub patterns for workouts
patterns_workout = [
path(
@@ -347,6 +371,7 @@ patterns_step = [
urlpatterns = [
path('', include((patterns_workout, 'workout'), namespace="workout")),
+ path('templates/', include((patterns_templates, 'templates'), namespace="templates")),
path('log/', include((patterns_log, 'log'), namespace="log")),
path('day/', include((patterns_day, 'day'), namespace="day")),
path('set/', include((patterns_set, 'set'), namespace="set")),
diff --git a/wger/manager/views/workout.py b/wger/manager/views/workout.py
index ec53b6cab..80c6f0586 100644
--- a/wger/manager/views/workout.py
+++ b/wger/manager/views/workout.py
@@ -92,6 +92,8 @@ def overview(request):
(current_workout, schedule) = Schedule.objects.get_current_workout(request.user)
template_data['workouts'] = workouts
template_data['current_workout'] = current_workout
+ template_data['title'] = _('Your workouts')
+ template_data['template_overview'] = False
return render(request, 'workout/overview.html', template_data)
@@ -148,6 +150,8 @@ def copy_workout(request, pk):
workout_copy.pk = None
workout_copy.name = workout_form.cleaned_data['name']
workout_copy.user = request.user
+ workout_copy.is_template = False
+ workout_copy.is_public = False
workout_copy.save()
# Copy the days
@@ -177,9 +181,7 @@ def copy_workout(request, pk):
setting_copy.set = current_set_copy
setting_copy.save()
- return HttpResponseRedirect(
- reverse('manager:workout:view', kwargs={'pk': workout_copy.id})
- )
+ return HttpResponseRedirect(workout_copy.get_absolute_url())
else:
workout_form = WorkoutCopyForm({'name': workout.name, 'description': workout.description})
workout_form.helper = FormHelper()
diff --git a/wger/urls.py b/wger/urls.py
index 389105c7e..9555cc809 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -58,6 +58,8 @@ router.register(r'day', manager_api_views.DayViewSet, basename='day')
router.register(r'set', manager_api_views.SetViewSet, basename='Set')
router.register(r'setting', manager_api_views.SettingViewSet, basename='Setting')
router.register(r'workout', manager_api_views.WorkoutViewSet, basename='workout')
+router.register(r'templates', manager_api_views.UserWorkoutTemplateViewSet, basename='templates')
+router.register(r'public-templates', manager_api_views.PublicWorkoutTemplateViewSet, basename='public-templates')
router.register(
r'workoutsession', manager_api_views.WorkoutSessionViewSet, basename='workoutsession'
)