diff options
Diffstat (limited to 'yaksh/views.py')
-rw-r--r-- | yaksh/views.py | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/yaksh/views.py b/yaksh/views.py index 9f7c7a9..6644705 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 @@ -34,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')) @@ -73,6 +75,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 +83,7 @@ def add_to_group(users): if not is_moderator(user): user.groups.add(group) + def index(request): """The start page. """ @@ -677,7 +681,12 @@ 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) + try: + demo_user = User.objects.get(username="yaksh_demo_user") + except User.DoesNotExist: + demo_user = None + courses = Course.objects.filter(Q(creator=user) | Q(creator=demo_user), + is_trial=False) 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, @@ -885,22 +894,25 @@ 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": + ques = Question() + extract_files(questions_file) + ques.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_questions(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" |