diff options
author | adityacp | 2016-08-30 22:39:45 +0530 |
---|---|---|
committer | adityacp | 2016-08-30 22:39:45 +0530 |
commit | 3b8a17eb28ff5360449c4a1b6098860069fc0dc3 (patch) | |
tree | 4d36aff10190e6ee97731b13e987df217124551d | |
parent | e8769f5d8c43ee7c2655133b676ae53d2987c79c (diff) | |
parent | aefc8eed3b0c18520059b4005978f1db9cf5591b (diff) | |
download | online_test-3b8a17eb28ff5360449c4a1b6098860069fc0dc3.tar.gz online_test-3b8a17eb28ff5360449c4a1b6098860069fc0dc3.tar.bz2 online_test-3b8a17eb28ff5360449c4a1b6098860069fc0dc3.zip |
removed extract_files and read_json files from views
-rw-r--r-- | yaksh/admin.py | 5 | ||||
-rw-r--r-- | yaksh/documentation/installation.rst | 50 | ||||
-rw-r--r-- | yaksh/file_utils.py | 22 | ||||
-rw-r--r-- | yaksh/models.py | 38 | ||||
-rw-r--r-- | yaksh/templates/yaksh/addteacher.html | 4 | ||||
-rw-r--r-- | yaksh/templates/yaksh/courses.html | 79 | ||||
-rw-r--r-- | yaksh/templates/yaksh/question.html | 21 | ||||
-rw-r--r-- | yaksh/templates/yaksh/user_data.html | 10 | ||||
-rw-r--r-- | yaksh/test_models.py | 141 | ||||
-rw-r--r-- | yaksh/urls.py | 1 | ||||
-rw-r--r-- | yaksh/views.py | 112 |
11 files changed, 345 insertions, 138 deletions
diff --git a/yaksh/admin.py b/yaksh/admin.py index 2ce3ac4..4ef2f3d 100644 --- a/yaksh/admin.py +++ b/yaksh/admin.py @@ -1,10 +1,13 @@ from yaksh.models import Question, Quiz from yaksh.models import TestCase, StandardTestCase, StdoutBasedTestCase, Course +from yaksh.models import Question, Quiz, Course, QuestionPaper +from yaksh.models import TestCase, StandardTestCase, StdoutBasedTestCase from django.contrib import admin admin.site.register(Question) admin.site.register(TestCase) admin.site.register(StandardTestCase) admin.site.register(StdoutBasedTestCase) -admin.site.register(Quiz) admin.site.register(Course) +admin.site.register(Quiz) +admin.site.register(QuestionPaper) diff --git a/yaksh/documentation/installation.rst b/yaksh/documentation/installation.rst index 4acee61..51efea7 100644 --- a/yaksh/documentation/installation.rst +++ b/yaksh/documentation/installation.rst @@ -64,4 +64,52 @@ This starts the code server **And entering the following admin credentials** * Username: admin - * Password: admin
\ No newline at end of file + * Password: admin + +Running The Code Server +----------------------- + +**Local Instance**: + +In a new terminal run the command:: + + sudo python /path/to/code_server.py + +Keep this instance running in the background + +**Using Docker**: + +1. Install docker + +2. Create a Docker Image using the Docker file: + + * Go to the directory where the project is located:: + + cd /path/to/online_test + + * Build a docker image using the Dockerfile:: + + sudo docker build --tag=yaksh_code_server:v1 . + +3. Start a Docker container:: + + docker run -d -p 8001:8001 -p 53579:53579 -v /path/to/online_test/yaksh/output:/src/yaksh/output yaksh_code_server:v1 + +**Note**: + * The default ports on which the code server runs and the pool port on which the former ports are available is specified in online_test/yaksh/settings.py. The code server also supports multiple ports + + * The server port is 8001 by default, this can be changed in the settings:: + + SERVER_PORTS = 8001 + + * Multiple ports can be specified as:: + + SERVER_PORTS = [8001, 8002, 8003, 8004, 8005] # Or use range(8001, 8040) for larger number of ports + + * The default pool port is 53579 by default, this can be changed in the settings:: + + SERVER_POOL_PORT = 53579 + + * The docker command to start a docker container when using multiple ports is:: + + docker run -d -p 8001-8039:8001-8039 -p 53579:53579 yaksh_code_server:v1 diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 8f6f6e5..afcf9e8 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -13,12 +13,10 @@ def copy_files(file_paths): file_name = os.path.basename(file_path) files.append(file_name) shutil.copy(file_path, os.getcwd()) - if extract and zipfile.is_zipfile(file_name): - unzip = zipfile.ZipFile(file_name) - for zip_files in unzip.namelist(): - files.append(zip_files) - unzip.extractall() - unzip.close() + if extract: + z_files = extract_files(file_name) + for file in z_files: + files.append(file) return files @@ -31,3 +29,15 @@ def delete_files(files): os.remove(file) else: shutil.rmtree(file) + + +def extract_files(zip_file): + zfiles = [] + if zipfile.is_zipfile(zip_file): + zip_file = zipfile.ZipFile(zip_file, 'r') + for z_file in zip_file.namelist(): + zfiles.append(z_file) + zip_file.extractall() + zip_file.close() + return zfiles + diff --git a/yaksh/models.py b/yaksh/models.py index 5bb44fa..e296524 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -16,6 +16,7 @@ import pytz import os import shutil import zipfile +import tempfile languages = ( @@ -292,7 +293,7 @@ class Question(models.Model): files_list = [] for f in files: zip_file.write(f.file.path, (os.path.basename(f.file.path))) - files_list = os.path.basename(f.file.path) + files_list.append(os.path.basename(f.file.path)) return files_list def _add_files_to_db(self, file_names): @@ -305,11 +306,13 @@ class Question(models.Model): def _add_json_to_zip(self, zip_file, q_dict): json_data = json.dumps(q_dict, indent=2) - with open("questions_dump.json", "w") as json_file: + tmp_file_path = tempfile.mkdtemp() + json_path = os.path.join(tmp_file_path, "questions_dump.json") + with open(json_path, "w") as json_file: json_file.write(json_data) - zip_file.write(json_file.name) + zip_file.write(json_path, os.path.basename(json_path)) zip_file.close() - os.remove(json_file.name) + shutil.rmtree(tmp_file_path) def __unicode__(self): return self.summary @@ -603,7 +606,9 @@ class QuestionPaper(models.Model): if self.quiz.has_prerequisite(): prerequisite = self._get_prequisite_paper() return prerequisite._is_questionpaper_passed(user) - + + def __unicode__(self): + return "Question Paper for " + self.quiz.description ############################################################################### class QuestionSet(models.Model): @@ -817,21 +822,28 @@ class AnswerPaper(models.Model): Adds the completed question to the list of answered questions and returns the next question. """ + next_question = self.next_question(question_id) self.questions_answered.add(question_id) self.questions_unanswered.remove(question_id) + if next_question.id == int(question_id): + return None + return next_question - return self.current_question() - - def skip(self, question_id): + def next_question(self, question_id): """ Skips the current question and returns the next sequentially available question. """ - questions = self.questions_unanswered.all() - question_cycle = cycle(questions) - for question in question_cycle: - if question.id==int(question_id): - return question_cycle.next() + unanswered_questions = self.questions_unanswered.all() + questions = list(unanswered_questions.values_list('id', flat=True)) + if len(questions) == 0: + return None + try: + index = questions.index(int(question_id)) + next_id = questions[index+1] + except (ValueError, IndexError): + next_id = questions[0] + return unanswered_questions.get(id=next_id) def time_left(self): """Return the time remaining for the user in seconds.""" diff --git a/yaksh/templates/yaksh/addteacher.html b/yaksh/templates/yaksh/addteacher.html index 7e04f71..6722a52 100644 --- a/yaksh/templates/yaksh/addteacher.html +++ b/yaksh/templates/yaksh/addteacher.html @@ -18,6 +18,7 @@ <button class="btn" type="button" name="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/courses");'>Cancel</button> </center></form> </div> <br><br> + <form action="{{ URL_ROOT }}/exam/manage/addteacher/{{ course.id }}/" method="post"> {% csrf_token %} {% if success == True %} @@ -56,8 +57,7 @@ </form> {% if status == True %} <div class="row"> - <div class="span6 offset4 wrap"> - <center><b><u>Teacher(s) Added</u></b></center><br> + <div class="span6 offset4 wrap"> {% if teachers_added %} {% for teacher in teachers_added %} <div class="well"> diff --git a/yaksh/templates/yaksh/courses.html b/yaksh/templates/yaksh/courses.html index 910a68d..4852d25 100644 --- a/yaksh/templates/yaksh/courses.html +++ b/yaksh/templates/yaksh/courses.html @@ -9,11 +9,11 @@ {% endblock %} {% block manage %} -<a href="{{URL_ROOT}}/exam/manage/allotted_course/">View Allotted Courses</a><br> + {% if not courses %} - <center><h4> No new Courses added </h4></center> + <center><h4> No new Courses created </h4></center> {% else %} -<center><h3> Course(s) Added</h3></center> +<center><h3> Course(s) Created</h3></center> {% for course in courses %} {% if user != course.creator and course.name != "Demo_course"%} <h4> {{course.creator.get_full_name}} added you to this course</h4> @@ -82,7 +82,76 @@ </div> <br><br> {% endfor %} - <button class="btn primary" type="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/add_course");'>Add New Course</button> - <button class="btn primary" type="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/addquiz");'>Add New Quiz</button> + {% endif %} + +{% if allotted_courses %} + <center><h3> Course(s) Allotted </h3></center> + + {% for course in allotted_courses %} + <div class="row show-grid"> + <div class="span14"> + <div class="row"> + <div class="span6"> + <p> + <b><u>Course</u></b> + {% if course.active %} + <span class="label success">Active</span> + {% else %} + <span class="label important">Closed</span> + {% endif %} + </p> + <a href="{{URL_ROOT}}/exam/manage/course_detail/{{course.id}}/">{{ course.name }}</a> + </br></br> + <div class="row"> + <div class="span6 wrap"> + <center><b><u> Course Creator</u></b></center> + {{course.creator}} + <center><b><u>Teacher(s) Added to {{ course }}</u></b></center> + {% if course.get_teachers %} + <div align="left"> + <form action="{{URL_ROOT}}/exam/manage/remove_teachers/{{ course.id }}/" method="post"> + {% csrf_token %} + {% for teacher in course.get_teachers %} + <div class="well"> + <div class="row"> + <div class="span3" style="width: auto;"> + <input type="checkbox" name="remove" value="{{ teacher.id }}"> {{ teacher.get_full_name }} + </div> + </div> + </div> + {% endfor %} + <button class="btn success" type="submit">Remove Selected</button> + </div> + {% else %} + <center><b>No Teacher(s) Added</b></center> + {% endif %} + </form> + </div> + </div> + </div> + <div class="span6"> + <p><b><a href="{{URL_ROOT}}/exam/manage/searchteacher/{{course.id}}/">Add Teacher</a></b></p> + </div> + <div class="span6"> + <p><b><u>Quiz(zes)</u></b></p> + {% if course.get_quizzes %} + {% for quiz in course.get_quizzes %} + <a href="{{URL_ROOT}}/exam/manage/addquiz/{{quiz.id}}/">{{ quiz.description }}</a><br> + {% endfor %} + {% else %} + <p><b>No quiz </b></p> + {% endif %} + </div> + </div> + </div> + </div> + <br><br> + {% endfor %} +{% else %} + <center><h4> No new Courses allotted </h4></center> {% endif %} +<button class="btn primary" type="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/add_course");'>Add New Course</button> + {% if courses or allotted_courses %} + <button class="btn primary" type="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/addquiz");'>Add New Quiz</button> +{% endif %} {% endblock %} diff --git a/yaksh/templates/yaksh/question.html b/yaksh/templates/yaksh/question.html index 73d851a..9a0f899 100644 --- a/yaksh/templates/yaksh/question.html +++ b/yaksh/templates/yaksh/question.html @@ -85,19 +85,6 @@ function call_skip(url) form.action = url form.submit(); } - {% if error_message == 'Correct Output'%} - {% if paper.questions_left %} - window.setTimeout(function() - { - location.href="{{ URL_ROOT }}/exam/{{ paper.current_question.id }}/check/{{ paper.attempt_number }}/{{ paper.question_paper.id }}/" - }, 2000); - {% else %} - window.setTimeout(function() - { - location.href="{{ URL_ROOT }}/exam/{{ question.id }}/check/{{ paper.attempt_number }}/{{ paper.question_paper.id }}/" - }, 2000); - {% endif %} - {% endif %} </script> {% endblock script %} @@ -165,11 +152,17 @@ function call_skip(url) <input type=hidden name="question_id" id="question_id" value={{ question.id }}></input> {% if question.type == "mcq" %} + {% if error_message %} + <p>{{ error_message }}</p> + {% endif %} {% for test_case in test_cases %} <input name="answer" type="radio" value="{{ test_case.options }}" />{{ test_case.options }} <br/> {% endfor %} {% endif %} {% if question.type == "mcc" %} + {% if error_message %} + <p>{{ error_message }}</p> + {% endif %} {% for test_case in test_cases %} <input name="answer" type="checkbox" value="{{ test_case.options }}"> {{ test_case.options }} <br> @@ -188,7 +181,7 @@ function call_skip(url) {% endif %} - {% if question.type == "mcq" or question.type == "mcc "%} + {% if question.type == "mcq" or question.type == "mcc"%} <br><button class="btn" type="submit" name="check" id="check">Submit Answer</button> {% elif question.type == "upload" %} <br><button class="btn" type="submit" name="check" id="check" onClick="return validate();">Upload</button> diff --git a/yaksh/templates/yaksh/user_data.html b/yaksh/templates/yaksh/user_data.html index 04544f9..2e7db50 100644 --- a/yaksh/templates/yaksh/user_data.html +++ b/yaksh/templates/yaksh/user_data.html @@ -56,11 +56,15 @@ User IP address: {{ paper.user_ip }} </p> <p>Student answer: {{ answers.0 }}</p> {% else %}{# non-mcq questions #} -<pre> -{% for answer in answers %}################################################################################ +{% for answer in answers %} +{% if not answer.skipped %} +<pre> +############################################################################### {{ answer.answer.strip }} # Autocheck: {{ answer.error }} -{% endfor %}</pre> +</pre> +{% endif %} +{% endfor %} {% endif %} {% with answers|last as answer %} <p><em>Marks: {{answer.marks}} </em> </p> diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 1716c73..5c34e8e 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -11,7 +11,7 @@ from django.core.files import File import zipfile import os import shutil - +import tempfile def setUpModule(): # create user profile @@ -62,6 +62,14 @@ def tearDownModule(): User.objects.all().delete() Question.objects.all().delete() Quiz.objects.all().delete() + dir_path1 = os.path.join(os.getcwd(), "yaksh", "data","question_25") + dir_path2 = os.path.join(os.getcwd(), "yaksh", "data","question_22") + dir_path3 = os.path.join(os.getcwd(), "yaksh", "data","question_24") + dir_path4 = os.path.join(os.getcwd(), "yaksh", "data","question_27") + shutil.rmtree(dir_path1) + shutil.rmtree(dir_path2) + shutil.rmtree(dir_path3) + #shutil.rmtree(dir_path4) ############################################################################### @@ -109,8 +117,21 @@ class QuestionTestCases(unittest.TestCase): user=self.user2 ) self.question2.save() + + # create a temp directory and add files for loading questions test file_path = os.path.join(os.getcwd(), "yaksh", "test.txt") - shutil.copy(file_path, "/tmp/") + self.load_tmp_path = tempfile.mkdtemp() + shutil.copy(file_path, self.load_tmp_path) + file1 = os.path.join(self.load_tmp_path, "test.txt") + + # create a temp directory and add files for dumping questions test + self.dump_tmp_path = tempfile.mkdtemp() + shutil.copy(file_path, self.dump_tmp_path) + file2 = os.path.join(self.dump_tmp_path, "test.txt") + file = open(file2, "r") + django_file = File(file) + file = FileUpload.objects.create(file=django_file, question=self.question2) + self.question1.tags.add('python', 'function') self.assertion_testcase = StandardTestCase(question=self.question1, test_case='assert myfunc(12, 13) == 15' @@ -127,10 +148,14 @@ class QuestionTestCases(unittest.TestCase): "language": "Python", "type": "Code", "test_case_type": "standardtestcase", "testcase": self.test_case_upload_data, - "files": ['/tmp/test.txt'], + "files": [file1], "summary": "Json Demo"}] self.json_questions_data = json.dumps(questions_data) + def tearDown(self): + shutil.rmtree(self.load_tmp_path) + shutil.rmtree(self.dump_tmp_path) + def test_question(self): """ Test question """ self.assertEqual(self.question1.summary, 'Demo question') @@ -145,15 +170,17 @@ class QuestionTestCases(unittest.TestCase): tag_list.append(tag.name) self.assertEqual(tag_list, ['python', 'function']) - def test_dump_questions_into_json(self): + def test_dump_questions(self): """ Test dump questions into json """ question = Question() question_id = [self.question2.id] questions_zip = question.dump_questions(question_id, self.user2) + que_file = FileUpload.objects.get(question=self.question2.id) zip_file = zipfile.ZipFile(questions_zip, "r") - zip_file.extractall("/tmp/") + tmp_path = tempfile.mkdtemp() + zip_file.extractall(tmp_path) test_case = self.question2.get_test_cases() - with open("/tmp/questions_dump.json", "r") as f: + with open("{0}/questions_dump.json".format(tmp_path), "r") as f: questions = json.loads(f.read()) for q in questions: self.assertEqual(self.question2.summary, q['summary']) @@ -164,14 +191,13 @@ class QuestionTestCases(unittest.TestCase): self.assertTrue(self.question2.active) self.assertEqual(self.question2.snippet, q['snippet']) self.assertEqual(self.question2.test_case_type, q['test_case_type']) + self.assertEqual(os.path.basename(que_file.file.path), q['files'][0]) self.assertEqual([case.get_field_value() for case in test_case], q['testcase']) for file in zip_file.namelist(): - os.remove(os.path.join("/tmp/", file)) + os.remove(os.path.join(tmp_path, file)) - def test_load_questions_from_json(self): + def test_load_questions(self): """ Test load questions into database from json """ - f_path = os.path.join(os.getcwd(), "yaksh", "data", - "question_25", "tmp", "test.txt") question = Question() result = question.load_questions(self.json_questions_data, self.user1) question_data = Question.objects.get(pk=25) @@ -187,8 +213,6 @@ class QuestionTestCases(unittest.TestCase): self.assertEqual(question_data.test_case_type, 'standardtestcase') self.assertEqual(os.path.basename(file.file.path), "test.txt") self.assertEqual([case.get_field_value() for case in test_case], self.test_case_upload_data) - rm_dir = os.path.dirname(os.path.dirname(f_path)) - shutil.rmtree(rm_dir) ############################################################################### @@ -442,7 +466,7 @@ class AnswerPaperTestCases(unittest.TestCase): self.answerpaper.save() # answers for the Answer Paper self.answer_right = Answer(question=Question.objects.get(id=1), - answer="Demo answer", + answer="Demo answer", correct=True, marks=1 ) self.answer_wrong = Answer(question=Question.objects.get(id=2), @@ -476,20 +500,85 @@ class AnswerPaperTestCases(unittest.TestCase): # Test completed_question() method of Answer Paper question = self.answerpaper.completed_question(1) self.assertEqual(self.answerpaper.questions_left(), 2) - # Test skip() method of Answer Paper + + # Test next_question() method of Answer Paper current_question = self.answerpaper.current_question() self.assertEqual(current_question.id, 2) - next_question_id = self.answerpaper.skip(current_question.id) + + # When + next_question_id = self.answerpaper.next_question(current_question.id) + + # Then self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id.id, 3) + + # Given, here question is already answered + current_question_id = 1 + + # When + next_question_id = self.answerpaper.next_question(current_question_id) + + # Then + self.assertTrue(next_question_id is not None) + self.assertEqual(next_question_id.id, 2) + + # Given, wrong question id + current_question_id = 12 + + # When + next_question_id = self.answerpaper.next_question(current_question_id) + + # Then + self.assertTrue(next_question_id is not None) + self.assertEqual(next_question_id.id, 2) + + # Given, last question in the list + current_question_id = 3 + + # When + next_question_id = self.answerpaper.next_question(current_question_id) + + # Then + self.assertTrue(next_question_id is not None) + self.assertEqual(next_question_id.id, 2) + + # Test get_questions_answered() method + # When questions_answered = self.answerpaper.get_questions_answered() + + # Then self.assertEqual(questions_answered.count(), 1) self.assertSequenceEqual(questions_answered, [self.questions[0]]) + + # When questions_unanswered = self.answerpaper.get_questions_unanswered() + + # Then self.assertEqual(questions_unanswered.count(), 2) self.assertSequenceEqual(questions_unanswered, [self.questions[1], self.questions[2]]) + # Test completed_question and next_question + # When all questions are answered + current_question = self.answerpaper.completed_question(2) + + # Then + self.assertEqual(self.answerpaper.questions_left(), 1) + self.assertEqual(current_question.id, 3) + + # When + current_question = self.answerpaper.completed_question(3) + + # Then + self.assertEqual(self.answerpaper.questions_left(), 0) + self.assertTrue(current_question is None) + + # When + next_question_id = self.answerpaper.next_question(current_question_id) + + # Then + self.assertTrue(next_question_id is None) + def test_update_marks(self): """ Test update_marks method of AnswerPaper""" self.answerpaper.update_marks('inprogress') @@ -602,7 +691,7 @@ class CourseTestCases(unittest.TestCase): def test_add_teachers(self): """ Test to add teachers to a course""" self.course.add_teachers(self.student1, self.student2) - self.assertSequenceEqual(self.course.get_teachers(), + self.assertSequenceEqual(self.course.get_teachers(), [self.student1, self.student2]) def test_remove_teachers(self): @@ -634,23 +723,23 @@ class CourseTestCases(unittest.TestCase): class TestCaseTestCases(unittest.TestCase): def setUp(self): self.user = User.objects.get(pk=1) - self.question1 = Question(summary='Demo question 1', + self.question1 = Question(summary='Demo question 1', language='Python', - type='Code', + type='Code', active=True, - description='Write a function', + description='Write a function', points=1.0, - test_case_type="standardtestcase", + test_case_type="standardtestcase", user=self.user, snippet='def myfunc()' ) - self.question2 = Question(summary='Demo question 2', + self.question2 = Question(summary='Demo question 2', language='Python', - type='Code', + type='Code', active=True, - description='Write to standard output', + description='Write to standard output', points=1.0, - test_case_type="stdoutbasedtestcase", + test_case_type="stdoutbasedtestcase", user=self.user, snippet='def myfunc()' ) @@ -676,13 +765,13 @@ class TestCaseTestCases(unittest.TestCase): def test_assertion_testcase(self): """ Test question """ self.assertEqual(self.assertion_testcase.question, self.question1) - self.assertEqual(self.assertion_testcase.test_case, + self.assertEqual(self.assertion_testcase.test_case, 'assert myfunc(12, 13) == 15') def test_stdout_based_testcase(self): """ Test question """ self.assertEqual(self.stdout_based_testcase.question, self.question2) - self.assertEqual(self.stdout_based_testcase.expected_output, + self.assertEqual(self.stdout_based_testcase.expected_output, 'Hello World' ) diff --git a/yaksh/urls.py b/yaksh/urls.py index cd97dd4..d14ed1d 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -97,7 +97,6 @@ urlpatterns += [ views.reject, {'was_enrolled': True}), url(r'^manage/searchteacher/(?P<course_id>\d+)/$', views.search_teacher), url(r'^manage/addteacher/(?P<course_id>\d+)/$', views.add_teacher, name='add_teacher'), - url(r'^manage/allotted_course/$', views.allotted_courses), url(r'^manage/remove_teachers/(?P<course_id>\d+)/$', views.remove_teachers, name='remove_teacher'), url(r'^manage/download_questions/$', views.show_all_questions), url(r'^manage/upload_questions/$', views.show_all_questions), diff --git a/yaksh/views.py b/yaksh/views.py index 616c3cc..bfa36fd 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -35,6 +35,7 @@ from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ from yaksh.xmlrpc_clients import code_server from settings import URL_ROOT from yaksh.models import AssignmentUpload +from file_utils import extract_files # The directory where user data can be saved. OUTPUT_DIR = abspath(join(dirname(__file__), 'output')) @@ -83,21 +84,6 @@ def add_to_group(users): user.groups.add(group) -def extract_files(questions_file): - if zipfile.is_zipfile(questions_file): - zip_file = zipfile.ZipFile(questions_file, 'r') - zip_file.extractall() - - -def read_json(json_file, user): - question = Question() - if os.path.exists(json_file): - with open(json_file, 'r') as q_file: - questions_list = q_file.read() - question.load_questions(questions_list, user) - os.remove(json_file) - - def index(request): """The start page. """ @@ -472,16 +458,22 @@ def skip(request, q_id, next_q=None, attempt_num=None, questionpaper_id=None): paper = get_object_or_404(AnswerPaper, user=request.user, attempt_number=attempt_num, question_paper=questionpaper_id) question = get_object_or_404(Question, pk=q_id) + if question in paper.questions_answered.all(): + next_q = paper.next_question(q_id) + return show_question(request, next_q, paper) + if request.method == 'POST' and question.type == 'code': user_code = request.POST.get('answer') new_answer = Answer(question=question, answer=user_code, correct=False, skipped=True) new_answer.save() paper.answers.add(new_answer) - if next_q is None: - next_q = paper.skip(q_id) if paper.skip(q_id) else question - else: + if next_q is not None: next_q = get_object_or_404(Question, pk=next_q) + if next_q not in paper.questions_unanswered.all(): + return show_question(request, question, paper) + else: + next_q = paper.next_question(q_id) return show_question(request, next_q, paper) @@ -493,7 +485,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): question_paper=questionpaper_id) question = get_object_or_404(Question, pk=q_id) if question in paper.questions_answered.all(): - next_q = paper.skip(q_id) + next_q = paper.next_question(q_id) return show_question(request, next_q, paper) if request.method == 'POST': @@ -522,7 +514,9 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): correct=False) new_answer.save() paper.answers.add(new_answer) - + if not user_answer: + msg = "Please submit a valid option or code" + return show_question(request, question, paper, msg) # If we were not skipped, we were asked to check. For any non-mcq # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. @@ -543,17 +537,8 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): new_answer.save() return show_question(request, question, paper, result.get('error')) else: - # Display the same question if user_answer is None - if not user_answer: - msg = "Please submit a valid option or code" - return show_question(request, question, paper, msg) - elif question.type == 'code' and user_answer: - msg = "Correct Output" - paper.completed_question(question.id) - return show_question(request, question, paper, msg) - else: - next_q = paper.completed_question(question.id) - return show_question(request, next_q, paper) + next_q = paper.completed_question(question.id) + return show_question(request, next_q, paper) else: return show_question(request, question, paper) @@ -576,11 +561,13 @@ def validate_answer(user, user_answer, question, json_data=None): expected_answer = question.get_test_case(correct=True).options if user_answer.strip() == expected_answer.strip(): correct = True + result['error'] = 'Correct answer' elif question.type == 'mcc': expected_answers = [] for opt in question.get_test_cases(correct=True): expected_answers.append(opt.options) if set(user_answer) == set(expected_answers): + result['error'] = 'Correct answer' correct = True elif question.type == 'code': user_dir = get_user_dir(user) @@ -681,10 +668,15 @@ def courses(request): ci = RequestContext(request) if not is_moderator(user): raise Http404('You are not allowed to view this page') - demo_user = User.objects.get(username="demo_user") + try: + demo_user = User.objects.get(username="demo_user") + except User.DoesNotExist: + demo_user = None courses = Course.objects.filter(Q(creator=user) | Q(creator=demo_user), is_trial=False) - return my_render_to_response('yaksh/courses.html', {'courses': courses}, + allotted_courses = Course.objects.filter(teachers=user, is_trial=False) + context = {'courses': courses, "allotted_courses": allotted_courses} + return my_render_to_response('yaksh/courses.html', context, context_instance=ci) @@ -891,8 +883,9 @@ def show_all_questions(request): questions_file = request.FILES['file'] file_name = questions_file.name.split('.') if file_name[-1] == "zip": + ques = Question() extract_files(questions_file) - read_json("questions_dump.json", user) + ques.read_json("questions_dump.json", user) else: message = "Please Upload a ZIP file" context['message'] = message @@ -1186,25 +1179,24 @@ def search_teacher(request, course_id): raise Http404('You are not allowed to view this page!') context = {} - course = get_object_or_404(Course, creator=user, pk=course_id) + course = get_object_or_404(Course, pk=course_id) context['course'] = course + if user != course.creator and user not in course.teachers.all(): + raise Http404('You are not allowed to view this page!') + if request.method == 'POST': u_name = request.POST.get('uname') - if len(u_name) == 0: - return my_render_to_response('yaksh/addteacher.html', context, - context_instance=ci) - else: + if not len(u_name) == 0: teachers = User.objects.filter(Q(username__icontains=u_name)| Q(first_name__icontains=u_name)|Q(last_name__icontains=u_name)| - Q(email__icontains=u_name)).exclude(Q(id=user.id)|Q(is_superuser=1)) + Q(email__icontains=u_name)).exclude(Q(id=user.id)|Q(is_superuser=1)| + Q(id=course.creator.id)) context['success'] = True context['teachers'] = teachers - return my_render_to_response('yaksh/addteacher.html', context, - context_instance=ci) - else: - return my_render_to_response('yaksh/addteacher.html', context, - context_instance=ci) + + return my_render_to_response('yaksh/addteacher.html', context, + context_instance=ci) @login_required @@ -1218,8 +1210,11 @@ def add_teacher(request, course_id): raise Http404('You are not allowed to view this page!') context = {} - course = get_object_or_404(Course, creator=user, pk=course_id) - context['course'] = course + course = get_object_or_404(Course, pk=course_id) + if user == course.creator or user in course.teachers.all(): + context['course'] = course + else: + raise Http404('You are not allowed to view this page!') if request.method == 'POST': teacher_ids = request.POST.getlist('check') @@ -1228,36 +1223,21 @@ def add_teacher(request, course_id): course.add_teachers(*teachers) context['status'] = True context['teachers_added'] = teachers - return my_render_to_response('yaksh/addteacher.html', context, - context_instance=ci) - else: - return my_render_to_response('yaksh/addteacher.html', context, + + return my_render_to_response('yaksh/addteacher.html', context, context_instance=ci) -@login_required -def allotted_courses(request): - """ show courses allotted to a user """ - - user = request.user - ci = RequestContext(request) - if not is_moderator(user): - raise Http404('You are not allowed to view this page!') - - courses = Course.objects.filter(teachers=user) - return my_render_to_response('yaksh/courses.html', {'courses': courses}, - context_instance=ci) - @login_required def remove_teachers(request, course_id): """ remove user from a course """ user = request.user - if not is_moderator(user): + course = get_object_or_404(Course, pk=course_id) + if not is_moderator(user) and (user != course.creator and user not in course.teachers.all()): raise Http404('You are not allowed to view this page!') - course = get_object_or_404(Course, creator=user, pk=course_id) if request.method == "POST": teacher_ids = request.POST.getlist('remove') teachers = User.objects.filter(id__in=teacher_ids) |