Send reminders only to related experts

This commit is contained in:
Christian Cueni 2023-10-31 08:14:19 +01:00
parent 5904df6634
commit 052ddfba8d
2 changed files with 60 additions and 3 deletions

View File

@ -3,14 +3,23 @@ from django.test import TestCase
from vbv_lernwelt.core.constants import TEST_STUDENT1_USER_ID
from vbv_lernwelt.core.create_default_users import create_default_users
from vbv_lernwelt.core.models import User
from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.course.models import CourseCompletion, CourseSession
from vbv_lernwelt.course.creators.test_course import (
create_test_course,
create_test_uk_circle_fahrzeug,
)
from vbv_lernwelt.course.models import (
CourseCompletion,
CourseSession,
CourseSessionUser,
)
from vbv_lernwelt.course.services import mark_course_completion
from vbv_lernwelt.course_session.models import CourseSessionAttendanceCourse
from vbv_lernwelt.course_session.services.attendance import (
AttendanceUserStatus,
update_attendance_list,
)
from vbv_lernwelt.learnpath.models import Circle, LearningPath
from vbv_lernwelt.notify.email.reminders.attendance import get_recipients
class AttendanceServicesTestCase(TestCase):
@ -88,3 +97,36 @@ class AttendanceServicesTestCase(TestCase):
self.assertEqual(cc.user, student)
self.assertEqual(cc.completion_status, "FAIL")
self.assertEqual(cc.page_id, self.attendance_course.learning_content.id)
class AttendanceReminderTestCase(TestCase):
def setUp(self):
create_default_users()
create_test_course(include_vv=False, with_sessions=True)
self.course_session = CourseSession.objects.get(title="Test Bern 2022 a")
self.attendance_course = (
self.course_session.coursesessionattendancecourse_set.first()
)
self.trainer = User.objects.get(username="test-trainer1@example.com")
self.other_circle_title = "Something different"
lp = LearningPath.objects.get(title="Test Lernpfad")
create_test_uk_circle_fahrzeug(lp, title=self.other_circle_title)
csu = CourseSessionUser.objects.get(user=self.trainer)
fahrzeug = Circle.objects.get(title="Fahrzeug")
csu.expert.add(fahrzeug)
def test_reminderOnlySendsToMembersAndRelevantExperts(self):
# promote user to expert, but in another circle
csu = CourseSessionUser.objects.get(user__email="test-student3@example.com")
other_circle = Circle.objects.get(title=self.other_circle_title)
csu.role = CourseSessionUser.Role.EXPERT
csu.save()
csu.expert.add(other_circle)
expected_csu = {
"test-student1@example.com",
"test-student2@example.com",
"test-trainer1@example.com",
}
csus = get_recipients(self.attendance_course)
self.assertEqual(set([u.user.email for u in csus]), expected_csu)

View File

@ -2,10 +2,12 @@ from collections import Counter
from datetime import timedelta
import structlog
from django.db.models import QuerySet
from django.utils import timezone
from vbv_lernwelt.course.models import CourseSessionUser
from vbv_lernwelt.course_session.models import CourseSessionAttendanceCourse
from vbv_lernwelt.learnpath.models import Circle
from vbv_lernwelt.notify.services import NotificationService
logger = structlog.get_logger(__name__)
@ -32,7 +34,7 @@ def send_attendance_reminder_notifications():
)
for attendance_course in attendance_courses:
cs_id = attendance_course.course_session.id
csu = CourseSessionUser.objects.filter(course_session_id=cs_id)
csu = get_recipients(attendance_course)
logger.info(
"Sending attendance course reminder notification",
start_time=start.isoformat(),
@ -52,3 +54,16 @@ def send_attendance_reminder_notifications():
label="attendance_course_reminder_notification_job",
)
return dict(results_counter)
def get_recipients(
attendance_course: CourseSessionAttendanceCourse,
) -> QuerySet["CourseSessionUser"]:
cs_id = attendance_course.course_session.id
circle_page = attendance_course.learning_content.get_parent_circle()
circle = Circle.objects.get(page_ptr=circle_page.id)
experts = circle.expert.all()
members = CourseSessionUser.objects.filter(
course_session_id=cs_id, role=CourseSessionUser.Role.MEMBER
)
return members | experts