summaryrefslogtreecommitdiffstats
path: root/engagements/views.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-04-23 11:16:38 +0100
committerMatthew Lemon <y@yulqen.org>2024-04-23 11:16:38 +0100
commit0f951dcf029d4af284467543a3afdf5bf6581a20 (patch)
treea48384210cdc168e3bd3ccff6d6d516eeed9e748 /engagements/views.py
parent8b084e9fe7a5f3a04c32daf9a24f7f2cf67300f9 (diff)
switched to Django
Diffstat (limited to 'engagements/views.py')
-rw-r--r--engagements/views.py182
1 files changed, 182 insertions, 0 deletions
diff --git a/engagements/views.py b/engagements/views.py
new file mode 100644
index 0000000..fb804df
--- /dev/null
+++ b/engagements/views.py
@@ -0,0 +1,182 @@
+from django.contrib.auth.decorators import login_required
+from django.contrib.auth.mixins import LoginRequiredMixin
+from django.db.models import Q
+from django.shortcuts import get_object_or_404, redirect, render
+from django.views.generic import ListView
+
+from instruments.models import SubInstrument
+
+from .forms import (
+ EngagementCreateForm,
+ EngagementEffortPlanningCreateForm,
+ EngagementEffortRegulationCreateForm,
+ EngagementEffortReportingCreateForm,
+ EngagementEffortTravelCreateForm,
+)
+from .models import Engagement, EngagementEffort, EngagementType, Organisation
+
+
+class RegulatedEntitiesView(ListView, LoginRequiredMixin):
+ context_object_name = "entities"
+ queryset = Organisation.objects.filter(is_regulated_entity=True).order_by("name")
+ template_name = "engagements/organisations.html"
+
+
+@login_required
+def engagement_planning(request):
+ user = request.user
+ reg_orgs = Organisation.objects.filter(is_regulated_entity=True)
+ my_orgs = reg_orgs.filter(lead_inspector=user)
+ return render(request, "engagements/index.html", {"entities": my_orgs})
+ # display the list and ask which one?
+
+
+@login_required
+def htmx_effort_planned(request, effid):
+ if request.method == "GET":
+ effort = EngagementEffort.objects.get(id=effid)
+ if effort.is_planned is True:
+ effort.is_planned = False
+ else:
+ effort.is_planned = True
+ effort.save()
+ return render(request, "engagements/snippets/effort_summary_panel.html", {"e" : effort})
+
+
+@login_required
+def engagement_detail(request, pk):
+ engagement = Engagement.objects.get(pk=pk)
+ subinstruments = SubInstrument.objects.filter(title__icontains="DSC")
+ effort = EngagementEffort.objects.filter(engagement=engagement).order_by("proposed_start_date")
+ dscs = []
+ for e in effort:
+ subs = e.sub_instruments.all()
+ for s in subs:
+ dscs.append(s)
+ dscs = set(dscs)
+ effort_total = sum(e.effort_total_hours() for e in effort)
+ effort_planned = sum(e.effort_total_planned_hours() for e in effort)
+ effort_actual = sum(e.effort_actual() for e in effort)
+ context = {
+ "engagement": engagement,
+ "subinstruments": subinstruments,
+ "effort": effort,
+ "effort_total": effort_total,
+ "effort_planned": effort_planned,
+ "effort_actual": effort_actual,
+ "dscs": dscs,
+ }
+ return render(request, "engagements/engagement_detail.html", context)
+
+
+@login_required
+def engagement_plan_for(request, orgslug):
+ org = Organisation.objects.get(slug=orgslug)
+ engagements = Engagement.objects.filter(external_party=org)
+ dscs = SubInstrument.objects.filter(itype="DSC").filter(effort__engagement__external_party=org).distinct()
+ context = {"entity": org, "engagements": engagements, "dscs": dscs}
+ return render(request, "engagements/ep_org.html", context)
+
+
+# class EngagementView(ListView, LoginRequiredMixin):
+# context_object_name = "engagements"
+# queryset = Engagement.objects.filter(engagement_type__name="INSPECTION")
+# template_name = "engagements/index.html"
+
+
+@login_required
+def engagement_effort_create(request, eid, etype=None):
+ forms = {
+ "TRAVEL": EngagementEffortTravelCreateForm,
+ "PLANNING": EngagementEffortPlanningCreateForm,
+ "REGULATION": EngagementEffortRegulationCreateForm,
+ "REPORTING": EngagementEffortReportingCreateForm,
+ }
+
+ if request.method == "POST":
+ engagement = Engagement.objects.get(pk=eid)
+ # use the specialised type of form on POST - it has the correct fields
+ form = forms[etype](request.POST)
+ if form.is_valid():
+ eff = form.save(commit=False)
+ eff.engagement = engagement
+ eff.save()
+ eff.officers.add(request.user)
+ eff.effort_type = etype
+ eff.save()
+ form.save_m2m()
+ return redirect("engagements:engagement_detail", pk=eid)
+ else:
+ engagement = Engagement.objects.get(pk=eid)
+ form = forms.get(etype, EngagementCreateForm)(
+ initial={
+ "proposed_start_date": engagement.proposed_start_date.isoformat(),
+ "engagement": engagement,
+ "effort_type": etype,
+ "officers": request.user,
+ },
+ )
+ return render(
+ request,
+ "engagements/engagement_effort_create.html",
+ {"form": form, "engagement": engagement, "etype": etype},
+ )
+
+
+@login_required
+def engagement_edit(request, eid):
+ e = get_object_or_404(Engagement, pk=eid)
+ form = EngagementCreateForm(request.POST or None, instance=e)
+ if request.method == "POST":
+ if form.is_valid():
+ form.save()
+ return redirect("engagements:engagement_detail", pk=e.pk)
+ return render(
+ request,
+ "engagements/engagement_form.html",
+ {"form": form, "title": f"Edit Engagement {e}"},
+ )
+
+
+@login_required
+def engagement_create(request, slug, reg=None):
+ if request.method == "POST":
+ form = EngagementCreateForm(request.POST)
+ if form.is_valid():
+ ef = form.save(commit=False)
+ ef.external_party = Organisation.objects.get(slug=slug)
+ ef.save()
+ return redirect("engagements:plan_for_org", orgslug=slug)
+ else:
+ if reg:
+ form = EngagementCreateForm(
+ initial={
+ "engagement_type": EngagementType.objects.filter(
+ Q(name="ASSESSMENT")
+ | Q(name="INSPECTION")
+ | Q(name="SAMPLING")
+ | Q(name="L1RIF")
+ | Q(name="L2RIF")
+ | Q(name="L3RIF")
+ | Q(name="L1RIF")
+ ),
+ "external_party": Organisation.objects.get(slug=slug),
+ "officers": request.user,
+ }
+ )
+ return render(
+ request,
+ "engagements/engagement_form.html",
+ {"form": form, "title": "Add New Regulatory Engagement"},
+ )
+ else:
+ form = EngagementCreateForm(
+ initial={
+ "officers": request.user,
+ }
+ )
+ return render(
+ request,
+ "engagements/engagement_form.html",
+ {"form": form, "title": "Add New Engagement"},
+ )