Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Format according to PEP8, remove unused imports

Roland Geider9fddd858a62a
diff --git a/manager/tests/weight_log.py b/manager/tests/weight_log.py
index ccde2f669..06decd20c 100644
--- a/manager/tests/weight_log.py
+++ b/manager/tests/weight_log.py
@@ -70,7 +70,6 @@ class WeightLogTestCase(WorkoutManagerTestCase):
self.assertEqual(response.status_code, 302)
self.assertGreater(count_after, count_before)
-
def test_add_weight_log_anonymous(self):
'''
Tests adding weight log entries as an anonymous user
@@ -78,7 +77,6 @@ class WeightLogTestCase(WorkoutManagerTestCase):
self.add_weight_log(fail=True)
-
def test_add_weight_log_owner(self):
'''
Tests adding weight log entries as the owner user
@@ -87,7 +85,6 @@ class WeightLogTestCase(WorkoutManagerTestCase):
self.user_login('admin')
self.add_weight_log(fail=False)
-
def test_add_weight_log_other(self):
'''
Tests adding weight log entries as a logged user not owning the data
@@ -126,7 +123,6 @@ class WeightLogEntryTestCase(WorkoutManagerTestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(date_after, datetime.date(2012, 1, 1))
-
def test_edit_log_entry_anonymous(self):
'''
Tests editing a weight log entries as an anonymous user
@@ -134,7 +130,6 @@ class WeightLogEntryTestCase(WorkoutManagerTestCase):
self.edit_log_entry(fail=True)
-
def test_edit_log_entry_owner(self):
'''
Tests editing a weight log entries as the owner user
@@ -143,7 +138,6 @@ class WeightLogEntryTestCase(WorkoutManagerTestCase):
self.user_login('admin')
self.edit_log_entry(fail=False)
-
def test_edit_log_entry_other(self):
'''
Tests editing a weight log entries as a logged user not owning the data
@@ -153,7 +147,6 @@ class WeightLogEntryTestCase(WorkoutManagerTestCase):
self.edit_log_entry(fail=True)
-
class WorkoutLogLiveServerTestCase(WorkoutManagerLiveServerTestCase):
'''
Selenium tests for workout log
diff --git a/weight/views.py b/weight/views.py
index b58942fd0..92f37dbb4 100644
--- a/weight/views.py
+++ b/weight/views.py
@@ -1,17 +1,17 @@
# -*- coding: utf-8 -*-
# This file is part of Workout Manager.
-#
+#
# 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.
-#
+#
# 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 logging
@@ -20,12 +20,9 @@ import json
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.forms import DateField
-from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.decorators import login_required
@@ -34,7 +31,6 @@ from django.utils.translation import ugettext_lazy
from django.db.models import Min
from django.db.models import Max
-from django.views.generic import DeleteView
from django.views.generic import CreateView
from django.views.generic import UpdateView
@@ -47,11 +43,14 @@ from workout_manager.constants import DATE_FORMATS
logger = logging.getLogger('workout_manager.custom')
+
class WeightForm(ModelForm):
creation_date = DateField(input_formats=DATE_FORMATS)
+
class Meta:
model = WeightEntry
- exclude=('user',)
+ exclude = ('user',)
+
class WeightAddView(YamlFormMixin, CreateView):
"""
@@ -59,20 +58,19 @@ class WeightAddView(YamlFormMixin, CreateView):
"""
active_tab = WEIGHT_TAB
model = WeightEntry
- form_class = WeightForm
+ form_class = WeightForm
custom_js = '''$(document).ready(function () {
init_weight_datepicker();
});'''
title = ugettext_lazy('Add weight entry')
form_action = reverse_lazy('weight-add')
success_url = reverse_lazy('weight.views.overview')
-
-
+
def form_valid(self, form):
"""
Set the owner of the entry here
"""
-
+
form.instance.user = self.request.user
return super(WeightAddView, self).form_valid(form)
@@ -88,50 +86,51 @@ class WeightUpdateView(YamlFormMixin, UpdateView):
init_weight_datepicker();
});'''
success_url = reverse_lazy('weight.views.overview')
-
-
+
def get_context_data(self, **kwargs):
context = super(WeightUpdateView, self).get_context_data(**kwargs)
context['form_action'] = reverse('weight-edit', kwargs={'pk': self.object.id})
context['title'] = _('Edit weight entry for the %s') % self.object.creation_date
-
+
return context
+
@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')])
-
+
+ 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 a plot with the weight data
-
+
More info about the D3 library can be found here:
* https://github.com/mbostock/d3
* http://d3js.org/
"""
template_data = {}
template_data['active_tab'] = WEIGHT_TAB
-
- min_date = WeightEntry.objects.filter(user = request.user).aggregate(Min('creation_date'))['creation_date__min']
- max_date = WeightEntry.objects.filter(user = request.user).aggregate(Max('creation_date'))['creation_date__max']
+
+ min_date = WeightEntry.objects.filter(user=request.user).aggregate(Min('creation_date'))['creation_date__min']
+ max_date = WeightEntry.objects.filter(user=request.user).aggregate(Max('creation_date'))['creation_date__max']
if min_date:
template_data['min_date'] = 'new Date(%(year)s, %(month)s, %(day)s)' % {'year': min_date.year,
'month': min_date.month,
@@ -144,22 +143,23 @@ def overview(request):
template_data,
context_instance=RequestContext(request))
+
@login_required
def get_weight_data(request):
# Process the data to pass it to the JS libraries to generate an SVG image
-
+
if request.is_ajax():
date_min = request.GET.get('date_min', False)
date_max = request.GET.get('date_max', True)
-
+
if date_min and date_max:
- weights = WeightEntry.objects.filter(user = request.user,
+ weights = WeightEntry.objects.filter(user=request.user,
creation_date__range=(date_min, date_max))
else:
- weights = WeightEntry.objects.filter(user = request.user)
-
+ weights = WeightEntry.objects.filter(user=request.user)
+
chart_data = []
-
+
for i in weights:
chart_data.append({'x': "%(month)s/%(day)s/%(year)s" % {
'year': i.creation_date.year,
@@ -167,8 +167,7 @@ def get_weight_data(request):
'day': i.creation_date.day},
'y': i.weight,
'id': i.id})
-
-
+
# Return the results to the server
mimetype = 'application/json'
return HttpResponse(json.dumps(chart_data), mimetype)