"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 19.08.22

"""
from django.shortcuts import get_object_or_404
from django.views import View
from django.utils.translation import gettext_lazy as _

from konova.forms.modals import RecordModalForm


class AbstractRecordView(View):
    model = None

    def get(self, request, id: str):
        """ Renders a modal form for recording an object

        Args:
            request (HttpRequest): The incoming request
            id (str): The object's id

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        form = RecordModalForm(request.POST or None, instance=obj, request=request)
        msg_succ = _("{} unrecorded") if obj.recorded else _("{} recorded")
        msg_succ = msg_succ.format(obj.identifier)
        return form.process_request(
            request,
            msg_succ,
            msg_error=_("Errors found:")
        )

    def post(self, request, id: str):
        """

        BaseModalForm provides the method process_request() which handles GET as well as POST requests. It was written
        for easier handling of function based views. To support process_request() on class based views, the post()
        call needs to be treated the same way as the get() call.

        Args:
            request (HttpRequest): The incoming request
            id (str): Intervention's id

        """
        return self.get(request, id)
