Files
PyRIGS/training/views.py
AJ 08171767a0 Port to Django 5.2 (#635)
* Port to Django 5.2

* Pin pluggy to 1.2.0

Any newer and the mystery importlib metadata error appears. Weird! >_>

* Update for premailer changed default

* Update view logic for is_ajax being changed to a template context processor

* Port a few more tests to pytest proper

Having two distinct test flavours is giving me a headache

* Version 1 dockerfile

Makes a VERY big image, I suspect we can optimise this a lot...

* Optimise dockerfile a little lot a bit

* fix(users): change logout link to POST request

* fix(tests): fix some syntax errors in test code

still got lots of failed tests :(

* fix: replace deprecated Django APIs

* ci: update Dockerfile

* ci: update Dockerfile

* fix(dependencies+tests): update EoL/vulnerable dependencies and improve test stability

* Upgrade Python from 3.10 to 3.12
* Update frontend dependencies, replace node-sass with sass

* chore(logging): ignore dangling obj reference warning from pypdf

* ci: update compose.yml for prod deployment

- Add Nginx as a reverse proxy
- Add cert-selfsign.sh for generating self-signed certificates
- Add certbot-issue.sh for Let's Encrypt certificate issuance and renewal
- Add cron-install.sh and cron-uninstall.sh for system cron management
- Add .env.example as an environment variable template

* style: reformat code

* ci: add more sleep trying to pass tests

* ci: ignore browser-based tests during ci testing

* fix: create home for the new user during Dockerfile building

* chore: remove heroku conf file

* fix: RIGS not franken anymore

* fix: restore is_ajax as a boolean and split out the context processor

* test: remove Selenium interaction tests and their CI workarounds

* fix: harden production settings (CSRF origins, env casts, ADMINS) and drop dead debug toolbar code

* deploy: serve static files from nginx, pin PGDATA, and make cert scripts loud on failure

* build: switch .dockerignore to an allowlist

* build: drop unused dependencies, soft-pin the rest and target Python 3.14

* build: move image to Python 3.14 / Node 24 and force DEBUG off

* ci: add dependabot config for uv, npm, docker and actions

* build: narrow Sass deprecation silencing to @import and require Node 24

* build: provide placeholder env for collectstatic now that DEBUG is off in the image

* deploy: add plain-HTTP nginx config for local development and use it in compose

* deploy: remove self-signed cert script

* build: make the image multi-arch with official node and python base images

* ci: lint and test on PRs, build the image on PRs and push it to GHCR on master

* docs: add local running and sample data instructions

* fix: report a form error instead of crashing when big power has no Power MIC

* fix: upgrade pypdf and urllib3 to patched releases

* fix: apply non-breaking npm audit fixes

* fix: strip whitespace in ALLOWED_HOSTS and drop misleading HSTS preload

* deploy: add a pyrigs healthcheck and make nginx wait for it

* build: pin the uv image version

* fix: correct the check-in person picker condition and use BeautifulSoup's string argument

* refactor: replace unique_together with UniqueConstraint

* build: replace pycodestyle with ruff and fix what it found

Removes unused imports and variables, and fixes a few real problems it
surfaced:
- EventCheckIn.active() referenced an undefined name and raised NameError;
  it now returns whether the check-in has no end time
- RIGS.admin defined EventChecklistAdmin twice; the second is now
  PowerTestRecordAdmin
- RIGS/tests/conftest.py used date/timedelta without importing them
- the signal-registering imports in apps.py are kept with noqa

pycodestyle config in setup.cfg is dropped.

* style: format the codebase with ruff

* style: normalise line endings, trailing whitespace and end-of-file newlines

* ci: run ruff and file hygiene through prek, and document it

---------

Co-authored-by: Hang <me@hangxu.me>
Co-authored-by: Joe Banks <joe@jb3.dev>
2026-10-01 23:43:41 +01:00

297 lines
11 KiB
Python

import reversion
from django.urls import reverse_lazy
from django.views import generic
from django.utils import timezone
from django.db import transaction
from django.db.models import Q, Count
from django.db.utils import IntegrityError
from PyRIGS.views import is_ajax, ModalURLMixin, get_related, PrintListView
from training import models, forms
from users import views
class ItemList(generic.ListView):
template_name = "item_list.html"
model = models.TrainingItem
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_title"] = "Training Items"
context["categories"] = models.TrainingCategory.objects.all()
return context
class ItemListExport(PrintListView):
model = models.TrainingItem
template_name = "item_list.xml"
def get_queryset(self):
return self.model.objects.filter(active=True)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["filename"] = "TrainingItemList.pdf"
context["categories"] = models.TrainingCategory.objects.all()
return context
class TraineeDetail(views.ProfileDetail):
template_name = "trainee_detail.html"
model = models.Trainee
def get_queryset(self):
return self.model.objects.prefetch_related("qualifications_obtained")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.request.user.pk == self.object.pk:
context["page_title"] = "Your Training Record"
else:
context["page_title"] = f"{self.object.get_full_name()}'s Training Record"
context["started_levels"] = self.object.started_levels()
context["completed_levels"] = self.object.level_qualifications.all()
context["categories"] = models.TrainingCategory.objects.all().prefetch_related("items")
return context
class TraineeItemDetail(generic.ListView):
model = models.TrainingItemQualification
template_name = "trainee_item_list.html"
def get_queryset(self):
return (
models.Trainee.objects.get(pk=self.kwargs["pk"])
.qualifications_obtained.search(self.request.GET.get("q"))
.order_by("item__category__reference_number", "item__reference_number")
.select_related("item", "trainee")
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
trainee = models.Trainee.objects.get(pk=self.kwargs["pk"])
context["trainee"] = models.Trainee.objects.get(pk=self.kwargs["pk"])
context["page_title"] = f"Detailed Training Record for <a href='{trainee.get_absolute_url()}'>{trainee}</a>"
return context
class LevelDetail(generic.DetailView):
template_name = "level_detail.html"
model = models.TrainingLevel
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_title"] = f"Training Level {self.object} {self.object.get_icon}"
context["users_with"] = map(
lambda qual: qual.trainee, models.TrainingLevelQualification.objects.filter(level=self.object)
)
context["u"] = models.Trainee.objects.get(pk=self.kwargs["u"]) if "u" in self.kwargs else self.request.user
return context
class LevelList(generic.ListView):
model = models.TrainingLevel
template_name = "level_list.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_title"] = "All Training Levels"
return context
class TraineeList(generic.ListView):
model = models.Trainee
template_name = "trainee_list.html"
paginate_by = 25
def get_queryset(self):
objects = self.model.objects.search(self.request.GET.get("q"))
if self.request.GET.get("is_supervisor", ""):
objects = objects.filter(is_supervisor=True)
return (
objects.annotate(
num_qualifications=Count(
"qualifications_obtained",
filter=Q(qualifications_obtained__depth=models.TrainingItemQualification.PASSED_OUT),
)
)
.order_by("-num_qualifications")
.prefetch_related("level_qualifications", "qualifications_obtained", "qualifications_obtained__item")
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_title"] = "Training Profile List"
return context
class AddQualification(generic.CreateView, ModalURLMixin):
template_name = "edit_training_record.html"
model = models.TrainingItemQualification
form_class = forms.AddQualificationForm
def form_valid(self, form, *args, **kwargs):
reversion.add_to_revision(form.cleaned_data["trainee"])
return super().form_valid(form, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["depths"] = models.TrainingItemQualification.CHOICES
if is_ajax(self.request):
context["override"] = "base_ajax.html"
else:
context["override"] = "base_training.html"
trainee = models.Trainee.objects.get(pk=self.kwargs["pk"])
context["page_title"] = f"Add Qualification for {trainee}"
get_related(context["form"], context)
return context
def get_success_url(self):
return self.get_close_url("add_qualification", "trainee_detail")
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["pk"] = self.kwargs["pk"]
return kwargs
class EditQualification(generic.UpdateView, ModalURLMixin):
template_name = "edit_training_record.html"
model = models.TrainingItemQualification
form_class = forms.QualificationForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["depths"] = models.TrainingItemQualification.CHOICES
context["page_title"] = f"Edit Qualification <em>{self.object}</em> for {self.object.trainee}"
get_related(context["form"], context)
return context
@transaction.atomic()
@reversion.create_revision()
def form_valid(self, form, *args, **kwargs):
reversion.add_to_revision(form.cleaned_data["trainee"])
return super().form_valid(form, *args, **kwargs)
def get_success_url(self):
return self.get_close_url("edit_qualification", "trainee_item_detail")
class AddLevelRequirement(generic.CreateView, ModalURLMixin):
template_name = "add_level_requirement.html"
model = models.TrainingLevelRequirement
form_class = forms.RequirementForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
level = models.TrainingLevel.objects.get(pk=self.kwargs["pk"])
context["page_title"] = f"Add Requirements to Training Level {level}"
get_related(context["form"], context)
return context
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["pk"] = self.kwargs["pk"]
return kwargs
def get_success_url(self):
return self.get_close_url("level_detail", "level_detail")
@transaction.atomic()
@reversion.create_revision()
def form_valid(self, form, *args, **kwargs):
reversion.add_to_revision(form.cleaned_data["level"])
return super().form_valid(form, *args, **kwargs)
class RemoveRequirement(generic.DeleteView):
model = models.TrainingLevelRequirement
template_name = "traininglevelrequirement_confirm_delete.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_title"] = f"Delete Requirement '{self.object}' from Training Level {self.object.level}?"
return context
def get_success_url(self):
return self.request.POST.get("next")
@transaction.atomic()
@reversion.create_revision()
def delete(self, *args, **kwargs):
reversion.add_to_revision(self.get_object().level)
return super().delete(*args, **kwargs)
class ConfirmLevel(generic.RedirectView):
@transaction.atomic()
@reversion.create_revision()
def get_redirect_url(self, *args, **kwargs):
trainee = models.Trainee.objects.get(pk=kwargs["pk"])
level_qualification, created = models.TrainingLevelQualification.objects.get_or_create(
trainee=trainee, level=models.TrainingLevel.objects.get(pk=kwargs["level_pk"])
)
if created:
user = self.request.user
reversion.set_user(user)
level_qualification.confirmed_by = self.request.user
level_qualification.confirmed_on = timezone.now()
level_qualification.save()
reversion.add_to_revision(trainee)
return reverse_lazy("trainee_detail", kwargs={"pk": kwargs["pk"]})
class SessionLog(generic.FormView):
template_name = "session_log_form.html"
form_class = forms.SessionLogForm
success_url = reverse_lazy("trainee_list")
def form_valid(self, form, *args, **kwargs):
for trainee in form.cleaned_data.get("trainees", []):
for depth in models.TrainingItemQualification.CHOICES:
for item in form.cleaned_data.get(f"items_{depth[0]}", []):
try:
with transaction.atomic():
models.TrainingItemQualification.objects.create(
trainee=trainee,
item=item,
supervisor=form.cleaned_data.get("supervisor"),
depth=depth[0],
notes=form.cleaned_data.get("notes"),
date=form.cleaned_data.get("date"),
)
reversion.add_to_revision(trainee)
except IntegrityError:
pass # There was an attempt to create a duplicate qualification, ignore it
return super().form_valid(form, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["depths"] = models.TrainingItemQualification.CHOICES
context["page_title"] = "Log Training Session"
get_related(context["form"], context)
return context
class ItemQualifications(generic.ListView):
template_name = "item_qualification_list.html"
model = models.TrainingItemQualification
paginate_by = 40
def get_queryset(self):
return (
models.TrainingItemQualification.objects.filter(item=self.kwargs["pk"])
.order_by("-depth")
.select_related("trainee")
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_title"] = f"People Qualified In {models.TrainingItem.objects.get(pk=self.kwargs['pk'])}"
return context