"""
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.urls import reverse
from django.views import View
from django.utils.translation import gettext_lazy as _

from konova.forms.modals import ResubmissionModalForm


class AbstractResubmissionView(View):
    model = None
    form_action_url_base = None
    redirect_url_base = None
    
    class Meta:
        abstract = True
    
    def get(self, request, id: str):
        """ Renders resubmission form for an object

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

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        form = ResubmissionModalForm(request.POST or None, instance=obj, request=request)
        form.action_url = reverse(self.form_action_url_base, args=(id,))
        return form.process_request(
            request,
            msg_success=_("Resubmission set"),
            redirect_url=reverse(self.redirect_url_base, args=(id,))
        )

    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)
