Files
python-tdd/src/apps/applets/utils.py
Disco DeDisco 7c249500bd
All checks were successful
ci/woodpecker/push/pyswiss Pipeline was successful
ci/woodpecker/push/main Pipeline was successful
refactor: extract apply_applet_toggle, rooms_for_user & natus helpers to utils; DRY toggle views
- epic/utils.py (new): _planet_house, _compute_distinctions, rooms_for_user
- applets/utils.py: apply_applet_toggle replaces 5 copy-pasted toggle loops
- dashboard/views.py: use apply_applet_toggle; fix double free_tokens/tithe_tokens query in wallet(); promote _compute_distinctions import to module level
- gameboard/views.py: use apply_applet_toggle & rooms_for_user; fix double free_tokens query in toggle_game_applets
- billboard/views.py: use apply_applet_toggle & rooms_for_user
- SCSS: %tt-token-fields placeholder in _tooltips.scss; _gameboard & _game-kit @extend it
- epic/tests/unit/test_utils.py (new): coverage for _planet_house fallback path

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 15:46:30 -04:00

22 lines
765 B
Python

from apps.applets.models import Applet, UserApplet
def apply_applet_toggle(user, context, checked_slugs):
"""Persist applet visibility choices for a given context."""
for applet in Applet.objects.filter(context=context):
UserApplet.objects.update_or_create(
user=user,
applet=applet,
defaults={"visible": applet.slug in checked_slugs},
)
def applet_context(user, context):
ua_map = {ua.applet_id: ua.visible for ua in user.user_applets.all()}
applets = {a.slug: a for a in Applet.objects.filter(context=context)}
return [
{"applet": applets[slug], "visible": ua_map.get(applets[slug].pk, applets[slug].default_visible)}
for slug in applets
if slug in applets
]