Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LTD-5465 Amend exporter setting of consignee and add website length validation #2181

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
7 changes: 6 additions & 1 deletion exporter/applications/forms/parties.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,12 @@ class Layout:
TITLE = "End user website address (optional)"
TITLE_AS_LABEL_FOR = "website"

website = forms.CharField(required=False, label="", help_text="Use the format https://www.example.com")
website = forms.CharField(
required=False,
label="",
help_text="Use the format https://www.example.com",
validators=[MaxLengthValidator(200, f"Website address should be 200 characters or less")],
)

def clean_website(self):
website = self.cleaned_data.get("website")
Expand Down
122 changes: 101 additions & 21 deletions exporter/applications/views/parties/consignees.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import logging

from django.shortcuts import render, redirect
from django.urls import reverse
from django.views.generic import TemplateView
from django.views.generic import TemplateView, FormView
from django.http import HttpResponseRedirect

from core.common.forms import BaseForm
from core.auth.views import LoginRequiredMixin
from core.wizard.views import BaseSessionWizardView

from exporter.applications.forms.parties import new_party_form_group
from exporter.applications.helpers.check_your_answers import convert_party
from exporter.applications.services import get_application, post_party, delete_party, validate_party
from exporter.applications.views.parties.base import AddParty, CopyParties, SetParty, DeleteParty, CopyAndSetParty
from exporter.applications.services import validate_party
from exporter.applications.views.parties.base import CopyParties, DeleteParty, CopyAndSetParty
from exporter.applications.forms.parties import (
PartyReuseForm,
PartySubTypeSelectForm,
PartyNameForm,
PartyWebsiteForm,
PartyAddressForm,
)
from exporter.applications.services import (
get_application,
post_party,
delete_party,
)
from exporter.core.helpers import (
str_to_bool,
)
from exporter.core.constants import (
SetPartyFormSteps,
)

from http import HTTPStatus
from lite_content.lite_exporter_frontend.applications import ConsigneeForm, ConsigneePage

from core.auth.views import LoginRequiredMixin
from .payloads import SetConsigneePayloadBuilder

log = logging.getLogger(__name__)

from core.decorators import expect_status


class Consignee(LoginRequiredMixin, TemplateView):
Expand All @@ -32,26 +63,75 @@ def get(self, request, **kwargs):
return redirect(reverse("applications:add_consignee", kwargs={"pk": application_id}))


class AddConsignee(LoginRequiredMixin, AddParty):
def __init__(self):
super().__init__(new_url="applications:set_consignee", copy_url="applications:consignees_copy")
class AddConsignee(LoginRequiredMixin, FormView):
form_class = PartyReuseForm
template_name = "core/form.html"

@property
def back_url(self):
return reverse("applications:task_list", kwargs={"pk": self.kwargs["pk"]}) + "#consignee"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form_title"] = self.form_class.title
return context

def form_valid(self, form):
reuse_party = str_to_bool(form.cleaned_data.get("reuse_party"))
if reuse_party:
success_url = reverse("applications:consignees_copy", kwargs=self.kwargs)
else:
success_url = reverse("applications:set_consignee", kwargs=self.kwargs)

class SetConsignee(LoginRequiredMixin, SetParty):
def __init__(self):
super().__init__(
url="applications:consignee_attach_document",
party_type="consignee",
form=new_party_form_group,
back_url="applications:consignee",
strings=ConsigneeForm,
post_action=post_party,
validate_action=validate_party,
)
return HttpResponseRedirect(success_url)


class SetConsignee(LoginRequiredMixin, BaseSessionWizardView):
party_type = "consignee"
form_list = [
(SetPartyFormSteps.PARTY_SUB_TYPE, PartySubTypeSelectForm),
(SetPartyFormSteps.PARTY_NAME, PartyNameForm),
(SetPartyFormSteps.PARTY_WEBSITE, PartyWebsiteForm),
(SetPartyFormSteps.PARTY_ADDRESS, PartyAddressForm),
]

def get_form_kwargs(self, step=None):
PartySubTypeSelectForm.title = "Select the type of consignee"
PartyNameForm.Layout.TITLE = "Consignee name"
PartyWebsiteForm.Layout.TITLE = "Consignee website address (optional)"
PartyAddressForm.Layout.TITLE = "Consignee address"
kwargs = super().get_form_kwargs(step)

if step == SetPartyFormSteps.PARTY_ADDRESS:
kwargs["request"] = self.request

return kwargs

def get_context_data(self, form, **kwargs):
context = super().get_context_data(form, **kwargs)
if isinstance(form, BaseForm):
context["title"] = form.Layout.TITLE
else:
context["title"] = form.title
return context

def get_payload(self, form_dict):
return SetConsigneePayloadBuilder().build(form_dict)

@expect_status(
HTTPStatus.CREATED,
"Error adding consignee to application",
"Unexpected error adding consignee to application",
)
def post_party_with_payload(self, pk, form_dict):
payload = self.get_payload(form_dict)
payload.update({"type": self.party_type})

return post_party(self.request, pk, payload)

def get_success_url(self, party_id):
return reverse("applications:consignee_attach_document", kwargs={"pk": self.kwargs["pk"], "obj_pk": party_id})

def done(self, form_list, form_dict, **kwargs):
response, _ = self.post_party_with_payload(self.kwargs["pk"], form_dict)

return redirect(self.get_success_url(response[self.party_type]["id"]))


class RemoveConsignee(LoginRequiredMixin, DeleteParty):
Expand Down
6 changes: 5 additions & 1 deletion exporter/applications/views/parties/end_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ def get_context_data(self, form, **kwargs):
return context

def get_form_kwargs(self, step=None):
kwargs = super().get_form_kwargs(step)
PartySubTypeSelectForm.title = "Select the type of end user"
PartyNameForm.Layout.TITLE = "End user name"
PartyWebsiteForm.Layout.TITLE = "End user website address (optional)"
PartyAddressForm.Layout.TITLE = "End user address"

kwargs = super().get_form_kwargs(step)
if step == SetPartyFormSteps.PARTY_ADDRESS:
kwargs["request"] = self.request
if step in (
Expand Down
14 changes: 14 additions & 0 deletions exporter/applications/views/parties/payloads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from core.wizard.payloads import MergingPayloadBuilder
from exporter.applications.views.goods.common.payloads import get_cleaned_data
from exporter.core.constants import (
SetPartyFormSteps,
)


class SetConsigneePayloadBuilder(MergingPayloadBuilder):
payload_dict = {
SetPartyFormSteps.PARTY_SUB_TYPE: get_cleaned_data,
SetPartyFormSteps.PARTY_NAME: get_cleaned_data,
SetPartyFormSteps.PARTY_WEBSITE: get_cleaned_data,
SetPartyFormSteps.PARTY_ADDRESS: get_cleaned_data,
}
Loading