From 592353ba30178654975f4c64602414cac7f41a9d Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 31 Mar 2017 16:33:51 +0530 Subject: Add download assignment feature for assignment based questions - Add method in AssignmentUpload to get assignments - Add function in views to serve assignment files --- yaksh/views.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 15 deletions(-) (limited to 'yaksh/views.py') 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 -- cgit From 5623c1225f66831d5e5db2089218b7904c3124b6 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 10 Apr 2017 16:15:59 +0530 Subject: Change download assignemnts to get files using quiz --- yaksh/views.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index d0735bd..46a2bdd 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -501,14 +501,15 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): 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, - question_paper=questionpaper_id).exists(): - assign_file = AssignmentUpload.objects.get( - assignmentQuestion=current_question, - assignmentFile__icontains=fname, user=user, - question_paper=questionpaper_id) + assignment_files = AssignmentUpload.objects.filter( + assignmentQuestion=current_question, + assignmentFile__icontains=fname, user=user, + question_paper=questionpaper_id) + if assignment_files.exists(): + assign_file = assignment_files.get( + assignmentQuestion=current_question, + assignmentFile__icontains=fname, user=user, + question_paper=questionpaper_id) os.remove(assign_file.assignmentFile.path) assign_file.delete() AssignmentUpload.objects.create(user=user, @@ -1429,14 +1430,14 @@ def download_course_csv(request, course_id): @login_required -def download_assignment_file(request, questionpaper_id, question_id=None, - user_id=None): +def download_assignment_file(request, quiz_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!') + qp = QuestionPaper.objects.get(quiz_id=quiz_id) assignment = AssignmentUpload() - assignment_files, file_name = assignment.get_assignments(questionpaper_id, - question_id, user_id + assignment_files, file_name = assignment.get_assignments(qp, question_id, + user_id ) zipfile_name = string_io() zip_file = zipfile.ZipFile(zipfile_name, "w") -- cgit From deaa592f01d5f270abca89c78620af1406fee67c Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 19 Apr 2017 12:54:41 +0530 Subject: Change urls, models, views - Add Assignment upload model manager - Change views to update marks for user if updated manually - Change variable names in views - Add url to provide assignment download for student --- yaksh/views.py | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index 46a2bdd..0db1812 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -497,9 +497,11 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): elif current_question.type == 'upload': # if time-up at upload question then the form is submitted without # validation - if 'assignment' in request.FILES: - assignment_filename = request.FILES.getlist('assignment') - qp = QuestionPaper.objects.get(id=questionpaper_id) + assignment_filename = request.FILES.getlist('assignment') + if not assignment_filename: + msg = "Please upload assignment file" + return show_question(request, current_question, paper, notification=msg) + for fname in assignment_filename: assignment_files = AssignmentUpload.objects.filter( assignmentQuestion=current_question, @@ -514,7 +516,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): assign_file.delete() AssignmentUpload.objects.create(user=user, assignmentQuestion=current_question, assignmentFile=fname, - question_paper=qp + question_paper_id=questionpaper_id ) user_answer = 'ASSIGNMENT UPLOADED' if not current_question.grade_assignment_upload: @@ -1114,11 +1116,11 @@ 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) - quiz_assignment_file_status = AssignmentUpload.objects.filter( + has_quiz_assignments = AssignmentUpload.objects.filter( question_paper_id=questionpaper_id ).exists() context = {"users": user_details, "quiz_id": quiz_id, - "quiz_file_status": quiz_assignment_file_status + "has_quiz_assignments": has_quiz_assignments } if user_id is not None: @@ -1129,7 +1131,7 @@ 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( + has_user_assignments = AssignmentUpload.objects.filter( question_paper_id=questionpaper_id, user_id=user_id ).exists() @@ -1139,17 +1141,17 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None): ) context = {'data': data, "quiz_id": quiz_id, "users": user_details, "attempts": attempts, "user_id": user_id, - "user_file_status": user_assignment_file_status, - "quiz_file_status": quiz_assignment_file_status + "has_user_assignments": has_user_assignments, + "has_quiz_assignments": has_quiz_assignments } if request.method == "POST": papers = data['papers'] for paper in papers: - for question, answers, errors in six.iteritems(paper.get_question_answers()): + for question, answers in six.iteritems(paper.get_question_answers()): marks = float(request.POST.get('q%d_marks' % question.id, 0)) - answers = answers[-1] - answers.set_marks(marks) - answers.save() + answer = answers[-1]['answer'] + answer.set_marks(marks) + answer.save() paper.update_marks() paper.comments = request.POST.get( 'comments_%d' % paper.question_paper.id, 'No comments') @@ -1330,7 +1332,10 @@ def view_answerpaper(request, questionpaper_id): quiz = get_object_or_404(QuestionPaper, pk=questionpaper_id).quiz if quiz.view_answerpaper and user in quiz.course.students.all(): data = AnswerPaper.objects.get_user_data(user, questionpaper_id) - context = {'data': data, 'quiz': quiz} + has_user_assignment = AssignmentUpload.objects.filter(user=user, + question_paper_id=questionpaper_id).exists() + context = {'data': data, 'quiz': quiz, + "has_user_assignment":has_user_assignment} return my_render_to_response('yaksh/view_answerpaper.html', context) else: return my_redirect('/exam/quizzes/') @@ -1432,18 +1437,17 @@ def download_course_csv(request, course_id): @login_required def download_assignment_file(request, quiz_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!') qp = QuestionPaper.objects.get(quiz_id=quiz_id) - assignment = AssignmentUpload() - assignment_files, file_name = assignment.get_assignments(qp, question_id, + assignment_files, file_name = AssignmentUpload.objects.get_assignments(qp, + 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( + folder = f_name.user.get_full_name().replace(" ", "_") + sub_folder = f_name.assignmentQuestion.summary.replace(" ", "_") + folder_name = os.sep.join((folder, sub_folder, os.path.basename( f_name.assignmentFile.name)) ) zip_file.write(f_name.assignmentFile.path, folder_name @@ -1452,7 +1456,7 @@ def download_assignment_file(request, quiz_id, question_id=None, user_id=None): zipfile_name.seek(0) response = HttpResponse(content_type='application/zip') response['Content-Disposition'] = '''attachment;\ - filename={0}'''.format( + filename={0}.zip'''.format( file_name.replace(" ", "_") ) response.write(zipfile_name.read()) -- cgit