Skip to content

Commit

Permalink
Grade Publishing Reminder Email in send_reminders (#2284)
Browse files Browse the repository at this point in the history
* Factor out Course.objects_with_missing_final_grades

* EmailTemplate: Typing, send_to_address helper

* Grade reminder email as part of send_reminders

* Use unordered_groupby in web-view as well

* unnecessary-lambda-assignment is checked by ruff

* Introduce and use MonthAndDay instead of datetime.date

* Move django-unrelated stuff into evap.tools

* Enforce kw-only args on EmailTemplate.send_to_user

---------

Co-authored-by: janno42 <[email protected]>
Co-authored-by: Niklas Mohrin <[email protected]>
  • Loading branch information
3 people authored Oct 7, 2024
1 parent 079c9f4 commit bfd3d4b
Show file tree
Hide file tree
Showing 23 changed files with 493 additions and 145 deletions.
8 changes: 7 additions & 1 deletion evap/contributor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,13 @@ def evaluation_direct_delegation(request, evaluation_id):

# we don't provide the request here since send_to_user only uses it to display a warning message in case the user does not have
# an email address. In this special case, we don't want that warning. Instead, we want a mail to the admins.
template.send_to_user(delegate_user, subject_params, body_params, use_cc=True, additional_cc_users=[request.user])
template.send_to_user(
delegate_user,
subject_params=subject_params,
body_params=body_params,
use_cc=True,
additional_cc_users=[request.user],
)

messages.add_message(
request,
Expand Down
10 changes: 10 additions & 0 deletions evap/development/fixtures/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -133247,6 +133247,16 @@
"html_content": "<i>(English version below)</i><br /><br /><br />\r\n\r\n\r\nHallo {{ user.first_name }},<br /><br />\r\n\r\nes gibt noch nicht überprüfte Textantworten für eine oder mehrere Evaluierungen, bei denen der Evaluierungszeitraum abgelaufen ist und nicht mehr auf Notenveröffentlichungen gewartet werden muss. Bitte überprüfe die Textantworten für diese Evaluierungen möglichst bald:\r\n<ul>{% for evaluation, url in evaluation_url_tuples %}\r\n<li><a href='{{ url }}'>{{ evaluation.full_name_de }}</a></li>\r\n{% endfor %}</ul><br /><br />\r\n\r\n<i>(Dies ist eine automatisch versendete E-Mail.)</i><br /><br />\r\n\r\n<hr><br /><br />\r\n\r\nDear {{ user.first_name }},<br /><br />\r\n\r\nthere are text answers not yet reviewed for one or more evaluations where the evaluation period has ended and there is no need to wait for grade publishing. Please review the text answers for these evaluations as soon as possible:\r\n<ul>{% for evaluation, url in evaluation_url_tuples %}\r\n<li><a href='{{ url }}'>{{ evaluation.full_name_en }}</a></li>\r\n{% endfor %}</ul><br /><br />\r\n\r\n<i>(This is an automated message.)</i>"
}
},
{
"model": "evaluation.emailtemplate",
"pk": 10,
"fields": {
"name": "Grade Reminder",
"subject": "[EvaP] Notenveröffentlichung für {{semester.name_de}} / Grade publishing for {{semester.name_en}}",
"plain_content": "(English version below)\r\n\r\nLiebes Studienreferat,\r\n\r\nfür die folgenden Veranstaltungen im {{semester.name_de}} wurden bisher keine Notendokumente auf EvaP hochgeladen. Bitte prüfen Sie, ob die Noten bereits vorliegen und die Evaluierungsergebnisse veröffentlicht werden können.\r\n\r\n{% for responsible, courses in responsibles_and_courses_without_final_grades %}\r\n{{ responsible }}\r\n{% for course in courses %}\r\n- {{ course.name_de }} ({{ course.type }})\r\n{% endfor %}\r\n\r\n{% endfor %}\r\n\r\nBei Fragen und Rückmeldungen können Sie sich jederzeit gerne an das Evaluierungsteam wenden ({{ contact_email }}).\r\n\r\nFreundliche Grüße,\r\ndas Evaluierungsteam\r\n\r\n(Dies ist eine automatisch versendete E-Mail.)\r\n\r\n\r\n--\r\n\r\n\r\nDear office for student affairs,\r\n\r\nfor the following courses in {{semester.name_en}}, no grade documents have been uploaded on EvaP yet. Please check whether the grades are already available and whether the evaluation results can be published.\r\n\r\n{% for responsible, courses in responsibles_and_courses_without_final_grades %}\r\n{{ responsible }}\r\n{% for course in courses %}\r\n- {{ course.name_en }} ({{ course.type }})\r\n{% endfor %}\r\n\r\n{% endfor %}\r\n\r\nIf you have any questions or feedback, please contact the evaluation team ({{ contact_email }}).\r\n\r\nKind regards,\r\nthe Evaluation Team\r\n\r\n(This is an automated message.)",
"html_content": "<i>(English version below)</i><br /><br /><br />\r\n\r\nLiebes Studienreferat,<br /><br />\r\n\r\nfür die folgenden Veranstaltungen im {{semester.name_de}} wurden bisher keine Notendokumente auf EvaP hochgeladen. Bitte prüfen Sie, ob die Noten bereits vorliegen und die Evaluierungsergebnisse veröffentlicht werden können.<br /><br />\r\n\r\n{% for responsible, courses in responsibles_and_courses_without_final_grades %}\r\n{{ responsible }}\r\n<ul>\r\n{% for course in courses %}\r\n<li>{{ course.name_de }} ({{ course.type }})</li>\r\n{% endfor %}\r\n</ul><br/>\r\n{% endfor %}\r\n\r\nBei Fragen und Rückmeldungen können Sie sich jederzeit gerne an das Evaluierungsteam wenden (<a href=\"mailto:{{ contact_email }}\">{{ contact_email }}</a>).<br /><br />\r\n\r\nFreundliche Grüße,<br />\r\ndas Evaluierungsteam<br /><br />\r\n\r\n<i>(Dies ist eine automatisch versendete E-Mail.)</i><br /><br />\r\n\r\n<hr><br /><br />\r\n\r\nDear office for student affairs,<br /><br />\r\n\r\nfor the following courses in {{semester.name_en}}, no grade documents have been uploaded on EvaP yet. Please check whether the grades are already available and whether the evaluation results can be published.<br /><br />\r\n\r\n{% for responsible, courses in responsibles_and_courses_without_final_grades %}\r\n{{ responsible }}\r\n<ul>\r\n{% for course in courses %}\r\n<li>{{ course.name_en }} ({{ course.type }})</li>\r\n{% endfor %}\r\n</ul><br/>\r\n{% endfor %}\r\n\r\nIf you have any questions or feedback, please contact the evaluation team (<a href=\"mailto:{{ contact_email }}\">{{ contact_email }}</a>).<br /><br />\r\n\r\nKind regards,<br />\r\nthe Evaluation Team<br /><br />\r\n\r\n<i>(This is an automated message.)</i>"
}
},
{
"model": "evaluation.votetimestamp",
"pk": 1,
Expand Down
2 changes: 1 addition & 1 deletion evap/evaluation/management/commands/anonymize.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
TextAnswer,
UserProfile,
)
from evap.evaluation.tools import unordered_groupby
from evap.tools import unordered_groupby


