refactor: unify different reminders (manage.py)
This commit is contained in:
parent
7b610fa742
commit
98ad158913
|
|
@ -4,7 +4,6 @@ import structlog
|
|||
from django.utils import timezone
|
||||
|
||||
from vbv_lernwelt.assignment.models import AssignmentType
|
||||
from vbv_lernwelt.core.base import LoggedCommand
|
||||
from vbv_lernwelt.course.models import CourseSessionUser
|
||||
from vbv_lernwelt.course_session.models import (
|
||||
CourseSessionAssignment,
|
||||
|
|
@ -17,7 +16,7 @@ logger = structlog.get_logger(__name__)
|
|||
ASSIGNMENT_REMINDER_LEAD_TIME = timedelta(days=2)
|
||||
|
||||
|
||||
def assignment_reminder_members_notification_job():
|
||||
def send_assignment_reminder_notifications():
|
||||
start = timezone.now()
|
||||
end = timezone.now() + ASSIGNMENT_REMINDER_LEAD_TIME
|
||||
sent = []
|
||||
|
|
@ -74,18 +73,12 @@ def assignment_reminder_members_notification_job():
|
|||
)
|
||||
)
|
||||
|
||||
logger.debug(
|
||||
"Sent assigment reminders",
|
||||
start_time=start.isoformat(),
|
||||
end_time=end.isoformat(),
|
||||
label="assigment_reminders",
|
||||
sent=sent,
|
||||
)
|
||||
|
||||
return {"sent": sent}
|
||||
|
||||
|
||||
class Command(LoggedCommand):
|
||||
help = "Sends assignments course reminder notifications to participants and experts"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
results = assignment_reminder_members_notification_job()
|
||||
self.job_log.json_data = results
|
||||
self.job_log.save()
|
||||
logger.info(
|
||||
"Assignment course reminder notification job finished",
|
||||
label="assignment_course_reminder_notification_job",
|
||||
results=results,
|
||||
)
|
||||
|
|
@ -4,7 +4,6 @@ from datetime import timedelta
|
|||
import structlog
|
||||
from django.utils import timezone
|
||||
|
||||
from vbv_lernwelt.core.base import LoggedCommand
|
||||
from vbv_lernwelt.course.models import CourseSessionUser
|
||||
from vbv_lernwelt.course_session.models import CourseSessionAttendanceCourse
|
||||
from vbv_lernwelt.notify.services import NotificationService
|
||||
|
|
@ -14,7 +13,7 @@ logger = structlog.get_logger(__name__)
|
|||
PRESENCE_COURSE_REMINDER_LEAD_TIME = timedelta(weeks=2)
|
||||
|
||||
|
||||
def attendance_course_reminder_notification_job():
|
||||
def send_attendance_reminder_notifications():
|
||||
"""Checks if an attendance course is coming up and sends a reminder to the participants"""
|
||||
start = timezone.now()
|
||||
end = timezone.now() + PRESENCE_COURSE_REMINDER_LEAD_TIME
|
||||
|
|
@ -26,8 +25,8 @@ def attendance_course_reminder_notification_job():
|
|||
|
||||
logger.info(
|
||||
"Querying for attendance courses in specified time range",
|
||||
start_time=start,
|
||||
end_time=end,
|
||||
start_time=start.isoformat(),
|
||||
end_time=end.isoformat(),
|
||||
label="attendance_course_reminder_notification_job",
|
||||
num_attendance_courses=len(attendance_courses),
|
||||
)
|
||||
|
|
@ -36,8 +35,8 @@ def attendance_course_reminder_notification_job():
|
|||
csu = CourseSessionUser.objects.filter(course_session_id=cs_id)
|
||||
logger.info(
|
||||
"Sending attendance course reminder notification",
|
||||
start_time=start,
|
||||
end_time=end,
|
||||
start_time=start.isoformat(),
|
||||
end_time=end.isoformat(),
|
||||
label="attendance_course_reminder_notification_job",
|
||||
num_users=len(csu),
|
||||
course_session_id=cs_id,
|
||||
|
|
@ -53,17 +52,3 @@ def attendance_course_reminder_notification_job():
|
|||
label="attendance_course_reminder_notification_job",
|
||||
)
|
||||
return dict(results_counter)
|
||||
|
||||
|
||||
class Command(LoggedCommand):
|
||||
help = "Sends attendance course reminder notifications to participants"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
results = attendance_course_reminder_notification_job()
|
||||
self.job_log.json_data = results
|
||||
self.job_log.save()
|
||||
logger.info(
|
||||
"Attendance course reminder notification job finished",
|
||||
label="attendance_course_reminder_notification_job",
|
||||
results=results,
|
||||
)
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
from enum import Enum
|
||||
|
||||
import structlog
|
||||
|
||||
from vbv_lernwelt.core.base import LoggedCommand
|
||||
from vbv_lernwelt.notify.email.reminders.assigment import (
|
||||
send_assignment_reminder_notifications,
|
||||
)
|
||||
from vbv_lernwelt.notify.email.reminders.attendance import (
|
||||
send_attendance_reminder_notifications,
|
||||
)
|
||||
|
||||
logger = structlog.get_logger(__name__)
|
||||
|
||||
|
||||
class ReminderType(Enum):
|
||||
ASSIGNMENT = "assignment"
|
||||
ATTENDANCE = "attendance"
|
||||
|
||||
|
||||
ACTIONS = {
|
||||
ReminderType.ASSIGNMENT: send_assignment_reminder_notifications,
|
||||
ReminderType.ATTENDANCE: send_attendance_reminder_notifications,
|
||||
}
|
||||
|
||||
|
||||
class Command(LoggedCommand):
|
||||
help = "Sends Email Reminder Notifications"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
"--type",
|
||||
choices=[t.value for t in ReminderType] + ["all"],
|
||||
required=True,
|
||||
help="Type of reminder",
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
reminder_type = options["type"]
|
||||
|
||||
if reminder_type == "all":
|
||||
types = [ReminderType.ASSIGNMENT, ReminderType.ATTENDANCE]
|
||||
else:
|
||||
types = [ReminderType(reminder_type)]
|
||||
|
||||
results = {t.value: None for t in types}
|
||||
|
||||
for reminder in types:
|
||||
logger.info(f"Starting {reminder.name} reminder notification job")
|
||||
results[reminder.value] = ACTIONS[reminder]()
|
||||
logger.info(f"{reminder.name} reminder notification job finished")
|
||||
|
||||
self.job_log.json_data = results
|
||||
self.job_log.save()
|
||||
|
||||
logger.info("Reminder notification job finished")
|
||||
|
|
@ -20,8 +20,8 @@ from vbv_lernwelt.learnpath.models import (
|
|||
LearningContentEdoniqTest,
|
||||
)
|
||||
from vbv_lernwelt.notify.email.email_services import EmailTemplate
|
||||
from vbv_lernwelt.notify.management.commands.send_assigment_course_reminders import (
|
||||
assignment_reminder_members_notification_job,
|
||||
from vbv_lernwelt.notify.email.reminders.assigment import (
|
||||
send_assignment_reminder_notifications,
|
||||
)
|
||||
from vbv_lernwelt.notify.models import Notification
|
||||
|
||||
|
|
@ -174,7 +174,7 @@ class TestAssignmentCourseRemindersTest(TestCase):
|
|||
)
|
||||
|
||||
# WHEN
|
||||
assignment_reminder_members_notification_job()
|
||||
send_assignment_reminder_notifications()
|
||||
|
||||
# THEN
|
||||
self.assertEquals(3, len(Notification.objects.all()))
|
||||
|
|
@ -212,7 +212,7 @@ class TestAssignmentCourseRemindersTest(TestCase):
|
|||
)
|
||||
|
||||
# WHEN
|
||||
assignment_reminder_members_notification_job()
|
||||
send_assignment_reminder_notifications()
|
||||
|
||||
# THEN
|
||||
self.assertEquals(3, len(Notification.objects.all()))
|
||||
|
|
@ -234,7 +234,7 @@ class TestAssignmentCourseRemindersTest(TestCase):
|
|||
)
|
||||
|
||||
# WHEN
|
||||
assignment_reminder_members_notification_job()
|
||||
send_assignment_reminder_notifications()
|
||||
|
||||
# THEN
|
||||
self.assertEquals(1, len(Notification.objects.all()))
|
||||
|
|
@ -274,7 +274,7 @@ class TestAssignmentCourseRemindersTest(TestCase):
|
|||
)
|
||||
|
||||
# WHEN
|
||||
assignment_reminder_members_notification_job()
|
||||
send_assignment_reminder_notifications()
|
||||
|
||||
# THEN
|
||||
self.assertEquals(3, len(Notification.objects.all()))
|
||||
|
|
@ -10,8 +10,8 @@ from vbv_lernwelt.course.creators.test_course import create_test_course
|
|||
from vbv_lernwelt.course.models import CourseSession
|
||||
from vbv_lernwelt.course_session.models import CourseSessionAttendanceCourse
|
||||
from vbv_lernwelt.learnpath.models import LearningContentAttendanceCourse
|
||||
from vbv_lernwelt.notify.management.commands.send_attendance_course_reminders import (
|
||||
attendance_course_reminder_notification_job,
|
||||
from vbv_lernwelt.notify.email.reminders.attendance import (
|
||||
send_attendance_reminder_notifications,
|
||||
)
|
||||
from vbv_lernwelt.notify.models import Notification
|
||||
|
||||
|
|
@ -63,7 +63,7 @@ class TestAttendanceCourseReminders(TestCase):
|
|||
)
|
||||
self.csac_future.due_date.save()
|
||||
|
||||
attendance_course_reminder_notification_job()
|
||||
send_attendance_reminder_notifications()
|
||||
|
||||
self.assertEquals(4, len(Notification.objects.all()))
|
||||
notification = Notification.objects.get(
|
||||
Loading…
Reference in New Issue