From 4c78e1bf365f7600661cc19d66ece09c120d2284 Mon Sep 17 00:00:00 2001 From: jonavellecuerdo Date: Tue, 28 Nov 2023 16:56:16 -0500 Subject: [PATCH] [wip] --- pyproject.toml | 2 +- solenoid/emails/forms.py | 5 ++--- solenoid/emails/views.py | 16 +++++++++------- solenoid/records/views.py | 22 +++++++++------------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9f798b3..5afa7cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,7 @@ plugins = ["mypy_django_plugin.main"] exclude = ["tests", "migrations"] [[tool.mypy.overrides]] -module = ["solenoid.userauth.*", "solenoid.settings.*", "celery_progress.backend"] +module = ["solenoid.userauth.*", "solenoid.settings.*", "celery_progress.backend", "ckeditor.widgets"] disallow_untyped_calls = false disallow_untyped_defs = false ignore_errors = true diff --git a/solenoid/emails/forms.py b/solenoid/emails/forms.py index 5311453..2f0b1bb 100644 --- a/solenoid/emails/forms.py +++ b/solenoid/emails/forms.py @@ -1,5 +1,4 @@ from ckeditor.widgets import CKEditorWidget - from django import forms from .models import EmailMessage @@ -11,10 +10,10 @@ class Meta: fields = ["latest_text"] widgets = {"latest_text": CKEditorWidget()} - def __init__(self, *args, **kwargs): + def __init__(self, *args, **kwargs): # type: ignore[no-untyped-def] super(EmailMessageForm, self).__init__(*args, **kwargs) self.fields["latest_text"].label = "" - def save(self, *args, **kwargs): + def save(self, *args, **kwargs): # type: ignore[no-untyped-def] self.instance.new_citations = False super(EmailMessageForm, self).save() diff --git a/solenoid/emails/views.py b/solenoid/emails/views.py index 0b7f4f5..e6feb27 100644 --- a/solenoid/emails/views.py +++ b/solenoid/emails/views.py @@ -1,13 +1,14 @@ import logging from django.contrib import messages -from django.urls import reverse from django.db import close_old_connections, connection -from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden +from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect from django.shortcuts import get_object_or_404 -from django.views.generic import View, DetailView +from django.urls import reverse +from django.views.generic import DetailView, View from django.views.generic.edit import UpdateView from django.views.generic.list import ListView +from requests.models import Request from solenoid.people.models import Author from solenoid.records.models import Record @@ -16,11 +17,10 @@ from .forms import EmailMessageForm from .models import EmailMessage - logger = logging.getLogger(__name__) -def _get_or_create_emails(pk_list): +def _get_or_create_emails(pk_list: list) -> list: """Takes a list of pks of Records and gets or creates associated EmailMessages.""" close_old_connections() @@ -28,7 +28,9 @@ def _get_or_create_emails(pk_list): for author in Author.objects.filter(record__pk__in=pk_list).distinct(): records = Record.objects.filter(pk__in=pk_list, author=author) - email_pks.append(EmailMessage.get_or_create_for_records(records).pk) + + if email := EmailMessage.get_or_create_for_records(records): + email_pks.append(email.pk) # Because this is outside the request/response cycle, the connections # opened here don't close at the end of the function. They may be cleaned @@ -42,7 +44,7 @@ def _get_or_create_emails(pk_list): class EmailCreate(ConditionalLoginRequiredMixin, View): http_method_names = ["post"] - def post(self, request, *args, **kwargs): + def post(self, request: Request, *args, **kwargs) -> HttpResponseRedirect: pk_list = request.POST.getlist("records") email_pks = _get_or_create_emails(pk_list) try: diff --git a/solenoid/records/views.py b/solenoid/records/views.py index b54bba4..16a4d87 100644 --- a/solenoid/records/views.py +++ b/solenoid/records/views.py @@ -1,15 +1,13 @@ import logging -from requests.exceptions import HTTPError, Timeout - from django.conf import settings from django.contrib import messages from django.db import models from django.http import ( HttpRequest, HttpResponse, - HttpResponseRedirect, HttpResponsePermanentRedirect, + HttpResponseRedirect, ) from django.shortcuts import redirect, render from django.urls import reverse_lazy @@ -62,7 +60,7 @@ def _get_author_data(self, author_id: int, author_url: str) -> dict: author_data["ELEMENTS ID"] = author_id return author_data - def _get_author_record_id(self, author_data: dict) -> int | HttpResponse: + def _get_author_record_id(self, author_data: dict) -> int: try: author = Author.get_by_mit_id(author_data[Fields.MIT_ID]) if not author.dspace_id: @@ -85,26 +83,24 @@ def form_valid( # type: ignore ) -> HttpResponseRedirect | HttpResponsePermanentRedirect: author_id = form.cleaned_data["author_id"] author_url = f"{settings.ELEMENTS_ENDPOINT}users/{author_id}" - author_data = self._get_author_data(form, author_id) + author_data = self._get_author_data(author_id, author_url) author = self._get_author_record_id(author_data) result = task_import_papers_for_author.delay(author_url, author_data, author) task_id = result.task_id return redirect("records:status", task_id=task_id) def form_invalid(self, form: ImportForm) -> HttpResponse: - msg = ( - format_html( - "Something went wrong. Please try again, and if it " - 'still doesn\'t work, contact ' - "a Solenoid admin.", - mark_safe(settings.ADMINS[0][1]), - ), + msg = format_html( + "Something went wrong. Please try again, and if it " + 'still doesn\'t work, contact ' + "a Solenoid admin.", + mark_safe(settings.ADMINS[0][1]), ) messages.warning(self.request, msg) return super(Import, self).form_invalid(form) - def get_context_data(self, **kwargs) -> dict: + def get_context_data(self, **kwargs) -> dict: # type: ignore[no-untyped-def] context = super(Import, self).get_context_data(**kwargs) context["breadcrumbs"] = [ {"url": reverse_lazy("home"), "text": "dashboard"},