class Command(BaseCommand):
Expand Down
38 changes: 37 additions & 1 deletion evap/evaluation/management/commands/send_reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from django.conf import settings
from django.contrib.auth.models import Group
from django.core.management.base import BaseCommand
from django.db.models import Exists, OuterRef, Prefetch
from django.urls import reverse

from evap.evaluation.management.commands.tools import log_exceptions
from evap.evaluation.models import EmailTemplate, Evaluation
from evap.evaluation.models import Course, EmailTemplate, Evaluation, Semester
from evap.tools import MonthAndDay, unordered_groupby

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -36,6 +38,7 @@ def handle(self, *args, **options):
logger.info("send_reminders called.")
self.send_student_reminders()
self.send_textanswer_reminders()
self.send_grade_reminders()
logger.info("send_reminders finished.")

@staticmethod
Expand Down Expand Up @@ -74,3 +77,36 @@ def send_textanswer_reminders():
EmailTemplate.send_textanswer_reminder_to_user(manager, evaluation_url_tuples)

logger.info("sent text answer review reminders.")

@staticmethod
def send_grade_reminders():
today = MonthAndDay(day=datetime.date.today().day, month=datetime.date.today().month)
if today not in settings.GRADE_REMINDER_EMAIL_DATES:
return

courses_without_final_grades = Course.objects_with_missing_final_grades().order_by("name_en")
semesters = (
Semester.objects.filter(grade_documents_are_deleted=False)
.filter(Exists(courses_without_final_grades.filter(semester__pk=OuterRef("pk"))))
.prefetch_related(
Prefetch("courses", queryset=courses_without_final_grades, to_attr="courses_without_final_grades"),
)
)

for semester in semesters:
responsibles_and_courses_without_final_grades = unordered_groupby(
(responsible, course)
for course in semester.courses_without_final_grades
for responsible in course.responsibles.all()
)

for recipient in settings.GRADE_REMINDER_EMAIL_RECIPIENTS:
EmailTemplate.send_grade_reminder(
recipient, semester, responsibles_and_courses_without_final_grades.items()
)

logger.info(
"sent grade document reminders for %d semesters to %d people.",
len(semesters),
len(settings.GRADE_REMINDER_EMAIL_RECIPIENTS),
)
31 changes: 31 additions & 0 deletions evap/evaluation/migrations/0146_grade_reminder_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.db import migrations


emailtemplates = [
("Grade Reminder", "[EvaP] Notenveröffentlichung für {{semester.name_de}} / Grade publishing for {{semester.name_en}}"),
]


def insert_emailtemplates(apps, _schema_editor):
EmailTemplate = apps.get_model("evaluation", "EmailTemplate")

for name, subject in emailtemplates:
EmailTemplate.objects.create(name=name, subject=subject, plain_content="", html_content="")


def remove_emailtemplates(apps, _schema_editor):
EmailTemplate = apps.get_model("evaluation", "EmailTemplate")

for name, __ in emailtemplates:
EmailTemplate.objects.filter(name=name).delete()


class Migration(migrations.Migration):

dependencies = [
('evaluation', '0145_rename_degree_to_program'),
]

operations = [
migrations.RunPython(insert_emailtemplates, reverse_code=remove_emailtemplates),
]
Loading

0 comments on commit bfd3d4b

Please sign in to comment.