"""This module holds the mixins for the ``apiv2`` module of ``assessments`` app"""


class UserContextSerializerMixin:
    """Init the Serializer class with a user from the context"""

    def __init__(self, *args, **kwargs):
        """Check we have a valid context before and init user"""

        context = kwargs.get('context', {})

        try:
            self.user = context['user']
        except KeyError:
            raise KeyError('A context must be supplied with a ``user`` key.')

        super().__init__(*args, **kwargs)


class SessionContextSerializerMixin:
    """Init the Serializer class with a session from the context"""

    def __init__(self, *args, **kwargs):
        """Check we have a valid context before and init session"""

        context = kwargs.get('context', {})

        try:
            self.session = context['session']
        except KeyError:
            raise KeyError('A context must be supplied with a ``session`` key.')

        super().__init__(*args, **kwargs)
