Skip to content

Commit

Permalink
[wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
jonavellecuerdo committed Dec 1, 2023
1 parent 2960a46 commit 4c78e1b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions solenoid/emails/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from ckeditor.widgets import CKEditorWidget

from django import forms

from .models import EmailMessage
Expand All @@ -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()
16 changes: 9 additions & 7 deletions solenoid/emails/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,19 +17,20 @@
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()
email_pks = []

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
Expand All @@ -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:
Expand Down
22 changes: 9 additions & 13 deletions solenoid/records/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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 mailto="{}">'
"a Solenoid admin</a>.",
mark_safe(settings.ADMINS[0][1]),
),
msg = format_html(
"Something went wrong. Please try again, and if it "
'still doesn\'t work, contact <a mailto="{}">'
"a Solenoid admin</a>.",
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"},
Expand Down

0 comments on commit 4c78e1b

Please sign in to comment.