summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--yaksh/models.py46
-rw-r--r--yaksh/views.py44
2 files changed, 72 insertions, 18 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index 73d4b27..d626b26 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -10,9 +10,13 @@ from django.forms.models import model_to_dict
from django.contrib.contenttypes.models import ContentType
from taggit.managers import TaggableManager
from django.utils import timezone
+from django.core.files import File
+from StringIO import StringIO
import pytz
import os
import shutil
+import zipfile
+
languages = (
("python", "Python"),
@@ -231,27 +235,32 @@ 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 = []
+ zip_file_name = StringIO()
+ zip_file = zipfile.ZipFile(zip_file_name, "a")
for question in questions:
test_case = question.get_test_cases()
+ files = question._add_and_get_files(zip_file)
q_dict = {'summary': question.summary,
'description': question.description,
- 'points': question.points,
- 'language': question.language,
- 'type': question.type,
- 'active': question.active,
+ 'points': question.points, 'language': question.language,
+ 'type': question.type, 'active': question.active,
'test_case_type': question.test_case_type,
'snippet': question.snippet,
- 'testcase': [case.get_field_value() for case in test_case]}
+ 'testcase': [case.get_field_value() for case in test_case],
+ 'files': files}
questions_dict.append(q_dict)
-
- return json.dumps(questions_dict, indent=2)
+ question._add_json_to_zip(zip_file, questions_dict)
+ return zip_file_name
def load_from_json(self, questions_list, user):
questions = json.loads(questions_list)
+ ques = Question()
for question in questions:
question['user'] = user
+ files = question.pop('files')
test_cases = question.pop('testcase')
que, result = Question.objects.get_or_create(**question)
+ que._add_files_to_db(files)
model_class = get_model_class(que.test_case_type)
for test_case in test_cases:
model_class.objects.get_or_create(question=que, **test_case)
@@ -278,6 +287,29 @@ class Question(models.Model):
return test_case
+ def _add_and_get_files(self, zip_file):
+ files = FileUpload.objects.filter(question=self)
+ for file in files:
+ zip_file.write(file.file.path, (os.path.basename(file.file.path)))
+ files_list = [os.path.basename(file.file.name) for file in files]
+ return files_list
+
+ def _add_files_to_db(self, files):
+ if files:
+ for file_name in files:
+ file = open(file_name, 'r')
+ django_file = File(file)
+ f = FileUpload.objects.get_or_create(file=django_file, question=self)
+ os.remove(file_name)
+
+ 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:
+ json_file.write(json_data)
+ zip_file.write(json_file.name)
+ zip_file.close()
+ os.remove(json_file.name)
+
def __unicode__(self):
return self.summary
diff --git a/yaksh/views.py b/yaksh/views.py
index e1ec44e..87e6005 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -22,6 +22,7 @@ import pytz
from taggit.models import Tag
from itertools import chain
import json
+import zipfile
# Local imports.
from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course
@@ -73,6 +74,7 @@ def is_moderator(user):
if user.groups.filter(name='moderator').exists():
return True
+
def add_to_group(users):
""" add users to moderator group """
group = Group.objects.get(name="moderator")
@@ -80,6 +82,22 @@ def add_to_group(users):
if not is_moderator(user):
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_from_json(questions_list, user)
+ os.remove(json_file)
+
+
def index(request):
"""The start page.
"""
@@ -663,9 +681,11 @@ def courses(request):
ci = RequestContext(request)
if not is_moderator(user):
raise Http404('You are not allowed to view this page')
- courses = Course.objects.filter(creator=user, is_trial=False)
+ demo_user = User.objects.get(username="demo_user")
+ courses = Course.objects.filter(Q(creator=user) | Q(creator=demo_user),
+ is_trial=False)
return my_render_to_response('yaksh/courses.html', {'courses': courses},
- context_instance=ci)
+ context_instance=ci)
@login_required
@@ -869,22 +889,24 @@ def show_all_questions(request):
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)
+ file_name = questions_file.name.split('.')
+ if file_name[-1] == "zip":
+ extract_files(questions_file)
+ read_json("questions_dump.json", user)
else:
- message = "Please Upload a JSON file"
+ message = "Please Upload a ZIP file"
context['message'] = message
if request.POST.get('download') == 'download':
question_ids = request.POST.getlist('question')
if question_ids:
question = Question()
- 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)
+ zip_file = question.dump_into_json(question_ids, user)
+ response = HttpResponse(content_type='application/zip')
+ response['Content-Disposition'] = '''attachment;\
+ filename={0}_questions.zip'''.format(user)
+ zip_file.seek(0)
+ response.write(zip_file.read())
return response
else:
context['msg'] = "Please select atleast one question to download"