From: Guillaume Pellerin Date: Fri, 6 Feb 2026 22:58:59 +0000 (+0100) Subject: add async notif api view X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=779bbd3c47a63ba547cedd286791ada87138d660;p=teleforma.git add async notif api view --- diff --git a/teleforma/models/core.py b/teleforma/models/core.py index 8de0783f..78a7a466 100644 --- a/teleforma/models/core.py +++ b/teleforma/models/core.py @@ -497,9 +497,9 @@ class Conference(models.Model): else: transport = httpx.HTTPTransport(uds=settings.CHANNEL_URL) async with httpx.AsyncClient(transport=transport) as client: - response = await client.post("http://localhost" + reverse('teleforma-live-conference-notify'), + response = await client.post("http://channels" + reverse('teleforma-live-conference-notify-async'), data={'id': self.id}, timeout=20.0) - assert response.status_code == 200 + #assert response.status_code == 200 def notify_sync(self): if self.streaming and not self.notified_live: diff --git a/teleforma/urls.py b/teleforma/urls.py index 2fded73d..549e6dc9 100644 --- a/teleforma/urls.py +++ b/teleforma/urls.py @@ -278,15 +278,20 @@ urlpatterns = [ # chat path('chat/messages', - ChatMessageView.as_view(), name='teleforma-chat-messages'), + ChatMessageView.as_view(), name='teleforma-chat-messages'), + # notification path('notification', - NotificationView.as_view(), name='teleforma-notification'), + NotificationView.as_view(), name='teleforma-notification'), # must be called on channels instance path('live_conference_notify', LiveConferenceNotify.as_view(), name='teleforma-live-conference-notify'), + # must be called on channels instance + path('live_conference_notify_async', + LiveConferenceNotifyAsync.as_view(), name='teleforma-live-conference-notify-async'), + # QUIZ url(r'^desk/periods/(?P.*)/courses/(?P.*)/quiz/(?P[\w-]+)/$', QuizQuestionView.as_view(), name="teleforma-quiz"), ] diff --git a/teleforma/views/core.py b/teleforma/views/core.py index 6366c71f..3b82c3c3 100644 --- a/teleforma/views/core.py +++ b/teleforma/views/core.py @@ -1065,6 +1065,42 @@ class LiveConferenceNotify(APIView): return Response({'status': 'ok'}) +class LiveConferenceNotifyAsync(APIView): + + async def post(self, request): + """ + notify users a new live conference is starting + """ + conference_id = request.data.get('id') + if not conference_id: + raise Exception('No conference id in request') + conference = Conference.objects.get(pk=int(conference_id)) + students = Student.objects.filter(period=conference.period, platform_only=True) + text = f"""Une conférence live "{conference.course.title}" commence""" + url = reverse('teleforma-conference-detail', kwargs={'period_id': conference.period.id, 'pk': conference.id}) + + # notify students + for student in students: + try: + if student.user: + courses = get_courses(student.user, period=conference.period) + for course in courses: + if conference.course == course['course'] and \ + conference.course_type in course['types']: + await notify(student.user, text, url) + logger.info("Student notified: " + student.user.username) + except Exception as e: + logger.warning("Student NOT notified: " + str(student.id)) + logger.warning(e) + continue + + # notify staff + for user in User.objects.filter(is_staff=True): + notify(user, text, url) + + return Response({'status': 'ok'}) + + class ProfessorListView(View): @jsonrpc_method('teleforma.get_professor_list')