Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Use License model to explicitly set a license for ingredients

Roland Geider38963bbf9b67
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index ab4782e98..4790f477a 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -34,6 +34,7 @@ from easy_thumbnails.signal_handlers import generate_aliases_global
from wger.core.models import Language
from wger.core.models import License
+from wger.utils.models import AbstractLicenseModel
from wger.utils.constants import EMAIL_FROM
from wger.utils.cache import delete_template_fragment_cache, reset_workout_canonical_form
from wger.utils.cache import cache_mapper
@@ -123,7 +124,7 @@ class ExerciseCategory(models.Model):
return False
-class Exercise(models.Model):
+class Exercise(AbstractLicenseModel, models.Model):
'''
Model for an exercise
'''
@@ -174,20 +175,6 @@ class Exercise(models.Model):
blank=True)
'''Equipment needed by this exercise'''
- license = models.ForeignKey(License,
- verbose_name=_('License'),
- default=1)
- '''The exercise's license'''
-
- license_author = models.CharField(verbose_name=_('Author'),
- max_length=50,
- blank=True,
- null=True,
- help_text=_('If you are not the author of the image, enter '
- 'the name here. This is needed for some licenses '
- 'e.g. the CC-BY-SA.'))
- '''The author if it is not the uploader'''
-
# Non-editable fields
user = models.CharField(verbose_name=_('User'), null=True, blank=True, max_length=100)
'''The user that submitted the exercise'''
diff --git a/wger/nutrition/api/resources.py b/wger/nutrition/api/resources.py
index 7625a7685..e0c0628fa 100644
--- a/wger/nutrition/api/resources.py
+++ b/wger/nutrition/api/resources.py
@@ -26,9 +26,13 @@ from wger.nutrition.models import NutritionPlan
from wger.nutrition.models import Meal
from wger.nutrition.models import MealItem
from wger.utils.resources import UserObjectsOnlyAuthorization
+from wger.core.api.resources import LicenseResource
class IngredientResource(ModelResource):
+
+ license = fields.ToOneField(LicenseResource, 'license')
+
class Meta:
queryset = Ingredient.objects.all()
filtering = {'id': ALL,
@@ -43,7 +47,9 @@ class IngredientResource(ModelResource):
'protein': ALL,
'sodium': ALL,
'status': ALL,
- 'update_date': ALL}
+ 'update_date': ALL,
+ 'license': ALL,
+ 'license_author': ALL}
class WeightUnitResource(ModelResource):
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index bd07361ed..19d8e54c6 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -30,6 +30,7 @@ from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext
from django.utils import translation
+
from wger.core.models import Language
from wger.utils.constants import EMAIL_FROM
@@ -38,6 +39,7 @@ from wger.utils.cache import cache_mapper
from wger.utils.fields import Html5TimeField
from wger.utils.fields import Html5DecimalField
from wger.utils.fields import Html5IntegerField
+from wger.utils.models import AbstractLicenseModel
MEALITEM_WEIGHT_GRAM = '1'
MEALITEM_WEIGHT_UNIT = '2'
@@ -183,7 +185,7 @@ class NutritionPlan(models.Model):
return 4
-class Ingredient(models.Model):
+class Ingredient(AbstractLicenseModel, models.Model):
'''
An ingredient, with some approximate nutrition values
'''
diff --git a/wger/nutrition/templates/ingredient/overview.html b/wger/nutrition/templates/ingredient/overview.html
index 9d9fdbfb2..c19b60cf6 100644
--- a/wger/nutrition/templates/ingredient/overview.html
+++ b/wger/nutrition/templates/ingredient/overview.html
@@ -83,5 +83,4 @@ $(document).ready(function() {
</p>
{% endif %}
-{% cc_by_sa_sidebar language %}
{% endblock %}
diff --git a/wger/nutrition/templates/ingredient/view.html b/wger/nutrition/templates/ingredient/view.html
index 346f821d2..3ccb46721 100644
--- a/wger/nutrition/templates/ingredient/view.html
+++ b/wger/nutrition/templates/ingredient/view.html
@@ -276,6 +276,6 @@ the search.{% endblocktrans %}
{% endif %}
{% endif %}
-{% cc_by_sa_sidebar language %}
+{% license_sidebar ingredient.license author %}
{% endblock %}
diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.py
index 6e4d914c8..7434ec19a 100644
--- a/wger/nutrition/tests/test_ingredient.py
+++ b/wger/nutrition/tests/test_ingredient.py
@@ -57,7 +57,9 @@ class EditIngredientTestCase(WorkoutManagerEditTestCase):
'fat_saturated': 3.14,
'fibres': 2.1,
'protein': 20,
- 'carbohydrates': 10}
+ 'carbohydrates': 10,
+ 'license': 2,
+ 'license_author': 'me!'}
def post_test_hook(self):
'''
@@ -85,7 +87,9 @@ class AddIngredientTestCase(WorkoutManagerAddTestCase):
'fat_saturated': 3.14,
'fibres': 2.1,
'protein': 20,
- 'carbohydrates': 10}
+ 'carbohydrates': 10,
+ 'license': 2,
+ 'license_author': 'me!'}
def post_test_hook(self):
'''
@@ -391,6 +395,8 @@ class IngredientApiTestCase(ApiBaseResourceTestCase):
resource = 'ingredient'
user = None
resource_updatable = False
+ data = {'language': 1,
+ 'license': 2}
class IngredientDetailApiTestCase(ApiBaseResourceTestCase):
diff --git a/wger/nutrition/tests/test_ingredient_overview.py b/wger/nutrition/tests/test_ingredient_overview.py
index 7b0eec918..656debced 100644
--- a/wger/nutrition/tests/test_ingredient_overview.py
+++ b/wger/nutrition/tests/test_ingredient_overview.py
@@ -38,7 +38,9 @@ class OverviewPlanTestCase(WorkoutManagerTestCase):
"fat_saturated": 3.24,
"fibres": 0.0,
"protein": 25.63,
- "carbohydrates": 0.0
+ "carbohydrates": 0.0,
+ 'license': 1,
+ 'license_author': 'internet'
}
for i in range(0, 50):
self.client.post(reverse('ingredient-add'), data)
diff --git a/wger/nutrition/views/ingredient.py b/wger/nutrition/views/ingredient.py
index fb0d3c3c4..7c8fa949e 100644
--- a/wger/nutrition/views/ingredient.py
+++ b/wger/nutrition/views/ingredient.py
@@ -114,7 +114,24 @@ class IngredientDeleteView(WgerDeleteMixin, DeleteView, WgerPermissionMixin):
return context
-class IngredientEditView(WgerFormMixin, UpdateView, WgerPermissionMixin):
+class IngredientMixin(WgerFormMixin):
+ '''
+ Manually set the order of the fields
+ '''
+ form_fields = ['name',
+ 'energy',
+ 'protein',
+ 'carbohydrates',
+ 'carbohydrates_sugar',
+ 'fat',
+ 'fat_saturated',
+ 'fibres',
+ 'sodium',
+ 'license',
+ 'license_author']
+
+
+class IngredientEditView(IngredientMixin, UpdateView, WgerPermissionMixin):
'''
Generic view to update an existing ingredient
'''
@@ -126,7 +143,7 @@ class IngredientEditView(WgerFormMixin, UpdateView, WgerPermissionMixin):
permission_required = 'nutrition.change_ingredient'
-class IngredientCreateView(WgerFormMixin, CreateView):
+class IngredientCreateView(IngredientMixin, CreateView):
'''
Generic view to add a new ingredient
'''
diff --git a/wger/utils/models.py b/wger/utils/models.py
new file mode 100644
index 000000000..fe04111ff
--- /dev/null
+++ b/wger/utils/models.py
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+
+# 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
+
+from django.db import models
+from django.utils.translation import ugettext_lazy as _
+
+from wger.core.models import License
+
+
+'''
+Abstract model classes
+'''
+
+
+class AbstractLicenseModel(models.Model):
+ '''
+ Abstract class that adds license information to a model
+ '''
+
+ license = models.ForeignKey(License,
+ verbose_name=_('License'),
+ default=1)
+ '''The item's license'''
+
+ license_author = models.CharField(verbose_name=_('Author'),
+ max_length=50,
+ blank=True,
+ null=True,
+ help_text=_('If you are not the author, enter the name or '
+ 'source here. This is needed for some licenses '
+ 'e.g. the CC-BY-SA.'))
+ '''The author if it is not the uploader'''
+
+ class Meta:
+ abstract = True