From d53feb82cdd74e25045171acf70e513729ba993c Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 15 Apr 2016 11:40:25 +0530 Subject: rebase changes --- yaksh/forms.py | 11 +++- yaksh/models.py | 3 ++ yaksh/templates/yaksh/showquestions.html | 13 +++++ yaksh/urls.py | 5 ++ yaksh/views.py | 90 +++++++++++++++++++++++++++++--- 5 files changed, 113 insertions(+), 9 deletions(-) (limited to 'yaksh') diff --git a/yaksh/forms.py b/yaksh/forms.py index 16f82fb..37741b9 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -152,7 +152,7 @@ class QuestionForm(forms.ModelForm): class Meta: model = Question - fields = '__all__' + exclude = ['user'] class RandomQuestionForm(forms.Form): @@ -165,8 +165,9 @@ class RandomQuestionForm(forms.Form): class QuestionFilterForm(forms.Form): def __init__(self, *args, **kwargs): + user = kwargs.pop("user") super(QuestionFilterForm, self).__init__(*args, **kwargs) - questions = Question.objects.all() + questions = Question.objects.filter(user_id=user) points_list = questions.values_list('points', flat=True).distinct() points_options = [('select', 'Select Marks')] points_options.extend([(point, point) for point in points_list]) @@ -188,6 +189,7 @@ class CourseForm(forms.ModelForm): model = Course fields = ['name', 'active', 'enrollment'] + class ProfileForm(forms.ModelForm): """ profile form for students and moderators """ @@ -205,3 +207,8 @@ class ProfileForm(forms.ModelForm): super(ProfileForm, self).__init__(*args, **kwargs) self.fields['first_name'].initial = user.first_name self.fields['last_name'].initial = user.last_name + + +class UploadFileForm(forms.Form): + file = forms.FileField() + diff --git a/yaksh/models.py b/yaksh/models.py index 561b334..c76d655 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -173,6 +173,9 @@ class Question(models.Model): # Tags for the Question. tags = TaggableManager(blank=True) + # user for particular question + user = models.ForeignKey(User, related_name="user") + def consolidate_answer_data(self, test_cases, user_answer): test_case_data_dict = [] question_info_dict = {} diff --git a/yaksh/templates/yaksh/showquestions.html b/yaksh/templates/yaksh/showquestions.html index ca51d1a..ba96728 100644 --- a/yaksh/templates/yaksh/showquestions.html +++ b/yaksh/templates/yaksh/showquestions.html @@ -10,6 +10,18 @@ {% endblock %} {% block manage %} + +{% if questions %} +Download Questions
+{% endif %} + +

Upload csv file for adding questions

+
+{% csrf_token %} +{{ upload_form.as_p }} + +
+
{% csrf_token %}
@@ -31,6 +43,7 @@

