Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move the exercises to its own app
Roland Geidere5e37d86b0da
diff --git a/exercises/__init__.py b/exercises/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/exercises/models.py b/exercises/models.pynew file mode 100644index 000000000..0e12bdfe4--- /dev/null+++ b/exercises/models.py@@ -0,0 +1,135 @@+# This file is part of Workout Manager.+#+# Foobar 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.+#+# 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/>.++from django.db import models+from django.utils.translation import ugettext as _++import calendar++class TrainingSchedule(models.Model):+ """Model for a training schedule+ """+ creation_date = models.DateField(_('Creation date'), auto_now_add=True)+ comment = models.CharField(_('Comment'), max_length=100, blank=True)+++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return str(self.creation_date)+++# Mhh...+DAYS_OF_WEEK_CHOICES = []+cal = calendar.Calendar()+for i in cal.iterweekdays():+ DAYS_OF_WEEK_CHOICES.append((int(i), calendar.day_name[i]))++class Day(models.Model):+ """Model for a training day+ """+ CHOICES = DAYS_OF_WEEK_CHOICES++ training = models.ForeignKey(TrainingSchedule, verbose_name = _('Training'))+ description = models.CharField(max_length=100,+ verbose_name = _('Description'),+ help_text=_('Ususally a description about what parts are trained, like "Arms" or "Pull Day"'))+ day = models.IntegerField(max_length=1, choices=CHOICES, verbose_name = _('Day'))++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return "%s for TP %s" % (self.description, unicode(self.training))++++class ExerciseCategory(models.Model):+ """Model for an exercise category+ """+ name = models.CharField(_('Name'), max_length=100)++ # Metaclass to set some other properties+ class Meta:+ verbose_name_plural = _("Exercise Categories")+ ordering = ["name", ]++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return self.name++++class Exercise(models.Model):+ """Model for an exercise+ """+ category = models.ForeignKey(ExerciseCategory, verbose_name = _('Category'))+ name = models.CharField(max_length=200, verbose_name = _('Name'))++ # Metaclass to set some other properties+ class Meta:+ ordering = ["name", ]++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return self.name++class ExerciseComment(models.Model):+ """Model for an exercise comment+ """+ exercise = models.ForeignKey(Exercise, verbose_name = _('Exercise'))+ comment = models.CharField(max_length=200,+ blank=True,+ verbose_name = _('Comment'),+ help_text=_('Some comment about how to correctly do this exercise'))++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return self.comment++++class Set(models.Model):+ """Model for a set of exercises+ """++ exerciseday = models.ForeignKey(Day, verbose_name = _('Exercise day'))+ exercises = models.ManyToManyField(Exercise, verbose_name = _('Exercises'))+ order = models.IntegerField(max_length=1, blank=True, null=True, verbose_name = _('Order')) #TODO: null=True???+ sets = models.IntegerField(verbose_name = _('Sets'))++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return "Set %s for %s" % (self.order or '-/-', unicode(self.exerciseday))++++class Setting(models.Model):+ """Settings for an exercise (weight, reps, etc.)+ """++ sets = models.ForeignKey(Set, verbose_name = _('Sets'))+ exercises = models.ForeignKey(Exercise, verbose_name = _('Exercises'))+ reps = models.IntegerField(verbose_name = _('Repetitions'))+ comment = models.CharField(max_length=100, blank=True, verbose_name = _('Comment'))+++ def __unicode__(self):+ """Return a more human-readable representation+ """+ return "settings for exercise %s in set %s" % (self.exercises.id, self.sets.id)\ No newline at end of filediff --git a/manager/templates/exercise/edit.html b/exercises/templates/edit.htmlsimilarity index 100%rename from manager/templates/exercise/edit.htmlrename to exercises/templates/edit.htmldiff --git a/manager/templates/exercise/edit_category.html b/exercises/templates/edit_category.htmlsimilarity index 86%rename from manager/templates/exercise/edit_category.htmlrename to exercises/templates/edit_category.htmlindex e927dc88b..f908ccc82 100644--- a/manager/templates/exercise/edit_category.html+++ b/exercises/templates/edit_category.html@@ -20,7 +20,7 @@{% endif %}</h2>-<form action="{% url manager.views.exercise_category_edit category.id %}"+<form action="{% url exercises.views.exercise_category_edit category.id %}"method="post"class="ym-form"id="category_edit_form">diff --git a/manager/templates/exercise/overview.html b/exercises/templates/overview.htmlsimilarity index 84%rename from manager/templates/exercise/overview.htmlrename to exercises/templates/overview.htmlindex c3856a5c6..91f2facd0 100644--- a/manager/templates/exercise/overview.html+++ b/exercises/templates/overview.html@@ -19,7 +19,7 @@ $(document).ready(function() {// Init the autocompleter$("#exercise-search").autocomplete({- source: "{% url manager.views.exercise_search %}",+ source: "{% url exercises.views.exercise_search %}",minLength: 2,select: function(event, ui) {@@ -92,14 +92,14 @@ $(document).ready(function() {{% for exercise in category.exercise_set.all %}<tr><td>- <a href="{% url manager.views.exercise_view exercise.id %}">{{ exercise.name }}</a>+ <a href="{% url exercises.views.exercise_view exercise.id %}">{{ exercise.name }}</a></td><td style="text-align:right;"><div class="editoptions">- <a href="{% url manager.views.exercise_edit exercise.id %}"+ <a href="{% url exercises.views.exercise_edit exercise.id %}"title="{% trans 'Edit' %}">[e]</a>- <a href="{% url manager.views.exercise_delete exercise.id %}"+ <a href="{% url exercises.views.exercise_delete exercise.id %}"title="{% trans 'Delete' %}"class="delete-exercise">[x]</a></div>@@ -117,7 +117,7 @@ $(document).ready(function() {</tbody></table>-<!-- <a href="{% url manager.views.exercise_category_edit category.id %}">[e]</a> -->+<!-- <a href="{% url exercises.views.exercise_category_edit category.id %}">[e]</a> --></div>{% endfor %}</div>@@ -132,8 +132,8 @@ $(document).ready(function() {<h3>{% trans "Options" %}</h3><p>- <a href="{% url manager.views.exercise_edit None %}">{% trans "Add new exercise" %}</a> |- <a href="{% url manager.views.exercise_category_edit None %}">{% trans "Add category" %}</a>+ <a href="{% url exercises.views.exercise_edit None %}">{% trans "Add new exercise" %}</a> |+ <a href="{% url exercises.views.exercise_category_edit None %}">{% trans "Add category" %}</a></p><h3>{% trans "Search" %}</h3>diff --git a/manager/templates/exercise/view.html b/exercises/templates/view.htmlsimilarity index 64%rename from manager/templates/exercise/view.htmlrename to exercises/templates/view.htmlindex 0cf2be35c..03fd62120 100644--- a/manager/templates/exercise/view.html+++ b/exercises/templates/view.html@@ -24,14 +24,14 @@{% for comment in exercise.exercisecomment_set.all %}<li>{% if comment_edit.id == comment.id %}- <form action="{% url manager.views.exercise_view exercise.id comment.id %}" method="post">+ <form action="{% url exercises.views.exercise_view exercise.id comment.id %}" method="post">{% csrf_token %}{{ comment_edit_form.comment }}<input type="submit" value="{% trans "Save" %}" /></form>{% else %}- <a href="{% url manager.views.exercisecomment_delete comment.id %}" title="{% trans "Delete" %}">[x]</a>- <a href="{% url manager.views.exercise_view exercise.id comment.id %}" title="{% trans "Edit" %}">[e]</a>+ <a href="{% url exercises.views.exercisecomment_delete comment.id %}" title="{% trans "Delete" %}">[x]</a>+ <a href="{% url exercises.views.exercise_view exercise.id comment.id %}" title="{% trans "Edit" %}">[e]</a>{{ comment }}{% endif %}</li>@@ -41,7 +41,7 @@</ul><h3>{% trans "Add new comment" %}</h3>-<form action = "{% url manager.views.exercise_view exercise.id %}"+<form action = "{% url exercises.views.exercise_view exercise.id %}"method = "post"id = "new_comment_form">{% csrf_token %}@@ -61,7 +61,7 @@<h3>{% trans "Options" %}</h3><p>- <a href="{% url manager.views.exercise_edit exercise.id %}">{% trans "Edit" %}</a> |- <a href="{% url manager.views.exercise_delete exercise.id %}">{% trans "Delete" %}</a>+ <a href="{% url exercises.views.exercise_edit exercise.id %}">{% trans "Edit" %}</a> |+ <a href="{% url exercises.views.exercise_delete exercise.id %}">{% trans "Delete" %}</a></p>{% endblock %}\ No newline at end of filediff --git a/exercises/urls.py b/exercises/urls.pynew file mode 100644index 000000000..974474690--- /dev/null+++ b/exercises/urls.py@@ -0,0 +1,14 @@+from django.conf.urls import patterns, include, url++urlpatterns = patterns('exercises.views',+ url(r'^exercise/overview/$', 'exercise_overview'),+ url(r'^exercise/search/$', 'exercise_search'),+ url(r'^exercise/view/(?P<id>\d+)$', 'exercise_view'),+ url(r'^exercise/view/(?P<id>\d+)/edit/comment/(?P<comment_id>\d+)$', 'exercise_view'),+ url(r'^exercise/edit/(?P<id>\d*)$', 'exercise_edit'),+ url(r'^exercise/delete/(?P<id>\d*)$', 'exercise_delete'),+ url(r'^exercise/comment/delete/(?P<id>\d+)$', 'exercisecomment_delete'),+ url(r'^exercise/category/edit/(?P<id>\d*)$', 'exercise_category_edit'),+ url(r'^exercise/category/delete/(?P<id>\d*)$', 'exercise_category_delete'),++)diff --git a/exercises/views.py b/exercises/views.pynew file mode 100644index 000000000..109852e38--- /dev/null+++ b/exercises/views.py@@ -0,0 +1,218 @@+# This file is part of Workout Manager.+#+# 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.+#+# 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+import logging+import calendar+import json++from django.shortcuts import render_to_response+from django.shortcuts import get_object_or_404+from django.http import HttpResponse+from django.http import HttpResponseRedirect+from django.http import Http404+from django.forms import ModelForm+from django.forms.models import modelformset_factory+from django.core.context_processors import csrf+from django.contrib.auth import authenticate, login, logout+from django.contrib.auth.decorators import permission_required, login_required+from django.utils.translation import ugettext as _+++from manager.models import Exercise+from manager.models import ExerciseComment+from manager.models import ExerciseCategory++logger = logging.getLogger('workout_manager.custom')++# ************************+# Exercise comment functions+# ************************+@permission_required('manager.add_exercisecomment')+def exercisecomment_delete(request, id):+ # Load the comment+ comment = get_object_or_404(ExerciseComment, pk=id)+ exercise_id = comment.exercise.id+ comment.delete()++ return HttpResponseRedirect('/exercise/view/%s' % exercise_id)++++# ************************+# Exercise functions+# ************************++class ExerciseCommentForm(ModelForm):+ class Meta:+ model = ExerciseComment+ exclude=('exercise',)++class ExerciseForm(ModelForm):+ class Meta:+ model = Exercise++class ExerciseCategoryForm(ModelForm):+ class Meta:+ model = ExerciseCategory++def exercise_overview(request):+ """Overview with all exercises+ """++ template_data = {}+ template_data['categories'] = ExerciseCategory.objects.all()++ return render_to_response('overview.html', template_data)+++def exercise_view(request, id, comment_id=None):+ """ Detail view for an exercise+ """+ template_data = {}+ template_data['comment_edit'] = False++ # Load the exercise itself+ exercise = get_object_or_404(Exercise, pk=id)+ template_data['exercise'] = exercise++ #+ # We can create and edit comments from this page, so look for Posts+ template_data.update(csrf(request))++ # Adding a new comment+ if request.method == 'POST' and not comment_id:+ comment_form = ExerciseCommentForm(request.POST)+ comment_form.exercise = exercise++ # If the data is valid, save and redirect+ if comment_form.is_valid():+ new_comment = comment_form.save(commit=False)+ new_comment.exercise = exercise+ new_comment.save()+ return HttpResponseRedirect('/exercise/view/%s' % id)+ else:+ comment_form = ExerciseCommentForm()++ # Editing a comment+ if comment_id:+ exercise_comment = get_object_or_404(ExerciseComment, pk=comment_id)+ template_data['comment_edit'] = exercise_comment++ comment_edit_form = ExerciseCommentForm(instance=exercise_comment)+ template_data['comment_edit_form'] = comment_edit_form++ if request.method == 'POST':+ comment_form = ExerciseCommentForm(request.POST, instance=exercise_comment)++ # If the data is valid, save and redirect+ if comment_form.is_valid():+ comment = comment_form.save(commit=False)+ comment.save()+ return HttpResponseRedirect('/exercise/view/%s' % id)+++ template_data['comment_form'] = comment_form++ # Render+ return render_to_response('view.html', template_data)++@permission_required('manager.change_exercise')+def exercise_edit(request, id=None):+ template_data = {}+ template_data.update(csrf(request))++ if not id:+ exercise = Exercise()+ else:+ exercise = get_object_or_404(Exercise, pk=id)+ template_data['exercise'] = exercise++ if request.method == 'POST':+ exercise_form = ExerciseForm(request.POST, instance=exercise)++ # If the data is valid, save and redirect+ if exercise_form.is_valid():+ exercise = exercise_form.save()+ id = exercise.id+ return HttpResponseRedirect('/exercise/view/%s' % id)+ else:+ exercise_form = ExerciseForm(instance=exercise)++ template_data['edit_form'] = exercise_form++ return render_to_response('edit.html', template_data)++@permission_required('manager.delete_exercise')+def exercise_delete(request, id):+ # Load the exercise+ exercise = get_object_or_404(Exercise, pk=id)+ exercise.delete()++ return HttpResponseRedirect('/exercise/overview/')++@permission_required('manager.change_exercisecategory')+def exercise_category_edit(request, id):+ template_data = {}+ template_data.update(csrf(request))++ if not id:+ category = ExerciseCategory()+ else:+ category = get_object_or_404(ExerciseCategory, pk=id)+ template_data['category'] = category++ if request.method == 'POST':+ category_form = ExerciseCategoryForm(request.POST, instance=category)++ # If the data is valid, save and redirect+ if category_form.is_valid():+ category = category_form.save()+ return HttpResponseRedirect('/exercise/overview/')+ else:+ category_form = ExerciseCategoryForm(instance=category)++ template_data['category_form'] = category_form++ return render_to_response('edit_category.html', template_data)++@permission_required('manager.delete_exercisecategory')+def exercise_category_delete(request, id):+ # Load the category+ category = get_object_or_404(ExerciseCategory, pk=id)+ category.delete()++ return HttpResponseRedirect('/exercise/overview/')++def exercise_search(request):+ """Search an exercise, return the result as a JSON list+ """++ if request.is_ajax():+ q = request.GET.get('term', '')++ # Perform the search+ exercises = Exercise.objects.filter(name__icontains = q )[:20]+ results = []+ for exercise in exercises:+ exercise_json = {}+ exercise_json['id'] = exercise.id+ exercise_json['name'] = exercise.name+ exercise_json['value'] = exercise.name+ results.append(exercise_json)+ data = json.dumps(results)+ else:+ data = 'fail'++ # Return the results to the server+ mimetype = 'application/json'+ return HttpResponse(data, mimetype)\ No newline at end of filediff --git a/manager/urls.py b/manager/urls.pyindex d885533b9..5658a73d3 100644--- a/manager/urls.py+++ b/manager/urls.py@@ -15,16 +15,4 @@ urlpatterns = patterns('manager.views',url(r'^workout/(?P<id>\d+)/day/(?P<day_id>\d+)/delete/set/(?P<set_id>\d+)$', 'delete_set'),url(r'^workout/(?P<id>\d+)/set/(?P<set_id>\d+)/exercise/(?P<exercise_id>\d+)/edit/setting/(?P<setting_id>\d*)$', 'edit_setting'),url(r'^workout/(?P<id>\d+)/set/(?P<set_id>\d+)/exercise/(?P<exercise_id>\d+)/delete/setting$', 'delete_setting'),--- url(r'^exercise/overview/$', 'exercise_overview'),- url(r'^exercise/search/$', 'exercise_search'),- url(r'^exercise/view/(?P<id>\d+)$', 'exercise_view'),- url(r'^exercise/view/(?P<id>\d+)/edit/comment/(?P<comment_id>\d+)$', 'exercise_view'),- url(r'^exercise/edit/(?P<id>\d*)$', 'exercise_edit'),- url(r'^exercise/delete/(?P<id>\d*)$', 'exercise_delete'),- url(r'^exercise/comment/delete/(?P<id>\d+)$', 'exercisecomment_delete'),- url(r'^exercise/category/edit/(?P<id>\d*)$', 'exercise_category_edit'),- url(r'^exercise/category/delete/(?P<id>\d*)$', 'exercise_category_delete'),-)diff --git a/manager/views.py b/manager/views.pyindex cd8827283..47305173f 100644--- a/manager/views.py+++ b/manager/views.py@@ -337,190 +337,6 @@ def delete_set(request, id, day_id, set_id):return HttpResponseRedirect('/workout/%s/view/' % id)-# ************************-# Exercise comment functions-# ************************-@permission_required('manager.add_exercisecomment')-def exercisecomment_delete(request, id):- # Load the comment- comment = get_object_or_404(ExerciseComment, pk=id)- exercise_id = comment.exercise.id- comment.delete()-- return HttpResponseRedirect('/exercise/view/%s' % exercise_id)----# ************************-# Exercise functions-# ************************--class ExerciseCommentForm(ModelForm):- class Meta:- model = ExerciseComment- exclude=('exercise',)--class ExerciseForm(ModelForm):- class Meta:- model = Exercise--class ExerciseCategoryForm(ModelForm):- class Meta:- model = ExerciseCategory--def exercise_overview(request):- """Overview with all exercises- """-- template_data = {}- template_data['categories'] = ExerciseCategory.objects.all()-- return render_to_response('exercise/overview.html', template_data)---def exercise_view(request, id, comment_id=None):- """ Detail view for an exercise- """- template_data = {}- template_data['comment_edit'] = False-- # Load the exercise itself- exercise = get_object_or_404(Exercise, pk=id)- template_data['exercise'] = exercise-- #- # We can create and edit comments from this page, so look for Posts- template_data.update(csrf(request))-- # Adding a new comment- if request.method == 'POST' and not comment_id:- comment_form = ExerciseCommentForm(request.POST)- comment_form.exercise = exercise-- # If the data is valid, save and redirect- if comment_form.is_valid():- new_comment = comment_form.save(commit=False)- new_comment.exercise = exercise- new_comment.save()- return HttpResponseRedirect('/exercise/view/%s' % id)- else:- comment_form = ExerciseCommentForm()-- # Editing a comment- if comment_id:- exercise_comment = get_object_or_404(ExerciseComment, pk=comment_id)- template_data['comment_edit'] = exercise_comment-- comment_edit_form = ExerciseCommentForm(instance=exercise_comment)- template_data['comment_edit_form'] = comment_edit_form-- if request.method == 'POST':- comment_form = ExerciseCommentForm(request.POST, instance=exercise_comment)-- # If the data is valid, save and redirect- if comment_form.is_valid():- comment = comment_form.save(commit=False)- comment.save()- return HttpResponseRedirect('/exercise/view/%s' % id)--- template_data['comment_form'] = comment_form-- # Render- return render_to_response('exercise/view.html', template_data)--@permission_required('manager.change_exercise')-def exercise_edit(request, id=None):- template_data = {}- template_data.update(csrf(request))-- if not id:- exercise = Exercise()- else:- exercise = get_object_or_404(Exercise, pk=id)- template_data['exercise'] = exercise-- if request.method == 'POST':- exercise_form = ExerciseForm(request.POST, instance=exercise)-- # If the data is valid, save and redirect- if exercise_form.is_valid():- exercise = exercise_form.save()- id = exercise.id- return HttpResponseRedirect('/exercise/view/%s' % id)- else:- exercise_form = ExerciseForm(instance=exercise)-- template_data['edit_form'] = exercise_form-- return render_to_response('exercise/edit.html', template_data)--@permission_required('manager.delete_exercise')-def exercise_delete(request, id):- # Load the exercise- exercise = get_object_or_404(Exercise, pk=id)- exercise.delete()-- return HttpResponseRedirect('/exercise/overview/')--@permission_required('manager.change_exercisecategory')-def exercise_category_edit(request, id):- template_data = {}- template_data.update(csrf(request))-- if not id:- category = ExerciseCategory()- else:- category = get_object_or_404(ExerciseCategory, pk=id)- template_data['category'] = category-- if request.method == 'POST':- category_form = ExerciseCategoryForm(request.POST, instance=category)-- # If the data is valid, save and redirect- if category_form.is_valid():- category = category_form.save()- return HttpResponseRedirect('/exercise/overview/')- else:- category_form = ExerciseCategoryForm(instance=category)-- template_data['category_form'] = category_form-- return render_to_response('exercise/edit_category.html', template_data)--@permission_required('manager.delete_exercisecategory')-def exercise_category_delete(request, id):- # Load the category- category = get_object_or_404(ExerciseCategory, pk=id)- category.delete()-- return HttpResponseRedirect('/exercise/overview/')--def exercise_search(request):- """Search an exercise, return the result as a JSON list- """-- if request.is_ajax():- q = request.GET.get('term', '')-- # Perform the search- exercises = Exercise.objects.filter(name__icontains = q )[:20]- results = []- for exercise in exercises:- exercise_json = {}- exercise_json['id'] = exercise.id- exercise_json['name'] = exercise.name- exercise_json['value'] = exercise.name- results.append(exercise_json)- data = json.dumps(results)- else:- data = 'fail'-- # Return the results to the server- mimetype = 'application/json'- return HttpResponse(data, mimetype)--# ************************# Settings functions# ************************diff --git a/workout_manager/settings.py b/workout_manager/settings.pyindex 39edd7e2c..694cffefc 100644--- a/workout_manager/settings.py+++ b/workout_manager/settings.py@@ -113,7 +113,8 @@ TEMPLATE_DIRS = (# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".# Always use forward slashes, even on Windows.# Don't forget to use absolute paths, not relative paths.- '/media/multimedia/www/workout_manager/workout_manager/manager/templates'+ '/media/multimedia/www/workout_manager/workout_manager/manager/templates',+ '/media/multimedia/www/workout_manager/workout_manager/exercises/templates')INSTALLED_APPS = (@@ -129,6 +130,7 @@ INSTALLED_APPS = (# 'django.contrib.admindocs','manager','weight',+ 'exercises',)# A sample logging configuration. The only tangible loggingdiff --git a/workout_manager/urls.py b/workout_manager/urls.pyindex 1f79a5d69..616533e98 100644--- a/workout_manager/urls.py+++ b/workout_manager/urls.py@@ -6,5 +6,6 @@ admin.autodiscover()urlpatterns = patterns('',url(r'^admin/', include(admin.site.urls)),url(r'^', include('manager.urls')),- url(r'^', include('weight.urls')),+ url(r'^', include('exercises.urls')),+ #url(r'^', include('weight.urls')),)\ No newline at end of file