Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add UUID field to exercises

Roland Geiderd2c7bbc4b259
diff --git a/wger/exercises/management/commands/download-exercise-images.py b/wger/exercises/management/commands/download-exercise-images.py
index 1af1fb143..7bf5ec608 100644
--- a/wger/exercises/management/commands/download-exercise-images.py
+++ b/wger/exercises/management/commands/download-exercise-images.py
@@ -51,9 +51,9 @@ class Command(BaseCommand):
help = ('Download exercise images from wger.de and update the local database\n'
'\n'
'ATTENTION: The script will download the images from the server and add them\n'
- ' to local exercises. If you happen to have edited them, the script\n'
- ' *might* add the wrong images to the wrong exercises if they match\n'
- ' the remote ID.')
+ ' to your local exercises. The exercises are identified by\n'
+ ' their UUID field, if you manually edited or changed it\n'
+ ' the script will not be able to match them.')
def handle(self, *args, **options):
@@ -61,8 +61,8 @@ class Command(BaseCommand):
raise ImproperlyConfigured('Please set MEDIA_ROOT in your settings file')
remote_url = options['remote_url']
- val = URLValidator()
try:
+ val = URLValidator()
val(remote_url)
except ValidationError:
raise CommandError('Please enter a valid URL')
@@ -75,13 +75,14 @@ class Command(BaseCommand):
result = json.load(urllib.urlopen(exercise_api.format(remote_url)))
for exercise_json in result['results']:
exercise_name = exercise_json['name']
+ exercise_uuid = exercise_json['uuid']
exercise_id = exercise_json['id']
self.stdout.write('')
self.stdout.write(u"*** Processing {0} (ID: {1})".format(exercise_name, exercise_id))
try:
- exercise = Exercise.objects.get(pk=exercise_id)
+ exercise = Exercise.objects.get(uuid=exercise_uuid)
except Exercise.DoesNotExist:
self.stdout.write(' Remote exercise not found in local DB, skipping...')
continue
diff --git a/wger/exercises/migrations/0002_auto_20150307_1841.py b/wger/exercises/migrations/0002_auto_20150307_1841.py
new file mode 100644
index 000000000..4a4a1d342
--- /dev/null
+++ b/wger/exercises/migrations/0002_auto_20150307_1841.py
@@ -0,0 +1,34 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+import uuid
+
+def generate_uuids(apps, schema_editor):
+ '''
+ Generate new UUIDs for each exercise
+ :param apps:
+ :param schema_editor:
+ :return:
+ '''
+ Excercise = apps.get_model("exercises", "Exercise")
+ for exercise in Excercise.objects.all():
+ exercise.uuid = uuid.uuid4()
+ exercise.save()
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('exercises', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='exercise',
+ name='uuid',
+ field=models.CharField(editable=False, max_length=36, verbose_name='UUID', default=uuid.uuid4),
+ preserve_default=True,
+ ),
+ migrations.RunPython(generate_uuids),
+ ]
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index 999accc68..f7526bf1c 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -16,6 +16,7 @@
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
import six
+import uuid
import logging
import bleach
@@ -198,6 +199,14 @@ class Exercise(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
verbose_name=_('Language'))
'''The exercise's language'''
+ uuid = models.CharField(verbose_name='UUID',
+ max_length=36,
+ editable=False,
+ default=uuid.uuid4)
+ '''
+ Globally unique ID, to identify the exercise across installations
+ '''
+
#
# Django methods
#
diff --git a/wger/software/templates/changelog.html b/wger/software/templates/changelog.html
index 643a5bd26..d427510b0 100644
--- a/wger/software/templates/changelog.html
+++ b/wger/software/templates/changelog.html
@@ -45,8 +45,9 @@
<li>
Other improvements and bugfixes
<a href="https://github.com/rolandgeider/wger/issues/99">#99</a>
- <a href="https://github.com/rolandgeider/wger/issues/108">#100</a>
+ <a href="https://github.com/rolandgeider/wger/issues/100">#100</a>
<a href="https://github.com/rolandgeider/wger/issues/108">#108</a>
+ <a href="https://github.com/rolandgeider/wger/issues/110">#110</a>
<a href="https://github.com/rolandgeider/wger/issues/117">#117</a>
<a href="https://github.com/rolandgeider/wger/issues/131">#131</a>
<a href="https://github.com/rolandgeider/wger/issues/135">#135</a>