Codebase hardening: 102 fixes across 35+ files
Deep audit identified 106 findings; 102 fixed, 4 deferred. Covers 8 areas: - Settings & deploy: env-gated DEBUG/SECRET_KEY, HTTPS headers, gunicorn, celery worker - Auth (registered_user): password write_only, request.data fixes, transaction safety, proper HTTP status codes - Workout app: IDOR protection, get_object_or_404, prefetch_related N+1 fixes, transaction.atomic - Video/scripts: path traversal sanitization, HLS trigger guard, auth on cache wipe - Models (exercise/equipment/muscle/superset): null-safe __str__, stable IDs, prefetch support - Generator views: helper for registered_user lookup, logger.exception, bulk_update, transaction wrapping - Generator core (rules/selector/generator): push-pull ratio, type affinity normalization, modality checks, side-pair exact match, word-boundary regex, equipment cache clearing - Generator services (plan_builder/analyzer/normalizer): transaction.atomic, muscle cache, bulk_update, glutes classification fix Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
68
generator/services/workout_generation/section_builders.py
Normal file
68
generator/services/workout_generation/section_builders.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import random
|
||||
from typing import Iterable, Optional
|
||||
|
||||
|
||||
def section_exercise_count(section: str, fitness_level: int, rng=random) -> int:
|
||||
"""Return section exercise count range by fitness level."""
|
||||
level = fitness_level or 2
|
||||
if section == 'warmup':
|
||||
if level <= 1:
|
||||
return rng.randint(5, 7)
|
||||
if level >= 3:
|
||||
return rng.randint(3, 5)
|
||||
return rng.randint(4, 6)
|
||||
if section == 'cooldown':
|
||||
if level <= 1:
|
||||
return rng.randint(4, 5)
|
||||
if level >= 3:
|
||||
return rng.randint(2, 3)
|
||||
return rng.randint(3, 4)
|
||||
raise ValueError(f'Unknown section: {section}')
|
||||
|
||||
|
||||
def rounded_duration(
|
||||
raw_duration: int,
|
||||
*,
|
||||
min_duration: int,
|
||||
duration_multiple: int,
|
||||
) -> int:
|
||||
"""Round duration to configured multiple and clamp to minimum."""
|
||||
return max(min_duration, round(raw_duration / duration_multiple) * duration_multiple)
|
||||
|
||||
|
||||
def build_duration_entries(
|
||||
exercises: Iterable,
|
||||
*,
|
||||
duration_min: int,
|
||||
duration_max: int,
|
||||
min_duration: int,
|
||||
duration_multiple: int,
|
||||
rng=random,
|
||||
) -> list[dict]:
|
||||
"""Build ordered duration entries from exercises."""
|
||||
entries = []
|
||||
for idx, ex in enumerate(exercises, start=1):
|
||||
duration = rng.randint(duration_min, duration_max)
|
||||
entries.append({
|
||||
'exercise': ex,
|
||||
'duration': rounded_duration(
|
||||
duration,
|
||||
min_duration=min_duration,
|
||||
duration_multiple=duration_multiple,
|
||||
),
|
||||
'order': idx,
|
||||
})
|
||||
return entries
|
||||
|
||||
|
||||
def build_section_superset(name: str, entries: list[dict]) -> Optional[dict]:
|
||||
"""Build a single-round warmup/cooldown superset payload."""
|
||||
if not entries:
|
||||
return None
|
||||
return {
|
||||
'name': name,
|
||||
'rounds': 1,
|
||||
'rest_between_rounds': 0,
|
||||
'exercises': entries,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user