From 04e8fa22dc488cb03a0c1ac3013bf65ebe0aac0e Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Mon, 5 Oct 2020 10:36:34 +0100 Subject: very bad implementation for form to create event from caf page... --- ctrack/register/forms.py | 92 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 30 deletions(-) (limited to 'ctrack/register/forms.py') 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) -- cgit v1.2.3