Skip to content

Commit

Permalink
Merge branch 'gym-refactor' of github.com:rolandgeider/wger into gym-…
Browse files Browse the repository at this point in the history
…refactor

Conflicts:
	wger/core/tests/test_user.py
  • Loading branch information
rolandgeider committed Oct 28, 2015
2 parents 6462697 + 51dc10d commit 1f6e0b5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
20 changes: 19 additions & 1 deletion wger/core/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# You should have received a copy of the GNU Affero General Public License

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse, reverse_lazy

from wger.manager.tests.testcase import (
WorkoutManagerTestCase,
Expand Down Expand Up @@ -134,3 +134,21 @@ class UserListTestCase(WorkoutManagerAccessTestCase):
'trainer2',
'trainer3',
'trainer4')


class UserDetailPageTestCase(WorkoutManagerAccessTestCase):
'''
Test accessing the user detail page
'''

url = reverse_lazy('core:user:overview', kwargs={'pk': 2})
user_success = ('trainer1',
'trainer2',
'manager1',
'general_manager1',
'general_manager2')
user_fail = ('trainer4',
'trainer5',
'manager3',
'member1',
'member2')
24 changes: 17 additions & 7 deletions wger/core/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class UserDetailView(WgerPermissionMixin, DetailView):
User overview for gyms
'''
model = User
permission_required = ('gym.manage_gym', 'gym.gym_trainer')
permission_required = ('gym.manage_gym', 'gym.manage_gyms', 'gym.gym_trainer')
template_name = 'user/overview.html'
context_object_name = 'current_user'

Expand All @@ -426,11 +426,16 @@ def dispatch(self, request, *args, **kwargs):
Only managers for this gym can access the members
'''
user = request.user
if user.is_authenticated() and user.userprofile.gym == self.get_object().userprofile.gym:
return super(UserDetailView, self).dispatch(request, *args, **kwargs)
else:

if not user.is_authenticated():
return HttpResponseForbidden()

if (user.has_perm('gym.manage_gym') or user.has_perm('gym.gym_trainer')) \
and user.userprofile.gym != self.get_object().userprofile.gym:
return HttpResponseForbidden()

return super(UserDetailView, self).dispatch(request, *args, **kwargs)

def get_context_data(self, **kwargs):
'''
Send some additional data to the template
Expand Down Expand Up @@ -459,7 +464,7 @@ class UserListView(WgerPermissionMixin, ListView):
Overview of all users in the instance
'''
model = User
permission_required = ('gym.manage_gyms')
permission_required = ('gym.manage_gyms',)
template_name = 'user/list.html'

def get_queryset(self):
Expand All @@ -469,7 +474,7 @@ def get_queryset(self):
out = {'admins': [],
'members': []}

for u in User.objects.select_related('usercache').all():
for u in User.objects.select_related('usercache', 'userprofile__gym').all():
out['members'].append({'obj': u,
'last_log': u.usercache.last_activity})

Expand All @@ -480,6 +485,11 @@ def get_context_data(self, **kwargs):
Pass other info to the template
'''
context = super(UserListView, self).get_context_data(**kwargs)
context['user_table'] = {'keys': [_('ID'), _('Username'), _('Name'), _('Last activity')],
context['show_gym'] = True
context['user_table'] = {'keys': [_('ID'),
_('Username'),
_('Name'),
_('Last activity'),
_('Gym')],
'users': context['object_list']['members']}
return context
11 changes: 11 additions & 0 deletions wger/gym/templates/gym/partial_user_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@
<td data-order="{{current_user.last_log|date:'U'}}">
{{current_user.last_log|default:'-/-'}}
</td>
{% if show_gym %}
<td>
{% if current_user.obj.userprofile.gym_id %}
<a href="{{ current_user.obj.userprofile.gym.get_absolute_url }}">
{{ current_user.obj.userprofile.gym }}
</a>
{% else %}
-/-
{% endif %}
</td>
{% endif %}
</tr>
{% empty %}
<tr>
Expand Down

0 comments on commit 1f6e0b5

Please sign in to comment.