Self hosted FLOSS fitness/workout, nutrition and weight tracker
First version of export view for gym members
Roland Geider637e373a6f3c
diff --git a/wger/gym/templates/gym/member_list.html b/wger/gym/templates/gym/member_list.htmlindex aa29ecefa..6bf376255 100644--- a/wger/gym/templates/gym/member_list.html+++ b/wger/gym/templates/gym/member_list.html@@ -146,6 +146,9 @@ $(document).ready( function () {<li><a href="{% url 'gym:gym:edit' gym.id %}" class="wger-modal-dialog">{% trans "Edit"%}</a></li>+ <li>+ <a href="{% url 'gym:export:users' gym.id %}">{% trans "Export"%}</a>+ </li></ul></div>{% endif %}diff --git a/wger/gym/urls.py b/wger/gym/urls.pyindex 6c0f2a510..525bb2c36 100644--- a/wger/gym/urls.py+++ b/wger/gym/urls.py@@ -28,7 +28,8 @@ from wger.gym.views import (admin_notes,document,contract,- contract_type+ contract_type,+ export)@@ -143,6 +144,13 @@ patterns_contract_types = [name='delete'),]+# sub patterns for exports+patterns_export = [+ url(r'^users/(?P<gym_pk>\d+)$',+ export.users,+ name='users'),+]+## All patterns for this app#@@ -155,4 +163,5 @@ urlpatterns = [url(r'^document/', include(patterns_documents, namespace="document")),url(r'^contract/', include(patterns_contracts, namespace="contract")),url(r'^contract-type/', include(patterns_contract_types, namespace="contract_type")),+ url(r'^export/', include(patterns_export, namespace="export")),]diff --git a/wger/gym/views/export.py b/wger/gym/views/export.pynew file mode 100644index 000000000..f7aac2970--- /dev/null+++ b/wger/gym/views/export.py@@ -0,0 +1,99 @@+# -*- 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+import csv+import datetime+import logging++from django.db.models import Q+from django.contrib.auth.decorators import login_required+from django.contrib.auth.models import (+ Group,+ Permission,+ User+)+from django.core.urlresolvers import reverse, reverse_lazy+from django.http.response import (+ HttpResponseForbidden,+ HttpResponse,+ HttpResponseRedirect+)+from django.shortcuts import render, get_object_or_404+from django.utils.translation import ugettext as _+from django.utils.translation import ugettext_lazy+from django.views.generic import (+ ListView,+ DeleteView,+ CreateView,+ UpdateView+)++from wger.gym.forms import GymUserAddForm, GymUserPermisssionForm+from wger.gym.helpers import get_user_last_activity, is_any_gym_admin+from wger.gym.models import (+ Gym,+ GymAdminConfig,+ GymUserConfig+)+from wger.config.models import GymConfig as GlobalGymConfig+from wger.utils.generic_views import (+ WgerFormMixin,+ WgerDeleteMixin,+ WgerPermissionMixin+)+from wger.utils.helpers import password_generator+++logger = logging.getLogger(__name__)+++@login_required+def users(request, gym_pk):+ '''+ Exports all users in selected gym+ '''+ gym = get_object_or_404(Gym, pk=gym_pk)++ if not request.user.has_perm('gym.manage_gyms') \+ and not request.user.has_perm('gym.manage_gym'):+ return HttpResponseForbidden()++ if request.user.has_perm('gym.manage_gym') \+ and request.user.userprofile.gym != gym:+ return HttpResponseForbidden()++ # Crease CSV 'file'+ response = HttpResponse(content_type='text/csv')+ writer = csv.writer(response, delimiter='\t', quoting=csv.QUOTE_ALL)+ writer.writerow([_('Nr.'),+ _('Username'),+ _('First name'),+ _('Last name'),+ _('Gym')])+ for userprofile in gym.userprofile_set.all():+ user = userprofile.user+ writer.writerow([user.id,+ user.username,+ user.first_name,+ user.last_name,+ gym.name])++ # Send the data to the browser+ today = datetime.date.today()+ filename = 'User-data-gym-{gym}-{t.year}-{t.month:02d}-{t.day:02d}.csv'.format(t=today,+ gym=gym.id)+ response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)+ response['Content-Length'] = len(response.content)+ return response