+
{% for i in questions %}   {{ i }}
diff --git a/yaksh/urls.py b/yaksh/urls.py index b32bc36..4e72084 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -74,6 +74,7 @@ urlpatterns += [ views.enroll, {'was_rejected': True}), url(r'manage/reject/(?P\d+)/(?P\d+)/$', views.reject), url(r'manage/enrolled/reject/(?P\d+)/(?P\d+)/$', + views.reject, {'was_enrolled': True}), url(r'manage/toggle_status/(?P\d+)/$', views.toggle_course_status), url(r'^ajax/questionpaper/(?P.+)/$', views.ajax_questionpaper), @@ -89,4 +90,8 @@ urlpatterns += [ url(r'^manage/addteacher/(?P\d+)/$', views.add_teacher), url(r'^manage/allotted_course/$', views.allotted_courses), url(r'^manage/remove_teachers/(?P\d+)/$', views.remove_teachers) + url(r'^manage/download_questions/$', views.download_questions), + url(r'^manage/upload_questions/$', views.upload_questions) ] + + diff --git a/yaksh/views.py b/yaksh/views.py index a91da29..1d79a4c 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -23,7 +23,7 @@ from yaksh.models import Quiz, Question, QuestionPaper, QuestionSet, Course from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm, TestCaseFormSet,\ - QuestionFilterForm, CourseForm, ProfileForm + QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm from yaksh.xmlrpc_clients import code_server from settings import URL_ROOT from yaksh.models import AssignmentUpload @@ -166,6 +166,8 @@ def add_question(request, question_id=None): test_case_formset = add_or_delete_test_form(request.POST, form.save(commit=False)) if 'save_question' in request.POST: qtn = form.save(commit=False) + qtn.user = user + qtn.save() test_case_formset = TestCaseFormSet(request.POST, prefix='test', instance=qtn) form.save() question = Question.objects.order_by("-id")[0] @@ -780,6 +782,7 @@ def show_all_users(request): def ajax_questions_filter(request): """Ajax call made when filtering displayed questions.""" + user = request.user filter_dict = {} question_type = request.POST.get('question_type') marks = request.POST.get('marks') @@ -794,7 +797,7 @@ def ajax_questions_filter(request): if language != "select": filter_dict['language'] = str(language) - questions = list(Question.objects.filter(**filter_dict)) + questions = list(Question.objects.filter(**filter_dict).filter(user_id=user.id)) return my_render_to_response('yaksh/ajax_question_filter.html', {'questions': questions}) @@ -814,12 +817,14 @@ def show_all_questions(request): if data is not None: for i in data: question = Question.objects.get(id=i).delete() - questions = Question.objects.all() - form = QuestionFilterForm() + questions = Question.objects.filter(user_id=user.id) + form = QuestionFilterForm(user=user) + upload_form = UploadFileForm() context = {'papers': [], 'question': None, 'questions': questions, - 'form': form + 'form': form, + 'upload_form': upload_form } return my_render_to_response('yaksh/showquestions.html', context, context_instance=ci) @@ -944,9 +949,11 @@ def ajax_questionpaper(request, query): """ During question paper creation, ajax call made to get question details. """ + + user = request.user if query == 'marks': question_type = request.POST.get('question_type') - questions = Question.objects.filter(type=question_type) + questions = Question.objects.filter(type=question_type, user=user) marks = questions.values_list('points').distinct() return my_render_to_response('yaksh/ajax_marks.html', {'marks': marks}) elif query == 'questions': @@ -958,7 +965,7 @@ def ajax_questionpaper(request, query): random_question_list = ",".join(random_questions).split(',') question_list = fixed_question_list + random_question_list questions = list(Question.objects.filter(type=question_type, - points=marks_selected)) + points=marks_selected, user=user)) questions = [question for question in questions \ if not str(question.id) in question_list] return my_render_to_response('yaksh/ajax_questions.html', @@ -1151,3 +1158,72 @@ 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!') + questions = Question.objects.filter(user_id = user.id) + response = HttpResponse(content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="{0}_Questions.csv"'.format(user) + writer = csv.writer(response) + header = [ + 'summary', + 'description', + 'points', + 'test', + 'ref_code_path', + 'options', + 'language', + 'type', + 'active', + 'snippet' + ] + writer.writerow(header) + for que in questions: + row = [ + que.summary, + que.description, + que.points, + que.test, + que.ref_code_path, + que.options, + que.language, + que.type, + que.active, + que.snippet + ] + writer.writerow(row) + return response + + +@login_required +def upload_questions(request): + user = request.user + ci = RequestContext(request) + + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + + if request.method == 'POST': + form = UploadFileForm(request.POST, request.FILES) + if form.is_valid(): + csvfile = request.FILES['file'] + if csvfile.name.split('.')[1] == "csv": + reader = csv.DictReader(csvfile, delimiter=',') + for row in reader: + Question.objects.get_or_create(summary=row['summary'], + description=row['description'], points=row['points'], + test=row['test'], ref_code_path=row['ref_code_path'], + options=row['options'], language=row['language'], + type=row['type'], active=row['active'], + snippet=row['snippet'], user=user) + return my_redirect('/exam/manage/questions') + else: + raise Http404('Please Upload a csv file') + else: + return my_redirect('/exam/manage/questions') + else: + return my_redirect('/exam/manage/questions') -- cgit From 7a4438b7572f828c88958c12b2cd9666784f1bb5 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 15 Apr 2016 11:46:27 +0530 Subject: further rebase changes --- yaksh/forms.py | 2 +- yaksh/views.py | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'yaksh') diff --git a/yaksh/forms.py b/yaksh/forms.py index 37741b9..c5bec4c 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -167,7 +167,7 @@ class QuestionFilterForm(forms.Form): def __init__(self, *args, **kwargs): user = kwargs.pop("user") super(QuestionFilterForm, self).__init__(*args, **kwargs) - questions = Question.objects.filter(user_id=user) + questions = Question.objects.filter(user_id=user.id) points_list = questions.values_list('points', flat=True).distinct() points_options = [('select', 'Select Marks')] points_options.extend([(point, point) for point in points_list]) diff --git a/yaksh/views.py b/yaksh/views.py index 1d79a4c..81b2a60 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -186,14 +186,16 @@ def add_question(request, question_id=None): 'formset': test_case_formset}, context_instance=ci) else: - d = Question.objects.get(id=question_id) + d = Question.objects.get(id=question_id, user_id=user.id) form = QuestionForm(request.POST, instance=d) test_case_formset = add_or_delete_test_form(request.POST, d) if 'save_question' in request.POST: qtn = form.save(commit=False) + qtn.user = user + qtn.save() test_case_formset = TestCaseFormSet(request.POST, prefix='test', instance=qtn) form.save() - question = Question.objects.get(id=question_id) + question = Question.objects.get(id=question_id, user_id=user.id) if test_case_formset.is_valid(): test_case_formset.save() return my_redirect("/exam/manage/questions") @@ -217,7 +219,7 @@ def add_question(request, question_id=None): 'formset': test_case_formset}, context_instance=ci) else: - d = Question.objects.get(id=question_id) + d = Question.objects.get(id=question_id, user_id=user.id) form = QuestionForm(instance=d) test_case_formset = TestCaseFormSet(prefix='test', instance=d) @@ -816,7 +818,7 @@ def show_all_questions(request): data = request.POST.getlist('question') if data is not None: for i in data: - question = Question.objects.get(id=i).delete() + question = Question.objects.get(id=i, user_id=user.id).delete() questions = Question.objects.filter(user_id=user.id) form = QuestionFilterForm(user=user) upload_form = UploadFileForm() @@ -1002,7 +1004,7 @@ def design_questionpaper(request): if random_questions: for random_question, num in zip(random_questions, random_number): qid = random_question.split(',')[0] - question = Question.objects.get(id=int(qid)) + question = Question.objects.get(id=int(qid), user_id=user.id) marks = question.points question_set = QuestionSet(marks=marks, num_questions=num) question_set.save() @@ -1206,7 +1208,6 @@ def upload_questions(request): if not is_moderator(user): raise Http404('You are not allowed to view this page!') - if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): -- cgit From b74128df59de974032d630a267a15e7d0c91a593 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 15 Apr 2016 12:37:59 +0530 Subject: models testcase changes --- yaksh/tests.py | 8 +++++--- yaksh/urls.py | 1 - 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'yaksh') diff --git a/yaksh/tests.py b/yaksh/tests.py index d3ff4fc..de9b39e 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -27,7 +27,7 @@ def setUpModule(): # create 20 questions for i in range(1, 21): - Question.objects.create(summary='Q%d' % (i), points=1, type='code') + Question.objects.create(summary='Q%d' % (i), points=1, type='code', user=user) # create a quiz quiz = Quiz.objects.create(start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0), @@ -72,10 +72,11 @@ class ProfileTestCases(unittest.TestCase): class QuestionTestCases(unittest.TestCase): def setUp(self): # Single question details + self.user = User.objects.get(pk=1) self.question = Question(summary='Demo question', language='Python', type='Code', active=True, description='Write a function', points=1.0, - snippet='def myfunc()') + snippet='def myfunc()', user=self.user) self.question.save() self.question.tags.add('python', 'function') self.testcase = TestCase(question=self.question, @@ -121,10 +122,11 @@ class QuestionTestCases(unittest.TestCase): ############################################################################### class TestCaseTestCases(unittest.TestCase): def setUp(self): + self.user = User.objects.get(pk=1) self.question = Question(summary='Demo question', language='Python', type='Code', active=True, description='Write a function', points=1.0, - snippet='def myfunc()') + snippet='def myfunc()', user=self.user) self.question.save() self.testcase = TestCase(question=self.question, func_name='def myfunc', kw_args='a=10,b=11', diff --git a/yaksh/urls.py b/yaksh/urls.py index 4e72084..533a6f9 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -74,7 +74,6 @@ urlpatterns += [ views.enroll, {'was_rejected': True}), url(r'manage/reject/(?P\d+)/(?P\d+)/$', views.reject), url(r'manage/enrolled/reject/(?P\d+)/(?P\d+)/$', - views.reject, {'was_enrolled': True}), url(r'manage/toggle_status/(?P\d+)/$', views.toggle_course_status), url(r'^ajax/questionpaper/(?P.+)/$', views.ajax_questionpaper), -- cgit From 83b4826a242a6825197e42eb233f9301f99f3716 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 25 Apr 2016 14:34:54 +0530 Subject: added test cases for loading and dumping questions --- yaksh/models.py | 21 +++++++++++ yaksh/templates/yaksh/showquestions.html | 2 +- yaksh/tests.py | 38 +++++++++++++++++++ yaksh/views.py | 64 ++++++++------------------------ 4 files changed, 76 insertions(+), 49 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index c76d655..b7df67c 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -211,6 +211,27 @@ class Question(models.Model): return json.dumps(question_info_dict) + def dump_questions_into_json(self, user): + questions = Question.objects.filter(user_id = user.id) + questions_dict = [{'summary': question.summary, + 'description': question.description, + 'points': question.points, 'test': question.test, + 'ref_code_path': question.ref_code_path, + 'options': question.options, 'language': question.language, + 'type': question.type, 'active': question.active, + 'snippet': question.snippet} for question in questions] + return json.dumps(questions_dict, indent=2) + + def load_questions_from_json(self, questions_list, user): + questions = json.loads(questions_list) + for question in questions: + Question.objects.get_or_create(summary=question['summary'], + description=question['description'], points=question['points'], + test=question['test'], ref_code_path=question['ref_code_path'], + options=question['options'], language=question['language'], + type=question['type'], active=question['active'], + snippet=question['snippet'], user=user) + def __unicode__(self): return self.summary diff --git a/yaksh/templates/yaksh/showquestions.html b/yaksh/templates/yaksh/showquestions.html index ba96728..19fc06e 100644 --- a/yaksh/templates/yaksh/showquestions.html +++ b/yaksh/templates/yaksh/showquestions.html @@ -15,7 +15,7 @@ Download Questions
{% endif %} -

