aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/caf
diff options
context:
space:
mode:
authorMR Lemon <matt@matthewlemon>2020-04-20 16:39:16 +0100
committerMR Lemon <matt@matthewlemon>2020-04-20 16:39:16 +0100
commit6c06361481ae9565b3da523b360c480ead48c47e (patch)
treeaccea8eac4fbad4ba37b26e776ccd48c2baa33bb /ctrack/caf
parent324f91f31141fd35ad108142922956893dc4fd3e (diff)
Completed a manual forms.Form to create a new ApplicableSystem - REFERENCE CODE
Diffstat (limited to 'ctrack/caf')
-rw-r--r--ctrack/caf/forms.py12
-rw-r--r--ctrack/caf/views.py10
2 files changed, 15 insertions, 7 deletions
diff --git a/ctrack/caf/forms.py b/ctrack/caf/forms.py
index e9eef3b..f470dae 100644
--- a/ctrack/caf/forms.py
+++ b/ctrack/caf/forms.py
@@ -10,26 +10,24 @@ from django.urls import reverse
from ctrack.caf.models import ApplicableSystem
from ctrack.caf.models import CAF
+from ctrack.organisations.models import Organisation
CAFCreateInlineFormset = inlineformset_factory(
CAF, ApplicableSystem, fields=("name", "organisation"), extra=2)
class ApplicableSystemCreateFromOrgForm(forms.Form):
- choices = ()
name = forms.CharField(max_length=255)
description = forms.CharField(widget=forms.Textarea)
- organisation = forms.ModelChoiceField(queryset=None)
- caf = forms.ChoiceField(choices=(
- choices
- ))
+ organisation = forms.ModelChoiceField(queryset=Organisation.objects.all())
+ caf = forms.ModelChoiceField(queryset=CAF.objects.all())
def __init__(self, org_id, slug, org_name, org_cafs, *args, **kwargs):
super().__init__(*args, **kwargs)
cancel_redirect = reverse("organisations:detail", args=[slug])
- ApplicableSystemCreateFromOrgForm.choices = org_cafs
+ # we need to create the choices we can use for the CAF dropdown in the form
+ self.fields['caf'].queryset = CAF.objects.filter(pk__in=[caf.pk for caf in org_cafs])
self.helper = FormHelper(self)
- self.helper.form_method = "post"
self.helper.layout = Layout(
Fieldset(
f"Create a new system for {org_name}",
diff --git a/ctrack/caf/views.py b/ctrack/caf/views.py
index 2bfe7ad..003cb18 100644
--- a/ctrack/caf/views.py
+++ b/ctrack/caf/views.py
@@ -63,6 +63,16 @@ class ApplicableSystemCreateFromOrg(LoginRequiredMixin, FormView):
context["organisation"] = Organisation.objects.get(slug=self.kwargs["slug"])
return context
+ def form_valid(self, form):
+ ass = ApplicableSystem.objects.create(
+ name=form.cleaned_data["name"],
+ description=form.cleaned_data["description"],
+ organisation=form.cleaned_data["organisation"],
+ caf=form.cleaned_data["caf"]
+ )
+ return super().form_valid(form)
+
+
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
org = Organisation.objects.get(slug=self.kwargs["slug"])