def add_weight(request): logging.debug("add_weight called") datafields = ["weight", "body_fat", "body_water", "bones_weight", "muscles_weight"] try: if request.method == 'POST': logging.debug("add_weight post request with items %s" % repr(request.POST.items())) datestring = request.POST.get('weight_date') try: d = datetime.datetime.strptime(datestring, "%d.%m.%Y") date = datetime.date(year=d.year, month=d.month, day=d.day) except ValueError, exc: try: d = datetime.datetime.strptime(datestring, "%Y-%m-%d") date = datetime.date(year=d.year, month=d.month, day=d.day) except ValueError, exc: logging.exception("Exception occured in add_weight: %s" % exc) result = {'success': False, 'msg': "Ungueltiges Datum: %s" % datestring} return HttpResponse(json.dumps(result)) new_weight = Weight(date=date, user=request.user) for field in datafields: if field in request.POST: try: value = str(parsefloat(request.POST.get(field))) setattr(new_weight, field, value) except ValueError as exc: logging.exception("Exception occured in add_weight parsing field %s: %s" % (field, exc)) result = {'success': False, 'msg': "Ungueltiger Wert in %s: %s" % (field, request.POST.get(field))} new_weight.save() result = {'success': True} return HttpResponse(json.dumps(result))
def test_get_data_view(self): required_attrs = ["weight", "body_fat", "body_water", "bones_weight", "muscles_weight", "lean_weight"] url = reverse("health.views.get_data") weight = Weight() weight.body_fat = 10 weight.weight = 80 weight.user = User.objects.get(username='******') weight.date = datetime.date(2050, 1, 1) weight.save() resp = self.c.get(url) self.assertEqual(resp.status_code, 200) data = json.loads(resp.content) self.assertIsInstance(data, dict) for attr in required_attrs: self.assertTrue(attr in data['data']) # check if calculation of lean weight is correct (weight - (weight * bodyfat / 100) weight = data['data']['weight'][-1][1] bf = data['data']['body_fat'][-1][1] lean_weight = data['data']['lean_weight'][-1][1] self.assertEqual(lean_weight, weight - (weight * bf / 100))
logging.exception("Exception occured in add_weight: %s" % exc) result = {'success': False, 'msg': "Ungueltiges Gewicht: %s" % request.POST.get('weight')} return HttpResponse(simplejson.dumps(result)) try: d = datetime.datetime.strptime(datestring, "%d.%m.%Y") date = datetime.date(year=d.year, month=d.month, day=d.day) except ValueError, exc: try: d = datetime.datetime.strptime(datestring, "%Y-%m-%d") date = datetime.date(year=d.year, month=d.month, day=d.day) except ValueError, exc: logging.exception("Exception occured in add_weight: %s" % exc) result = {'success': False, 'msg': "Ungueltiges Datum: %s" % datestring} return HttpResponse(simplejson.dumps(result)) new_weight = Weight(date=date, weight=weight, user=request.user) new_weight.save() result = {'success': True} return HttpResponse(simplejson.dumps(result)) else: return HttpResponseBadRequest() except Exception, exc: logging.exception("Exception occured in add_weight: %s" % exc) result = {'success': False, 'msg': "Fehler aufgetreten: %s" % str(exc)} return HttpResponse(simplejson.dumps(result)) @login_required def add_weightgoal(request): try: if request.method == 'POST':