summaryrefslogtreecommitdiff
path: root/yaksh/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/views.py')
-rw-r--r--yaksh/views.py250
1 files changed, 142 insertions, 108 deletions
diff --git a/yaksh/views.py b/yaksh/views.py
index 397e7c8..e4a9038 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -52,6 +52,8 @@ from .file_utils import extract_files, is_csv
from .send_emails import (send_user_mail,
generate_activation_key, send_bulk_mail)
from .decorators import email_verified, has_profile
+from .tasks import regrade_papers
+from notifications_plugin.models import Notification
def my_redirect(url):
@@ -439,6 +441,7 @@ def prof_manage(request, msg=None):
courses = Course.objects.get_queryset().filter(
Q(creator=user) | Q(teachers=user),
is_trial=False).distinct().order_by("-active")
+
paginator = Paginator(courses, 20)
page = request.GET.get('page')
courses = paginator.get_page(page)
@@ -1113,12 +1116,54 @@ def course_detail(request, course_id):
@login_required
@email_verified
-def enroll(request, course_id, user_id=None, was_rejected=False):
+def enroll_user(request, course_id, user_id=None, was_rejected=False):
user = request.user
if not is_moderator(user):
raise Http404('You are not allowed to view this page')
- course = get_object_or_404(Course, pk=course_id)
+ course = get_object_or_404(Course, id=course_id)
+ if not course.is_active_enrollment():
+ msg = (
+ 'Enrollment for this course has been closed,'
+ ' please contact your '
+ 'instructor/administrator.'
+ )
+ messages.warning(request, msg)
+ return redirect('yaksh:course_students', course_id=course_id)
+
+ if not course.is_creator(user) and not course.is_teacher(user):
+ raise Http404('This course does not belong to you')
+
+ user = User.objects.get(id=user_id)
+ course.enroll(was_rejected, user)
+ messages.success(request, 'Enrolled student successfully')
+ return redirect('yaksh:course_students', course_id=course_id)
+
+
+@login_required
+@email_verified
+def reject_user(request, course_id, user_id=None, was_enrolled=False):
+ user = request.user
+ if not is_moderator(user):
+ raise Http404('You are not allowed to view this page')
+ course = get_object_or_404(Course, id=course_id)
+ if not course.is_creator(user) and not course.is_teacher(user):
+ raise Http404('This course does not belong to you')
+ user = User.objects.get(id=user_id)
+ course.reject(was_enrolled, user)
+ messages.success(request, "Rejected students successfully")
+ return redirect('yaksh:course_students', course_id=course_id)
+
+
+@login_required
+@email_verified
+def enroll_reject_user(request,
+ course_id, was_enrolled=False, was_rejected=False):
+ user = request.user
+ if not is_moderator(user):
+ raise Http404('You are not allowed to view this page')
+ course = get_object_or_404(Course, id=course_id)
+
if not course.is_active_enrollment():
msg = (
'Enrollment for this course has been closed,'
@@ -1126,23 +1171,31 @@ def enroll(request, course_id, user_id=None, was_rejected=False):
'instructor/administrator.'
)
messages.warning(request, msg)
- return my_redirect(reverse('yaksh:course_students', args=[course_id]))
+ return redirect('yaksh:course_students', course_id=course_id)
if not course.is_creator(user) and not course.is_teacher(user):
raise Http404('This course does not belong to you')
if request.method == 'POST':
- enroll_ids = request.POST.getlist('check')
- else:
- enroll_ids = [user_id]
- if not enroll_ids:
- messages.warning(request, "Please select atleast one student")
- return my_redirect(reverse('yaksh:course_students', args=[course_id]))
-
- users = User.objects.filter(id__in=enroll_ids)
- course.enroll(was_rejected, *users)
- messages.success(request, "Enrolled student(s) successfully")
- return my_redirect(reverse('yaksh:course_students', args=[course_id]))
+ if 'enroll' in request.POST:
+ enroll_ids = request.POST.getlist('check')
+ if not enroll_ids:
+ messages.warning(request, "Please select atleast one student")
+ return redirect('yaksh:course_students', course_id=course_id)
+ users = User.objects.filter(id__in=enroll_ids)
+ course.enroll(was_rejected, *users)
+ messages.success(request, "Enrolled student(s) successfully")
+ return redirect('yaksh:course_students', course_id=course_id)
+ if 'reject' in request.POST:
+ reject_ids = request.POST.getlist('check')
+ if not reject_ids:
+ messages.warning(request, "Please select atleast one student")
+ return redirect('yaksh:course_students', course_id=course_id)
+ users = User.objects.filter(id__in=reject_ids)
+ course.reject(was_enrolled, *users)
+ messages.success(request, "Rejected students successfully")
+ return redirect('yaksh:course_students', course_id=course_id)
+ return redirect('yaksh:course_students', course_id=course_id)
@login_required
@@ -1178,31 +1231,6 @@ def send_mail(request, course_id, user_id=None):
@login_required
@email_verified
-def reject(request, course_id, user_id=None, was_enrolled=False):
- user = request.user
- if not is_moderator(user):
- raise Http404('You are not allowed to view this page')
-
- course = get_object_or_404(Course, pk=course_id)
- if not course.is_creator(user) and not course.is_teacher(user):
- raise Http404('This course does not belong to you')
-
- if request.method == 'POST':
- reject_ids = request.POST.getlist('check')
- else:
- reject_ids = [user_id]
- if not reject_ids:
- messages.warning(request, "Please select atleast one student")
- return my_redirect(reverse('yaksh:course_students', args=[course_id]))
-
- users = User.objects.filter(id__in=reject_ids)
- course.reject(was_enrolled, *users)
- messages.success(request, "Rejected students successfully")
- return my_redirect(reverse('yaksh:course_students', args=[course_id]))
-
-
-@login_required
-@email_verified
def toggle_course_status(request, course_id):
user = request.user
if not is_moderator(user):
@@ -1267,18 +1295,6 @@ def monitor(request, quiz_id=None, course_id=None):
if not is_moderator(user):
raise Http404('You are not allowed to view this page!')
- if quiz_id is None:
- courses = Course.objects.filter(
- Q(creator=user) | Q(teachers=user),
- is_trial=False
- ).order_by("-active").distinct()
- paginator = Paginator(courses, 30)
- page = request.GET.get('page')
- courses = paginator.get_page(page)
- context = {
- "papers": [], "objects": courses, "msg": "Monitor"
- }
- return my_render_to_response(request, 'yaksh/monitor.html', context)
# quiz_id is not None.
try:
quiz = get_object_or_404(Quiz, id=quiz_id)
@@ -1801,7 +1817,7 @@ def download_quiz_csv(request, course_id, quiz_id):
@login_required
@email_verified
def grade_user(request, quiz_id=None, user_id=None, attempt_number=None,
- course_id=None):
+ course_id=None, extra_context=None):
"""Present an interface with which we can easily grade a user's papers
and update all their marks and also give comments for each paper.
"""
@@ -1860,6 +1876,7 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None,
context = {
"data": data,
"quiz_id": quiz_id,
+ "quiz": quiz,
"users": user_details,
"attempts": attempts,
"user_id": user_id,
@@ -1886,7 +1903,8 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None,
course_id=course.id, user_id=user.id)
if course_status.exists():
course_status.first().set_grade()
-
+ if extra_context:
+ context.update(extra_context)
return my_render_to_response(request, 'yaksh/grade_user.html', context)
@@ -2142,56 +2160,31 @@ def create_demo_course(request):
@login_required
@email_verified
-def grader(request, extra_context=None):
- user = request.user
- if not is_moderator(user):
- raise Http404('You are not allowed to view this page!')
- courses = Course.objects.filter(is_trial=False)
- user_courses = list(courses.filter(creator=user)) + \
- list(courses.filter(teachers=user))
- context = {'courses': user_courses}
- if extra_context:
- context.update(extra_context)
- return my_render_to_response(request, 'yaksh/regrade.html', context)
-
-
-@login_required
-@email_verified
-def regrade(request, course_id, question_id=None, answerpaper_id=None,
- questionpaper_id=None):
+def regrade(request, course_id, questionpaper_id, question_id=None,
+ answerpaper_id=None):
user = request.user
course = get_object_or_404(Course, pk=course_id)
if not is_moderator(user) or (course.is_creator(user) and
course.is_teacher(user)):
raise Http404('You are not allowed to view this page!')
+ questionpaper = get_object_or_404(QuestionPaper, pk=questionpaper_id)
details = []
- if answerpaper_id is not None and question_id is None:
- answerpaper = get_object_or_404(AnswerPaper, pk=answerpaper_id)
- for question in answerpaper.questions.all():
- details.append(answerpaper.regrade(question.id))
- course_status = CourseStatus.objects.filter(
- user=answerpaper.user, course=answerpaper.course)
- if course_status.exists():
- course_status.first().set_grade()
- if questionpaper_id is not None and question_id is not None:
- answerpapers = AnswerPaper.objects.filter(
- questions=question_id,
- question_paper_id=questionpaper_id, course_id=course_id)
- for answerpaper in answerpapers:
- details.append(answerpaper.regrade(question_id))
- course_status = CourseStatus.objects.filter(
- user=answerpaper.user, course=answerpaper.course)
- if course_status.exists():
- course_status.first().set_grade()
- if answerpaper_id is not None and question_id is not None:
- answerpaper = get_object_or_404(AnswerPaper, pk=answerpaper_id)
- details.append(answerpaper.regrade(question_id))
- course_status = CourseStatus.objects.filter(user=answerpaper.user,
- course=answerpaper.course)
- if course_status.exists():
- course_status.first().set_grade()
-
- return grader(request, extra_context={'details': details})
+ quiz = questionpaper.quiz
+ data = {"user_id": user.id, "course_id": course_id,
+ "questionpaper_id": questionpaper_id, "question_id": question_id,
+ "answerpaper_id": answerpaper_id, "quiz_id": quiz.id,
+ "quiz_name": quiz.description, "course_name": course.name
+ }
+ regrade_papers.delay(data)
+ msg = dedent("""
+ {0} is submitted for re-evaluation. You will receive a
+ notification for the re-evaluation status
+ """.format(quiz.description)
+ )
+ messages.info(request, msg)
+ return redirect(
+ reverse("yaksh:grade_user", args=[quiz.id, course_id])
+ )
@login_required
@@ -3215,11 +3208,16 @@ def course_students(request, course_id):
if not course.is_creator(user) and not course.is_teacher(user):
raise Http404("You are not allowed to view {0}".format(
course.name))
- enrolled = course.get_enrolled()
- requested = course.get_requests()
- rejected = course.get_rejected()
- context = {"enrolled": enrolled, "requested": requested, "course": course,
- "rejected": rejected, "is_students": True}
+ enrolled_users = course.get_enrolled()
+ requested_users = course.get_requests()
+ rejected_users = course.get_rejected()
+ context = {
+ "enrolled_users": enrolled_users,
+ "requested_users": requested_users,
+ "course": course,
+ "rejected_users": rejected_users,
+ "is_students": True
+ }
return my_render_to_response(request, 'yaksh/course_detail.html', context)
@@ -3280,6 +3278,41 @@ def download_course_progress(request, course_id):
@login_required
@email_verified
+def view_notifications(request):
+ user = request.user
+ notifcations = Notification.objects.get_unread_receiver_notifications(
+ user.id
+ )
+ if is_moderator(user):
+ template = "manage.html"
+ else:
+ template = "user.html"
+ context = {"template": template, "notifications": notifcations,
+ "current_date_time": timezone.now()}
+ return my_render_to_response(
+ request, 'yaksh/view_notifications.html', context
+ )
+
+
+@login_required
+@email_verified
+def mark_notification(request, message_uid=None):
+ user = request.user
+ if message_uid:
+ Notification.objects.mark_single_notification(
+ user.id, message_uid, True
+ )
+ else:
+ if request.method == 'POST':
+ msg_uuids = request.POST.getlist("uid")
+ Notification.objects.mark_bulk_msg_notifications(
+ user.id, msg_uuids, True)
+ messages.success(request, "Marked notifcation(s) as read")
+ return redirect(reverse("yaksh:view_notifications"))
+
+
+@login_required
+@email_verified
def course_forum(request, course_id):
user = request.user
base_template = 'user.html'
@@ -3291,12 +3324,14 @@ def course_forum(request, course_id):
if (not course.is_creator(user) and not course.is_teacher(user)
and not course.is_student(user)):
raise Http404('You are not enrolled in {0} course'.format(course.name))
- if 'search' in request.GET:
- search_term = request.GET['search']
- posts = course.post.filter(active=True, title__icontains=search_term)
+ search_term = request.GET.get('search_post')
+ if search_term:
+ posts = course.post.get_queryset().filter(
+ active=True, title__icontains=search_term)
else:
- posts = course.post.filter(active=True).order_by('-modified_at')
- paginator = Paginator(posts, 10)
+ posts = course.post.get_queryset().filter(
+ active=True).order_by('-modified_at')
+ paginator = Paginator(posts, 1)
page = request.GET.get('page')
posts = paginator.get_page(page)
if request.method == "POST":
@@ -3314,7 +3349,6 @@ def course_forum(request, course_id):
'user': user,
'course': course,
'base_template': base_template,
- 'posts': posts,
'moderator': moderator,
'objects': posts,
'form': form,