diff options
Diffstat (limited to '')
-rw-r--r-- | ctrack/caf/templates/caf/caf_detail.html | 2 | ||||
-rw-r--r-- | ctrack/register/forms.py | 92 | ||||
-rw-r--r-- | ctrack/register/urls.py | 5 | ||||
-rw-r--r-- | ctrack/register/views.py | 28 |
4 files changed, 95 insertions, 32 deletions
diff --git a/ctrack/caf/templates/caf/caf_detail.html b/ctrack/caf/templates/caf/caf_detail.html index 500ae62..4e0e2e5 100644 --- a/ctrack/caf/templates/caf/caf_detail.html +++ b/ctrack/caf/templates/caf/caf_detail.html @@ -134,7 +134,7 @@ <div class="col-md-12"> <div class="card bg-light"> <div class="card-body"> - <div class="card-title text-muted">CAF History <a href="#" class="btn btn-outline-primary btn-sm float-right">Add + <div class="card-title text-muted">CAF History <a href="{% url "register:create_from_caf" object.pk %}" class="btn btn-outline-primary btn-sm float-right">Add new...</a></div> <div class="table table-responsive"> <table class="table"> diff --git a/ctrack/register/forms.py b/ctrack/register/forms.py index 4da9be9..4f91bd8 100644 --- a/ctrack/register/forms.py +++ b/ctrack/register/forms.py @@ -4,43 +4,75 @@ from django import forms from django.shortcuts import get_object_or_404 from django.urls import reverse +from ctrack.caf.models import CAF from ctrack.organisations.models import Person, Organisation from ctrack.register.models import EngagementEvent, EngagementType class EngagementEventCreateForm(forms.ModelForm): - def __init__(self, org_slug, user, *args, **kwargs): + def __init__(self, user, caf=None, org_slug=None, *args, **kwargs): super().__init__(*args, **kwargs) - org = get_object_or_404(Organisation, slug=org_slug) - cancel_redirect = reverse("core:home") - self.fields["participants"].queryset = Person.objects.filter(organisation__slug=org_slug) - self.fields["type"].queryset = EngagementType.objects.all().order_by("descriptor") - self.fields["related_caf"].queryset = org.caf_set.all() - self.fields["related_caf"].label = "Related CAFs" - self.helper = FormHelper(self) - self.helper.layout = Layout( - Field("type"), - "short_description", - "participants", - "related_caf", - # "user", - Hidden("user", "none"), - "date", - "end_date", - "response_date_requested", - "response_received", - "document_link", - "comments", - ButtonHolder( - Submit("submit", "Submit", css_class="btn-primary"), - Button( - "cancel", - "Cancel", - onclick=f"location.href='{cancel_redirect}';", - css_class="btn-danger", + if caf: + org = CAF.objects.get(pk=caf).organisation + cancel_redirect = reverse("core:home") + self.fields["related_caf"].initial = caf + self.fields["participants"].queryset = Person.objects.filter(organisation__pk=org.pk) + self.fields["type"].queryset = EngagementType.objects.all().order_by("descriptor") + self.helper = FormHelper(self) + self.helper.layout = Layout( + Field("type"), + "short_description", + "participants", + "related_caf", + # "user", + Hidden("user", "none"), + "date", + "end_date", + "response_date_requested", + "response_received", + "document_link", + "comments", + ButtonHolder( + Submit("submit", "Submit", css_class="btn-primary"), + Button( + "cancel", + "Cancel", + onclick=f"location.href='{cancel_redirect}';", + css_class="btn-danger", + ), ), - ), - ) + ) + else: + org = get_object_or_404(Organisation, slug=org_slug) + cancel_redirect = reverse("core:home") + self.fields["participants"].queryset = Person.objects.filter(organisation__slug=org_slug) + self.fields["type"].queryset = EngagementType.objects.all().order_by("descriptor") + self.fields["related_caf"].queryset = org.caf_set.all() + self.fields["related_caf"].label = "Related CAFs" + self.helper = FormHelper(self) + self.helper.layout = Layout( + Field("type"), + "short_description", + "participants", + "related_caf", + # "user", + Hidden("user", "none"), + "date", + "end_date", + "response_date_requested", + "response_received", + "document_link", + "comments", + ButtonHolder( + Submit("submit", "Submit", css_class="btn-primary"), + Button( + "cancel", + "Cancel", + onclick=f"location.href='{cancel_redirect}';", + css_class="btn-danger", + ), + ), + ) def save(self, commit=True): ee = super().save(commit=False) diff --git a/ctrack/register/urls.py b/ctrack/register/urls.py index b650402..c5afb73 100644 --- a/ctrack/register/urls.py +++ b/ctrack/register/urls.py @@ -1,6 +1,6 @@ from django.urls import path -from ctrack.register.views import EngagementEventCreate, EngagementEventDelete +from ctrack.register.views import EngagementEventCreate, EngagementEventDelete, EngagementEventCreateFromCaf app_name = "register" @@ -10,5 +10,8 @@ urlpatterns = [ ), path( "engagement-event/delete/<int:pk>/for-org/<slug:slug>", view=EngagementEventDelete.as_view(), name="ee_delete" + ), + path( + "engagement-event/create/from-caf/<int:caf_id>", view=EngagementEventCreateFromCaf.as_view(), name="create_from_caf" ) ] diff --git a/ctrack/register/views.py b/ctrack/register/views.py index 5a0d84f..eb2d748 100644 --- a/ctrack/register/views.py +++ b/ctrack/register/views.py @@ -4,6 +4,7 @@ from django.http import HttpResponseRedirect from django.urls import reverse_lazy from django.views.generic import FormView, DeleteView +from ctrack.caf.models import CAF from ctrack.organisations.models import Organisation from ctrack.register.forms import EngagementEventCreateForm from ctrack.register.models import EngagementEvent @@ -42,3 +43,30 @@ class EngagementEventCreate(LoginRequiredMixin, FormView): def get_success_url(self): return reverse_lazy("organisations:detail", args=[self.kwargs["slug"]]) + + +class EngagementEventCreateFromCaf(LoginRequiredMixin, FormView): + fields = "__all__" + form_class = EngagementEventCreateForm + template_name = "register/engagementevent_form.html" + + def form_valid(self, form): + ee = form.save(commit=False) + ee.user = self.request.user + form.save() + return HttpResponseRedirect(self.get_success_url()) + + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs["org_slug"] = self.kwargs.get("slug") + kwargs["user"] = get_user_model() + kwargs["caf"] = self.kwargs.get("caf_id") + return kwargs + + def get_context_data(self, **kwargs): + context = super().get_context_data() + return context + + def get_success_url(self): + org_slug = CAF.objects.get(pk=self.kwargs["caf_id"]).organisation.slug + return reverse_lazy("organisations:detail", args=[org_slug]) |