Skip to content

Commit

Permalink
Display initial values for bulk import and services
Browse files Browse the repository at this point in the history
  • Loading branch information
tortila committed Aug 23, 2023
1 parent 45f876f commit c9553c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
22 changes: 19 additions & 3 deletions apps/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class Meta:
fields = ["name", "website", "description", "authorised_by_org"]


class ServicesForm(forms.Form):
class ServicesForm(forms.ModelForm):
"""
Part of multi-step registration form (screen 2)
"""
Expand All @@ -265,6 +265,21 @@ class ServicesForm(forms.Form):
),
)

def __init__(self, *args, **kwargs):
"""
Implement injecting initial values for services field (for editing existing objects).
By default the initial value is passed as a queryset,
but TaggableManager does not handle that well - we pass a list instead.
"""
super().__init__(*args, **kwargs)
instance = kwargs.get("instance")
if instance:
self.initial = {"services": [s for s in instance.services.slugs()]}

class Meta:
model = ac_models.ProviderRequest
fields = ["services"]


class CredentialForm(forms.ModelForm):
class Meta:
Expand Down Expand Up @@ -587,7 +602,7 @@ class Meta:
)


class LocationExtraForm(forms.Form):
class LocationExtraForm(forms.ModelForm):
"""
A form for information relating to the location step, not a to a
single one of the locations listed on the location step.
Expand All @@ -605,7 +620,8 @@ class LocationExtraForm(forms.Form):
)

class Meta:
exclude = ["request"]
model = ac_models.ProviderRequest
fields = ["location_import_required"]


class LocationStepForm(BetterMultiModelForm):
Expand Down
8 changes: 0 additions & 8 deletions apps/accounts/models/provider_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,6 @@ def from_kwargs(**kwargs) -> "ProviderRequest":
pr_data.setdefault("status", ProviderRequestStatus.OPEN.value)
return ProviderRequest.objects.create(**pr_data)

def set_services_from_slugs(self, service_slugs: Iterable[str]) -> None:
"""
Given list of service slugs (corresponding to Tag slugs)
apply matching services to the ProviderRequest object
"""
services = Service.objects.filter(slug__in=service_slugs)
self.services.set(services)

@classmethod
def get_service_choices(cls) -> List[Tuple[int, str]]:
"""
Expand Down
15 changes: 11 additions & 4 deletions apps/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def done(self, form_list, form_dict, **kwargs):

# process SERVICES form: assign services to ProviderRequest
services_form = form_dict[steps.SERVICES.value]
pr.set_services_from_slugs(services_form.cleaned_data["services"])
services = services_form.cleaned_data["services"]
pr.services.set(*services)
pr.created_by = self.request.user
pr.save()

Expand Down Expand Up @@ -341,9 +342,12 @@ def done(self, form_list, form_dict, **kwargs):
network_explanation = extra_network_form.cleaned_data.get(
"missing_network_explanation"
)
if network_explanation:
pr.missing_network_explanation = network_explanation
pr.save()
network_import_required = extra_network_form.cleaned_data.get(
"network_import_required"
)
pr.missing_network_explanation = bool(network_explanation)
pr.network_import_required = bool(network_import_required)
pr.save()

# process CONSENT form
consent_form = form_dict[steps.CONSENT.value]
Expand Down Expand Up @@ -444,7 +448,9 @@ def get_instance_dict(self, request_id):
self.Steps.ORG_DETAILS.value: pr_instance,
self.Steps.LOCATIONS.value: {
"locations": location_qs,
"extra": pr_instance,
},
self.Steps.SERVICES.value: pr_instance,
self.Steps.GREEN_EVIDENCE.value: evidence_qs,
self.Steps.NETWORK_FOOTPRINT.value: {
"ips": ip_qs,
Expand All @@ -456,6 +462,7 @@ def get_instance_dict(self, request_id):
return instance_dict

def get_form_instance(self, step):
# TODO: optimize this - do not construct instance_dict on every call
request_id = self.kwargs.get("request_id")
if not request_id:
return None
Expand Down

0 comments on commit c9553c5

Please sign in to comment.