Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Use django 1.7 AppConfig for signals

Roland Geider33a5405053d2
diff --git a/wger/config/__init__.py b/wger/config/__init__.py
index e69de29bb..1cdac5f30 100644
--- a/wger/config/__init__.py
+++ b/wger/config/__init__.py
@@ -0,0 +1,22 @@
+# -*- 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
+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
+
+
+from wger import get_version
+
+VERSION = get_version()
+default_app_config = 'wger.config.apps.ConfigConfig'
diff --git a/wger/config/apps.py b/wger/config/apps.py
new file mode 100644
index 000000000..3e1a7b9a7
--- /dev/null
+++ b/wger/config/apps.py
@@ -0,0 +1,25 @@
+# -*- 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
+
+from django.apps import AppConfig
+
+
+class ConfigConfig(AppConfig):
+ name = 'wger.config'
+ verbose_name = "Config"
+
+ def ready(self):
+ import wger.config.signals
diff --git a/wger/config/models.py b/wger/config/models.py
index 2ecc0d9b6..c51231b9e 100644
--- a/wger/config/models.py
+++ b/wger/config/models.py
@@ -18,7 +18,6 @@
import logging
from django.db import models
-from django.db.models.signals import post_save
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.core.cache import cache
@@ -100,30 +99,6 @@ class LanguageConfig(models.Model):
super(LanguageConfig, self).delete(*args, **kwargs)
-def init_language_config(sender, instance, created, **kwargs):
- '''
- Creates language config entries when new languages are created
- (all combinations of all languages)
- '''
- for language_source in Language.objects.all():
- for language_target in Language.objects.all():
- if not LanguageConfig.objects.filter(language=language_source)\
- .filter(language_target=language_target)\
- .exists():
-
- for item in LanguageConfig.SHOW_ITEM_LIST:
- config = LanguageConfig()
- config.language = language_source
- config.language_target = language_target
- config.item = item[0]
- if language_source == language_target:
- config.show = True
- else:
- config.show = False
- config.save()
-post_save.connect(init_language_config, sender=Language)
-
-
@python_2_unicode_compatible
class GymConfig(models.Model):
'''
diff --git a/wger/config/signals.py b/wger/config/signals.py
new file mode 100644
index 000000000..3f4933676
--- /dev/null
+++ b/wger/config/signals.py
@@ -0,0 +1,46 @@
+# -*- 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
+
+
+from django.db.models.signals import post_save
+from django.dispatch import receiver
+
+from wger.config.models import LanguageConfig
+from wger.core.models import Language
+
+
+@receiver(post_save, sender=Language)
+def init_language_config(sender, instance, created, **kwargs):
+ '''
+ Creates language config entries when new languages are created
+ (all combinations of all languages)
+ '''
+ for language_source in Language.objects.all():
+ for language_target in Language.objects.all():
+ if not LanguageConfig.objects.filter(language=language_source)\
+ .filter(language_target=language_target)\
+ .exists():
+
+ for item in LanguageConfig.SHOW_ITEM_LIST:
+ config = LanguageConfig()
+ config.language = language_source
+ config.language_target = language_target
+ config.item = item[0]
+ if language_source == language_target:
+ config.show = True
+ else:
+ config.show = False
+ config.save()
diff --git a/wger/core/__init__.py b/wger/core/__init__.py
index a915bb265..703386020 100644
--- a/wger/core/__init__.py
+++ b/wger/core/__init__.py
@@ -19,3 +19,4 @@
from wger import get_version
VERSION = get_version()
+default_app_config = 'wger.core.apps.CoreConfig'
diff --git a/wger/core/apps.py b/wger/core/apps.py
new file mode 100644
index 000000000..4596b9757
--- /dev/null
+++ b/wger/core/apps.py
@@ -0,0 +1,25 @@
+# -*- 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
+
+from django.apps import AppConfig
+
+
+class CoreConfig(AppConfig):
+ name = 'wger.core'
+ verbose_name = "Core"
+
+ def ready(self):
+ import wger.core.signals
diff --git a/wger/core/models.py b/wger/core/models.py
index b1105e33e..f13f4dd7b 100644
--- a/wger/core/models.py
+++ b/wger/core/models.py
@@ -19,7 +19,6 @@ import decimal
from django.db import models
from django.db.models import IntegerField
-from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError
@@ -28,7 +27,6 @@ from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from wger.gym.models import Gym
-from wger.utils.helpers import disable_for_loaddata
from wger.utils.constants import TWOPLACES
from wger.utils.units import AbstractWeight
@@ -442,16 +440,6 @@ by the US Department of Agriculture. It is extremely complete, with around
return self
-# Every new user gets a profile
-@disable_for_loaddata
-def create_user_profile(sender, instance, created, **kwargs):
- if created:
- UserProfile.objects.create(user=instance)
-
-
-post_save.connect(create_user_profile, sender=User)
-
-
@python_2_unicode_compatible
class DaysOfWeek(models.Model):
'''
diff --git a/wger/core/signals.py b/wger/core/signals.py
new file mode 100644
index 000000000..e0dcf204d
--- /dev/null
+++ b/wger/core/signals.py
@@ -0,0 +1,34 @@
+# -*- 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
+
+
+from django.contrib.auth.models import User
+from django.db.models.signals import post_save
+
+from wger.core.models import UserProfile
+from wger.utils.helpers import disable_for_loaddata
+
+
+@disable_for_loaddata
+def create_user_profile(sender, instance, created, **kwargs):
+ '''
+ Every new user gets a profile
+ '''
+ if created:
+ UserProfile.objects.create(user=instance)
+
+
+post_save.connect(create_user_profile, sender=User)
diff --git a/wger/exercises/__init__.py b/wger/exercises/__init__.py
index a915bb265..7d76565c1 100644
--- a/wger/exercises/__init__.py
+++ b/wger/exercises/__init__.py
@@ -9,7 +9,7 @@
#
# 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
+# 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
@@ -19,3 +19,4 @@
from wger import get_version
VERSION = get_version()
+default_app_config = 'wger.exercises.apps.ExerciseConfig'
diff --git a/wger/exercises/apps.py b/wger/exercises/apps.py
new file mode 100644
index 000000000..64f00e5ee
--- /dev/null
+++ b/wger/exercises/apps.py
@@ -0,0 +1,25 @@
+# -*- 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
+
+from django.apps import AppConfig
+
+
+class ExerciseConfig(AppConfig):
+ name = 'wger.exercises'
+ verbose_name = "Exercise"
+
+ def ready(self):
+ import wger.exercises.signals
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index 65d9df5c9..d5c25a7f0 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -29,9 +29,6 @@ from django.core.urlresolvers import reverse
from django.core import mail
from django.core.cache import cache
from django.core.validators import MinLengthValidator
-from django.db.models.signals import pre_save
-from django.db.models.signals import post_delete
-from easy_thumbnails.files import get_thumbnailer
from easy_thumbnails.signals import saved_file
from easy_thumbnails.signal_handlers import generate_aliases
@@ -460,46 +457,6 @@ class ExerciseImage(AbstractSubmissionModel, AbstractLicenseModel, models.Model)
fail_silently=True)
-def delete_exercise_image_on_delete(sender, instance, **kwargs):
- '''
- Delete the image, along with its thumbnails, from the disk
- '''
-
- thumbnailer = get_thumbnailer(instance.image)
- thumbnailer.delete_thumbnails()
- instance.image.delete(save=False)
-
-
-post_delete.connect(delete_exercise_image_on_delete, sender=ExerciseImage)
-
-
-def delete_exercise_image_on_update(sender, instance, **kwargs):
- '''
- Delete the corresponding image from the filesystem when the an ExerciseImage
- object was changed
- '''
- if not instance.pk:
- return False
-
- try:
- old_file = ExerciseImage.objects.get(pk=instance.pk).image
- except ExerciseImage.DoesNotExist:
- return False
-
- new_file = instance.image
- if not old_file == new_file:
- thumbnailer = get_thumbnailer(instance.image)
- thumbnailer.delete_thumbnails()
- instance.image.delete(save=False)
-
-
-pre_save.connect(delete_exercise_image_on_update, sender=ExerciseImage)
-
-
-# Generate thumbnails when uploading a new image
-saved_file.connect(generate_aliases)
-
-
@python_2_unicode_compatible
class ExerciseComment(models.Model):
'''
diff --git a/wger/exercises/signals.py b/wger/exercises/signals.py
new file mode 100644
index 000000000..80a4660b1
--- /dev/null
+++ b/wger/exercises/signals.py
@@ -0,0 +1,61 @@
+# -*- 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
+
+
+from django.db.models.signals import pre_save
+from django.db.models.signals import post_delete
+from django.dispatch import receiver
+from easy_thumbnails.files import get_thumbnailer
+from easy_thumbnails.signal_handlers import generate_aliases
+from easy_thumbnails.signals import saved_file
+
+from wger.exercises.models import ExerciseImage
+
+
+@receiver(post_delete, sender=ExerciseImage)
+def delete_exercise_image_on_delete(sender, instance, **kwargs):
+ '''
+ Delete the image, along with its thumbnails, from the disk
+ '''
+
+ thumbnailer = get_thumbnailer(instance.image)
+ thumbnailer.delete_thumbnails()
+ instance.image.delete(save=False)
+
+
+@receiver(pre_save, sender=ExerciseImage)
+def delete_exercise_image_on_update(sender, instance, **kwargs):
+ '''
+ Delete the corresponding image from the filesystem when the an ExerciseImage
+ object was changed
+ '''
+ if not instance.pk:
+ return False
+
+ try:
+ old_file = ExerciseImage.objects.get(pk=instance.pk).image
+ except ExerciseImage.DoesNotExist:
+ return False
+
+ new_file = instance.image
+ if not old_file == new_file:
+ thumbnailer = get_thumbnailer(instance.image)
+ thumbnailer.delete_thumbnails()
+ instance.image.delete(save=False)
+
+
+# Generate thumbnails when uploading a new image
+saved_file.connect(generate_aliases)
diff --git a/wger/gym/__init__.py b/wger/gym/__init__.py
index 23fd07cad..48d808608 100644
--- a/wger/gym/__init__.py
+++ b/wger/gym/__init__.py
@@ -1,17 +1,21 @@
-import wger.gym.signals
+# -*- coding: utf-8 -*-
-
-# TODO: django1.7
-# http://stackoverflow.com/questions/2719038/where-should-signal-handlers-liv
-
-# When you upgrade to Django 1.7, you would remove the "import .signals" from
-# your __ init__.py and add an yourapp/apps.py like the following:
+# This file is part of wger Workout Manager.
#
-# from django.apps import AppConfig
+# 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.
#
-# class ReportsConfig(AppConfig):
-# name = 'reports'
-# verbose_name = "Reports"
+# 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.
#
-# def ready(self):
-# import signals
+# 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 import get_version
+
+VERSION = get_version()
+default_app_config = 'wger.gym.apps.GymConfig'
diff --git a/wger/gym/apps.py b/wger/gym/apps.py
new file mode 100644
index 000000000..3804b4f1a
--- /dev/null
+++ b/wger/gym/apps.py
@@ -0,0 +1,25 @@
+# -*- 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
+
+from django.apps import AppConfig
+
+
+class GymConfig(AppConfig):
+ name = 'wger.gym'
+ verbose_name = "Gym"
+
+ def ready(self):
+ import wger.gym.signals