import json

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.utils.decorators import method_decorator


class LoginRequiredMixin:
    """
    Login required mixin.
    """

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)


class JSONResponseMixin:
    """
    A mixin that can be used to render a JSON response.
    """

    response_class = HttpResponse

    def render_to_response(self, context, **response_kwargs):
        """
        Returns a JSON response, transforming 'context' to make the payload.
        """
        response_kwargs["content_type"] = "application/json"
        return self.response_class(
            self.convert_context_to_json(context), **response_kwargs
        )

    def convert_context_to_json(self, context):
        """
        Convert the context dictionary into a JSON object
        """
        return json.dumps(context)

import json

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.utils.decorators import method_decorator


class LoginRequiredMixin:
    """
    Login required mixin.
    """

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)


class JSONResponseMixin:
    """
    A mixin that can be used to render a JSON response.
    """

    response_class = HttpResponse

    def render_to_response(self, context, **response_kwargs):
        """
        Returns a JSON response, transforming 'context' to make the payload.
        """
        response_kwargs["content_type"] = "application/json"
        return self.response_class(
            self.convert_context_to_json(context), **response_kwargs
        )

    def convert_context_to_json(self, context):
        """
        Convert the context dictionary into a JSON object
        """
        return json.dumps(context)

Login required mixin. A mixin that can be used to render a JSON response. Returns a JSON response, transforming 'context' to make the payload. Convert the context dictionary into a JSON object