Upload csv file for adding questions

+

Upload json file for adding questions

{% csrf_token %} {{ upload_form.as_p }} diff --git a/yaksh/tests.py b/yaksh/tests.py index de9b39e..f78d151 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -73,11 +73,19 @@ 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', type='Code', active=True, description='Write a function', points=1.0, snippet='def myfunc()', user=self.user) self.question.save() + + self.question1 = 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() + self.question.tags.add('python', 'function') self.testcase = TestCase(question=self.question, func_name='def myfunc', kw_args='a=10,b=11', @@ -96,6 +104,11 @@ class QuestionTestCases(unittest.TestCase): } self.answer_data_json = json.dumps(answer_data) self.user_answer = "demo_answer" + questions_data = [{"snippet": "def fact()", "active": True, "points": 1.0, + "ref_code_path": "", "description": "factorial of a no", + "language": "Python", "test": "", "type": "Code", + "options": "", "summary": "Json Demo"}] + self.json_questions_data = json.dumps(questions_data) def test_question(self): """ Test question """ @@ -118,6 +131,31 @@ class QuestionTestCases(unittest.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_questions_into_json(self.user1)) + for que in questions: + self.assertEqual(self.question1.summary, que['summary']) + self.assertEqual(self.question1.language, que['language']) + self.assertEqual(self.question1.type, que['type']) + self.assertEqual(self.question1.description, que['description']) + self.assertEqual(self.question1.points, que['points']) + self.assertTrue(self.question1.active) + self.assertEqual(self.question1.snippet, que['snippet']) + + def test_load_questions_from_json(self): + """ Test load questions into database from json """ + question = Question() + result = question.load_questions_from_json(self.json_questions_data, self.user1) + question_data = Question.objects.all().last() + self.assertEqual(question_data.summary, 'Json Demo') + self.assertEqual(question_data.language, 'Python') + self.assertEqual(question_data.type, 'Code') + self.assertEqual(question_data.description, 'factorial of a no') + self.assertEqual(question_data.points, 1.0) + self.assertTrue(question_data.active) + self.assertEqual(question_data.snippet, 'def fact()') ############################################################################### class TestCaseTestCases(unittest.TestCase): diff --git a/yaksh/views.py b/yaksh/views.py index 81b2a60..a7b9ff9 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -815,10 +815,9 @@ def show_all_questions(request): 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: - for i in data: - question = Question.objects.get(id=i, user_id=user.id).delete() + question_ids = request.POST.getlist('question') + if question_ids is not None: + question = Question.objects.filter(id__in=question_ids, user_id=user.id).delete() questions = Question.objects.filter(user_id=user.id) form = QuestionFilterForm(user=user) upload_form = UploadFileForm() @@ -955,7 +954,7 @@ def ajax_questionpaper(request, query): user = request.user if query == 'marks': question_type = request.POST.get('question_type') - questions = Question.objects.filter(type=question_type, user=user) + questions = Question.objects.filter(type=question_type, user_id=user.id) marks = questions.values_list('points').distinct() return my_render_to_response('yaksh/ajax_marks.html', {'marks': marks}) elif query == 'questions': @@ -967,7 +966,7 @@ def ajax_questionpaper(request, query): random_question_list = ",".join(random_questions).split(',') question_list = fixed_question_list + random_question_list questions = list(Question.objects.filter(type=question_type, - points=marks_selected, user=user)) + points=marks_selected, user_id=user.id)) questions = [question for question in questions \ if not str(question.id) in question_list] return my_render_to_response('yaksh/ajax_questions.html', @@ -1167,37 +1166,11 @@ def download_questions(request): user = request.user if not is_moderator(user): raise Http404('You are not allowed to view this page!') - questions = Question.objects.filter(user_id = user.id) - response = HttpResponse(content_type='text/csv') - response['Content-Disposition'] = 'attachment; filename="{0}_Questions.csv"'.format(user) - writer = csv.writer(response) - header = [ - 'summary', - 'description', - 'points', - 'test', - 'ref_code_path', - 'options', - 'language', - 'type', - 'active', - 'snippet' - ] - writer.writerow(header) - for que in questions: - row = [ - que.summary, - que.description, - que.points, - que.test, - que.ref_code_path, - que.options, - que.language, - que.type, - que.active, - que.snippet - ] - writer.writerow(row) + question = Question() + questions = question.dump_questions_into_json(user) + response = HttpResponse(questions, content_type='text/json') + response['Content-Disposition'] = 'attachment; filename="{0}_questions.json"'\ + .format(user) return response @@ -1211,19 +1184,14 @@ def upload_questions(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): - csvfile = request.FILES['file'] - if csvfile.name.split('.')[1] == "csv": - reader = csv.DictReader(csvfile, delimiter=',') - for row in reader: - Question.objects.get_or_create(summary=row['summary'], - description=row['description'], points=row['points'], - test=row['test'], ref_code_path=row['ref_code_path'], - options=row['options'], language=row['language'], - type=row['type'], active=row['active'], - snippet=row['snippet'], user=user) + questions_file = request.FILES['file'] + if questions_file.name.split('.')[1] == "json": + questions_list = questions_file.read() + question = Question() + question.load_questions_from_json(questions_list, user) return my_redirect('/exam/manage/questions') else: - raise Http404('Please Upload a csv file') + raise Http404("Please Upload a JSON file") else: return my_redirect('/exam/manage/questions') else: -- cgit From 94b449a10e8ba709f64aa56cb18640ba93642cee Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 25 Apr 2016 15:03:16 +0530 Subject: small test case change --- yaksh/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'yaksh') diff --git a/yaksh/tests.py b/yaksh/tests.py index f78d151..8129ed5 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -148,7 +148,7 @@ class QuestionTestCases(unittest.TestCase): """ Test load questions into database from json """ question = Question() result = question.load_questions_from_json(self.json_questions_data, self.user1) - question_data = Question.objects.all().last() + question_data = Question.objects.get(pk=27) self.assertEqual(question_data.summary, 'Json Demo') self.assertEqual(question_data.language, 'Python') self.assertEqual(question_data.type, 'Code') -- cgit From 51ad57a7ef8fa4cab7f4e590a260f18e3040ef74 Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 27 Apr 2016 18:04:42 +0530 Subject: rebase changes and function name changes --- yaksh/models.py | 4 ++-- yaksh/views.py | 30 +++++++++++++----------------- 2 files changed, 15 insertions(+), 19 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index b7df67c..79db206 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -211,7 +211,7 @@ class Question(models.Model): return json.dumps(question_info_dict) - def dump_questions_into_json(self, user): + def dump_into_json(self, user): questions = Question.objects.filter(user_id = user.id) questions_dict = [{'summary': question.summary, 'description': question.description, @@ -222,7 +222,7 @@ class Question(models.Model): 'snippet': question.snippet} for question in questions] return json.dumps(questions_dict, indent=2) - def load_questions_from_json(self, questions_list, user): + def load_from_json(self, questions_list, user): questions = json.loads(questions_list) for question in questions: Question.objects.get_or_create(summary=question['summary'], diff --git a/yaksh/views.py b/yaksh/views.py index a7b9ff9..8b69619 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -186,16 +186,14 @@ def add_question(request, question_id=None): 'formset': test_case_formset}, context_instance=ci) else: - d = Question.objects.get(id=question_id, user_id=user.id) + d = Question.objects.get(id=question_id) form = QuestionForm(request.POST, instance=d) test_case_formset = add_or_delete_test_form(request.POST, d) if 'save_question' in request.POST: qtn = form.save(commit=False) - qtn.user = user - qtn.save() test_case_formset = TestCaseFormSet(request.POST, prefix='test', instance=qtn) form.save() - question = Question.objects.get(id=question_id, user_id=user.id) + question = Question.objects.get(id=question_id) if test_case_formset.is_valid(): test_case_formset.save() return my_redirect("/exam/manage/questions") @@ -219,7 +217,7 @@ def add_question(request, question_id=None): 'formset': test_case_formset}, context_instance=ci) else: - d = Question.objects.get(id=question_id, user_id=user.id) + d = Question.objects.get(id=question_id) form = QuestionForm(instance=d) test_case_formset = TestCaseFormSet(prefix='test', instance=d) @@ -815,9 +813,10 @@ def show_all_questions(request): raise Http404("You are not allowed to view this page !") if request.method == 'POST' and request.POST.get('delete') == 'delete': - question_ids = request.POST.getlist('question') - if question_ids is not None: - question = Question.objects.filter(id__in=question_ids, user_id=user.id).delete() + data = request.POST.getlist('question') + if data is not None: + for i in data: + question = Question.objects.get(id=i).delete() questions = Question.objects.filter(user_id=user.id) form = QuestionFilterForm(user=user) upload_form = UploadFileForm() @@ -954,7 +953,7 @@ def ajax_questionpaper(request, query): user = request.user if query == 'marks': question_type = request.POST.get('question_type') - questions = Question.objects.filter(type=question_type, user_id=user.id) + questions = Question.objects.filter(type=question_type, user=user) marks = questions.values_list('points').distinct() return my_render_to_response('yaksh/ajax_marks.html', {'marks': marks}) elif query == 'questions': @@ -966,7 +965,7 @@ def ajax_questionpaper(request, query): random_question_list = ",".join(random_questions).split(',') question_list = fixed_question_list + random_question_list questions = list(Question.objects.filter(type=question_type, - points=marks_selected, user_id=user.id)) + points=marks_selected, user=user)) questions = [question for question in questions \ if not str(question.id) in question_list] return my_render_to_response('yaksh/ajax_questions.html', @@ -1003,7 +1002,7 @@ def design_questionpaper(request): if random_questions: for random_question, num in zip(random_questions, random_number): qid = random_question.split(',')[0] - question = Question.objects.get(id=int(qid), user_id=user.id) + question = Question.objects.get(id=int(qid)) marks = question.points question_set = QuestionSet(marks=marks, num_questions=num) question_set.save() @@ -1167,7 +1166,7 @@ def download_questions(request): if not is_moderator(user): raise Http404('You are not allowed to view this page!') question = Question() - questions = question.dump_questions_into_json(user) + questions = question.dump_into_json(user) response = HttpResponse(questions, content_type='text/json') response['Content-Disposition'] = 'attachment; filename="{0}_questions.json"'\ .format(user) @@ -1188,11 +1187,8 @@ def upload_questions(request): if questions_file.name.split('.')[1] == "json": questions_list = questions_file.read() question = Question() - question.load_questions_from_json(questions_list, user) + question.load_from_json(questions_list, user) return my_redirect('/exam/manage/questions') else: raise Http404("Please Upload a JSON file") - else: - return my_redirect('/exam/manage/questions') - else: - return my_redirect('/exam/manage/questions') + return my_redirect('/exam/manage/questions') -- cgit From 5bb441584628e65fe08ce1f240ad43f5f3ca88a8 Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 27 Apr 2016 18:22:17 +0530 Subject: test case changes --- yaksh/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'yaksh') diff --git a/yaksh/tests.py b/yaksh/tests.py index 8129ed5..eedcd30 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -134,7 +134,7 @@ class QuestionTestCases(unittest.TestCase): def test_dump_questions_into_json(self): """ Test dump questions into json """ question = Question() - questions = json.loads(question.dump_questions_into_json(self.user1)) + questions = json.loads(question.dump_into_json(self.user1)) for que in questions: self.assertEqual(self.question1.summary, que['summary']) self.assertEqual(self.question1.language, que['language']) @@ -147,7 +147,7 @@ class QuestionTestCases(unittest.TestCase): def test_load_questions_from_json(self): """ Test load questions into database from json """ question = Question() - result = question.load_questions_from_json(self.json_questions_data, self.user1) + result = question.load_from_json(self.json_questions_data, self.user1) question_data = Question.objects.get(pk=27) self.assertEqual(question_data.summary, 'Json Demo') self.assertEqual(question_data.language, 'Python') -- cgit From 2f7891874aec1dec962d55e3b2aaed1c61d7acaa Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 4 May 2016 22:55:19 +0530 Subject: rebase changes and changes as per comments --- yaksh/templates/yaksh/showquestions.html | 9 ++++-- yaksh/tests.py | 14 ++++----- yaksh/urls.py | 4 +-- yaksh/views.py | 54 ++++++++++++++------------------ 4 files changed, 39 insertions(+), 42 deletions(-) (limited to 'yaksh') diff --git a/yaksh/templates/yaksh/showquestions.html b/yaksh/templates/yaksh/showquestions.html index 19fc06e..6e2253a 100644 --- a/yaksh/templates/yaksh/showquestions.html +++ b/yaksh/templates/yaksh/showquestions.html @@ -19,9 +19,11 @@ {% csrf_token %} {{ upload_form.as_p }} - + - +{% if message %} +

