diff options
author | adityacp | 2017-03-31 16:33:51 +0530 |
---|---|---|
committer | adityacp | 2017-04-10 11:51:08 +0530 |
commit | 592353ba30178654975f4c64602414cac7f41a9d (patch) | |
tree | a0177635e0abecf419a78824e0d45c3afbe5e482 | |
parent | ad73f7b938b7620ab4be202dc33c699cf5cb4ac5 (diff) | |
download | online_test-592353ba30178654975f4c64602414cac7f41a9d.tar.gz online_test-592353ba30178654975f4c64602414cac7f41a9d.tar.bz2 online_test-592353ba30178654975f4c64602414cac7f41a9d.zip |
Add download assignment feature for assignment based questions
- Add method in AssignmentUpload to get assignments
- Add function in views to serve assignment files
-rw-r--r-- | yaksh/models.py | 24 | ||||
-rw-r--r-- | yaksh/views.py | 83 |
2 files changed, 91 insertions, 16 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 802a1fc..2d3aadb 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -79,7 +79,8 @@ test_status = ( def get_assignment_dir(instance, filename): return os.sep.join(( - instance.user.username, str(instance.assignmentQuestion.id), filename + instance.question_paper.quiz.description, instance.user.username, + str(instance.assignmentQuestion.id), filename )) @@ -1310,7 +1311,26 @@ class AssignmentUpload(models.Model): user = models.ForeignKey(User) assignmentQuestion = models.ForeignKey(Question) assignmentFile = models.FileField(upload_to=get_assignment_dir) + question_paper = models.ForeignKey(QuestionPaper, blank=True, null=True) + def get_assignments(self, qp_id, que_id=None, user_id=None): + if que_id and user_id: + assignment_files = AssignmentUpload.objects.filter( + assignmentQuestion_id=que_id, user_id=user_id, + question_paper_id=qp_id + ) + user_name = User.objects.get(id=user_id) + file_name = user_name.get_full_name().replace(" ", "_") + else: + assignment_files = AssignmentUpload.objects.filter( + question_paper_id=qp_id + ) + + file_name = "%s_Assignment_files" %( + assignment_files[0].question_paper.quiz.description + ) + + return assignment_files, file_name ############################################################################### class TestCase(models.Model): @@ -1372,7 +1392,9 @@ class HookTestCase(TestCase): mark_fraction - Float, indicating fraction of the weight to a test case error - String, error message if success is false + In case of assignment upload there will be no user answer ''' + success = False err = "Incorrect Answer" # Please make this more specific mark_fraction = 0.0 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 |