Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

All members of a gym must have a gym config

Roland Geider4cccc9f498d2
diff --git a/wger/config/models.py b/wger/config/models.py
index 34c16939d..06d815fc7 100644
--- a/wger/config/models.py
+++ b/wger/config/models.py
@@ -23,7 +23,8 @@ from django.utils.translation import ugettext_lazy as _
from django.core.cache import cache
from wger.core.models import Language
from wger.core.models import UserProfile
-from wger.gym.models import Gym
+from wger.gym.helpers import is_any_gym_admin
+from wger.gym.models import Gym, GymUserConfig
from wger.utils.cache import delete_template_fragment_cache
from wger.utils.cache import cache_mapper
@@ -149,9 +150,24 @@ class GymConfig(models.Model):
def save(self, *args, **kwargs):
'''
- All users that have no gym set in the profile are edited
+ Perform additional tasks
'''
if self.default_gym:
+
+ # All users that have no gym set in the profile are edited
UserProfile.objects.filter(gym=None).update(gym=self.default_gym)
+ # All users in the gym must have a gym config
+ for profile in UserProfile.objects.filter(gym=self.default_gym):
+ user = profile.user
+ if not is_any_gym_admin(user):
+ try:
+ user.gymuserconfig
+ except GymUserConfig.DoesNotExist:
+ config = GymUserConfig()
+ config.gym = self.default_gym
+ config.user = user
+ config.save()
+ logger.debug('Creating GymUserConfig for user {0}'.format(user.username))
+
return super(GymConfig, self).save(*args, **kwargs)
diff --git a/wger/config/tests/test_gym_config.py b/wger/config/tests/test_gym_config.py
index 78aaf1bcf..0ce5958f8 100644
--- a/wger/config/tests/test_gym_config.py
+++ b/wger/config/tests/test_gym_config.py
@@ -20,7 +20,7 @@ from django.core.urlresolvers import reverse
from wger.config.models import GymConfig
from wger.core.models import UserProfile
-from wger.gym.models import Gym
+from wger.gym.models import Gym, GymUserConfig
from wger.manager.tests.testcase import WorkoutManagerTestCase
@@ -49,13 +49,13 @@ class GymConfigTestCase(WorkoutManagerTestCase):
new_user = User.objects.all().last()
self.assertEqual(new_user.userprofile.gym, gym)
+ self.assertEqual(new_user.gymuserconfig.gym, gym)
def test_no_default_gym(self):
'''
Test the user registration without a default gym
'''
- gym = Gym.objects.get(pk=2)
gym_config = GymConfig.objects.get(pk=1)
gym_config.default_gym = None
gym_config.save()
@@ -70,6 +70,7 @@ class GymConfigTestCase(WorkoutManagerTestCase):
new_user = User.objects.all().last()
self.assertEqual(new_user.userprofile.gym_id, None)
+ self.assertRaises(GymUserConfig.DoesNotExist, GymUserConfig.objects.get, user=new_user)
def test_update_userprofile(self):
'''
@@ -77,6 +78,7 @@ class GymConfigTestCase(WorkoutManagerTestCase):
'''
UserProfile.objects.update(gym=None)
+ GymUserConfig.objects.all().delete()
self.assertEqual(UserProfile.objects.exclude(gym=None).count(), 0)
gym = Gym.objects.get(pk=2)
@@ -84,4 +86,8 @@ class GymConfigTestCase(WorkoutManagerTestCase):
gym_config.default_gym = gym
gym_config.save()
+ # 24 users in total
self.assertEqual(UserProfile.objects.filter(gym=gym).count(), 24)
+
+ # 13 non-managers
+ self.assertEqual(GymUserConfig.objects.filter(gym=gym).count(), 13)
diff --git a/wger/core/views/user.py b/wger/core/views/user.py
index f27ca0452..45ded49aa 100644
--- a/wger/core/views/user.py
+++ b/wger/core/views/user.py
@@ -32,7 +32,7 @@ from django.contrib.auth.views import login as django_loginview
from django.contrib import messages
from django.views.generic import RedirectView, UpdateView, DetailView
from rest_framework.authtoken.models import Token
-from wger.gym.models import AdminUserNote
+from wger.gym.models import AdminUserNote, GymUserConfig
from wger.utils.constants import USER_TAB
from wger.utils.generic_views import WgerPermissionMixin
@@ -210,10 +210,17 @@ def registration(request):
language = Language.objects.get(short_name=translation.get_language())
user.userprofile.notification_language = language
+ # Set default gym, if needed
gym_config = GymConfig.objects.get(pk=1)
- if gym_config:
+ if gym_config.default_gym:
user.userprofile.gym = gym_config.default_gym
+ # Create gym user configuration object
+ config = GymUserConfig()
+ config.gym = gym_config.default_gym
+ config.user = user
+ config.save()
+
user.userprofile.save()
user = authenticate(username=username, password=password)