Skip to content

Commit

Permalink
Add UUID field to exercises
Browse files Browse the repository at this point in the history
This allows to uniquely identify exercises across installations, e.g. to sync
the images or similar tasks.

Fixes #110
  • Loading branch information
rolandgeider committed Mar 8, 2015
1 parent 61fc1cd commit d2c7bbc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
11 changes: 6 additions & 5 deletions wger/exercises/management/commands/download-exercise-images.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ 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):

if not settings.MEDIA_ROOT:
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')
Expand All @@ -75,13 +75,14 @@ def handle(self, *args, **options):
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
Expand Down
34 changes: 34 additions & 0 deletions wger/exercises/migrations/0002_auto_20150307_1841.py
Original file line number Diff line number Diff line change
@@ -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),
]
9 changes: 9 additions & 0 deletions wger/exercises/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.

import six
import uuid
import logging
import bleach

Expand Down Expand Up @@ -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
#
Expand Down
3 changes: 2 additions & 1 deletion wger/software/templates/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ <h4>2015-XX-XX</h4>
<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>
Expand Down

0 comments on commit d2c7bbc

Please sign in to comment.