From 48e81cc7a088ac61293a26102af0ff2d7a6d5536 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Mon, 18 May 2020 22:52:13 +0530 Subject: [PATCH 01/11] Course deadline notification task - WIP quiz deadline notification task. --- notification/__init__.py | 0 notification/admin.py | 3 +++ notification/apps.py | 5 ++++ notification/models.py | 3 +++ notification/tasks.py | 53 ++++++++++++++++++++++++++++++++++++++++ notification/tests.py | 3 +++ notification/views.py | 3 +++ online_test/settings.py | 14 ++++++++++- 8 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 notification/__init__.py create mode 100644 notification/admin.py create mode 100644 notification/apps.py create mode 100644 notification/models.py create mode 100644 notification/tasks.py create mode 100644 notification/tests.py create mode 100644 notification/views.py diff --git a/notification/__init__.py b/notification/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/notification/admin.py b/notification/admin.py new file mode 100644 index 000000000..8c38f3f3d --- /dev/null +++ b/notification/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/notification/apps.py b/notification/apps.py new file mode 100644 index 000000000..40b3eb9cd --- /dev/null +++ b/notification/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class NotificationConfig(AppConfig): + name = 'notification' diff --git a/notification/models.py b/notification/models.py new file mode 100644 index 000000000..71a836239 --- /dev/null +++ b/notification/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/notification/tasks.py b/notification/tasks.py new file mode 100644 index 000000000..a9b50dfff --- /dev/null +++ b/notification/tasks.py @@ -0,0 +1,53 @@ +from textwrap import dedent + +from django.utils import timezone + +from celery import task +from notifications_plugin.models import NotificationMessage, Notification + +from yaksh.models import Course, Quiz, QuestionPaper, AnswerPaper + + +@task(name='course_deadline_task') +def course_deadline_task(): + courses = Course.objects.filter(active=True, is_trial=False) + for course in courses: + if course.is_active_enrollment(): + message = dedent(""" + The deadline for the course {0} is {1}, please complete + the course if not completed before the deadline. + """.format(course.name, course.end_enroll_time) + ) + students = course.students.all() + creator = course.creator + if students: + students_id = students.values_list('id', flat=True) + notification_type = "warning" + nm = NotificationMessage.objects.add_single_message( + creator_id=creator.id, summary='Course Notification', + description=message, msg_type=notification_type + ) + Notification.objects.add_bulk_user_notifications( + receiver_ids=students_id, msg_id=nm.id + ) + + +@task(name='quiz_deadline_task') +def quiz_deadline_task(): + courses = Course.objects.filter(active=True, is_trial=False) + for course in courses: + students = course.students.all() + creator = course.creator + modules = course.learning_modules.all() + for module in modules: + units = module.learning_unit.all() + for unit in units: + if unit.type == 'quiz': + quiz = unit.quiz + if not quiz.is_expired(): + message = dedent(""" + The deadline for the quiz {0} is {1}, please + complete the quiz if not completed before the + deadline. + """.format(quiz, quiz.end_date_time) + ) \ No newline at end of file diff --git a/notification/tests.py b/notification/tests.py new file mode 100644 index 000000000..7ce503c2d --- /dev/null +++ b/notification/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/notification/views.py b/notification/views.py new file mode 100644 index 000000000..91ea44a21 --- /dev/null +++ b/notification/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/online_test/settings.py b/online_test/settings.py index 3b89c28bf..a3467718f 100644 --- a/online_test/settings.py +++ b/online_test/settings.py @@ -7,6 +7,7 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ +from celery.schedules import crontab from yaksh.pipeline.settings import AUTH_PIPELINE import os from decouple import config @@ -51,7 +52,8 @@ 'rest_framework', 'api', 'corsheaders', - 'rest_framework.authtoken' + 'rest_framework.authtoken', + 'notification' ) MIDDLEWARE = ( @@ -229,6 +231,16 @@ CELERY_BROKER_URL = 'redis://localhost' CELERY_RESULT_BACKEND = 'django-db' +CELERY_BEAT_SCHEDULE = { + 'send-summary-every-hour': { + 'task': 'course_deadline_task', + 'schedule': crontab( + hour='00', minute=3, day_of_week='Monday', day_of_month='*', + month_of_year='*' + ), + }, +} + REST_FRAMEWORK = { # Use Django's standard `django.contrib.auth` permissions, # or allow read-only access for unauthenticated users. From 30c79fd86e0c8e20517d8c92e5b0cfa8e77a7a6a Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Mon, 18 May 2020 23:50:39 +0530 Subject: [PATCH 02/11] Send quiz deadline notifications to user --- notification/tasks.py | 11 ++++++++++- online_test/settings.py | 11 +++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/notification/tasks.py b/notification/tasks.py index a9b50dfff..2f1ed5832 100644 --- a/notification/tasks.py +++ b/notification/tasks.py @@ -37,8 +37,9 @@ def quiz_deadline_task(): courses = Course.objects.filter(active=True, is_trial=False) for course in courses: students = course.students.all() + students_id = students.values_list('id', flat=True) creator = course.creator - modules = course.learning_modules.all() + modules = course.learning_module.all() for module in modules: units = module.learning_unit.all() for unit in units: @@ -50,4 +51,12 @@ def quiz_deadline_task(): complete the quiz if not completed before the deadline. """.format(quiz, quiz.end_date_time) + ) + notification_type = 'warning' + nm = NotificationMessage.objects.add_single_message( + creator_id=creator.id, summary='Quiz Notification', + description=message, msg_type=notification_type + ) + Notification.objects.add_bulk_user_notifications( + receiver_ids=students_id, msg_id=nm.id ) \ No newline at end of file diff --git a/online_test/settings.py b/online_test/settings.py index a3467718f..824839754 100644 --- a/online_test/settings.py +++ b/online_test/settings.py @@ -232,13 +232,20 @@ CELERY_RESULT_BACKEND = 'django-db' CELERY_BEAT_SCHEDULE = { - 'send-summary-every-hour': { + 'send-course-deadline-notifications-once-a-week': { 'task': 'course_deadline_task', 'schedule': crontab( - hour='00', minute=3, day_of_week='Monday', day_of_month='*', + hour='08', minute=43, day_of_week='Friday', day_of_month='*', month_of_year='*' ), }, + 'send-quiz-deadline-notifications-once-a-week': { + 'task': 'quiz_deadline_task', + 'schedule': crontab( + hour='09', minute=47, day_of_week='Friday', day_of_month='*', + month_of_year='*' + ), + } } REST_FRAMEWORK = { From 5a023159deb2d59de2a4b71e7c24dfd82e95f308 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Tue, 19 May 2020 15:11:12 +0530 Subject: [PATCH 03/11] Resolve comments --- notification/tasks.py | 67 ++++++++++++++++++++++++----------------- online_test/settings.py | 16 ---------- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/notification/tasks.py b/notification/tasks.py index 2f1ed5832..535c30fa0 100644 --- a/notification/tasks.py +++ b/notification/tasks.py @@ -3,12 +3,21 @@ from django.utils import timezone from celery import task +from celery.decorators import periodic_task +from celery.task.schedules import crontab from notifications_plugin.models import NotificationMessage, Notification from yaksh.models import Course, Quiz, QuestionPaper, AnswerPaper -@task(name='course_deadline_task') +@periodic_task( + run_every=( + crontab( + hour='15', minute=6, day_of_week='Tuesday', day_of_month='*', + month_of_year='*' + ) + ), name='course_deadline_task' +) def course_deadline_task(): courses = Course.objects.filter(active=True, is_trial=False) for course in courses: @@ -18,10 +27,9 @@ def course_deadline_task(): the course if not completed before the deadline. """.format(course.name, course.end_enroll_time) ) - students = course.students.all() creator = course.creator - if students: - students_id = students.values_list('id', flat=True) + students_id = course.students.values_list('id', flat=True) + if students_id: notification_type = "warning" nm = NotificationMessage.objects.add_single_message( creator_id=creator.id, summary='Course Notification', @@ -32,31 +40,36 @@ def course_deadline_task(): ) -@task(name='quiz_deadline_task') +@periodic_task( + run_every=( + crontab( + hour='15', minute=8, day_of_week='Tuesday', day_of_month='*', + month_of_year='*' + ) + ), name='quiz_deadline_task' +) def quiz_deadline_task(): courses = Course.objects.filter(active=True, is_trial=False) for course in courses: - students = course.students.all() - students_id = students.values_list('id', flat=True) + students_id = course.students.values_list('id', flat=True) creator = course.creator - modules = course.learning_module.all() - for module in modules: - units = module.learning_unit.all() - for unit in units: - if unit.type == 'quiz': - quiz = unit.quiz - if not quiz.is_expired(): - message = dedent(""" - The deadline for the quiz {0} is {1}, please - complete the quiz if not completed before the - deadline. - """.format(quiz, quiz.end_date_time) + quizzes = course.get_quizzes() + if students_id: + for quiz in quizzes: + if not quiz.is_expired(): + message = dedent(""" + The deadline for the quiz {0} of course {1} is + {2}, please complete the quiz if not completed + before the deadline. + """.format( + quiz.description, course.name, quiz.end_date_time ) - notification_type = 'warning' - nm = NotificationMessage.objects.add_single_message( - creator_id=creator.id, summary='Quiz Notification', - description=message, msg_type=notification_type - ) - Notification.objects.add_bulk_user_notifications( - receiver_ids=students_id, msg_id=nm.id - ) \ No newline at end of file + ) + notification_type = 'warning' + nm = NotificationMessage.objects.add_single_message( + creator_id=creator.id, summary='Quiz Notification', + description=message, msg_type=notification_type + ) + Notification.objects.add_bulk_user_notifications( + receiver_ids=students_id, msg_id=nm.id + ) diff --git a/online_test/settings.py b/online_test/settings.py index 824839754..a59280322 100644 --- a/online_test/settings.py +++ b/online_test/settings.py @@ -231,22 +231,6 @@ CELERY_BROKER_URL = 'redis://localhost' CELERY_RESULT_BACKEND = 'django-db' -CELERY_BEAT_SCHEDULE = { - 'send-course-deadline-notifications-once-a-week': { - 'task': 'course_deadline_task', - 'schedule': crontab( - hour='08', minute=43, day_of_week='Friday', day_of_month='*', - month_of_year='*' - ), - }, - 'send-quiz-deadline-notifications-once-a-week': { - 'task': 'quiz_deadline_task', - 'schedule': crontab( - hour='09', minute=47, day_of_week='Friday', day_of_month='*', - month_of_year='*' - ), - } -} REST_FRAMEWORK = { # Use Django's standard `django.contrib.auth` permissions, From dabcbb4351ce8da9051a774406ee52ec10f1e805 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Tue, 19 May 2020 15:13:49 +0530 Subject: [PATCH 04/11] Remove crontab import from settings.py --- online_test/settings.py | 1 - 1 file changed, 1 deletion(-) diff --git a/online_test/settings.py b/online_test/settings.py index a59280322..843ff6588 100644 --- a/online_test/settings.py +++ b/online_test/settings.py @@ -7,7 +7,6 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ -from celery.schedules import crontab from yaksh.pipeline.settings import AUTH_PIPELINE import os from decouple import config From 0b0410c70c95f64cf15861ac6188e45f0e9802cb Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Wed, 20 May 2020 19:04:46 +0530 Subject: [PATCH 05/11] Send a course deadline notification by email --- notification/tasks.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/notification/tasks.py b/notification/tasks.py index 535c30fa0..724db3197 100644 --- a/notification/tasks.py +++ b/notification/tasks.py @@ -8,12 +8,12 @@ from notifications_plugin.models import NotificationMessage, Notification from yaksh.models import Course, Quiz, QuestionPaper, AnswerPaper - +from yaksh.send_emails import send_bulk_mail @periodic_task( run_every=( crontab( - hour='15', minute=6, day_of_week='Tuesday', day_of_month='*', + hour='08', minute=36, day_of_week='*/3', day_of_month='*', month_of_year='*' ) ), name='course_deadline_task' @@ -38,12 +38,15 @@ def course_deadline_task(): Notification.objects.add_bulk_user_notifications( receiver_ids=students_id, msg_id=nm.id ) + subject = 'Course Notification' + send_bulk_mail(subject=subject, email_body=message, + recipients=students_id, attachments=None) @periodic_task( run_every=( crontab( - hour='15', minute=8, day_of_week='Tuesday', day_of_month='*', + hour='09', minute=12, day_of_week='*/3', day_of_month='*', month_of_year='*' ) ), name='quiz_deadline_task' From 6416f79f339742cfe386db90ec71638093482e70 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Fri, 22 May 2020 14:38:48 +0530 Subject: [PATCH 06/11] Show notifications on student interface --- yaksh/templates/user.html | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/yaksh/templates/user.html b/yaksh/templates/user.html index 4e3974b02..31d624f61 100644 --- a/yaksh/templates/user.html +++ b/yaksh/templates/user.html @@ -15,6 +15,16 @@