36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
from functools import wraps
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from events.models import Event
|
|
from .models import Game
|
|
|
|
|
|
def require_game_enabled(steam_app_id: int | None = None, *, path: str | None = None):
|
|
"""Gate a view (or, via @decorate_view, an API operation) behind an
|
|
EventGame being enabled for the current event. Steam games are looked up
|
|
by steam_app_id (the original, positional form); custom non-Steam games
|
|
(no steam_app_id) are looked up by their Game.path instead, e.g.
|
|
require_game_enabled(path="clock")."""
|
|
|
|
def decorator(view_func):
|
|
@wraps(view_func)
|
|
def wrapper(request, *args, **kwargs):
|
|
event = get_object_or_404(Event, path=kwargs.get("event_path"))
|
|
games = Game.objects.enabled_for_event(event)
|
|
games = (
|
|
games.filter(steam_app_id=steam_app_id)
|
|
if steam_app_id is not None
|
|
else games.filter(path=path)
|
|
)
|
|
|
|
if not games.exists():
|
|
raise PermissionDenied
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
return decorator
|