# -*- coding: utf-8 -*-
# pylint: disable=C0103,W0613,W0622
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from django_fixmystreet.fmsproxy.models import AssignmentHandler
from . import serializers
from ..views import FmsPrivateViewMixin


class AbstractReportAssignmentView(FmsPrivateViewMixin, APIView):

    def _get_bad_request(self, serializer):
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class ReportAssignmentAcceptView(AbstractReportAssignmentView):

    def post(self, request, pk=None, format=None):
        serializer = serializers.ReportAssignmentAcceptSerializer(data=request.DATA)  # pylint: disable=C0103,E1120,E1123
        if serializer.is_valid():
            assignment = AssignmentHandler(pk, request.user)
            assignment.accept(serializer.data)
            response_data = {
                "message": "Request successfully processed.",
            }
            return Response(response_data)

        return self._get_bad_request(serializer)


class ReportAssignmentRejectView(AbstractReportAssignmentView):

    def post(self, request, pk=None, format=None):
        serializer = serializers.ReportAssignmentRejectSerializer(data=request.DATA)  # pylint: disable=C0103,E1120,E1123
        if serializer.is_valid():
            assignment = AssignmentHandler(pk, request.user)
            assignment.reject(serializer.data)
            response_data = {
                "message": "Request successfully processed.",
            }
            return Response(response_data)

        return self._get_bad_request(serializer)


class ReportAssignmentCloseView(AbstractReportAssignmentView):

    def post(self, request, pk=None, format=None):
        serializer = serializers.ReportAssignmentCloseSerializer(data=request.DATA)  # pylint: disable=C0103,E1120,E1123
        if serializer.is_valid():
            assignment = AssignmentHandler(pk, request.user)
            assignment.close(serializer.data)
            response_data = {
                "message": "Request successfully processed.",
            }
            return Response(response_data)

        return self._get_bad_request(serializer)

# -*- coding: utf-8 -*-
# pylint: disable=C0103,W0613,W0622
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from django_fixmystreet.fmsproxy.models import AssignmentHandler, \
    NotWaitingForThirdPartyError, NotAssignedToThirdPartyError, NotAuthorizedToThirdPartyError
from . import serializers
from ..views import FmsPrivateViewMixin


class AbstractReportAssignmentView(FmsPrivateViewMixin, APIView):

    def _get_assignment_handler(self, pk, user):
        try:
            return AssignmentHandler(pk, user)
        except (NotWaitingForThirdPartyError, NotAssignedToThirdPartyError, NotAuthorizedToThirdPartyError):
            return self._get_response_403()

    def _get_response_200(self, context=None):
        if not context:
            context = {"message": "Request successfully processed."}
        return Response(context)

    def _get_response_400(self, errors):
        return Response(errors, status=status.HTTP_400_BAD_REQUEST)

    def _get_response_403(self, context=None):
        if not context:
            context = {"detail": "Access or action forbidden."}
        return Response(context, status=status.HTTP_403_FORBIDDEN)


class ReportAssignmentAcceptView(AbstractReportAssignmentView):

    def post(self, request, pk=None, format=None):
        serializer = serializers.ReportAssignmentAcceptSerializer(data=request.DATA)  # pylint: disable=C0103,E1120,E1123
        if not serializer.is_valid():
            return self._get_response_400(serializer.errors)

        assignment = self._get_assignment(pk, request.user)
        assignment.accept(serializer.data)
        return self._get_response_200()


class ReportAssignmentRejectView(AbstractReportAssignmentView):

    def post(self, request, pk=None, format=None):
        serializer = serializers.ReportAssignmentRejectSerializer(data=request.DATA)  # pylint: disable=C0103,E1120,E1123
        if not serializer.is_valid():
            return self._get_response_400(serializer.errors)

        assignment = self._get_assignment(pk, request.user)
        assignment.reject(serializer.data)
        return self._get_response_200()


class ReportAssignmentCloseView(AbstractReportAssignmentView):

    def post(self, request, pk=None, format=None):
        serializer = serializers.ReportAssignmentCloseSerializer(data=request.DATA)  # pylint: disable=C0103,E1120,E1123
        if not serializer.is_valid():
            return self._get_response_400(serializer.errors)

        assignment = self._get_assignment(pk, request.user)
        assignment.close(serializer.data)
        return self._get_response_200()

Add exception handling. Improve code readability.