from typing import *

from django.http import HttpRequest, JsonResponse
from django.views import View

from wcd_settings.setters import SettingsSetter
from wcd_settings.registries import SettingsDescriptor


__all__ = 'BaseView', 'DescriptorResolverViewMixin',


class BaseView(View):
    def render_to_response(self, context, status: int = 200):
        return JsonResponse(context, status=status)


class DescriptorResolverViewMixin:
    include: Optional[List[str]] = None
    exclude: Optional[List[str]] = None

    def get_descriptors(
        self,
        settings: SettingsSetter,
        request: HttpRequest,
    ) -> List[SettingsDescriptor]:
        registry = settings.resolver.registry
        include = self.include
        exclude = self.exclude

        return [
            descriptor for k, descriptor in registry.items()
            if (
                (include is None or k in include)
                and
                (exclude is None or k not in exclude)
            )
        ]
