diff options
author | adityacp | 2021-03-15 19:09:46 +0530 |
---|---|---|
committer | adityacp | 2021-03-15 19:09:46 +0530 |
commit | 800bc1c002b4a57f6c307ca37726d85946a5d7cb (patch) | |
tree | 4f8f3b931b78eebb0ea3b88e49bd6ed21ec2cdd3 | |
parent | 542433598aad0efffee7619e1f113425147bcec0 (diff) | |
parent | 6fda19daaa06482b8eb52eeb62f9b0a15d0a3da6 (diff) | |
download | online_test-800bc1c002b4a57f6c307ca37726d85946a5d7cb.tar.gz online_test-800bc1c002b4a57f6c307ca37726d85946a5d7cb.tar.bz2 online_test-800bc1c002b4a57f6c307ca37726d85946a5d7cb.zip |
Fix conflicts
-rw-r--r-- | .travis.yml | 3 | ||||
-rw-r--r-- | yaksh/admin.py | 2 | ||||
-rw-r--r-- | yaksh/base_evaluator.py | 2 | ||||
-rw-r--r-- | yaksh/evaluator_tests/test_simple_question_types.py | 10 | ||||
-rw-r--r-- | yaksh/fixtures/marks_correct.csv | 5 | ||||
-rw-r--r-- | yaksh/fixtures/marks_header_missing.csv | 2 | ||||
-rw-r--r-- | yaksh/fixtures/marks_header_modified.csv | 2 | ||||
-rw-r--r-- | yaksh/fixtures/marks_invalid_data.csv | 2 | ||||
-rw-r--r-- | yaksh/fixtures/marks_invalid_question_id.csv | 2 | ||||
-rw-r--r-- | yaksh/fixtures/marks_invalid_user.csv | 2 | ||||
-rw-r--r-- | yaksh/fixtures/marks_not_attempted_question.csv | 2 | ||||
-rw-r--r-- | yaksh/fixtures/marks_single_question.csv | 2 | ||||
-rw-r--r-- | yaksh/models.py | 107 | ||||
-rw-r--r-- | yaksh/tasks.py | 116 | ||||
-rw-r--r-- | yaksh/templates/yaksh/grade_user.html | 3 | ||||
-rw-r--r-- | yaksh/templates/yaksh/monitor.html | 48 | ||||
-rw-r--r-- | yaksh/templatetags/custom_filters.py | 15 | ||||
-rw-r--r-- | yaksh/test_models.py | 88 | ||||
-rw-r--r-- | yaksh/test_views.py | 72 | ||||
-rw-r--r-- | yaksh/views.py | 157 |
20 files changed, 389 insertions, 253 deletions
diff --git a/.travis.yml b/.travis.yml index 0fad559..27b6dfe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,11 +29,14 @@ before_script: # command to run tests and coverage script: - coverage erase + - celery multi start 1 -A online_test worker -B - coverage run -p manage.py test -v 2 yaksh - coverage run -p manage.py test -v 2 stats - coverage run -p manage.py test -v 2 grades - coverage run -p manage.py test -v 2 yaksh.live_server_tests.load_test - coverage run -p manage.py test -v 2 api + - celery multi stop 1 + - find . -type f -name "*.pid" -exec rm -f {} \; after_success: - coverage combine diff --git a/yaksh/admin.py b/yaksh/admin.py index 011e24f..1010536 100644 --- a/yaksh/admin.py +++ b/yaksh/admin.py @@ -64,4 +64,4 @@ admin.site.register(Topic) admin.site.register(TableOfContents) admin.site.register(LessonQuizAnswer) admin.site.register(Answer) -admin.site.register(AssignmentUpload)
\ No newline at end of file +admin.site.register(AssignmentUpload) diff --git a/yaksh/base_evaluator.py b/yaksh/base_evaluator.py index 567d5ad..c7b5c54 100644 --- a/yaksh/base_evaluator.py +++ b/yaksh/base_evaluator.py @@ -44,7 +44,7 @@ class BaseEvaluator(object): """Returns a string without any null and substitute characters""" stripped = "" for c in string: - if ord(c) is not 26 and ord(c) is not 0: + if ord(c) != 26 and ord(c) != 0: stripped = stripped + c return ''.join(stripped) diff --git a/yaksh/evaluator_tests/test_simple_question_types.py b/yaksh/evaluator_tests/test_simple_question_types.py index 5edd545..13d639e 100644 --- a/yaksh/evaluator_tests/test_simple_question_types.py +++ b/yaksh/evaluator_tests/test_simple_question_types.py @@ -662,16 +662,8 @@ class ArrangeQuestionTestCases(unittest.TestCase): regrade_answer.answer = 1 regrade_answer.save() details = self.answerpaper.regrade(self.question1.id) - err_msg = dedent("""\ - User: {0}; Quiz: {1}; Question: {2}. - {3} answer not a list.""".format( - self.user.username, - self.quiz.description, - self.question1.summary, - self.question1.type - )) self.assertFalse(details[0]) - self.assertEqual(details[1], err_msg) + self.assertIn("arrange answer not a list", details[1]) # Try regrade with incorrect answer # When diff --git a/yaksh/fixtures/marks_correct.csv b/yaksh/fixtures/marks_correct.csv index 9134da5..d739644 100644 --- a/yaksh/fixtures/marks_correct.csv +++ b/yaksh/fixtures/marks_correct.csv @@ -1,4 +1,3 @@ -username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments +user__username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments student1,1,good work,1,nice -student2,1,good work,0,bad - +student2,1,good work,0,bad
\ No newline at end of file diff --git a/yaksh/fixtures/marks_header_missing.csv b/yaksh/fixtures/marks_header_missing.csv index 8c3a747..81b0c77 100644 --- a/yaksh/fixtures/marks_header_missing.csv +++ b/yaksh/fixtures/marks_header_missing.csv @@ -1,3 +1,3 @@ -username,Q-1212-Dummy1-1.0-marks +user__username,Q-1212-Dummy1-1.0-marks student1,0.9 student2,1 diff --git a/yaksh/fixtures/marks_header_modified.csv b/yaksh/fixtures/marks_header_modified.csv index 08ba31d..f6d6859 100644 --- a/yaksh/fixtures/marks_header_modified.csv +++ b/yaksh/fixtures/marks_header_modified.csv @@ -1,3 +1,3 @@ -username,Q-1212-Dummmy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments +user__username,Q-1212-Dummmy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments student1,0.75,fine work,1,not nice student2,1,good work,0,not okay diff --git a/yaksh/fixtures/marks_invalid_data.csv b/yaksh/fixtures/marks_invalid_data.csv index 44fb2bb..b4af15b 100644 --- a/yaksh/fixtures/marks_invalid_data.csv +++ b/yaksh/fixtures/marks_invalid_data.csv @@ -1,3 +1,3 @@ -username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments +user__username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments student1,NA,good work,1,nice student2,1,good work,0,bad diff --git a/yaksh/fixtures/marks_invalid_question_id.csv b/yaksh/fixtures/marks_invalid_question_id.csv index eb1d921..629a673 100644 --- a/yaksh/fixtures/marks_invalid_question_id.csv +++ b/yaksh/fixtures/marks_invalid_question_id.csv @@ -1,3 +1,3 @@ -username,Q-12112-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments +user__username,Q-12112-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments student1,1,good work,1,nice student2,1,good work,0,bad diff --git a/yaksh/fixtures/marks_invalid_user.csv b/yaksh/fixtures/marks_invalid_user.csv index bd31071..5d5c200 100644 --- a/yaksh/fixtures/marks_invalid_user.csv +++ b/yaksh/fixtures/marks_invalid_user.csv @@ -1,3 +1,3 @@ -username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments +user__username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments student1,1,good work,1,nice student452,1,good work,0,bad diff --git a/yaksh/fixtures/marks_not_attempted_question.csv b/yaksh/fixtures/marks_not_attempted_question.csv index 3c3e2e7..ecce363 100644 --- a/yaksh/fixtures/marks_not_attempted_question.csv +++ b/yaksh/fixtures/marks_not_attempted_question.csv @@ -1,3 +1,3 @@ -username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments +user__username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments,Q-1213-Dummy2-1.0-marks,Q-1213-Dummy2-comments student1,1,good work,1,nice student2,0.3,very good,1,good diff --git a/yaksh/fixtures/marks_single_question.csv b/yaksh/fixtures/marks_single_question.csv index 9677730..00b74fe 100644 --- a/yaksh/fixtures/marks_single_question.csv +++ b/yaksh/fixtures/marks_single_question.csv @@ -1,3 +1,3 @@ -username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments +user__username,Q-1212-Dummy1-1.0-marks,Q-1212-Dummy1-comments student1,0.5,okay work student2,1,good work diff --git a/yaksh/models.py b/yaksh/models.py index dd3838a..613bf00 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -123,11 +123,13 @@ MOD_GROUP_NAME = 'moderator' def get_assignment_dir(instance, filename): - folder_name = instance.course.name.replace(" ", "_") - sub_folder_name = instance.question_paper.quiz.description.replace( - " ", "_") - return os.sep.join((folder_name, sub_folder_name, instance.user.username, - str(instance.assignmentQuestion.id), + course_id = instance.answer_paper.course_id + quiz_id = instance.answer_paper.question_paper.quiz_id + folder = f'Course_{course_id}' + sub_folder = f'Quiz_{quiz_id}' + user = instance.answer_paper.user.username + return os.sep.join((folder, sub_folder, user, + str(instance.assignmentQuestion_id), filename )) @@ -1382,7 +1384,9 @@ class Question(models.Model): # Solution for the question. solution = models.TextField(blank=True) - content = GenericRelation("TableOfContents", related_query_name='questions') + content = GenericRelation( + "TableOfContents", related_query_name='questions' + ) tc_code_types = { "python": [ @@ -1441,7 +1445,7 @@ class Question(models.Model): for file in files] if self.type == "upload": assignment_files = AssignmentUpload.objects.filter( - assignmentQuestion=self, user=user + assignmentQuestion=self ) if assignment_files: metadata['assign_files'] = [(file.assignmentFile.url, False) @@ -1840,7 +1844,8 @@ class QuestionPaper(models.Model): all_questions = questions return all_questions - def make_answerpaper(self, user, ip, attempt_num, course_id, special=False): + def make_answerpaper(self, + user, ip, attempt_num, course_id, special=False): """Creates an answer paper for the user to attempt the quiz""" try: ans_paper = AnswerPaper.objects.get(user=user, @@ -2037,6 +2042,7 @@ class AnswerPaperManager(models.Manager): ).order_by("id").values( "answerpaper__id", "question_id", "correct", "answer" ) + def _get_per_tc_data(answers, q_type): tc = [] for answer in answers["answer"]: @@ -2049,11 +2055,11 @@ class AnswerPaperManager(models.Manager): df = pd.DataFrame(answers) if not df.empty: for question in all_questions: - que = df[df["question_id"]==question.id].groupby( + que = df[df["question_id"] == question.id].groupby( "answerpaper__id").tail(1) if not que.empty: total_attempts = que.shape[0] - correct_attempts = que[que["correct"]==True].shape[0] + correct_attempts = que[que["correct"] == True].shape[0] per_tc_ans = {} if question.type in ["mcq", "mcc"]: per_tc_ans = _get_per_tc_data(que, question.type) @@ -2270,7 +2276,7 @@ class AnswerPaper(models.Model): ans_data = None if not df.empty: ans_data = df.groupby("question_id").tail(1) - for que_summary, que_id in question_ids: + for que_summary, que_id, que_comments in question_ids: if ans_data is not None: ans = ans_data['question_id'].to_list() marks = ans_data['marks'].to_list() @@ -2281,6 +2287,7 @@ class AnswerPaper(models.Model): que_data[que_summary] = 0 else: que_data[que_summary] = 0 + que_data[que_comments] = "NA" return que_data def current_question(self): @@ -2579,25 +2586,17 @@ class AnswerPaper(models.Model): self.user, self.question_paper.quiz.description, question_id ) - return False, msg + 'Question not in the answer paper.' + return False, f'{msg} Question not in the answer paper.' user_answer = self.answers.filter(question=question).last() - if not user_answer: - return False, msg + 'Did not answer.' + if not user_answer or not user_answer.answer: + return False, f'{msg} Did not answer.' if question.type in ['mcc', 'arrange']: try: answer = literal_eval(user_answer.answer) if type(answer) is not list: - return (False, - msg + '{0} answer not a list.'.format( - question.type - ) - ) + return (False, f'{msg} {question.type} answer not a list.') except Exception: - return (False, - msg + '{0} answer submission error'.format( - question.type - ) - ) + return (False, f'{msg} {question.type} answer submission error') else: answer = user_answer.answer json_data = question.consolidate_answer_data(answer) \ @@ -2645,16 +2644,19 @@ class AssignmentUploadManager(models.Manager): def get_assignments(self, qp, que_id=None, user_id=None, course_id=None): if que_id and user_id: assignment_files = AssignmentUpload.objects.filter( - assignmentQuestion_id=que_id, user_id=user_id, - question_paper=qp, course_id=course_id + assignmentQuestion_id=que_id, + answer_paper__user_id=user_id, + answer_paper__question_paper=qp, + answer_paper__course_id=course_id ) file_name = User.objects.get(id=user_id).get_full_name() else: assignment_files = AssignmentUpload.objects.filter( - question_paper=qp, course_id=course_id + answer_paper__question_paper=qp, + answer_paper__course_id=course_id ) file_name = "{0}_Assignment_files".format( - assignment_files[0].course.name + assignment_files[0].answer_paper.course.name ) return assignment_files, file_name @@ -2662,17 +2664,18 @@ class AssignmentUploadManager(models.Manager): ############################################################################## class AssignmentUpload(models.Model): - user = models.ForeignKey(User, on_delete=models.CASCADE) assignmentQuestion = models.ForeignKey(Question, on_delete=models.CASCADE) - assignmentFile = models.FileField( - upload_to=get_assignment_dir, max_length=255 - ) - question_paper = models.ForeignKey(QuestionPaper, blank=True, null=True, - on_delete=models.CASCADE) - course = models.ForeignKey(Course, null=True, blank=True, - on_delete=models.CASCADE) + assignmentFile = models.FileField(upload_to=get_assignment_dir, + max_length=255) + answer_paper = models.ForeignKey(AnswerPaper, blank=True, null=True, + on_delete=models.CASCADE) + upload_date = models.DateTimeField(auto_now=True) + objects = AssignmentUploadManager() + def __str__(self): + return f'Assignment File of the user {self.answer_paper.user}' + ############################################################################## class TestCase(models.Model): @@ -2896,7 +2899,6 @@ class TOCManager(models.Manager): toc.get_toc_as_yaml(file_path) return file_path - def get_question_stats(self, toc_id): answers = LessonQuizAnswer.objects.get_queryset().filter( toc_id=toc_id).order_by('id') @@ -2930,7 +2932,7 @@ class TOCManager(models.Manager): if j not in mydata: mydata[j] = 1 else: - mydata[j] +=1 + mydata[j] += 1 data = mydata.copy() if is_percent: for key, value in data.items(): @@ -2976,17 +2978,20 @@ class TOCManager(models.Manager): if not is_valid_time_format(time): messages.append( (False, - f"Invalid time format in {name}. " + f"Invalid time format in {name}. " "Format should be 00:00:00") - ) + ) else: if content_type == 1: topic = Topic.objects.create(**content) toc.append(TableOfContents( - course_id=course_id, lesson_id=lesson_id, time=time, - content_object=topic, content=content_type + course_id=course_id, + lesson_id=lesson_id, time=time, + content_object=topic, content=content_type )) - messages.append((True, f"{topic.name} added successfully")) + messages.append( + (True, f"{topic.name} added successfully") + ) else: content['user'] = user test_cases = content.pop("testcase") @@ -3003,10 +3008,13 @@ class TOCManager(models.Manager): else: que = Question.objects.create(**content) for test_case in test_cases: - test_case_type = test_case.pop('test_case_type') + test_case_type = test_case.pop( + 'test_case_type' + ) model_class = get_model_class(test_case_type) model_class.objects.get_or_create( - question=que, **test_case, type=test_case_type + question=que, + **test_case, type=test_case_type ) toc.append(TableOfContents( course_id=course_id, lesson_id=lesson_id, @@ -3022,7 +3030,12 @@ class TOCManager(models.Manager): class TableOfContents(models.Model): - toc_types = ((1, "Topic"), (2, "Graded Quiz"), (3, "Exercise"), (4, "Poll")) + toc_types = ( + (1, "Topic"), + (2, "Graded Quiz"), + (3, "Exercise"), + (4, "Poll") + ) course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='course') lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, @@ -3040,7 +3053,7 @@ class TableOfContents(models.Model): def get_toc_text(self): if self.content == 1: - content_name = self.content_object.name + content_name = self.content_object.name else: content_name = self.content_object.summary return content_name diff --git a/yaksh/tasks.py b/yaksh/tasks.py index 1c4658b..5068c64 100644 --- a/yaksh/tasks.py +++ b/yaksh/tasks.py @@ -1,6 +1,8 @@ # Python Imports from __future__ import absolute_import, unicode_literals from textwrap import dedent +import csv +import json # Django and celery imports from celery import shared_task @@ -8,7 +10,10 @@ from django.urls import reverse from django.shortcuts import get_object_or_404 # Local imports -from .models import Course, QuestionPaper, Quiz, AnswerPaper, CourseStatus +from .models import ( + Course, QuestionPaper, Quiz, AnswerPaper, CourseStatus, User, Question, + Answer +) from notifications_plugin.models import NotificationMessage, Notification @@ -80,3 +85,112 @@ def regrade_papers(data): notification = Notification.objects.add_single_notification( user_id, nm.id ) + + +@shared_task +def update_user_marks(data): + request_user = data.get("user_id") + course_id = data.get("course_id") + questionpaper_id = data.get("questionpaper_id") + csv_data = data.get("csv_data") + question_paper = QuestionPaper.objects.get(id=questionpaper_id) + def _get_header_info(reader): + 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 + try: + reader = csv.DictReader(csv_data) + question_ids = _get_header_info(reader) + _read_marks_csv( + reader, request_user, course_id, question_paper, question_ids + ) + except TypeError: + url = reverse( + "yaksh:monitor", args=[question_paper.quiz_id, course_id] + ) + message = dedent(""" + Unable to update quiz marks. Please re-upload correct CSV file + Click <a href="{0}">here</a> to view + """.format(url) + ) + nm = NotificationMessage.objects.add_single_message( + request_user, "{0} marks update status".format( + question_paper.quiz.description + ), message, "warning" + ) + notification = Notification.objects.add_single_notification( + request_user, nm.id + ) + + +def _read_marks_csv( + reader, request_user, course_id, question_paper, question_ids): + update_status = [] + for row in reader: + username = row['user__username'] + user = User.objects.filter(username=username).first() + if user: + answerpapers = question_paper.answerpaper_set.filter( + course_id=course_id, user_id=user.id) + else: + update_status.append(f'{username} user not found!') + continue + answerpaper = answerpapers.last() + if not answerpaper: + update_status.append(f'{username} has no answerpaper!') + continue + answers = answerpaper.answers.all() + questions = answerpaper.questions.values_list('id', flat=True) + for qid in question_ids: + question = Question.objects.filter(id=qid).first() + if not question: + update_status.append(f'{qid} is an invalid question id!') + 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='', 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) + if key1 in reader.fieldnames: + try: + answer.set_marks(float(row[key1])) + except ValueError: + update_status.append(f'{row[key1]} invalid marks!') + if key2 in reader.fieldnames: + answer.set_comment(row[key2]) + answer.save() + answerpaper.update_marks(state='completed') + answerpaper.save() + update_status.append( + 'Updated successfully for user: {0}, question: {1}'.format( + username, question.summary) + ) + url = reverse( + "yaksh:grade_user", + args=[question_paper.quiz_id, course_id] + ) + message = dedent(""" + Quiz mark update is complete. + Click <a href="{0}">here</a> to view + <br><br>{1} + """.format(url, "\n".join(update_status)) + ) + summary = "{0} marks update status".format( + question_paper.quiz.description + ) + nm = NotificationMessage.objects.add_single_message( + request_user, summary, message, "info" + ) + notification = Notification.objects.add_single_notification( + request_user, nm.id + ) diff --git a/yaksh/templates/yaksh/grade_user.html b/yaksh/templates/yaksh/grade_user.html index 4e1db2b..32cf09c 100644 --- a/yaksh/templates/yaksh/grade_user.html +++ b/yaksh/templates/yaksh/grade_user.html @@ -559,6 +559,9 @@ function searchNames() { {% endif %} </div> </div> + <br> + <b>Comment:</b> + <textarea class="form-control" readonly="">{{ans.answer.comment}}</textarea> </div> </div> <br> diff --git a/yaksh/templates/yaksh/monitor.html b/yaksh/templates/yaksh/monitor.html index c7755e7..6fd3cb1 100644 --- a/yaksh/templates/yaksh/monitor.html +++ b/yaksh/templates/yaksh/monitor.html @@ -52,7 +52,8 @@ $(document).ready(function() {% if quiz %} {% if papers %} <div class="row"> - <div class="card col-md-3"> + <div class="col-md-3"> + <div class="card"> <div class="card-body"> <div class="table-responsive"> <table id="course-detail" class="table"> @@ -83,34 +84,41 @@ $(document).ready(function() </table> </div> </div> - </div> - <div class="col-md-9"> - <div class="row"> - <div class="col-md-4"> + <div class="card-body"> + <div class="col"> + <div class="badge badge-info"> + Auto-Refreshes every 5 minutes + </div> + </div> + <br> + <div class="col"> <button type="button" class="btn btn-info" data-toggle="modal" data-target="#csvModal"> <i class="fa fa-download"></i> Download CSV </button> </div> - <div class="col-md-4"> + <br> + <div class="col"> <a href="{% url 'yaksh:show_statistics' papers.0.question_paper.id course.id %}" class="btn btn-primary"> <i class="fa fa-line-chart"></i> Question Statistics </a> </div> - <div class="col-md-4"> - <div class="badge badge-info"> - Auto-Refreshes every 5 minutes - </div> - </div> </div> - <hr> + </div> + </div> + <div class="col-md-9"> <div class="row"> - <div class="col-md-4"> - <p> - <b> - - Download the CSV file from the button above<br /> - - Edit and upload the same <br /> - </b> - </p> + <div class="col-md-5"> + <ul> + <li> + Download the CSV file from the button + </li> + <li> + Edit and upload the same + </li> + <li> + <b>Note: Do not change the CSV Headers</b> + </li> + </ul> </div> <div class="col-md-6"> <form id="upload_users" action="{% url 'yaksh:upload_marks' course.id papers.0.question_paper.id %}" method="POST" enctype="multipart/form-data"> @@ -123,7 +131,7 @@ $(document).ready(function() </form> </div> </div> - <br> + <hr> <div class="row"> <div class="col-md-3"> <b>Select Attempt number:</b> diff --git a/yaksh/templatetags/custom_filters.py b/yaksh/templatetags/custom_filters.py index b404758..bd97d2e 100644 --- a/yaksh/templatetags/custom_filters.py +++ b/yaksh/templatetags/custom_filters.py @@ -84,12 +84,15 @@ def get_answer_for_arrange_options(ans, question): ans = ans.decode("utf-8") else: ans = str(ans) - answer = literal_eval(ans) - testcases = [] - for answer_id in answer: - tc = question.get_test_case(id=int(answer_id)) - testcases.append(tc) - return testcases + try: + answer = literal_eval(ans) + testcases = [] + for answer_id in answer: + tc = question.get_test_case(id=int(answer_id)) + testcases.append(tc) + return testcases + except Exception: + return None @register.filter(name='replace_spaces') diff --git a/yaksh/test_models.py b/yaksh/test_models.py index a424b36..2ee6e82 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -107,6 +107,7 @@ def setUpModule(): MicroManager.objects.create(manager=user, course=course, quiz=quiz, student=course_user) + def tearDownModule(): User.objects.all().delete() Question.objects.all().delete() @@ -284,8 +285,9 @@ class LearningModuleTestCases(unittest.TestCase): self.learning_module_two = LearningModule.objects.get(name='LM2') self.creator = User.objects.get(username='creator') self.student = User.objects.get(username='course_user') - self.learning_unit_one = LearningUnit.objects.get(id=1) - self.learning_unit_two = LearningUnit.objects.get(id=2) + learning_units = self.learning_module.learning_unit.order_by("order") + self.learning_unit_one = learning_units[0] + self.learning_unit_two = learning_units[1] self.quiz = Quiz.objects.get(description='demo quiz 1') self.lesson = Lesson.objects.get(name='L1') self.course = Course.objects.get(name='Python Course') @@ -1420,10 +1422,10 @@ class AnswerPaperTestCases(unittest.TestCase): expected_score = {"Q4": 1.0} # When score = self.answerpaper_single_question.get_per_question_score( - [(question_name, question_id)] + [(question_name, question_id, f"{question_id}-comments")] ) # Then - self.assertEqual(score, expected_score) + self.assertEqual(score['Q4'], expected_score['Q4']) # Given question_id = self.question2.id @@ -1431,10 +1433,10 @@ class AnswerPaperTestCases(unittest.TestCase): expected_score = {"Q2": 0.0} # When score = self.answerpaper.get_per_question_score( - [(question_name, question_id)] + [(question_name, question_id, f"{question_id}-comments")] ) # Then - self.assertEqual(score, expected_score) + self.assertEqual(score["Q2"], expected_score["Q2"]) # Given question_id = 131 @@ -1442,10 +1444,10 @@ class AnswerPaperTestCases(unittest.TestCase): expected_score = {'NA': 0} # When score = self.answerpaper.get_per_question_score( - [(question_name, question_id)] + [(question_name, question_id, f"{question_id}-comments")] ) # Then - self.assertEqual(score, expected_score) + self.assertEqual(score["NA"], expected_score["NA"]) def test_returned_question_is_not_none(self): # Test add_completed_question and next_question @@ -2201,17 +2203,37 @@ class TestCaseTestCases(unittest.TestCase): class AssignmentUploadTestCases(unittest.TestCase): + def setUp(self): - self.user1 = User.objects.get(username="creator") - self.user1.first_name = "demo" - self.user1.last_name = "user" - self.user1.save() - self.user2 = User.objects.get(username="demo_user3") - self.user2.first_name = "demo" - self.user2.last_name = "user3" - self.user2.save() - self.quiz = Quiz.objects.get(description="demo quiz 1") - self.course = Course.objects.get(name="Python Course") + self.user1 = User.objects.create_user( + username='creator1', + password='demo', + email='demo@test1.com' + ) + self.user2 = User.objects.create_user( + username='creator2', + password='demo', + email='demo@test2.com' + ) + self.quiz = Quiz.objects.create( + start_date_time=datetime( + 2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc + ), + end_date_time=datetime( + 2199, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc + ), + duration=30, active=True, + attempts_allowed=1, time_between_attempts=0, + description='demo quiz 1', pass_criteria=0, + instructions="Demo Instructions" + ) + + self.course = Course.objects.create( + name="Python Course", + enrollment="Enroll Request", + creator=self.user1 + ) + self.questionpaper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=0.0, shuffle_questions=True ) @@ -2223,17 +2245,22 @@ class AssignmentUploadTestCases(unittest.TestCase): self.questionpaper.fixed_question_order = "{0}".format( self.question.id) self.questionpaper.fixed_questions.add(self.question) + + attempt = 1 + ip = '127.0.0.1' + self.answerpaper1 = self.questionpaper.make_answerpaper( + self.user1, ip, attempt, self.course.id + ) + file_path1 = os.path.join(tempfile.gettempdir(), "upload1.txt") file_path2 = os.path.join(tempfile.gettempdir(), "upload2.txt") self.assignment1 = AssignmentUpload.objects.create( - user=self.user1, assignmentQuestion=self.question, - assignmentFile=file_path1, question_paper=self.questionpaper, - course=self.course + assignmentQuestion=self.question, + assignmentFile=file_path1, answer_paper=self.answerpaper1, ) self.assignment2 = AssignmentUpload.objects.create( - user=self.user2, assignmentQuestion=self.question, - assignmentFile=file_path2, question_paper=self.questionpaper, - course=self.course + assignmentQuestion=self.question, + assignmentFile=file_path2, answer_paper=self.answerpaper1, ) def test_get_assignments_for_user_files(self): @@ -2242,7 +2269,7 @@ class AssignmentUploadTestCases(unittest.TestCase): self.user1.id, self.course.id ) self.assertIn("upload1.txt", assignment_files[0].assignmentFile.name) - self.assertEqual(assignment_files[0].user, self.user1) + self.assertEqual(assignment_files[0].answer_paper.user, self.user1) actual_file_name = self.user1.get_full_name().replace(" ", "_") file_name = file_name.replace(" ", "_") self.assertEqual(file_name, actual_file_name) @@ -2253,7 +2280,8 @@ class AssignmentUploadTestCases(unittest.TestCase): ) files = [os.path.basename(file.assignmentFile.name) for file in assignment_files] - question_papers = [file.question_paper for file in assignment_files] + question_papers = [ + file.answer_paper.question_paper for file in assignment_files] self.assertIn("upload1.txt", files) self.assertIn("upload2.txt", files) self.assertEqual(question_papers[0].quiz, self.questionpaper.quiz) @@ -2261,6 +2289,14 @@ class AssignmentUploadTestCases(unittest.TestCase): file_name = file_name.replace(" ", "_") self.assertIn(actual_file_name, file_name) + def tearDown(self): + self.questionpaper.delete() + self.question.delete() + self.course.delete() + self.quiz.delete() + self.user2.delete() + self.user1.delete() + class CourseStatusTestCases(unittest.TestCase): def setUp(self): diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 58b7506..925c3d1 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -23,7 +23,6 @@ from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files import File from django.contrib.messages import get_messages from django.contrib.contenttypes.models import ContentType -from celery.contrib.testing.worker import start_worker from django.test import SimpleTestCase @@ -41,6 +40,8 @@ from online_test.celery_settings import app from notifications_plugin.models import Notification +app.conf.update(CELERY_ALWAYS_EAGER=True) + class TestUserRegistration(TestCase): def setUp(self): @@ -938,36 +939,38 @@ class TestDownloadAssignment(TestCase): ) self.question_paper.fixed_questions.add(self.question) + attempt = 1 + ip = '127.0.0.1' + self.answerpaper1 = self.question_paper.make_answerpaper( + self.student1, ip, attempt, self.course.id + ) + + self.answerpaper2 = self.question_paper.make_answerpaper( + self.student2, ip, attempt, self.course.id + ) + # create assignment file assignment_file1 = SimpleUploadedFile("file1.txt", b"Test") assignment_file2 = SimpleUploadedFile("file2.txt", b"Test") + self.assignment1 = AssignmentUpload.objects.create( - user=self.student1, assignmentQuestion=self.question, - course=self.course, - assignmentFile=assignment_file1, question_paper=self.question_paper + assignmentQuestion=self.question, + assignmentFile=assignment_file1, answer_paper=self.answerpaper1 ) self.assignment2 = AssignmentUpload.objects.create( - user=self.student2, assignmentQuestion=self.question, - course=self.course, - assignmentFile=assignment_file2, question_paper=self.question_paper + assignmentQuestion=self.question, + assignmentFile=assignment_file2, answer_paper=self.answerpaper1 + ) + + self.assignment1 = AssignmentUpload.objects.create( + assignmentQuestion=self.question, + assignmentFile=assignment_file1, answer_paper=self.answerpaper2 + ) + self.assignment2 = AssignmentUpload.objects.create( + assignmentQuestion=self.question, + assignmentFile=assignment_file2, answer_paper=self.answerpaper2 ) - def tearDown(self): - self.client.logout() - self.user.delete() - self.student1.delete() - self.student2.delete() - self.assignment1.delete() - self.assignment2.delete() - self.quiz.delete() - self.course.delete() - self.learning_module.delete() - self.learning_unit.delete() - self.mod_group.delete() - dir_name = self.course.name.replace(" ", "_") - file_path = os.sep.join((settings.MEDIA_ROOT, dir_name)) - if os.path.exists(file_path): - shutil.rmtree(file_path) def test_download_assignment_denies_student(self): """ @@ -1037,10 +1040,27 @@ class TestDownloadAssignment(TestCase): zip_file = string_io(response.content) zipped_file = zipfile.ZipFile(zip_file, 'r') self.assertIsNone(zipped_file.testzip()) - self.assertIn('file2.txt', zipped_file.namelist()[0]) + self.assertIn('file1.txt', zipped_file.namelist()[0]) zip_file.close() zipped_file.close() + def tearDown(self): + self.client.logout() + self.user.delete() + self.student1.delete() + self.student2.delete() + self.assignment1.delete() + self.assignment2.delete() + self.quiz.delete() + self.learning_module.delete() + self.learning_unit.delete() + self.mod_group.delete() + dir_name = f'Course_{self.course.id}' + file_path = os.sep.join((settings.MEDIA_ROOT, dir_name)) + if os.path.exists(file_path): + shutil.rmtree(file_path) + self.course.delete() + class TestAddQuiz(TestCase): def setUp(self): @@ -4418,9 +4438,6 @@ class TestGrader(SimpleTestCase): end_time=timezone.now()+timezone.timedelta(minutes=20), ) - self.celery_worker = start_worker(app) - self.celery_worker.__enter__() - def tearDown(self): User.objects.all().delete() Course.objects.all().delete() @@ -4429,7 +4446,6 @@ class TestGrader(SimpleTestCase): QuestionPaper.objects.all().delete() AnswerPaper.objects.all().delete() self.mod_group.delete() - self.celery_worker.__exit__(None, None, None) def test_regrade_denies_anonymous(self): # Given diff --git a/yaksh/views.py b/yaksh/views.py index 8422110..7b1e038 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 @@ -720,9 +721,7 @@ def show_question(request, question, paper, error_message=None, if question.type == 'upload': assignment_files = AssignmentUpload.objects.filter( assignmentQuestion_id=question.id, - course_id=course_id, - user=request.user, - question_paper_id=paper.question_paper_id + answer_paper=paper ) files = FileUpload.objects.filter(question_id=question.id, hide=False) course = Course.objects.get(id=course_id) @@ -856,22 +855,15 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None, course_id=course_id, module_id=module_id, previous_question=current_question ) + uploaded_files = [] for fname in assignment_filename: fname._name = fname._name.replace(" ", "_") - # assignment_files = AssignmentUpload.objects.filter( - # assignmentQuestion=current_question, course_id=course_id, - # assignmentFile__icontains=fname, user=user, - # question_paper=questionpaper_id) - # if assignment_files.exists(): - # assign_file = assignment_files.first() - # if os.path.exists(assign_file.assignmentFile.path): - # os.remove(assign_file.assignmentFile.path) - # assign_file.delete() - AssignmentUpload.objects.create( - user=user, assignmentQuestion=current_question, - course_id=course_id, - assignmentFile=fname, question_paper_id=questionpaper_id - ) + uploaded_files.append(AssignmentUpload( + assignmentQuestion=current_question, + assignmentFile=fname, + answer_paper_id=paper.id + )) + AssignmentUpload.objects.bulk_create(uploaded_files) user_answer = 'ASSIGNMENT UPLOADED' if not current_question.grade_assignment_upload: new_answer = Answer( @@ -1842,7 +1834,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", @@ -1874,6 +1869,7 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None, and update all their marks and also give comments for each paper. """ current_user = request.user + papers = AnswerPaper.objects.filter(user=current_user) if not is_moderator(current_user): raise Http404('You are not allowed to view this page!') if not course_id: @@ -1898,7 +1894,8 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None, course.is_teacher(current_user): raise Http404('This course does not belong to you') has_quiz_assignments = AssignmentUpload.objects.filter( - course_id=course_id, question_paper_id__in=questionpaper_id + answer_paper__course_id=course_id, + answer_paper__question_paper_id__in=questionpaper_id ).exists() context = { "users": user_details, @@ -1917,9 +1914,11 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None, attempt_number = attempts[0].attempt_number except IndexError: raise Http404('No attempts for paper') + has_user_assignments = AssignmentUpload.objects.filter( - course_id=course_id, question_paper_id__in=questionpaper_id, - user_id=user_id + answer_paper__course_id=course_id, + answer_paper__question_paper_id__in=questionpaper_id, + answer_paper__user_id=user_id ).exists() user = User.objects.get(id=user_id) data = AnswerPaper.objects.get_user_data( @@ -2186,8 +2185,8 @@ def view_answerpaper(request, questionpaper_id, course_id): data = AnswerPaper.objects.get_user_data(user, questionpaper_id, course_id) has_user_assignments = AssignmentUpload.objects.filter( - user=user, course_id=course.id, - question_paper_id=questionpaper_id + answer_paper__user=user, answer_paper__course_id=course.id, + answer_paper__question_paper_id=questionpaper_id ).exists() context = {'data': data, 'quiz': quiz, 'course_id': course.id, "has_user_assignments": has_user_assignments} @@ -2224,20 +2223,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]) ) @@ -2383,7 +2386,7 @@ def download_assignment_file(request, quiz_id, course_id, zipfile_name = string_io() zip_file = zipfile.ZipFile(zipfile_name, "w") for f_name in assignment_files: - folder = f_name.user.get_full_name().replace(" ", "_") + folder = f_name.answer_paper.user.get_full_name().replace(" ", "_") sub_folder = f_name.assignmentQuestion.summary.replace(" ", "_") folder_name = os.sep.join((folder, sub_folder, os.path.basename( f_name.assignmentFile.name)) @@ -4045,78 +4048,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 |