summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--yaksh/models.py21
-rw-r--r--yaksh/templates/yaksh/showquestions.html2
-rw-r--r--yaksh/tests.py38
-rw-r--r--yaksh/views.py64
4 files changed, 76 insertions, 49 deletions
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 @@
<a href="{{URL_ROOT}}/exam/manage/download_questions/">Download Questions</a><br>
{% endif %}
-<h4>Upload csv file for adding questions</h4>
+<h4>Upload json file for adding questions</h4>
<form action="{{URL_ROOT}}/exam/manage/upload_questions/" method="post" enctype="multipart/form-data">
{% 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: