def new_entry(): """ Page for adding a new weight entry """ if not g.user: flash("You must be logged in to view this page") return redirect("/unauthorized") else: form = WeightEntryForm() if form.validate_on_submit(): new_weight_log = WeightEntry( date = form.date.data, weight = form.weight.data, user_id = g.user.id ) g.user.weight_entries.append(new_weight_log) db.session.commit() return redirect('/weightentries/overview') return render_template("weight-entries/new-weight.html", form=form)
def fetch_weight(user, startdate=None, enddate=None): """ Error codes: 0 Success 1 User needs to connect their Withings account """ try: wauth = user.withings_auth except: logging.warning("Given user needs to connect to their Withings account") return 1 creds = withings.WithingsCredentials( consumer_key=settings.WITHINGS_CONSUMER_KEY, consumer_secret=settings.WITHINGS_CONSUMER_SECRET, access_token=wauth.oauth_token, access_token_secret=wauth.oauth_secret, user_id=wauth.uid) api = withings.WithingsApi(creds) logging.info("Logged in to API.") if not startdate and not enddate: entries = WeightEntry.objects.filter(user=user).order_by('-when') if entries: startdate = calendar.timegm(entries[0].when.utctimetuple()) + 1 logging.info("Using a start timestamp of " + str(startdate)) if startdate and enddate: measurements = api.get_measures(meastype=1, startdate=startdate, enddate=enddate) elif startdate and not enddate: measurements = api.get_measures(meastype=1, startdate=startdate) else: measurements = api.get_measures(meastype=1) logging.info("Grabbed measurements, adding to DB") if len(measurements) == 0: logging.info("Didn't grab shit, check the startdate?") for measurement in measurements: if not measurement.is_measure(): continue logging.debug("Daters: " + str(measurement.data)) entry = WeightEntry() entry.user = user entry.when = timezone.make_aware(measurement.date, timezone.get_default_timezone()) entry.weight = str(measurement.weight * KG_TO_LBS) entry.source = 1 entry.save() return 0
def test_user_weight_log(self): """ Does weight entry get added to user's log? """ test_weight = WeightEntry( date = "2021-01-01" weight = 180 user_id = self.uid ) self.u.weight_entries.append(test_weight) db.session.commit() self.assertEqual(len(self.u.weight_entries), 1)
def new(request): # TODO: let the user pick the date/time if request.method == "POST": try: weight = float(request.POST["weight"]) entry = WeightEntry() entry.user = request.user entry.weight = weight entry.when = timezone.make_aware(datetime.now(), timezone.get_default_timezone()) entry.source = WeightEntry.SOURCE_MANUAL entry.save() messages.success(request, "Successfully added your weight data") except: messages.error(request, "Unable to parse the weight you gave me") return render(request, "weight/new.html", locals())