{{ message }}

+{% endif %}
{% csrf_token %}
@@ -45,9 +47,12 @@
+{% if questions %} +
Select All
{% for i in questions %}   {{ i }}
{% endfor %} +{% endif %}

   diff --git a/yaksh/tests.py b/yaksh/tests.py index eedcd30..a3f3880 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -135,14 +135,14 @@ class QuestionTestCases(unittest.TestCase): """ Test dump questions into json """ question = Question() questions = json.loads(question.dump_into_json(self.user1)) - for que in questions: - self.assertEqual(self.question1.summary, que['summary']) - self.assertEqual(self.question1.language, que['language']) - self.assertEqual(self.question1.type, que['type']) - self.assertEqual(self.question1.description, que['description']) - self.assertEqual(self.question1.points, que['points']) + 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, que['snippet']) + self.assertEqual(self.question1.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 533a6f9..f660ed7 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -88,9 +88,9 @@ urlpatterns += [ url(r'^manage/searchteacher/(?P\d+)/$', views.search_teacher), url(r'^manage/addteacher/(?P\d+)/$', views.add_teacher), url(r'^manage/allotted_course/$', views.allotted_courses), - url(r'^manage/remove_teachers/(?P\d+)/$', views.remove_teachers) + url(r'^manage/remove_teachers/(?P\d+)/$', views.remove_teachers), url(r'^manage/download_questions/$', views.download_questions), - url(r'^manage/upload_questions/$', views.upload_questions) + url(r'^manage/upload_questions/$', views.show_all_questions) ] diff --git a/yaksh/views.py b/yaksh/views.py index 8b69619..2dde09f 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 = {} + filter_dict = {"user_id":user.id} question_type = request.POST.get('question_type') marks = request.POST.get('marks') language = request.POST.get('language') @@ -797,7 +797,7 @@ def ajax_questions_filter(request): if language != "select": filter_dict['language'] = str(language) - questions = list(Question.objects.filter(**filter_dict).filter(user_id=user.id)) + questions = list(Question.objects.filter(**filter_dict)) return my_render_to_response('yaksh/ajax_question_filter.html', {'questions': questions}) @@ -809,23 +809,35 @@ def show_all_questions(request): user = request.user ci = RequestContext(request) - if not user.is_authenticated() or not is_moderator(user): + context = {} + 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: - for i in data: - question = Question.objects.get(id=i).delete() + 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() + question = Question() + question.load_from_json(questions_list, user) + else: + message = "Please Upload a JSON file" + context['message'] = message + questions = Question.objects.filter(user_id=user.id) form = QuestionFilterForm(user=user) upload_form = UploadFileForm() - context = {'papers': [], - 'question': None, - 'questions': questions, - 'form': form, - 'upload_form': upload_form - } + context['papers'] = [] + context['question'] = None + context['questions'] = questions + context['form'] = form + context['upload_form'] = upload_form return my_render_to_response('yaksh/showquestions.html', context, context_instance=ci) @@ -1172,23 +1184,3 @@ def download_questions(request): .format(user) return response - -@login_required -def upload_questions(request): - user = request.user - ci = RequestContext(request) - - if not is_moderator(user): - raise Http404('You are not allowed to view this page!') - if request.method == 'POST': - 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) - return my_redirect('/exam/manage/questions') - else: - raise Http404("Please Upload a JSON file") - return my_redirect('/exam/manage/questions') -- cgit From a2d3d14e03c7f6a873b4f838752878d2bcab44aa Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 5 May 2016 12:52:47 +0530 Subject: changes for download questions --- yaksh/models.py | 6 +-- yaksh/templates/yaksh/ajax_question_filter.html | 3 +- yaksh/templates/yaksh/showquestions.html | 14 +++--- yaksh/tests.py | 59 +++++++++++++------------ yaksh/urls.py | 2 +- yaksh/views.py | 56 +++++++++++------------ 6 files changed, 72 insertions(+), 68 deletions(-) (limited to 'yaksh') 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 %}
  • {% 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 %} -Download Questions
    -{% endif %} -

    Upload json file for adding questions

    - + {% csrf_token %} {{ upload_form.as_p }} @@ -24,6 +20,9 @@ {% if message %}

    {{ message }}

    {% endif %} +{% if msg %} +

    {{ msg }}

    +{% endif %} {% csrf_token %}
    @@ -56,6 +55,9 @@

       - +   +{% if questions %} + +{% endif %} {% 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\d+)/$', views.add_teacher), url(r'^manage/allotted_course/$', views.allotted_courses), url(r'^manage/remove_teachers/(?P\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 - -- cgit From 01eee9b024b7717e264ed4b7a00873389fa07cd1 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 5 May 2016 15:21:06 +0530 Subject: model function change --- yaksh/models.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 0ee5a3d..aa08278 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -225,12 +225,8 @@ class Question(models.Model): def load_from_json(self, questions_list, user): questions = json.loads(questions_list) for question in questions: - Question.objects.get_or_create(summary=question['summary'], - description=question['description'], points=question['points'], - test=question['test'], ref_code_path=question['ref_code_path'], - options=question['options'], language=question['language'], - type=question['type'], active=question['active'], - snippet=question['snippet'], user=user) + question['user'] = user + Question.objects.get_or_create(**question) def __unicode__(self): return self.summary -- cgit From e81b13f7d94c0877801726fc85e967f36ba8bd90 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 5 May 2016 17:14:20 +0530 Subject: change dump json function --- yaksh/models.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index aa08278..6e59d7a 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -213,13 +213,16 @@ class Question(models.Model): 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, + questions_dict = [] + for question in questions: + q_dict = {'summary': question.summary, 'description': question.description, 'points': question.points, 'test': question.test, 'ref_code_path': question.ref_code_path, 'options': question.options, 'language': question.language, 'type': question.type, 'active': question.active, - 'snippet': question.snippet} for question in questions] + 'snippet': question.snippet} + questions_dict.append(q_dict) + return json.dumps(questions_dict, indent=2) def load_from_json(self, questions_list, user): -- cgit