Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added notification/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions notification/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin

from .models import Subscription


admin.site.register(Subscription)
5 changes: 5 additions & 0 deletions notification/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class NotificationConfig(AppConfig):
name = 'notification'
26 changes: 26 additions & 0 deletions notification/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey


class Subscription(models.Model):
user = models.ForeignKey(User,
related_name='subscription',
db_index=True,
on_delete=models.CASCADE)
subscribe = models.BooleanField(default=True)
target_ct = models.ForeignKey(ContentType,
blank=True,
null=True,
related_name='target_obj',
on_delete=models.CASCADE
)
target_id = models.PositiveIntegerField(null=True,
blank=True,
db_index=True)
target = GenericForeignKey('target_ct', 'target_id')
created = models.DateTimeField(auto_now_add=True, db_index=True)

class Meta:
ordering = ('-created', )
123 changes: 123 additions & 0 deletions notification/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from textwrap import dedent
from collections import OrderedDict

from django.utils import timezone
from django.core.mail import send_mail
from django.template.loader import render_to_string

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
from .models import Subscription


@periodic_task(
run_every=(
crontab(
hour='08', minute=36, day_of_week='*/3', 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:
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)
)
creator = course.creator
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',
description=message, msg_type=notification_type
)
Notification.objects.add_bulk_user_notifications(
receiver_ids=students_id, msg_id=nm.id
)


@periodic_task(
run_every=(
crontab(
hour='09', minute=12, day_of_week='*/3', 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_id = course.students.values_list('id', flat=True)
creator = course.creator
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
)


@periodic_task(
run_every=(
crontab(
hour='23', minute=30, day_of_week='Tuesday', day_of_month='*',
month_of_year='*'
)
), name='course_quiz_deadline_mail_task'
)
def course_quiz_deadline_mail_task():
courses = Course.objects.filter(active=True, is_trial=False)
for course in courses:
if course.is_active_enrollment():
students = course.students.all()
quizzes = course.get_quizzes()
for student in students:
subscribed = student.subscription.exists()
if subscribed:
data = []
for quiz in quizzes:
quiz_data = {}
answer_paper = AnswerPaper.objects.filter(
course=course, user=student
)
if answer_paper.exists():
quiz_data['quiz'] = quiz
quiz_data['status'] = True
else:
quiz_data['quiz'] = quiz
quiz_data['status'] = False
quiz_data['deadline'] = quiz.end_date_time
data.append(quiz_data)
msg_html = render_to_string('notification/email.html', {
'data': data
}
)
msg_plain = render_to_string('notification/email.txt', {
'data': data
}
)
send_mail('email title', msg_plain, 'some@sender.com',
[student.email], html_message=msg_html
)
3 changes: 3 additions & 0 deletions notification/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions notification/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
4 changes: 3 additions & 1 deletion online_test/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
'rest_framework',
'api',
'corsheaders',
'rest_framework.authtoken'
'rest_framework.authtoken',
'notification'
)

MIDDLEWARE = (
Expand Down Expand Up @@ -229,6 +230,7 @@
CELERY_BROKER_URL = 'redis://localhost'
CELERY_RESULT_BACKEND = 'django-db'


REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
Expand Down
10 changes: 10 additions & 0 deletions yaksh/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
<li class="nav-item"><a class="nav-link" href="{% url 'yaksh:index' %}"><i class="fa fa-home" style="size: 18px"></i>&nbsp;Home</a></li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="{% url 'yaksh:view_notifications' %}">
<i class="fa fa-bell" style="size: 18px"></i>&nbsp;Notifications
{% if request.custom_notifications > 0 %}
<span class="badge badge-success badge-pill">
{{request.custom_notifications}}
</span>
{% endif %}
</a>
</li>
<li class="nav-item dropdown my-lg-0" style="font-size: 1.2rem">
<a class="dropdown-toggle nav-link" id="user_dropdown" data-toggle="dropdown" href="#">{{user.get_full_name|title}}
</a>
Expand Down