From 6489d10f65e1d2b6ff50714caec6564ccf632a6f Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Sun, 19 Apr 2020 20:37:56 +0100 Subject: can add a new system via organisation detail page --- ctrack/caf/forms.py | 22 ++++++++++++++++++++-- .../caf/applicable_system_create_from_org.html | 2 +- ctrack/caf/templates/caf/caf_list.html | 2 +- ctrack/caf/views.py | 15 +++++++++++---- 4 files changed, 33 insertions(+), 8 deletions(-) (limited to 'ctrack/caf') diff --git a/ctrack/caf/forms.py b/ctrack/caf/forms.py index 776e9af..c2d75ff 100644 --- a/ctrack/caf/forms.py +++ b/ctrack/caf/forms.py @@ -1,5 +1,5 @@ from crispy_forms.helper import FormHelper -from crispy_forms.layout import Button, Field +from crispy_forms.layout import Button, Field, Hidden from crispy_forms.layout import ButtonHolder from crispy_forms.layout import Fieldset from crispy_forms.layout import Layout @@ -21,9 +21,27 @@ CAFCreateInlineFormset = inlineformset_factory( class ApplicableSystemCreateFromOrgForm(forms.ModelForm): + + def __init__(self, org_id, *args, **kwargs): + super().__init__(*args, **kwargs) + self.helper = FormHelper(self) + self.helper.layout = Layout( + Fieldset( + "Create a new System", + Field("name", css_class="form-control form-control-sm"), + Field("description", css_class="form-control form-control-sm"), + Hidden("organisation", org_id), + Field("caf", css_class="form-control form-control-sm") + ), + ButtonHolder( + Submit("submit", "Submit", css_class="btn-primary"), + Button("cancel", "Cancel", css_class="btn-danger") + ) + ) + class Meta: model = ApplicableSystem - fields = ["name", "description", "caf"] + fields = ["name", "description", "caf", "organisation"] class ApplicableSystemCreateForm(forms.ModelForm): diff --git a/ctrack/caf/templates/caf/applicable_system_create_from_org.html b/ctrack/caf/templates/caf/applicable_system_create_from_org.html index 4c356e0..0de8b7d 100644 --- a/ctrack/caf/templates/caf/applicable_system_create_from_org.html +++ b/ctrack/caf/templates/caf/applicable_system_create_from_org.html @@ -6,7 +6,7 @@
-

Creating form for

+

Creating form for {{ organisation }}

{% crispy form %}
diff --git a/ctrack/caf/templates/caf/caf_list.html b/ctrack/caf/templates/caf/caf_list.html index ce2940e..a5e4bbc 100644 --- a/ctrack/caf/templates/caf/caf_list.html +++ b/ctrack/caf/templates/caf/caf_list.html @@ -28,7 +28,7 @@ diff --git a/ctrack/caf/views.py b/ctrack/caf/views.py index 141c87e..07fd1b1 100644 --- a/ctrack/caf/views.py +++ b/ctrack/caf/views.py @@ -60,10 +60,17 @@ class ApplicableSystemCreateFromOrg(LoginRequiredMixin, CreateView): template_name = "caf/applicable_system_create_from_org.html" def get_context_data(self, **kwargs): - org = Organisation.objects.get(slug=self.kwargs["slug"]) - # TODO - now we have the organisation - we need to pass it to the form or store it in this class. - # We probably need to do return super().get_context_data(**kwargs) here to complete the func. - pass + context = super().get_context_data(**kwargs) + context["organisation"] = Organisation.objects.get(slug=self.kwargs["slug"]) + return context + + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs['org_id'] = Organisation.objects.get(slug=self.kwargs["slug"]).id + return kwargs + + def get_success_url(self): + return reverse_lazy("organisations:detail", args=[self.kwargs['slug']]) class ApplicableSystemCreate(LoginRequiredMixin, CreateView): -- cgit v1.2.3