Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add second unit for settings: Weight Units

Roland Geideraa12014813ab
diff --git a/wger/core/api/serializers.py b/wger/core/api/serializers.py
index 52e94f4ec..0ee7362df 100644
--- a/wger/core/api/serializers.py
+++ b/wger/core/api/serializers.py
@@ -22,8 +22,8 @@ from wger.core.models import (
Language,
DaysOfWeek,
License,
- RepetitionUnit
-)
+ RepetitionUnit,
+ WeightUnit)
class UserprofileSerializer(serializers.ModelSerializer):
@@ -67,7 +67,15 @@ class LicenseSerializer(serializers.ModelSerializer):
class RepetitionUnitSerializer(serializers.ModelSerializer):
'''
- License serializer
+ Repetition unit serializer
'''
class Meta:
model = RepetitionUnit
+
+
+class WeightUnitSerializer(serializers.ModelSerializer):
+ '''
+ Weight unit serializer
+ '''
+ class Meta:
+ model = WeightUnit
diff --git a/wger/core/api/views.py b/wger/core/api/views.py
index 272a389bb..54ef0f580 100644
--- a/wger/core/api/views.py
+++ b/wger/core/api/views.py
@@ -25,14 +25,15 @@ from wger.core.models import (
Language,
DaysOfWeek,
License,
- RepetitionUnit
-)
+ RepetitionUnit,
+ WeightUnit)
from wger.core.api.serializers import (
UsernameSerializer,
LanguageSerializer,
DaysOfWeekSerializer,
LicenseSerializer,
- RepetitionUnitSerializer
+ RepetitionUnitSerializer,
+ WeightUnitSerializer
)
from wger.core.api.serializers import UserprofileSerializer
from wger.utils.permissions import UpdateOnlyPermission, WgerPermission
@@ -104,9 +105,19 @@ class LicenseViewSet(viewsets.ReadOnlyModelViewSet):
class RepetitionUnitViewSet(viewsets.ReadOnlyModelViewSet):
'''
- API endpoint for setting units objects
+ API endpoint for repetition units objects
'''
queryset = RepetitionUnit.objects.all()
serializer_class = RepetitionUnitSerializer
ordering_fields = '__all__'
filter_fields = ('name', )
+
+
+class WeightUnitViewSet(viewsets.ReadOnlyModelViewSet):
+ '''
+ API endpoint for weight units objects
+ '''
+ queryset = WeightUnit.objects.all()
+ serializer_class = WeightUnitSerializer
+ ordering_fields = '__all__'
+ filter_fields = ('name', )
diff --git a/repetition_units.json b/wger/core/fixtures/repetition_units.json
similarity index 100%
rename from repetition_units.json
rename to wger/core/fixtures/repetition_units.json
diff --git a/wger/core/fixtures/weight_units.json b/wger/core/fixtures/weight_units.json
new file mode 100644
index 000000000..33e124f2c
--- /dev/null
+++ b/wger/core/fixtures/weight_units.json
@@ -0,0 +1,44 @@
+[
+ {
+ "pk": 1,
+ "fields": {
+ "name": "Kg"
+ },
+ "model": "core.weightunit"
+ },
+ {
+ "pk": 2,
+ "fields": {
+ "name": "Lb"
+ },
+ "model": "core.weightunit"
+ },
+ {
+ "pk": 3,
+ "fields": {
+ "name": "Body Weight"
+ },
+ "model": "core.weightunit"
+ },
+ {
+ "pk": 4,
+ "fields": {
+ "name": "Plates"
+ },
+ "model": "core.weightunit"
+ },
+ {
+ "pk": 5,
+ "fields": {
+ "name": "Kilometers Per Hour"
+ },
+ "model": "core.weightunit"
+ },
+ {
+ "pk": 6,
+ "fields": {
+ "name": "Miles Per Hour"
+ },
+ "model": "core.weightunit"
+ }
+]
diff --git a/wger/core/migrations/0008_weightunit.py b/wger/core/migrations/0008_weightunit.py
new file mode 100644
index 000000000..17cd28ced
--- /dev/null
+++ b/wger/core/migrations/0008_weightunit.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('core', '0007_repetitionunit'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='WeightUnit',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+ ('name', models.CharField(verbose_name='Name', max_length=100)),
+ ],
+ options={
+ 'ordering': ['name'],
+ },
+ ),
+ ]
diff --git a/wger/core/models.py b/wger/core/models.py
index cd319c508..7872aabb8 100644
--- a/wger/core/models.py
+++ b/wger/core/models.py
@@ -596,3 +596,33 @@ class RepetitionUnit(models.Model):
Unit has no owner information
'''
return None
+
+
+@python_2_unicode_compatible
+class WeightUnit(models.Model):
+ '''
+ Weight unit, used in combination with an amount such as '10 kg', '5 plates'
+ '''
+ class Meta:
+ '''
+ Set Meta options
+ '''
+ ordering = ["name", ]
+
+ name = models.CharField(max_length=100,
+ verbose_name=_('Name'))
+
+ def __str__(self):
+ '''
+ Return a more human-readable representation
+ '''
+ return self.name
+
+ #
+ # Own methods
+ #
+ def get_owner_object(self):
+ '''
+ Unit has no owner information
+ '''
+ return None
diff --git a/wger/core/templates/navigation.html b/wger/core/templates/navigation.html
index 7126dbb97..427b02440 100644
--- a/wger/core/templates/navigation.html
+++ b/wger/core/templates/navigation.html
@@ -209,7 +209,10 @@
<li><a href="{% url 'core:license:list' %}">{% trans "Licenses" %} </a></li>
{% endif %}
{% if perms.core.change_repetitionunit %}
- <li><a href="{% url 'core:repetition-unit:list' %}">{% trans "Setting units" %} </a></li>
+ <li><a href="{% url 'core:repetition-unit:list' %}">{% trans "Repetition units" %} </a></li>
+ {% endif %}
+ {% if perms.core.change_weightunit %}
+ <li><a href="{% url 'core:weight-unit:list' %}">{% trans "Weight units" %} </a></li>
{% endif %}
{% if perms.gym.manage_gyms %}
<li><a href="{% url 'core:user:list' %}">{% trans "User list" %} </a></li>
diff --git a/wger/core/templates/repetition_unit/list.html b/wger/core/templates/repetition_unit/list.html
index 6803b65e6..4e39b7907 100644
--- a/wger/core/templates/repetition_unit/list.html
+++ b/wger/core/templates/repetition_unit/list.html
@@ -1,7 +1,7 @@
{% extends "base.html" %}
{% load i18n staticfiles wger_extras %}
-{% block title %}{% trans "Setting units" %}{% endblock %}
+{% block title %}{% trans "Repetition units" %}{% endblock %}
{% block content %}
diff --git a/wger/core/templates/weight_unit/list.html b/wger/core/templates/weight_unit/list.html
new file mode 100644
index 000000000..651cb29ec
--- /dev/null
+++ b/wger/core/templates/weight_unit/list.html
@@ -0,0 +1,45 @@
+{% extends "base.html" %}
+{% load i18n staticfiles wger_extras %}
+
+{% block title %}{% trans "Weight units" %}{% endblock %}
+
+
+{% block content %}
+<ul class="list-group">
+ {% for unit in object_list %}
+ <li class="list-group-item ">
+ {% if unit.id != 1 and unit.id != 2 %}
+ <div class="btn-group pull-right">
+ <button type="button" class="btn btn-default dropdown-toggle btn-xs" data-toggle="dropdown">
+ <span class="glyphicon glyphicon-cog"></span>
+ </button>
+ <ul class="dropdown-menu" role="menu">
+ <li>
+ <a href="{% url 'core:weight-unit:edit' unit.id %}" class="wger-modal-dialog">{% trans 'Edit' %}</a>
+ </li>
+ <li>
+ <a href="{% url 'core:weight-unit:delete' unit.id %}" class="wger-modal-dialog">{% trans 'Delete' %}</a>
+ </li>
+ </ul>
+ </div>
+ {% endif %}
+ {{unit}}
+ </li>
+ {% empty %}
+ <li class="list-group-item">
+ {% trans "Nothing found" %}
+ </li>
+ {% endfor %}
+</ul>
+{% endblock %}
+
+
+
+
+{% block sidebar %}
+{% if perms.core.add_license %}
+ <a href="{% url 'core:weight-unit:add' %}" class="btn btn-success btn-sm wger-modal-dialog">
+ {% trans "Add" %}
+ </a>
+{% endif %}
+{% endblock %}
diff --git a/wger/core/tests/test_setting_unit.py b/wger/core/tests/test_repetition_unit.py
similarity index 90%
rename from wger/core/tests/test_setting_unit.py
rename to wger/core/tests/test_repetition_unit.py
index 63f8a14fc..ae7996c05 100644
--- a/wger/core/tests/test_setting_unit.py
+++ b/wger/core/tests/test_repetition_unit.py
@@ -16,10 +16,13 @@
from wger.core.models import RepetitionUnit
from wger.core.tests import api_base_test
-from wger.manager.tests.testcase import WorkoutManagerAccessTestCase, WorkoutManagerTestCase
-from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase
-from wger.manager.tests.testcase import WorkoutManagerEditTestCase
-from wger.manager.tests.testcase import WorkoutManagerAddTestCase
+from wger.manager.tests.testcase import (
+ WorkoutManagerAccessTestCase,
+ WorkoutManagerTestCase,
+ WorkoutManagerDeleteTestCase,
+ WorkoutManagerEditTestCase,
+ WorkoutManagerAddTestCase
+)
class RepresentationTestCase(WorkoutManagerTestCase):
@@ -108,3 +111,6 @@ class ApiTestCase(api_base_test.ApiBaseResourceTestCase):
pk = 1
resource = RepetitionUnit
private_resource = False
+
+ def get_resource_name(self):
+ return 'setting-repetition-unit'
diff --git a/wger/core/tests/test_weight_unit.py b/wger/core/tests/test_weight_unit.py
new file mode 100644
index 000000000..7d7c8a7e3
--- /dev/null
+++ b/wger/core/tests/test_weight_unit.py
@@ -0,0 +1,116 @@
+# 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/>.
+
+from wger.core.models import WeightUnit
+from wger.core.tests import api_base_test
+
+from wger.manager.tests.testcase import (
+ WorkoutManagerAccessTestCase,
+ WorkoutManagerTestCase,
+ WorkoutManagerDeleteTestCase,
+ WorkoutManagerEditTestCase,
+ WorkoutManagerAddTestCase
+)
+
+
+class RepresentationTestCase(WorkoutManagerTestCase):
+ '''
+ Test the representation of a model
+ '''
+
+ def test_representation(self):
+ '''
+ Test that the representation of an object is correct
+ '''
+ self.assertEqual("{0}".format(WeightUnit.objects.get(pk=1)), 'Repetitions')
+
+
+class OverviewTest(WorkoutManagerAccessTestCase):
+ '''
+ Tests the weight unit overview page
+ '''
+
+ url = 'core:weight-unit:list'
+ anonymous_fail = True
+
+
+class AddTestCase(WorkoutManagerAddTestCase):
+ '''
+ Tests adding a new unit
+ '''
+
+ object_class = WeightUnit
+ url = 'core:weight-unit:add'
+ data = {'name': 'Furlongs'}
+ user_success = 'admin',
+ user_fail = ('general_manager1',
+ 'general_manager2',
+ 'member1',
+ 'member2',
+ 'trainer2',
+ 'trainer3',
+ 'trainer4',
+ 'manager3')
+
+
+class DeleteTestCase(WorkoutManagerDeleteTestCase):
+ '''
+ Tests deleting a unit
+ '''
+
+ pk = 1
+ object_class = WeightUnit
+ url = 'core:weight-unit:delete'
+ user_success = 'admin',
+ user_fail = ('general_manager1',
+ 'general_manager2',
+ 'member1',
+ 'member2',
+ 'trainer2',
+ 'trainer3',
+ 'trainer4',
+ 'manager3')
+
+
+class EditTestCase(WorkoutManagerEditTestCase):
+ '''
+ Tests editing a unit
+ '''
+
+ pk = 1
+ object_class = WeightUnit
+ url = 'core:weight-unit:edit'
+ data = {'name': 'Furlongs'}
+ user_success = 'admin',
+ user_fail = ('general_manager1',
+ 'general_manager2',
+ 'member1',
+ 'member2',
+ 'trainer2',
+ 'trainer3',
+ 'trainer4',
+ 'manager3')
+
+
+class ApiTestCase(api_base_test.ApiBaseResourceTestCase):
+ '''
+ Tests the unit resource
+ '''
+ pk = 1
+ resource = WeightUnit
+ private_resource = False
+
+ def get_resource_name(self):
+ return 'setting-weight-unit'
diff --git a/wger/core/urls.py b/wger/core/urls.py
index 875b38f9b..4e27774f2 100644
--- a/wger/core/urls.py
+++ b/wger/core/urls.py
@@ -28,7 +28,8 @@ from wger.core.views import (
user,
misc,
license,
- repetition_units
+ repetition_units,
+ weight_units
)
# sub patterns for user
@@ -137,6 +138,22 @@ patterns_repetition_units = [
name='delete'),
]
+# sub patterns for setting units
+patterns_weight_units = [
+ url(r'^list$',
+ weight_units.ListView.as_view(),
+ name='list'),
+ url(r'^add$',
+ weight_units.AddView.as_view(),
+ name='add'),
+ url(r'^(?P<pk>\d+)/edit',
+ weight_units.UpdateView.as_view(),
+ name='edit'),
+ url(r'^(?P<pk>\d+)/delete',
+ weight_units.DeleteView.as_view(),
+ name='delete'),
+]
+
#
# Actual patterns
@@ -166,5 +183,6 @@ urlpatterns = [
url(r'^user/', include(patterns_user, namespace="user")),
url(r'^license/', include(patterns_license, namespace="license")),
- url(r'^setting-unit/', include(patterns_repetition_units, namespace="repetition-unit")),
+ url(r'^repetition-unit/', include(patterns_repetition_units, namespace="repetition-unit")),
+ url(r'^weight-unit/', include(patterns_weight_units, namespace="weight-unit")),
]
diff --git a/wger/core/views/weight_units.py b/wger/core/views/weight_units.py
new file mode 100644
index 000000000..6acf02470
--- /dev/null
+++ b/wger/core/views/weight_units.py
@@ -0,0 +1,110 @@
+# -*- 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
+
+import logging
+
+from django.core.urlresolvers import reverse, reverse_lazy
+from django.utils.translation import ugettext as _
+from django.utils.translation import ugettext_lazy
+from django.http import HttpResponseForbidden
+from django.views.generic import (
+ ListView,
+ DeleteView,
+ CreateView,
+ UpdateView
+)
+
+from wger.utils.generic_views import (
+ WgerFormMixin,
+ WgerDeleteMixin,
+ WgerPermissionMixin
+)
+
+from wger.core.models import License, WeightUnit
+
+logger = logging.getLogger(__name__)
+
+
+class ListView(WgerPermissionMixin, ListView):
+ '''
+ Overview of all available weight units
+ '''
+ model = WeightUnit
+ permission_required = 'core.add_weightunit'
+ template_name = 'weight_unit/list.html'
+
+
+class AddView(WgerFormMixin, CreateView, WgerPermissionMixin):
+ '''
+ View to add a new weight unit
+ '''
+
+ model = WeightUnit
+ fields = ['name']
+ title = ugettext_lazy('Add')
+ success_url = reverse_lazy('core:weight-unit:list')
+ form_action = reverse_lazy('core:weight-unit:add')
+ permission_required = 'core.add_weightunit'
+
+
+class UpdateView(WgerFormMixin, UpdateView, WgerPermissionMixin):
+ '''
+ View to update an existing weight unit
+ '''
+
+ model = WeightUnit
+ fields = ['name']
+ success_url = reverse_lazy('core:weight-unit:list')
+ form_action_urlname = 'core:weight-unit:edit'
+ permission_required = 'core.change_weightunit'
+
+ def get_context_data(self, **kwargs):
+ '''
+ Send some additional data to the template
+ '''
+ context = super(UpdateView, self).get_context_data(**kwargs)
+ context['title'] = _(u'Edit {0}').format(self.object)
+ return context
+
+
+class DeleteView(WgerDeleteMixin, DeleteView, WgerPermissionMixin):
+ '''
+ View to delete an existing license
+ '''
+
+ model = WeightUnit
+ success_url = reverse_lazy('core:weight-unit:list')
+ permission_required = 'core.delete_weightunit'
+ form_action_urlname = 'core:weight-unit:delete'
+
+ def dispatch(self, request, *args, **kwargs):
+ '''
+ Deleting the unit with ID 1 (repetitions) is not allowed
+
+ This is the default and is hard coded in a couple of places
+ '''
+ if self.kwargs['pk'] == '1':
+ return HttpResponseForbidden()
+
+ return super(DeleteView, self).dispatch(request, *args, **kwargs)
+
+ def get_context_data(self, **kwargs):
+ '''
+ Send some additional data to the template
+ '''
+ context = super(DeleteView, self).get_context_data(**kwargs)
+ context['title'] = _(u'Delete {0}?').format(self.object)
+ return context
diff --git a/wger/software/templates/api.html b/wger/software/templates/api.html
index 88c818008..e90554eaf 100644
--- a/wger/software/templates/api.html
+++ b/wger/software/templates/api.html
@@ -34,6 +34,8 @@ it in the header, see the link on the sidebar for details.</p>
<li>license</li>
<li>muscle</li>
<li>weightunit</li>
+ <li>setting-repetitionunit</li>
+ <li>setting-weightunit</li>
</ul>
</div>
<div class="col-xs-6">
diff --git a/wger/tasks.py b/wger/tasks.py
index 93ceaa509..14826d90f 100644
--- a/wger/tasks.py
+++ b/wger/tasks.py
@@ -252,6 +252,7 @@ def load_fixtures(settings_path=None):
call_command("loaddata", path + "licenses.json")
call_command("loaddata", path + "days_of_week.json")
call_command("loaddata", path + "repetition_units.json")
+ call_command("loaddata", path + "weight_units.json")
# Config
path = os.path.join(current_dir, 'config', 'fixtures/')
diff --git a/wger/urls.py b/wger/urls.py
index 13bb1dd60..00bef92db 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -96,7 +96,8 @@ router.register(r'userprofile', core_api_views.UserProfileViewSet, base_name='us
router.register(r'language', core_api_views.LanguageViewSet, base_name='language')
router.register(r'daysofweek', core_api_views.DaysOfWeekViewSet, base_name='daysofweek')
router.register(r'license', core_api_views.LicenseViewSet, base_name='license')
-router.register(r'repetitionunit', core_api_views.RepetitionUnitViewSet, base_name='setting-unit')
+router.register(r'setting-repetitionunit', core_api_views.RepetitionUnitViewSet, base_name='setting-repetition-unit')
+router.register(r'setting-weightunit', core_api_views.WeightUnitViewSet, base_name='setting-weight-unit')
# Exercises app
router.register(r'exercise', exercises_api_views.ExerciseViewSet, base_name='exercise')