def test_health_utils(self): s = "1.2" self.assertEqual(1.2, parsefloat(s)) s = "1,234.567.890" self.assertEqual(1.234567890, parsefloat(s)) s = "NoFloat" self.assertRaises(ValueError, parsefloat, s)
def add_weightgoal(request): try: if request.method == 'POST': logging.debug("add_weightgoal post request with items %s" % repr(request.POST.items())) datestring = "" try: datestring = request.POST.get('date') d = datetime.datetime.strptime(datestring, "%d.%m.%Y") except Exception, exc: logging.exception("Exception occured in add_weight: %s" % exc) result = {'success': False, 'msg': "Ungueltiges Datum: %s" % datestring} return HttpResponse(simplejson.dumps(result)) try: weight = str(parsefloat(request.POST.get('weight'))) except ValueError, exc: 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)) due_date = datetime.date(year=d.year, month=d.month, day=d.day) date = datetime.date.today() new_goal = Goal(date=date, due_date=due_date, target_weight=weight, user=request.user) new_goal.save() result = {'success': True} return HttpResponse(simplejson.dumps(result))
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 add_weight(request): logging.debug("add_weight called") try: if request.method == 'POST': logging.debug("add_weight post request with items %s" % repr(request.POST.items())) datestring = request.POST.get('date') try: weight = str(parsefloat(request.POST.get('weight'))) except ValueError, exc: 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))