From 2960a465fe565f491863d7043a4662e7f1aa9ae0 Mon Sep 17 00:00:00 2001 From: jonavellecuerdo Date: Tue, 28 Nov 2023 15:47:19 -0500 Subject: [PATCH] [wip] --- pyproject.toml | 2 +- solenoid/people/forms.py | 2 +- solenoid/people/signals.py | 9 ++++++--- solenoid/people/views.py | 26 ++++++++++++++------------ 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3a79bbf..9f798b3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ django_settings_module = "solenoid.settings.base" disallow_untyped_calls = true disallow_untyped_defs = true plugins = ["mypy_django_plugin.main"] -exclude = ["tests"] +exclude = ["tests", "migrations"] [[tool.mypy.overrides]] module = ["solenoid.userauth.*", "solenoid.settings.*", "celery_progress.backend"] diff --git a/solenoid/people/forms.py b/solenoid/people/forms.py index d399c11..55e4180 100644 --- a/solenoid/people/forms.py +++ b/solenoid/people/forms.py @@ -8,7 +8,7 @@ class Meta: model = Liaison fields = ("first_name", "last_name", "email_address") - def __init__(self, *args, **kwargs): + def __init__(self, *args, **kwargs): # type: ignore[no-untyped-def] super(LiaisonCreateForm, self).__init__(*args, **kwargs) self.fields["dlc"] = forms.ModelMultipleChoiceField( queryset=DLC.objects.all(), required=False diff --git a/solenoid/people/signals.py b/solenoid/people/signals.py index d9f4b18..5dad428 100644 --- a/solenoid/people/signals.py +++ b/solenoid/people/signals.py @@ -1,12 +1,15 @@ from django.db.models.signals import post_save +from django.db import models from django.dispatch import receiver from solenoid.emails.models import EmailMessage -from .models import DLC +from .models import DLC, Liaison -def update_emails_with_dlcs(dlcs, liaison=None): +def update_emails_with_dlcs( + dlcs: models.QuerySet | list, liaison: Liaison | None = None +) -> None: """ For a given liaison and set of DLCs, update all unsent EmailMessages associated with those DLCs to have that Liaison. @@ -25,6 +28,6 @@ def update_emails_with_dlcs(dlcs, liaison=None): @receiver(post_save, sender=DLC) -def update_emails_with_dlcs_on_save(sender, **kwargs): +def update_emails_with_dlcs_on_save(sender: models.Model, **kwargs) -> None: # type: ignore[no-untyped-def] dlc = kwargs["instance"] update_emails_with_dlcs([dlc], dlc.liaison) diff --git a/solenoid/people/views.py b/solenoid/people/views.py index 8f145f8..8c6cf3b 100644 --- a/solenoid/people/views.py +++ b/solenoid/people/views.py @@ -1,8 +1,10 @@ import logging +from typing import Any + from django.contrib import messages from django.urls import reverse_lazy -from django.http import HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic.list import ListView @@ -21,7 +23,7 @@ class LiaisonCreate(ConditionalLoginRequiredMixin, CreateView): form_class = LiaisonCreateForm success_url = reverse_lazy("people:liaison_list") - def get_context_data(self, **kwargs): + def get_context_data(self, **kwargs): # type: ignore[no-untyped-def] context = super(LiaisonCreate, self).get_context_data(**kwargs) context["title"] = "Add liaison" context["breadcrumbs"] = [ @@ -32,7 +34,7 @@ def get_context_data(self, **kwargs): context["form_id"] = "liaison-create" return context - def form_valid(self, form): + def form_valid(self, form: LiaisonCreateForm) -> HttpResponseRedirect: liaison = form.save() dlcs = form.cleaned_data["dlc"] liaison.dlc_set.add(*dlcs) @@ -44,7 +46,7 @@ class LiaisonList(ConditionalLoginRequiredMixin, ListView): model = Liaison queryset = Liaison.objects.all() - def get_context_data(self, **kwargs): + def get_context_data(self, **kwargs): # type: ignore[no-untyped-def] context = super(LiaisonList, self).get_context_data(**kwargs) context["dlc_set"] = DLC.objects.all() context["title"] = "Manage liaisons" @@ -62,7 +64,7 @@ class LiaisonUpdate(ConditionalLoginRequiredMixin, UpdateView): form_class = LiaisonCreateForm success_url = reverse_lazy("people:liaison_list") - def get_context_data(self, **kwargs): + def get_context_data(self, **kwargs): # type: ignore[no-untyped-def] context = super(LiaisonUpdate, self).get_context_data(**kwargs) context["title"] = "Edit liaison" context["breadcrumbs"] = [ @@ -73,12 +75,12 @@ def get_context_data(self, **kwargs): context["form_id"] = "liaison-update" return context - def get_initial(self): + def get_initial(self) -> dict[str, Any]: initial = super(LiaisonUpdate, self).get_initial() initial["dlc"] = DLC.objects.filter(liaison=self.get_object()) return initial - def post(self, request, *args, **kwargs): + def post(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def] # This would normally be set in post(), but we're not calling super, so # we need to do it ourselves. self.object = self.get_object() @@ -88,8 +90,8 @@ def post(self, request, *args, **kwargs): if form.is_valid(): try: dlcs = DLC.objects.filter(pk__in=request.POST.getlist("dlc")) - except KeyError: - logger.exception() + except KeyError as e: + logger.exception(e) raise liaison = self.get_object() @@ -112,11 +114,11 @@ def post(self, request, *args, **kwargs): return self.form_invalid(form) -class LiaisonDelete(ConditionalLoginRequiredMixin, DeleteView): +class LiaisonDelete(ConditionalLoginRequiredMixin, DeleteView): # type: ignore model = Liaison queryset = Liaison.objects.all() - def get_context_data(self, **kwargs): + def get_context_data(self, **kwargs) -> dict[str, Any]: # type: ignore[no-untyped-def] context = super(LiaisonDelete, self).get_context_data(**kwargs) context["title"] = "Delete liaison ({name})".format(name=self.get_object()) context["breadcrumbs"] = [ @@ -126,6 +128,6 @@ def get_context_data(self, **kwargs): ] return context - def get_success_url(self): + def get_success_url(self) -> Any: messages.success(self.request, "Liaison deleted.") return reverse_lazy("people:liaison_list")