Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add CSV export option to weight data

Roland Geider8f99a8555b4b
diff --git a/weight/templates/weight_overview.html b/weight/templates/weight_overview.html
index 1b7ac161c..5a5c57e10 100644
--- a/weight/templates/weight_overview.html
+++ b/weight/templates/weight_overview.html
@@ -111,4 +111,8 @@
{% block sidebar %}
<h3>{% trans "Filter options" %}</h3>
<p>Filter widgets etc come here</p>
-{% endblock %}
\ No newline at end of file
+
+<h3>{% trans "Options" %}</h3>
+<p><a href="{% url weight.views.export_csv %}">{% trans "Export as CSV" %}</a></p>
+
+{% endblock %}
diff --git a/weight/urls.py b/weight/urls.py
index d6c99e885..87e0cca2a 100644
--- a/weight/urls.py
+++ b/weight/urls.py
@@ -2,6 +2,8 @@ from django.conf.urls import patterns, include, url
urlpatterns = patterns('weight.views',
url(r'^weight/add/(?P<id>\d*)$', 'add'),
+ url(r'^weight/export_csv/$', 'export_csv'),
url(r'^weight/overview/$', 'overview'),
+
)
diff --git a/weight/views.py b/weight/views.py
index cafab100c..3142f87fa 100644
--- a/weight/views.py
+++ b/weight/views.py
@@ -13,15 +13,17 @@
# You should have received a copy of the GNU Affero General Public License
import logging
-import datetime
+import csv
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
+from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.forms import ModelForm
from django.core.context_processors import csrf
-from django.contrib.auth.decorators import permission_required, login_required
+from django.contrib.auth.decorators import login_required
+from django.utils.translation import ugettext as _
from weight.models import WeightEntry
@@ -67,6 +69,28 @@ def add(request, id=None):
template_data,
context_instance=RequestContext(request))
+@login_required
+def export_csv(request):
+ """Exports the saved weight data as a CSV file
+ """
+
+ # Prepare the response headers
+ filename = "%s-%s" % (_('weightdata'), request.user.username)
+ response = HttpResponse(mimetype='text/csv')
+ response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename
+
+ # Convert all weight data to CSV
+ writer = csv.writer(response)
+
+ weights = WeightEntry.objects.filter(user = request.user)
+ writer.writerow([_('Weight'), _('Date')])
+
+ for entry in weights:
+ writer.writerow([entry.weight, entry.creation_date])
+
+ # Send the data to the browser
+ return response
+
@login_required
def overview(request):
"""Shows an overview of weight data