From 0d6b2c4afc046764e21c0fc98b187e87fc12b9c4 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 21 May 2026 09:42:40 -0400 Subject: [PATCH] [tasks] Add unit handling to weigh-ins --- vrobbler/apps/profiles/forms.py | 1 + .../0032_userprofile_weigh_in_units.py | 24 +++++++ vrobbler/apps/profiles/models.py | 11 ++++ vrobbler/apps/scrobbles/importers/scale.py | 34 +++++----- .../templates/scrobbles/scrobble_detail.html | 66 +++++++++++++++++++ 5 files changed, 120 insertions(+), 16 deletions(-) create mode 100644 vrobbler/apps/profiles/migrations/0032_userprofile_weigh_in_units.py diff --git a/vrobbler/apps/profiles/forms.py b/vrobbler/apps/profiles/forms.py index b2cad4c..e3d0c5e 100644 --- a/vrobbler/apps/profiles/forms.py +++ b/vrobbler/apps/profiles/forms.py @@ -35,6 +35,7 @@ class UserProfileForm(forms.ModelForm): "enable_public_widgets", "widget_custom_css", "home_scrobble_limit", + "weigh_in_units", ] widgets = { "lastfm_password": forms.PasswordInput(render_value=True), diff --git a/vrobbler/apps/profiles/migrations/0032_userprofile_weigh_in_units.py b/vrobbler/apps/profiles/migrations/0032_userprofile_weigh_in_units.py new file mode 100644 index 0000000..c199e0a --- /dev/null +++ b/vrobbler/apps/profiles/migrations/0032_userprofile_weigh_in_units.py @@ -0,0 +1,24 @@ +# Generated by Django 4.2.29 on 2026-05-21 13:38 + +from django.db import migrations, models + +import profiles.models + + +class Migration(migrations.Migration): + + dependencies = [ + ("profiles", "0031_add_home_scrobble_limit"), + ] + + operations = [ + migrations.AddField( + model_name="userprofile", + name="weigh_in_units", + field=models.CharField( + choices=profiles.models.WeighUnit.choices, + default=profiles.models.WeighUnit.METRIC, + max_length=16, + ), + ), + ] diff --git a/vrobbler/apps/profiles/models.py b/vrobbler/apps/profiles/models.py index 753d8cb..670e635 100644 --- a/vrobbler/apps/profiles/models.py +++ b/vrobbler/apps/profiles/models.py @@ -16,6 +16,11 @@ BNULL = {"blank": True, "null": True} logger = logging.getLogger(__name__) +class WeighUnit(models.TextChoices): + METRIC = "metric", "Metric (kg, cm)" + IMPERIAL = "imperial", "Imperial (lbs, in)" + + class UserProfile(TimeStampedModel): user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile") timezone = models.CharField( @@ -66,6 +71,12 @@ class UserProfile(TimeStampedModel): home_scrobble_limit = models.IntegerField(default=20) + weigh_in_units = models.CharField( + max_length=16, + choices=WeighUnit.choices, + default=WeighUnit.METRIC, + ) + def __str__(self): return f"User profile for {self.user}" diff --git a/vrobbler/apps/scrobbles/importers/scale.py b/vrobbler/apps/scrobbles/importers/scale.py index 9f5e113..d03a0b7 100644 --- a/vrobbler/apps/scrobbles/importers/scale.py +++ b/vrobbler/apps/scrobbles/importers/scale.py @@ -10,28 +10,28 @@ from tasks.models import Task logger = logging.getLogger(__name__) FLOAT_COLUMNS = { - "WEIGHT": "weight_kg", + "WEIGHT": "weight", "BMI": "bmi", - "BODY_FAT": "body_fat_pct", - "BONE": "bone_kg", - "MUSCLE": "muscle_kg", - "WATER": "water_pct", + "BODY_FAT": "body_fat", + "BONE": "bone", + "MUSCLE": "muscle", + "WATER": "water", "VISCERAL_FAT": "visceral_fat", - "WAIST": "waist_cm", + "WAIST": "waist", "CALORIES": "calories", "BMR": "bmr", "TDEE": "tdee", "HEART_RATE": "heart_rate", - "CHEST": "chest_cm", - "BICEPS": "biceps_cm", - "NECK": "neck_cm", - "THIGH": "thigh_cm", - "HIPS": "hips_cm", - "CALIPER": "caliper_mm", - "CALIPER_1": "caliper_1_mm", - "CALIPER_2": "caliper_2_mm", - "CALIPER_3": "caliper_3_mm", - "LBM": "lbm_kg", + "CHEST": "chest", + "BICEPS": "biceps", + "NECK": "neck", + "THIGH": "thigh", + "HIPS": "hips", + "CALIPER": "caliper", + "CALIPER_1": "caliper_1", + "CALIPER_2": "caliper_2", + "CALIPER_3": "caliper_3", + "LBM": "lbm", "WHR": "whr", "WHTR": "whtr", } @@ -83,6 +83,8 @@ def import_scale_csv(file_path, user_id): if comment: log_dict["comment"] = comment + log_dict["unit_type"] = user.profile.weigh_in_units + existing = Scrobble.objects.filter( timestamp=start, task=weigh_in, diff --git a/vrobbler/templates/scrobbles/scrobble_detail.html b/vrobbler/templates/scrobbles/scrobble_detail.html index b59e000..b203811 100644 --- a/vrobbler/templates/scrobbles/scrobble_detail.html +++ b/vrobbler/templates/scrobbles/scrobble_detail.html @@ -6,6 +6,24 @@ {% block title %}{{object.name}}{% endblock %} +{% block head_extra %} + +{% endblock %} + {% block lists %}
@@ -20,6 +38,54 @@ {% if object.media_type == "Track" %}

Source: {{ object.source }}{% if object.log.mopidy_source %} ({{ object.log.mopidy_source|capfirst }}){% endif %}

{% endif %} +{% if object.media_type == "Task" and object.log.weight %} +
+
+
Weight
+
{{ object.log.weight }} {% if object.log.unit_type == "imperial" %}lbs{% else %}kg{% endif %}
+ {% if object.log.body_fat %} +
Body Fat
+
{{ object.log.body_fat }}%
+ {% endif %} + {% if object.log.bmi %} +
BMI
+
{{ object.log.bmi }}
+ {% endif %} + {% if object.log.muscle %} +
Muscle
+
{{ object.log.muscle }} {% if object.log.unit_type == "imperial" %}lbs{% else %}kg{% endif %}
+ {% endif %} + {% if object.log.bone %} +
Bone
+
{{ object.log.bone }} {% if object.log.unit_type == "imperial" %}lbs{% else %}kg{% endif %}
+ {% endif %} + {% if object.log.water %} +
Water
+
{{ object.log.water }}%
+ {% endif %} + {% if object.log.visceral_fat %} +
Visceral Fat
+
{{ object.log.visceral_fat }}
+ {% endif %} + {% if object.log.waist %} +
Waist
+
{{ object.log.waist }} {% if object.log.unit_type == "imperial" %}in{% else %}cm{% endif %}
+ {% endif %} + {% if object.log.lbm %} +
Lean Mass
+
{{ object.log.lbm }} {% if object.log.unit_type == "imperial" %}lbs{% else %}kg{% endif %}
+ {% endif %} + {% if object.log.calories %} +
Calories
+
{{ object.log.calories }}
+ {% endif %} + {% if object.log.comment %} +
Comment
+
{{ object.log.comment }}
+ {% endif %} +
+
+{% endif %} {% if object.media_type == "Task" and object.logdata.description %}

{{ object.logdata.description }}

{% endif %}