diff options
author | adityacp | 2016-05-05 12:52:47 +0530 |
---|---|---|
committer | adityacp | 2016-05-05 12:52:47 +0530 |
commit | a2d3d14e03c7f6a873b4f838752878d2bcab44aa (patch) | |
tree | 61e0e7fe69010f59bc85bdf091a6c1c8ad127fe7 /yaksh | |
parent | 2f7891874aec1dec962d55e3b2aaed1c61d7acaa (diff) | |
download | online_test-a2d3d14e03c7f6a873b4f838752878d2bcab44aa.tar.gz online_test-a2d3d14e03c7f6a873b4f838752878d2bcab44aa.tar.bz2 online_test-a2d3d14e03c7f6a873b4f838752878d2bcab44aa.zip |
changes for download questions
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/models.py | 6 | ||||
-rw-r--r-- | yaksh/templates/yaksh/ajax_question_filter.html | 3 | ||||
-rw-r--r-- | yaksh/templates/yaksh/showquestions.html | 14 | ||||
-rw-r--r-- | yaksh/tests.py | 59 | ||||
-rw-r--r-- | yaksh/urls.py | 2 | ||||
-rw-r--r-- | yaksh/views.py | 56 |
6 files changed, 72 insertions, 68 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 79db206..0ee5a3d 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -211,8 +211,8 @@ class Question(models.Model): return json.dumps(question_info_dict) - def dump_into_json(self, user): - questions = Question.objects.filter(user_id = user.id) + def dump_into_json(self, question_ids, user): + questions = Question.objects.filter(id__in = question_ids, user_id = user.id) questions_dict = [{'summary': question.summary, 'description': question.description, 'points': question.points, 'test': question.test, @@ -511,7 +511,7 @@ class AnswerPaperManager(models.Manager): return answerpapers.values_list('user', flat=True).distinct() def get_latest_attempts(self, questionpaper_id): - papers = self.get_answerpapers_for_quiz(questionpaper_id) + papers = self._get_answerpapers_for_quiz(questionpaper_id) users = self._get_answerpapers_users(papers) latest_attempts = [] for user in users: diff --git a/yaksh/templates/yaksh/ajax_question_filter.html b/yaksh/templates/yaksh/ajax_question_filter.html index 11bf660..a63354c 100644 --- a/yaksh/templates/yaksh/ajax_question_filter.html +++ b/yaksh/templates/yaksh/ajax_question_filter.html @@ -7,7 +7,8 @@ {% for question in questions %} <li> <label> - <input type="checkbox" name="question" data-qid="{{question.id}}"> <a href="{{URL_ROOT}}/exam/manage/addquestion/{{ question.id }}">{{ question }}</a><br> + <input type="checkbox" name="question" data-qid="{{question.id}}" value= + "{{question.id}}"> <a href="{{URL_ROOT}}/exam/manage/addquestion/{{ question.id }}">{{ question }}</a><br> </label> </li> {% endfor %} diff --git a/yaksh/templates/yaksh/showquestions.html b/yaksh/templates/yaksh/showquestions.html index 6e2253a..ea42797 100644 --- a/yaksh/templates/yaksh/showquestions.html +++ b/yaksh/templates/yaksh/showquestions.html @@ -11,12 +11,8 @@ {% block manage %} -{% if questions %} -<a href="{{URL_ROOT}}/exam/manage/download_questions/">Download Questions</a><br> -{% endif %} - <h4>Upload json file for adding questions</h4> -<form action="{{URL_ROOT}}/exam/manage/upload_questions/" method="post" enctype="multipart/form-data"> +<form action="" method="post" enctype="multipart/form-data"> {% csrf_token %} {{ upload_form.as_p }} <button class="btn" type="submit" name="upload" value="upload">Upload File</button> @@ -24,6 +20,9 @@ {% if message %} <h4>{{ message }}</h4> {% endif %} +{% if msg %} +<h4>{{ msg }}</h4> +{% endif %} <form name=frm action="" method="post"> {% csrf_token %} <div class="row" id="selectors"> @@ -56,6 +55,9 @@ </div> <br> <button class="btn" type="button" onclick='location.replace("{{URL_ROOT}}/exam/manage/addquestion/");'>Add Question</button> -<button class="btn" type="submit" onClick="return confirm_delete(frm);" name='delete' value='delete'>Delete Selected</button> +<button class="btn" type="submit" onClick="return confirm_delete(frm);" name='delete' value='delete'>Delete Selected</button> +{% if questions %} +<button class="btn" type="submit" name='download' value='download'>Download Selected</button> +{% endif %} </form> {% endblock %} diff --git a/yaksh/tests.py b/yaksh/tests.py index a3f3880..58b8518 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -72,22 +72,22 @@ class ProfileTestCases(unittest.TestCase): class QuestionTestCases(unittest.TestCase): def setUp(self): # Single question details - self.user = User.objects.get(pk=1) - self.user1 = User.objects.get(pk=2) - self.question = Question(summary='Demo question', language='Python', + self.user1 = User.objects.get(pk=1) + self.user2 = User.objects.get(pk=2) + self.question1 = Question(summary='Demo question', language='Python', type='Code', active=True, description='Write a function', points=1.0, - snippet='def myfunc()', user=self.user) - self.question.save() + snippet='def myfunc()', user=self.user1) + self.question1.save() - self.question1 = Question(summary='Demo Json', language='python', + self.question2 = Question(summary='Demo Json', language='python', type='code', active=True, description='factorial of a no', points=2.0, - snippet='def fact()', user=self.user1) - self.question1.save() + snippet='def fact()', user=self.user2) + self.question2.save() - self.question.tags.add('python', 'function') - self.testcase = TestCase(question=self.question, + self.question1.tags.add('python', 'function') + self.testcase = TestCase(question=self.question1, func_name='def myfunc', kw_args='a=10,b=11', pos_args='12,13', expected_answer='15') answer_data = { "test": "", @@ -99,7 +99,7 @@ class QuestionTestCases(unittest.TestCase): "kw_args": {"a": "10", "b": "11"} }], - "id": self.question.id, + "id": self.question1.id, "ref_code_path": "", } self.answer_data_json = json.dumps(answer_data) @@ -112,37 +112,38 @@ class QuestionTestCases(unittest.TestCase): def test_question(self): """ Test question """ - self.assertEqual(self.question.summary, 'Demo question') - self.assertEqual(self.question.language, 'Python') - self.assertEqual(self.question.type, 'Code') - self.assertFalse(self.question.options) - self.assertEqual(self.question.description, 'Write a function') - self.assertEqual(self.question.points, 1.0) - self.assertTrue(self.question.active) - self.assertEqual(self.question.snippet, 'def myfunc()') + self.assertEqual(self.question1.summary, 'Demo question') + self.assertEqual(self.question1.language, 'Python') + self.assertEqual(self.question1.type, 'Code') + self.assertFalse(self.question1.options) + self.assertEqual(self.question1.description, 'Write a function') + self.assertEqual(self.question1.points, 1.0) + self.assertTrue(self.question1.active) + self.assertEqual(self.question1.snippet, 'def myfunc()') tag_list = [] - for tag in self.question.tags.all(): + for tag in self.question1.tags.all(): tag_list.append(tag.name) self.assertEqual(tag_list, ['python', 'function']) def test_consolidate_answer_data(self): """ Test consolidate_answer_data function """ - result = self.question.consolidate_answer_data([self.testcase], + result = self.question1.consolidate_answer_data([self.testcase], self.user_answer) self.assertEqual(result, self.answer_data_json) def test_dump_questions_into_json(self): """ Test dump questions into json """ question = Question() - questions = json.loads(question.dump_into_json(self.user1)) + question_id = ['24'] + questions = json.loads(question.dump_into_json(question_id, self.user1)) for q in questions: - self.assertEqual(self.question1.summary, q['summary']) - self.assertEqual(self.question1.language, q['language']) - self.assertEqual(self.question1.type, q['type']) - self.assertEqual(self.question1.description, q['description']) - self.assertEqual(self.question1.points, q['points']) - self.assertTrue(self.question1.active) - self.assertEqual(self.question1.snippet, q['snippet']) + self.assertEqual(self.question2.summary, q['summary']) + self.assertEqual(self.question2.language, q['language']) + self.assertEqual(self.question2.type, q['type']) + self.assertEqual(self.question2.description, q['description']) + self.assertEqual(self.question2.points, q['points']) + self.assertTrue(self.question2.active) + self.assertEqual(self.question2.snippet, q['snippet']) def test_load_questions_from_json(self): """ Test load questions into database from json """ diff --git a/yaksh/urls.py b/yaksh/urls.py index f660ed7..18a64c2 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -89,7 +89,7 @@ urlpatterns += [ url(r'^manage/addteacher/(?P<course_id>\d+)/$', views.add_teacher), url(r'^manage/allotted_course/$', views.allotted_courses), url(r'^manage/remove_teachers/(?P<course_id>\d+)/$', views.remove_teachers), - url(r'^manage/download_questions/$', views.download_questions), + 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 2dde09f..a986d4c 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -783,7 +783,7 @@ def ajax_questions_filter(request): """Ajax call made when filtering displayed questions.""" user = request.user - filter_dict = {"user_id":user.id} + filter_dict = {"user_id": user.id} question_type = request.POST.get('question_type') marks = request.POST.get('marks') language = request.POST.get('language') @@ -813,22 +813,36 @@ def show_all_questions(request): if not is_moderator(user): raise Http404("You are not allowed to view this page !") - if request.method == 'POST' and request.POST.get('delete') == 'delete': - data = request.POST.getlist('question') - if data is not None: - question = Question.objects.filter(id__in=data, user_id=user.id).delete() + if request.method == 'POST': + if request.POST.get('delete') == 'delete': + data = request.POST.getlist('question') + if data is not None: + question = Question.objects.filter(id__in=data, user_id=user.id).delete() - if request.method == 'POST' and request.POST.get('upload') == 'upload': - form = UploadFileForm(request.POST, request.FILES) - if form.is_valid(): - questions_file = request.FILES['file'] - if questions_file.name.split('.')[-1] == "json": - questions_list = questions_file.read() + if request.POST.get('upload') == 'upload': + form = UploadFileForm(request.POST, request.FILES) + if form.is_valid(): + questions_file = request.FILES['file'] + if questions_file.name.split('.')[-1] == "json": + questions_list = questions_file.read() + question = Question() + question.load_from_json(questions_list, user) + else: + message = "Please Upload a JSON file" + context['message'] = message + + if request.POST.get('download') == 'download': + question_ids = request.POST.getlist('question') + if question_ids: question = Question() - question.load_from_json(questions_list, user) + questions = question.dump_into_json(question_ids, user) + response = HttpResponse(questions, content_type='text/json') + response['Content-Disposition'] = 'attachment; filename=\ + "{0}_questions.json"'.format(user) + return response else: - message = "Please Upload a JSON file" - context['message'] = message + msg = "Please select atleast one question" + context['msg'] = msg questions = Question.objects.filter(user_id=user.id) form = QuestionFilterForm(user=user) @@ -1170,17 +1184,3 @@ def remove_teachers(request, course_id): teachers = User.objects.filter(id__in=teacher_ids) course.remove_teachers(*teachers) return my_redirect('/exam/manage/courses') - - -@login_required -def download_questions(request): - user = request.user - if not is_moderator(user): - raise Http404('You are not allowed to view this page!') - question = Question() - questions = question.dump_into_json(user) - response = HttpResponse(questions, content_type='text/json') - response['Content-Disposition'] = 'attachment; filename="{0}_questions.json"'\ - .format(user) - return response - |