API Reference
Health weight module for managing user weight and body composition.
This module provides CRUD operations and data models for user weight tracking including BMI, body composition metrics, and various health indicators.
Exports
- CRUD: get_all_health_weight, get_health_weight_number, get_all_health_weight_by_user_id, get_health_weight_by_id_and_user_id, get_health_weight_with_pagination, get_health_weight_by_date, create_health_weight, edit_health_weight, delete_health_weight
- Schemas: HealthWeightBase, HealthWeightCreate, HealthWeightUpdate, HealthWeightRead, HealthWeightListResponse
- Enums: Source
- Models: HealthWeight (ORM model)
- Utils: calculate_bmi, calculate_bmi_all_user_entries
HealthWeightBase
Bases: BaseModel
Pydantic model for health weight data.
This model defines the structure and validation rules for health weight information, including body composition metrics and related health indicators.
Attributes:
| Name | Type | Description |
|---|---|---|
date |
date | None
|
The date of the health weight measurement. |
weight |
StrictFloat | None
|
Weight in kilograms. Must be between 0 and 500. |
bmi |
StrictFloat | None
|
Body Mass Index value. Must be between 0 and 100. |
body_fat |
StrictFloat | None
|
Body fat percentage. Must be between 0 and 100. |
body_water |
StrictFloat | None
|
Body water percentage. Must be between 0 and 100. |
bone_mass |
StrictFloat | None
|
Bone mass in kilograms. Must be between 0 and 500. |
muscle_mass |
StrictFloat | None
|
Muscle mass in kilograms. Must be between 0 and 500. |
physique_rating |
StrictInt | None
|
Physique rating score. Must be greater than or equal to 0. |
visceral_fat |
StrictFloat | None
|
Visceral fat percentage. Must be between 0 and 100. |
metabolic_age |
StrictInt | None
|
Metabolic age in years. Must be between 0 and 120. |
source |
Source | None
|
The source or device from which the weight data was obtained. |
Model Configuration
- Populates from ORM attributes
- Forbids extra fields
- Validates assignments
- Uses enum values for serialization
Source code in backend/app/health/health_weight/schema.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
HealthWeightCreate
Bases: HealthWeightBase
Validator for HealthWeightCreate model that automatically sets the date field.
This validator runs after model initialization to ensure that if no date is provided, it defaults to today's date.
Source code in backend/app/health/health_weight/schema.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
set_default_date
set_default_date()
Set date to today if not provided.
Returns:
| Type | Description |
|---|---|
HealthWeightCreate
|
The validated model instance with date set. |
Source code in backend/app/health/health_weight/schema.py
99 100 101 102 103 104 105 106 107 108 109 | |
HealthWeightListResponse
Bases: BaseModel
Response model for listing health weight records.
Attributes:
| Name | Type | Description |
|---|---|---|
total |
StrictInt
|
Total number of weight records for the user. |
num_records |
StrictInt | None
|
Number of records in this response. |
page_number |
StrictInt | None
|
Current page number. |
records |
list[HealthWeightRead]
|
List of health weight records. |
Source code in backend/app/health/health_weight/schema.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
HealthWeightModel
Bases: Base
User health weight and body composition data.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
Mapped[int]
|
Primary key. |
user_id |
Mapped[int]
|
Foreign key to users table. |
date |
Mapped[date]
|
Calendar date of the measurement. |
weight |
Mapped[Decimal]
|
Weight in kilograms. |
bmi |
Mapped[Decimal | None]
|
Body Mass Index. |
body_fat |
Mapped[Decimal | None]
|
Body fat percentage. |
body_water |
Mapped[Decimal | None]
|
Body hydration percentage. |
bone_mass |
Mapped[Decimal | None]
|
Bone mass percentage. |
muscle_mass |
Mapped[Decimal | None]
|
Muscle mass percentage. |
physique_rating |
Mapped[int | None]
|
Physique rating score. |
visceral_fat |
Mapped[Decimal | None]
|
Visceral fat rating. |
metabolic_age |
Mapped[int | None]
|
Calculated metabolic age. |
source |
Mapped[str | None]
|
Data source. |
user |
Mapped[str | None]
|
Relationship to Users model. |
Source code in backend/app/health/health_weight/models.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
HealthWeightRead
Bases: HealthWeightBase
Schema for reading a health weight record.
Extends HealthWeightBase with identifier fields required for retrieving and referencing weight records in the system.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
StrictInt
|
Unique identifier for the weight record to update. |
user_id |
StrictInt
|
Foreign key reference to the user. |
Source code in backend/app/health/health_weight/schema.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
HealthWeightUpdate
Bases: HealthWeightRead
Schema for updating health weight records.
Inherits from HealthWeightRead to maintain consistency with read operations while allowing modifications to health weight data. This schema is used for PUT/PATCH requests to update existing health weight entries.
Source code in backend/app/health/health_weight/schema.py
130 131 132 133 134 135 136 137 | |
Source
Bases: Enum
Enumeration of data sources for health weight records.
Attributes:
| Name | Type | Description |
|---|---|---|
GARMIN |
Garmin fitness tracking platform as a data source. |
Source code in backend/app/health/health_weight/schema.py
13 14 15 16 17 18 19 20 21 | |
calculate_bmi
calculate_bmi(health_weight, user_id, db)
Calculate the Body Mass Index (BMI) for a health weight record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
health_weight
|
HealthWeightCreate | HealthWeightUpdate
|
Health weight record with weight value. |
required |
user_id
|
int
|
Unique identifier of the user. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
HealthWeightCreate | HealthWeightUpdate
|
Updated health weight record with calculated BMI. |
Source code in backend/app/health/health_weight/utils.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
calculate_bmi_all_user_entries
calculate_bmi_all_user_entries(user_id, db)
Calculate and update BMI for all health weight entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
User ID whose entries should be processed. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in backend/app/health/health_weight/utils.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
create_health_weight
create_health_weight(user_id, health_weight, db)
Create a new health weight entry for a user.
This function creates a new health weight record in the database. If the date is not provided, it defaults to the current date. If BMI is not provided, it is automatically calculated using the user's height and the provided weight.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
The ID of the user for whom the health weight entry is being created. |
required |
health_weight
|
HealthWeightCreate
|
The health weight data to be created, containing fields such as weight, date, and optionally BMI. |
required |
db
|
Session
|
The database session used for database operations. |
required |
Returns:
| Type | Description |
|---|---|
HealthWeight
|
health_weight_models.HealthWeightCreate: The created health weight model instance. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
|
Note
- The function automatically sets the date to current timestamp if not provided.
- BMI is calculated automatically if not provided in the input.
- The database transaction is rolled back in case of any errors.
Source code in backend/app/health/health_weight/crud.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
delete_health_weight
delete_health_weight(user_id, health_weight_id, db)
Delete a health weight record for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
User ID who owns the health weight record. |
required |
health_weight_id
|
int
|
Health weight record ID to delete. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If record not found or database error. |
Source code in backend/app/health/health_weight/crud.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
edit_health_weight
edit_health_weight(user_id, health_weight, db)
Edit an existing health weight record for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
User ID who owns the health weight record. |
required |
health_weight
|
HealthWeightUpdate
|
Health weight data to update. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
HealthWeight
|
Updated health weight object. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If record not found or database error. |
Source code in backend/app/health/health_weight/crud.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
get_all_health_weight
get_all_health_weight(db)
Retrieve all health weight records from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
list[HealthWeight]
|
List of HealthWeight models ordered by date descending. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/health/health_weight/crud.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
get_all_health_weight_by_user_id
get_all_health_weight_by_user_id(user_id, db)
Retrieve all health weight records for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
User ID to fetch records for. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
list[HealthWeight]
|
List of HealthWeight models ordered by date descending. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/health/health_weight/crud.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
get_health_weight_by_date
get_health_weight_by_date(user_id, date, db)
Retrieve health weight record for a user on a specific date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
User ID. |
required |
date
|
str
|
Date string for the weight record. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
HealthWeight | None
|
HealthWeight model if found, None otherwise. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/health/health_weight/crud.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
get_health_weight_by_id_and_user_id
get_health_weight_by_id_and_user_id(health_weight_id, user_id, db)
Retrieve health weight record by ID and user ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
health_weight_id
|
int
|
Health weight record ID to fetch. |
required |
user_id
|
int
|
User ID to fetch record for. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
HealthWeight | None
|
HealthWeight model if found, None otherwise. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/health/health_weight/crud.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
get_health_weight_number
get_health_weight_number(user_id, db)
Retrieve total count of health weight records for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
User ID to count records for. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Total number of health weight records. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/health/health_weight/crud.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
get_health_weight_with_pagination
get_health_weight_with_pagination(user_id, db, page_number=1, num_records=5)
Retrieve paginated health weight records for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
User ID to fetch records for. |
required |
db
|
Session
|
Database session. |
required |
page_number
|
int
|
Page number to retrieve (1-indexed). |
1
|
num_records
|
int
|
Number of records per page. |
5
|
Returns:
| Type | Description |
|---|---|
list[HealthWeight]
|
List of HealthWeight models for the requested page. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/health/health_weight/crud.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |