diff options
Diffstat (limited to 'yaksh/views.py')
-rw-r--r-- | yaksh/views.py | 83 |
1 files changed, 68 insertions, 15 deletions
diff --git a/yaksh/views.py b/yaksh/views.py index db7498c..d0735bd 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -22,6 +22,11 @@ from taggit.models import Tag from itertools import chain import json import six +import zipfile +try: + from StringIO import StringIO as string_io +except ImportError: + from io import BytesIO as string_io # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, FileUpload,\ @@ -494,20 +499,28 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): # validation if 'assignment' in request.FILES: assignment_filename = request.FILES.getlist('assignment') + qp = QuestionPaper.objects.get(id=questionpaper_id) for fname in assignment_filename: if AssignmentUpload.objects.filter( assignmentQuestion=current_question, - assignmentFile__icontains=fname, user=user).exists(): + assignmentFile__icontains=fname, user=user, + question_paper=questionpaper_id).exists(): assign_file = AssignmentUpload.objects.get( assignmentQuestion=current_question, - assignmentFile__icontains=fname, user=user) + assignmentFile__icontains=fname, user=user, + question_paper=questionpaper_id) os.remove(assign_file.assignmentFile.path) assign_file.delete() AssignmentUpload.objects.create(user=user, - assignmentQuestion=current_question, assignmentFile=fname + assignmentQuestion=current_question, assignmentFile=fname, + question_paper=qp ) user_answer = 'ASSIGNMENT UPLOADED' if not current_question.grade_assignment_upload: + new_answer = Answer(question=current_question, answer=user_answer, + correct=False, error=json.dumps([])) + new_answer.save() + paper.answers.add(new_answer) next_q = paper.add_completed_question(current_question.id) return show_question(request, next_q, paper) else: @@ -910,14 +923,15 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): if 'remove-fixed' in request.POST: question_ids = request.POST.getlist('added-questions', None) - que_order = question_paper.fixed_question_order.split(",") - for qid in question_ids: - que_order.remove(qid) - if que_order: - question_paper.fixed_question_order = ",".join(que_order) - else: - question_paper.fixed_question_order = "" - question_paper.save() + if question_paper.fixed_question_order: + que_order = question_paper.fixed_question_order.split(",") + for qid in question_ids: + que_order.remove(qid) + if que_order: + question_paper.fixed_question_order = ",".join(que_order) + else: + question_paper.fixed_question_order = "" + question_paper.save() question_paper.fixed_questions.remove(*question_ids) if 'add-random' in request.POST: @@ -1099,7 +1113,12 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None): .values("id") user_details = AnswerPaper.objects\ .get_users_for_questionpaper(questionpaper_id) - context = {"users": user_details, "quiz_id": quiz_id} + quiz_assignment_file_status = AssignmentUpload.objects.filter( + question_paper_id=questionpaper_id + ).exists() + context = {"users": user_details, "quiz_id": quiz_id, + "quiz_file_status": quiz_assignment_file_status + } if user_id is not None: attempts = AnswerPaper.objects.get_user_all_attempts\ @@ -1109,14 +1128,18 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None): attempt_number = attempts[0].attempt_number except IndexError: raise Http404('No attempts for paper') - + user_assignment_file_status = AssignmentUpload.objects.filter( + question_paper_id=questionpaper_id, + user_id=user_id + ).exists() user = User.objects.get(id=user_id) data = AnswerPaper.objects.get_user_data(user, questionpaper_id, attempt_number ) - context = {'data': data, "quiz_id": quiz_id, "users": user_details, - "attempts": attempts, "user_id": user_id + "attempts": attempts, "user_id": user_id, + "user_file_status": user_assignment_file_status, + "quiz_file_status": quiz_assignment_file_status } if request.method == "POST": papers = data['papers'] @@ -1403,3 +1426,33 @@ def download_course_csv(request, course_id): for student in students: writer.writerow(student) return response + + +@login_required +def download_assignment_file(request, questionpaper_id, question_id=None, + user_id=None): + user = request.user + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + assignment = AssignmentUpload() + assignment_files, file_name = assignment.get_assignments(questionpaper_id, + question_id, user_id + ) + zipfile_name = string_io() + zip_file = zipfile.ZipFile(zipfile_name, "w") + for f_name in assignment_files: + name = f_name.user.get_full_name().replace(" ", "_") + folder_name = os.sep.join((name, os.path.basename( + f_name.assignmentFile.name)) + ) + zip_file.write(f_name.assignmentFile.path, folder_name + ) + zip_file.close() + zipfile_name.seek(0) + response = HttpResponse(content_type='application/zip') + response['Content-Disposition'] = '''attachment;\ + filename={0}'''.format( + file_name.replace(" ", "_") + ) + response.write(zipfile_name.read()) + return response |