summaryrefslogtreecommitdiff
path: root/yaksh/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/views.py')
-rw-r--r--yaksh/views.py116
1 files changed, 35 insertions, 81 deletions
diff --git a/yaksh/views.py b/yaksh/views.py
index 1965191..bddea26 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -33,6 +33,7 @@ except ImportError:
from io import BytesIO as string_io
import re
# Local imports.
+from online_test.celery_settings import app
from yaksh.code_server import get_result as get_result_from_code_server
from yaksh.models import (
Answer, AnswerPaper, AssignmentUpload, Course, FileUpload, FloatTestCase,
@@ -57,7 +58,7 @@ 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 .tasks import regrade_papers, update_user_marks
from notifications_plugin.models import Notification
@@ -1842,7 +1843,10 @@ def download_quiz_csv(request, course_id, quiz_id):
attempt_number=attempt_number
).order_by("user__first_name")
que_summaries = [
- (f"Q-{que.id}-{que.summary}-{que.points}-marks", que.id) for que in questions
+ (f"Q-{que.id}-{que.summary}-{que.points}-marks", que.id,
+ f"Q-{que.id}-{que.summary}-comments"
+ )
+ for que in questions
]
user_data = list(answerpapers.values(
"user__username", "user__first_name", "user__last_name",
@@ -2224,20 +2228,24 @@ def regrade(request, course_id, questionpaper_id, question_id=None,
course.is_teacher(user)):
raise Http404('You are not allowed to view this page!')
questionpaper = get_object_or_404(QuestionPaper, pk=questionpaper_id)
- 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)
+ is_celery_alive = app.control.ping()
+ if is_celery_alive:
+ 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)
+ else:
+ msg = "Unable to submit for regrade. Please contact admin"
+ messages.warning(request, msg)
return redirect(
reverse("yaksh:grade_user", args=[quiz.id, course_id])
)
@@ -4045,78 +4053,24 @@ def upload_marks(request, course_id, questionpaper_id):
if not is_csv_file:
messages.warning(request, "The file uploaded is not a CSV file.")
return redirect('yaksh:monitor', quiz.id, course_id)
- try:
- reader = csv.DictReader(
- csv_file.read().decode('utf-8').splitlines(),
- dialect=dialect)
- except TypeError:
- messages.warning(request, "Bad CSV file")
- return redirect('yaksh:monitor', quiz.id, course_id)
- question_ids = _get_header_info(reader)
- _read_marks_csv(request, reader, course, question_paper, question_ids)
- return redirect('yaksh:monitor', quiz.id, course_id)
-
-
-def _get_header_info(reader):
- user_ids, question_ids = [], []
- fields = reader.fieldnames
- for field in fields:
- if field.startswith('Q') and field.count('-') > 0:
- qid = int(field.split('-')[1])
- if qid not in question_ids:
- question_ids.append(qid)
- return question_ids
-
-
-def _read_marks_csv(request, reader, course, question_paper, question_ids):
- messages.info(request, 'Marks Uploaded!')
- for row in reader:
- username = row['username']
- user = User.objects.filter(username=username).first()
- if user:
- answerpapers = question_paper.answerpaper_set.filter(course=course,
- user_id=user.id)
+ data = {
+ "course_id": course_id, "questionpaper_id": questionpaper_id,
+ "csv_data": csv_file.read().decode('utf-8').splitlines(),
+ "user_id": request.user.id
+ }
+ is_celery_alive = app.control.ping()
+ if is_celery_alive:
+ update_user_marks.delay(data)
+ msg = dedent("""
+ {0} is submitted for marks update. You will receive a
+ notification for the update status
+ """.format(quiz.description)
+ )
+ messages.info(request, msg)
else:
- messages.info(request, '{0} user not found!'.format(username))
- continue
- answerpaper = answerpapers.last()
- if not answerpaper:
- messages.info(request, '{0} has no answerpaper!'.format(username))
- continue
- answers = answerpaper.answers.all()
- questions = answerpaper.questions.all().values_list('id', flat=True)
- for qid in question_ids:
- question = Question.objects.filter(id=qid).first()
- if not question:
- messages.info(request,
- '{0} is an invalid question id!'.format(qid))
- continue
- if qid in questions:
- answer = answers.filter(question_id=qid).last()
- if not answer:
- answer = Answer(question_id=qid, marks=0, correct=False,
- answer='Created During Marks Update!',
- error=json.dumps([]))
- answer.save()
- answerpaper.answers.add(answer)
- key1 = 'Q-{0}-{1}-{2}-marks'.format(qid, question.summary,
- question.points)
- key2 = 'Q-{0}-{1}-comments'.format(qid, question.summary,
- question.points)
- if key1 in reader.fieldnames:
- try:
- answer.set_marks(float(row[key1]))
- except ValueError:
- messages.info(request,
- '{0} invalid marks!'.format(row[key1]))
- if key2 in reader.fieldnames:
- answer.set_comment(row[key2])
- answer.save()
- answerpaper.update_marks(state='completed')
- answerpaper.save()
- messages.info(request,
- 'Updated successfully for user: {0}, question: {1}'.format(
- username, question.summary))
+ msg = "Unable to submit for marks update. Please check with admin"
+ messages.warning(request, msg)
+ return redirect('yaksh:monitor', quiz.id, course_id)
@login_required