diff options
author | MR Lemon <matt@matthewlemon> | 2020-04-21 16:09:32 +0100 |
---|---|---|
committer | MR Lemon <matt@matthewlemon> | 2020-04-21 16:09:32 +0100 |
commit | 271409371dc71b6c9108b2e56cb82ff8ce74415f (patch) | |
tree | 7302833703b22d2e0c8fe5cef2a181da59a26c6d /ctrack/caf | |
parent | 563ccf1d64a3bdc81d6d1fe9c45b95684d5a3868 (diff) |
can now add a system from a CAF detail page
Diffstat (limited to 'ctrack/caf')
-rw-r--r-- | ctrack/caf/forms.py | 31 | ||||
-rw-r--r-- | ctrack/caf/templates/caf/applicable_system_create_from_caf.html | 16 | ||||
-rw-r--r-- | ctrack/caf/templates/caf/caf_detail.html | 2 | ||||
-rw-r--r-- | ctrack/caf/urls.py | 3 | ||||
-rw-r--r-- | ctrack/caf/views.py | 23 |
5 files changed, 71 insertions, 4 deletions
diff --git a/ctrack/caf/forms.py b/ctrack/caf/forms.py index f470dae..f79e56a 100644 --- a/ctrack/caf/forms.py +++ b/ctrack/caf/forms.py @@ -16,6 +16,37 @@ CAFCreateInlineFormset = inlineformset_factory( CAF, ApplicableSystem, fields=("name", "organisation"), extra=2) +class ApplicableSystemCreateFromCafForm(forms.Form): + name = forms.CharField(max_length=255) + description = forms.CharField(widget=forms.Textarea) + organisation = forms.ModelChoiceField(queryset=Organisation.objects.all()) + caf = forms.ModelChoiceField(queryset=CAF.objects.all()) + + def __init__(self, *args, **kwargs): + # We must pop the kwargs before we pass to super() + # https://stackoverflow.com/a/8973101 + caf_id = kwargs.pop("caf_id") + org_id = kwargs.pop("org_id") + super().__init__(*args, **kwargs) + caf = CAF.objects.get(pk=caf_id) + cancel_redirect = reverse("caf:detail", args=[caf_id]) + self.fields['caf'].queryset = CAF.objects.filter(pk=caf_id) + self.helper = FormHelper(self) + self.helper.layout = Layout( + Fieldset( + f"Create a new system for {caf}", + Field("name", css_class="for-control form-control-sm"), + Field("description", cass_class="form-control form-control-sm"), + Hidden("caf", caf_id), + Hidden("organisation", org_id), + ), + ButtonHolder( + Submit("submit", "Submit", css_class="btn-primary"), + Button("cancel", "Cancel", onclick=f"location.href='{cancel_redirect}';", css_class="btn-danger") + ) + ) + + class ApplicableSystemCreateFromOrgForm(forms.Form): name = forms.CharField(max_length=255) description = forms.CharField(widget=forms.Textarea) diff --git a/ctrack/caf/templates/caf/applicable_system_create_from_caf.html b/ctrack/caf/templates/caf/applicable_system_create_from_caf.html new file mode 100644 index 0000000..fffb5f4 --- /dev/null +++ b/ctrack/caf/templates/caf/applicable_system_create_from_caf.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} +{% block title %} + Create a new Applicable System to an existing CAF +{% endblock title %} + +{% load crispy_forms_tags %} + +{% block content %} + <div class="container"> + <div class="row"> + <div class="col-sm-6"> + {% crispy form %} + </div> + </div> + </div> +{% endblock content %} diff --git a/ctrack/caf/templates/caf/caf_detail.html b/ctrack/caf/templates/caf/caf_detail.html index eea5c3a..e301b54 100644 --- a/ctrack/caf/templates/caf/caf_detail.html +++ b/ctrack/caf/templates/caf/caf_detail.html @@ -71,7 +71,7 @@ <div class="col-sm-12 p-2"> <div class="card bg-light"> <div class="card-body"> - <div class="card-title text-muted">Applicable Systems</div> + <div class="card-title text-muted">Applicable Systems | <small><a href="{% url "caf:as_create_from_caf" object.pk %}">Add new...</a></small></div> <div class="card-text"> <div> <table class="table-sm table-responsive"> diff --git a/ctrack/caf/urls.py b/ctrack/caf/urls.py index 5b64be4..ca84609 100644 --- a/ctrack/caf/urls.py +++ b/ctrack/caf/urls.py @@ -2,7 +2,7 @@ from django.urls import path from django.views.decorators.cache import cache_page from ctrack.caf.views import ListCAF, ListApplicableSystem, caf_detail_view, ApplicableSystemDetail, \ - ApplicableSystemCreateFromOrg + ApplicableSystemCreateFromOrg, applicable_system_create_from_caf app_name = "caf" @@ -11,5 +11,6 @@ urlpatterns = [ path("applicablesystems", cache_page(60 * 60)(ListApplicableSystem.as_view()), name="es_list"), path("applicablesystems/<int:pk>", ApplicableSystemDetail.as_view(), name="ass_detail"), path("applicablesystem/<slug:slug>", ApplicableSystemCreateFromOrg.as_view(), name="create_from_org"), + path("applicablesystem/create-from-caf/<int:caf_id>", applicable_system_create_from_caf, name="as_create_from_caf"), path("<int:pk>", caf_detail_view, name="detail") ] diff --git a/ctrack/caf/views.py b/ctrack/caf/views.py index ded9233..251024d 100644 --- a/ctrack/caf/views.py +++ b/ctrack/caf/views.py @@ -1,10 +1,11 @@ from django.contrib.auth.mixins import LoginRequiredMixin +from django.http import HttpResponseRedirect from django.shortcuts import render -from django.urls import reverse_lazy +from django.urls import reverse_lazy, reverse from django.views.generic import ListView, DetailView, FormView from ctrack.assessments.models import CAFAssessmentOutcomeScore -from ctrack.caf.forms import ApplicableSystemCreateFromOrgForm +from ctrack.caf.forms import ApplicableSystemCreateFromOrgForm, ApplicableSystemCreateFromCafForm from ctrack.caf.models import ApplicableSystem, CAF from ctrack.organisations.models import Organisation @@ -54,6 +55,24 @@ class ApplicableSystemDetail(LoginRequiredMixin, DetailView): template_name = "caf/applicablesystem_detail.html" +def applicable_system_create_from_caf(request, caf_id): + org_id = CAF.objects.get(pk=caf_id).organisation().id + if request.method=="POST": + form = ApplicableSystemCreateFromCafForm(request.POST, caf_id=caf_id, org_id=org_id) + if form.is_valid(): + ApplicableSystem.objects.create( + name=form.cleaned_data["name"], + description=form.cleaned_data["description"], + caf=form.cleaned_data["caf"], + organisation=form.cleaned_data["organisation"] + ) + return HttpResponseRedirect(reverse("caf:detail", args=[caf_id])) + else: + form = ApplicableSystemCreateFromCafForm(caf_id=caf_id, org_id=org_id) + + return render(request, "caf/applicable_system_create_from_caf.html", {"form": form}) + + class ApplicableSystemCreateFromOrg(LoginRequiredMixin, FormView): form_class = ApplicableSystemCreateFromOrgForm template_name = "caf/applicable_system_create_from_org.html" |