Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Create license_author_history model

Tom Bowyer355a5c425e6c
diff --git a/wger/core/migrations/0015_licenseauthorhistory.py b/wger/core/migrations/0015_licenseauthorhistory.py
new file mode 100644
index 000000000..75833d590
--- /dev/null
+++ b/wger/core/migrations/0015_licenseauthorhistory.py
@@ -0,0 +1,28 @@
+# Generated by Django 3.2.13 on 2022-05-28 22:47
+
+import django.core.validators
+from django.db import migrations, models
+import django.utils.timezone
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('core', '0014_merge_20210818_1735'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='LicenseAuthorHistory',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('model_id', models.IntegerField(validators=[django.core.validators.MinValueValidator(1)], verbose_name='Model ID')),
+ ('model_type', models.CharField(choices=[('1', 'Exercise')], editable=False, max_length=3)),
+ ('name', models.CharField(max_length=120, verbose_name='Name')),
+ ('datetime', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Date and Time')),
+ ],
+ options={
+ 'ordering': ['model_id', 'model_type', 'name', 'datetime'],
+ },
+ ),
+ ]
diff --git a/wger/core/models/__init__.py b/wger/core/models/__init__.py
index 8b71cadd7..f7bd76a98 100644
--- a/wger/core/models/__init__.py
+++ b/wger/core/models/__init__.py
@@ -19,6 +19,7 @@ from .cache import UserCache
from .days_of_week import DaysOfWeek
from .language import Language
from .license import License
+from .license_author_history import LicenseAuthorHistory
from .profile import UserProfile
from .rep_unit import RepetitionUnit
from .weight_unit import WeightUnit
diff --git a/wger/core/models/license_author_history.py b/wger/core/models/license_author_history.py
new file mode 100644
index 000000000..d13197224
--- /dev/null
+++ b/wger/core/models/license_author_history.py
@@ -0,0 +1,87 @@
+# This file is part of wger Workout Manager <https://github.com/wger-project>.
+# Copyright (C) 2013 - 2021 wger Team
+#
+# 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 Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Django
+from django.db import models
+from django.db.models import IntegerField
+from django.utils import timezone
+from django.core.validators import (
+ MinValueValidator,
+)
+from django.utils.translation import gettext_lazy as _
+
+
+class LicenseAuthorHistory(models.Model):
+ """
+ Tracks model authors
+ """
+
+ MODEL_TYPE_EXERCISE = '1'
+
+ MODEL_TYPES = (
+ (MODEL_TYPE_EXERCISE, _('Exercise')),
+ )
+
+ model_id = IntegerField(
+ verbose_name=_('Model ID'),
+ validators=[MinValueValidator(1)]
+ )
+ """The model ID"""
+
+ model_type = models.CharField(
+ max_length=3,
+ choices=MODEL_TYPES,
+ editable=False,
+ )
+ """The model type"""
+
+ name = models.CharField(
+ max_length=120,
+ verbose_name=_('Name'),
+ )
+ """Name"""
+
+ datetime = models.DateTimeField(verbose_name=_('Date and Time'), default=timezone.now)
+ """Date and time entry was created"""
+
+ class Meta:
+ """
+ Set Meta options
+ """
+ ordering = [
+ "model_id",
+ "model_type",
+ "name",
+ "datetime",
+ ]
+
+ #
+ # Django methods
+ #
+ def __str__(self):
+ """
+ Return a more human-readable representation
+ """
+ return f"Model type: {self.model_type} Name: {self.name} Datetime: ({self.datetime})"
+
+ #
+ # Own methods
+ #
+ def get_owner_object(self):
+ """
+ License author history has no owner information
+ """
+ return None
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index 2b7686a36..c9e0c9f8d 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -16,6 +16,7 @@
# Third Party
from rest_framework import serializers
+from wger.core.models.license_author_history import LicenseAuthorHistory
# wger
from wger.exercises.models import (
@@ -193,6 +194,7 @@ class ExerciseSerializer(serializers.ModelSerializer):
muscles_secondary = serializers.PrimaryKeyRelatedField(many=True, queryset=Muscle.objects.all())
equipment = serializers.PrimaryKeyRelatedField(many=True, queryset=Equipment.objects.all())
variations = serializers.PrimaryKeyRelatedField(many=True, queryset=Variation.objects.all())
+ license_author_history = serializers.PrimaryKeyRelatedField(many=True, queryset=LicenseAuthorHistory.objects.all())
class Meta:
model = Exercise
@@ -211,6 +213,7 @@ class ExerciseSerializer(serializers.ModelSerializer):
"license",
"license_author",
"variations",
+ "license_author_history",
)
diff --git a/wger/exercises/management/commands/change-author.py b/wger/exercises/management/commands/change-author.py
index f8d9a1c1f..96cd1ea4d 100644
--- a/wger/exercises/management/commands/change-author.py
+++ b/wger/exercises/management/commands/change-author.py
@@ -16,6 +16,7 @@
# Django
from django.core.management.base import BaseCommand
+from wger.core.models.license_author_history import LicenseAuthorHistory
# wger
from wger.exercises.models.exercise import Exercise
@@ -64,8 +65,12 @@ class Command(BaseCommand):
self.print_error('Failed to find exercise')
return
- exercise.license_author = author_name
- exercise.save()
+ lh = LicenseAuthorHistory(
+ model_id=exercise.id,
+ model_type=LicenseAuthorHistory.MODEL_TYPE_EXERCISE,
+ name=author_name,
+ )
+ lh.save()
self.stdout.write(
self.style.SUCCESS(f"Exercise has been updated")
diff --git a/wger/exercises/models/exercise.py b/wger/exercises/models/exercise.py
index 43119261b..9d51f7433 100644
--- a/wger/exercises/models/exercise.py
+++ b/wger/exercises/models/exercise.py
@@ -37,6 +37,7 @@ from simple_history.models import HistoricalRecords
# wger
from wger.core.models import Language
+from wger.core.models.license_author_history import LicenseAuthorHistory
from wger.exercises.models import ExerciseBase
from wger.utils.cache import (
delete_template_fragment_cache,
@@ -193,6 +194,16 @@ class Exercise(AbstractLicenseModel, models.Model):
out.append(exercise)
return out
+ #
+ # Properties to expose the information about license history
+ #
+ @property
+ def license_author_history(self):
+ return LicenseAuthorHistory.objects.all().filter(
+ model_id=self.id,
+ model_type=LicenseAuthorHistory.MODEL_TYPE_EXERCISE,
+ )
+
#
# Own methods
#
diff --git a/wger/exercises/tests/test_change_author.py b/wger/exercises/tests/test_change_author.py
index 960d60e14..b1212d4c3 100644
--- a/wger/exercises/tests/test_change_author.py
+++ b/wger/exercises/tests/test_change_author.py
@@ -15,6 +15,7 @@
from io import StringIO
from django.core.management import call_command
+from wger.core.models.license_author_history import LicenseAuthorHistory
# wger
from wger.core.tests.base_testcase import WgerTestCase
@@ -53,9 +54,6 @@ class ChangeAuthorTestCase(WgerTestCase):
"""
Test to ensure command can handle an exercise id passed
"""
- exercise = Exercise.objects.get(id=1)
- self.assertNotEquals(exercise.license_author, "tom")
-
args = [
"--author-name",
"tom",
@@ -65,5 +63,7 @@ class ChangeAuthorTestCase(WgerTestCase):
call_command('change-author', *args, stdout=self.out, no_color=True)
self.assertIn('Exercise has been updated', self.out.getvalue())
- exercise = Exercise.objects.get(id=1)
- self.assertEquals(exercise.license_author, "tom")
+ exercise = LicenseAuthorHistory.objects.filter(
+ model_type=LicenseAuthorHistory.MODEL_TYPE_EXERCISE
+ )
+ self.assertEquals(len(exercise), 1)