from functools import wraps

from django.core.exceptions import PermissionDenied

from .models import Game


def require_game_enabled(steam_app_id: int):
    def decorator(view_func):
        @wraps(view_func)
        def wrapper(request, *args, **kwargs):
            try:
                game = Game.objects.get(steam_app_id=steam_app_id)
            except Game.DoesNotExist:
                raise PermissionDenied
            if not game.enabled:
                raise PermissionDenied
            return view_func(request, *args, **kwargs)

        return wrapper

    return decorator
