def test_assigned_to_new_group_as_teacher_renders_properly(self): descr_args = {'course_name': 'matematyka dyskretna'} rendered = render_description( NotificationType.ASSIGNED_TO_NEW_GROUP_AS_A_TEACHER, descr_args) self.assertEqual( 'Przydzielono Cię do grupy przedmiotu "matematyka dyskretna" jako prowadzącego.', rendered)
def test_added_new_group_renders_properly(self): descr_args = { 'course_name': 'matematyka dyskretna', 'teacher': 'Jan Kowalski' } rendered = render_description(NotificationType.ADDED_NEW_GROUP, descr_args) self.assertEqual( 'W przedmiocie "matematyka dyskretna" została dodana grupa prowadzona przez Jan Kowalski.', rendered)
def dispatch_notifications_task(user): """Dispatch all pending notifications for the given user by email. We batch all the e-mails for the user together and send them using one SMTP connection. We also wait for THROTTLE_SECONDS until we let the next task in the work queue to send emails. This rate limiting is introduced for Gmail to accept our queries. """ if user.employee: model, created = NotificationPreferencesTeacher.objects.get_or_create( user=user) elif user.student: model, created = NotificationPreferencesStudent.objects.get_or_create( user=user) else: return repo = get_notifications_repository() pending_notifications = repo.get_unsent_for_user(user) if not pending_notifications: return messages = [] for pn in pending_notifications: # User controls in account settings # what notifications will be send if not getattr(model, pn.description_id, None): continue ctx = { 'content': render_description(pn.description_id, pn.description_args), 'greeting': f'Dzień dobry, {user.first_name}', } message_contents = render_to_string('notifications/email_base.html', ctx) messages.append(( 'Wiadomość od Systemu Zapisów IIUWr', strip_tags(message_contents), settings.MASS_MAIL_FROM, [user.email], )) send_mass_mail(messages, fail_silently=False) # Only mark the notifications as sent if the e-mails went out successfully. for pn in pending_notifications: repo.mark_as_sent(user, pn) time.sleep(settings.EMAIL_THROTTLE_SECONDS)
def test_teacher_changed(self): teacher = EmployeeFactory() group = GroupFactory() mail.outbox = [] teacher_changed.send(sender=Group, instance=group) ctx = { 'content': render_description( NotificationType.ASSIGNED_TO_NEW_GROUP_AS_A_TEACHER, {"course_name": group.course.name}), 'greeting': f'Dzień dobry, {teacher.user.first_name}', } self.assertEqual(len(mail.outbox), 1) self.assertEqual( mail.outbox[0].body, strip_tags(render_to_string('notifications/email_base.html', ctx)))
def get_notifications(request): def trunc(text): """Cuts text at 200 characters and adds dots if it was indeed longer.""" return text[:200] + (text[200:] and '...') DATE_TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%f' repo = get_notifications_repository() notifications = [{ 'id': notification.id, 'description': trunc( render_description(notification.description_id, notification.description_args)), 'issued_on': notification.issued_on.strftime(DATE_TIME_FORMAT), 'target': notification.target, } for notification in repo.get_all_for_user(request.user)] notifications.sort(key=lambda x: x['issued_on'], reverse=True) return JsonResponse(notifications, safe=False)
def test_trying_to_render_with_insufficient_arguments_raises(self): descr_args = {} with self.assertRaises(DescriptionArgumentMissingException): rendered = render_description( NotificationType.TEACHER_HAS_BEEN_CHANGED_ENROLLED, descr_args)