From d27b168e6fa0789891acf8952a60a7c200caef44 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 20 Jun 2016 14:50:58 +0530 Subject: added files upload for questions --- yaksh/views.py | 73 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 21 deletions(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index 56746b0..b5de26d 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -22,14 +22,14 @@ import pytz from taggit.models import Tag from itertools import chain import json +import shutil # Local imports. -from yaksh.models import get_model_class, Quiz, Question, QuestionPaper,\ - QuestionSet, Course, Profile, Answer, AnswerPaper, User, TestCase,\ - has_profile +from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course +from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ - QuestionForm, RandomQuestionForm,\ - QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ - get_object_form + QuestionForm, RandomQuestionForm,\ + QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ + get_object_form, FileForm from yaksh.xmlrpc_clients import code_server from settings import URL_ROOT from yaksh.models import AssignmentUpload @@ -151,19 +151,27 @@ def add_question(request): if request.method == "POST" and 'save_question' in request.POST: question_form = QuestionForm(request.POST) + form = FileForm(request.POST, request.FILES) if question_form.is_valid(): new_question = question_form.save(commit=False) new_question.user = user new_question.save() + files = request.FILES.getlist('file_field') + if files: + for file in files: + QuestionsFileUpload.objects.get_or_create(question=new_question, files=file) return my_redirect("/exam/manage/addquestion/{0}".format(new_question.id)) else: return my_render_to_response('yaksh/add_question.html', - {'form': question_form}, + {'form': question_form, + 'upload_form': form}, context_instance=ci) else: question_form = QuestionForm() + form = FileForm() return my_render_to_response('yaksh/add_question.html', - {'form': question_form}, + {'form': question_form, + 'upload_form': form}, context_instance=ci) @login_required @@ -176,9 +184,24 @@ def edit_question(request, question_id=None): raise Http404('No Question Found') question_instance = Question.objects.get(id=question_id) - + if request.method == "POST" and 'delete_files' in request.POST: + remove_files_id = request.POST.getlist('clear') + if remove_files_id: + files = QuestionsFileUpload.objects.filter(id__in=remove_files_id) + file = QuestionsFileUpload() + file.delete_selected_files(files) if request.method == "POST" and 'save_question' in request.POST: question_form = QuestionForm(request.POST, instance=question_instance) + form = FileForm(request.POST, request.FILES) + files = request.FILES.getlist('file_field') + extract_files_id = request.POST.getlist('extract') + if files: + for file in files: + QuestionsFileUpload.objects.get_or_create(question=question_instance, files=file) + if extract_files_id: + files = QuestionsFileUpload.objects.filter(id__in=extract_files_id) + file = QuestionsFileUpload() + file.extract_files(files) if question_form.is_valid(): new_question = question_form.save(commit=False) test_case_type = question_form.cleaned_data.get('test_case_type') @@ -196,23 +219,29 @@ def edit_question(request, question_id=None): test_case_model_class = get_model_class(test_case_type) TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1) test_case_formset = TestCaseInlineFormSet(request.POST, request.FILES, instance=question_instance) + uploaded_files = QuestionsFileUpload.objects.filter(question_id=question_instance.id) return my_render_to_response('yaksh/add_question.html', {'form': question_form, 'test_case_formset': test_case_formset, - 'question_id': question_id}, + 'question_id': question_id, + 'upload_form': form, + 'uploaded_files': uploaded_files}, context_instance=ci) else: question_form = QuestionForm(instance=question_instance) + form = FileForm() test_case_type = question_instance.test_case_type test_case_form_class = get_object_form(model=test_case_type, exclude_fields=['question']) test_case_model_class = get_model_class(test_case_type) TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1) test_case_formset = TestCaseInlineFormSet(instance=question_instance) - + uploaded_files = QuestionsFileUpload.objects.filter(question_id=question_instance.id) return my_render_to_response('yaksh/add_question.html', {'form': question_form, 'test_case_formset': test_case_formset, - 'question_id': question_id}, + 'question_id': question_id, + 'upload_form': form, + 'uploaded_files': uploaded_files}, context_instance=ci) @login_required @@ -405,18 +434,14 @@ def show_question(request, question, paper, error_message=None): reason='Your time is up!' return complete(request, reason, paper.attempt_number, paper.question_paper.id) test_cases = question.get_test_cases() - context = {'question': question, - 'paper': paper, - 'error_message': error_message, - 'test_cases': test_cases, - 'last_attempt': question.snippet.encode('unicode-escape') - } - + files = QuestionsFileUpload.objects.filter(question_id=question.id) + context = {'question': question, 'paper': paper, 'error_message': error_message, + 'test_cases': test_cases, 'files': files, + 'last_attempt': question.snippet.encode('unicode-escape')} answers = paper.get_previous_answers(question) if answers: last_attempt = answers[0].answer context['last_attempt'] = last_attempt.encode('unicode-escape') - # context['last_attempt'] = answers[0].answer.encode('unicode-escape') ci = RequestContext(request) return my_render_to_response('yaksh/question.html', context, context_instance=ci) @@ -832,7 +857,13 @@ def show_all_questions(request): if request.POST.get('delete') == 'delete': data = request.POST.getlist('question') if data is not None: - question = Question.objects.filter(id__in=data, user_id=user.id).delete() + questions = Question.objects.filter(id__in=data, user_id=user.id) + for question in questions: + files = QuestionsFileUpload.objects.filter(question_id=question.id) + if files: + file = QuestionsFileUpload() + file.delete_all_files(files) + questions.delete() if request.POST.get('upload') == 'upload': form = UploadFileForm(request.POST, request.FILES) -- cgit From 80453af850b5080a43eb309b50151ecc8e5f6578 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 16:42:44 +0530 Subject: changed extract and delete function names in views.py --- yaksh/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index b5de26d..33be86d 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -189,7 +189,7 @@ def edit_question(request, question_id=None): if remove_files_id: files = QuestionsFileUpload.objects.filter(id__in=remove_files_id) file = QuestionsFileUpload() - file.delete_selected_files(files) + file.delete_files(files) if request.method == "POST" and 'save_question' in request.POST: question_form = QuestionForm(request.POST, instance=question_instance) form = FileForm(request.POST, request.FILES) @@ -201,7 +201,7 @@ def edit_question(request, question_id=None): if extract_files_id: files = QuestionsFileUpload.objects.filter(id__in=extract_files_id) file = QuestionsFileUpload() - file.extract_files(files) + file.set_extract_status(files) if question_form.is_valid(): new_question = question_form.save(commit=False) test_case_type = question_form.cleaned_data.get('test_case_type') @@ -862,7 +862,7 @@ def show_all_questions(request): files = QuestionsFileUpload.objects.filter(question_id=question.id) if files: file = QuestionsFileUpload() - file.delete_all_files(files) + file.delete_files(files) questions.delete() if request.POST.get('upload') == 'upload': -- cgit From 2a5eb7eaf366dc6c91b8955a288c7f8371f0e682 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 17:10:14 +0530 Subject: rebase changes --- yaksh/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index 33be86d..cba9d58 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -25,7 +25,8 @@ import json import shutil # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course -from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload +from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload, + has_profile from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ -- cgit From 0c8ce6ec96d4d44ac8c69eb964c7a3847c74b301 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 17:30:35 +0530 Subject: removed unused import and changed indentation --- yaksh/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index cba9d58..c659369 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -22,11 +22,11 @@ import pytz from taggit.models import Tag from itertools import chain import json -import shutil + # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course -from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload, - has_profile +from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload,\ + has_profile from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ -- cgit From 26157fd30edf834aa53c1bda58cdf277255d1fc4 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 28 Jul 2016 15:54:36 +0530 Subject: changes in views questions file upload --- yaksh/views.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index c659369..e1ec44e 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -25,7 +25,7 @@ import json # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course -from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, QuestionsFileUpload,\ +from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, FileUpload,\ has_profile from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ @@ -160,7 +160,7 @@ def add_question(request): files = request.FILES.getlist('file_field') if files: for file in files: - QuestionsFileUpload.objects.get_or_create(question=new_question, files=file) + FileUpload.objects.get_or_create(question=new_question, file=file) return my_redirect("/exam/manage/addquestion/{0}".format(new_question.id)) else: return my_render_to_response('yaksh/add_question.html', @@ -188,9 +188,9 @@ def edit_question(request, question_id=None): if request.method == "POST" and 'delete_files' in request.POST: remove_files_id = request.POST.getlist('clear') if remove_files_id: - files = QuestionsFileUpload.objects.filter(id__in=remove_files_id) - file = QuestionsFileUpload() - file.delete_files(files) + files = FileUpload.objects.filter(id__in=remove_files_id) + for file in files: + file.remove() if request.method == "POST" and 'save_question' in request.POST: question_form = QuestionForm(request.POST, instance=question_instance) form = FileForm(request.POST, request.FILES) @@ -198,11 +198,11 @@ def edit_question(request, question_id=None): extract_files_id = request.POST.getlist('extract') if files: for file in files: - QuestionsFileUpload.objects.get_or_create(question=question_instance, files=file) + FileUpload.objects.get_or_create(question=question_instance, file=file) if extract_files_id: - files = QuestionsFileUpload.objects.filter(id__in=extract_files_id) - file = QuestionsFileUpload() - file.set_extract_status(files) + files = FileUpload.objects.filter(id__in=extract_files_id) + for file in files: + file.set_extract_status() if question_form.is_valid(): new_question = question_form.save(commit=False) test_case_type = question_form.cleaned_data.get('test_case_type') @@ -220,7 +220,7 @@ def edit_question(request, question_id=None): test_case_model_class = get_model_class(test_case_type) TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1) test_case_formset = TestCaseInlineFormSet(request.POST, request.FILES, instance=question_instance) - uploaded_files = QuestionsFileUpload.objects.filter(question_id=question_instance.id) + uploaded_files = FileUpload.objects.filter(question_id=question_instance.id) return my_render_to_response('yaksh/add_question.html', {'form': question_form, 'test_case_formset': test_case_formset, @@ -236,7 +236,7 @@ def edit_question(request, question_id=None): test_case_model_class = get_model_class(test_case_type) TestCaseInlineFormSet = inlineformset_factory(Question, test_case_model_class, form=test_case_form_class, extra=1) test_case_formset = TestCaseInlineFormSet(instance=question_instance) - uploaded_files = QuestionsFileUpload.objects.filter(question_id=question_instance.id) + uploaded_files = FileUpload.objects.filter(question_id=question_instance.id) return my_render_to_response('yaksh/add_question.html', {'form': question_form, 'test_case_formset': test_case_formset, @@ -435,7 +435,7 @@ def show_question(request, question, paper, error_message=None): reason='Your time is up!' return complete(request, reason, paper.attempt_number, paper.question_paper.id) test_cases = question.get_test_cases() - files = QuestionsFileUpload.objects.filter(question_id=question.id) + files = FileUpload.objects.filter(question_id=question.id) context = {'question': question, 'paper': paper, 'error_message': error_message, 'test_cases': test_cases, 'files': files, 'last_attempt': question.snippet.encode('unicode-escape')} @@ -859,11 +859,10 @@ def show_all_questions(request): data = request.POST.getlist('question') if data is not None: questions = Question.objects.filter(id__in=data, user_id=user.id) - for question in questions: - files = QuestionsFileUpload.objects.filter(question_id=question.id) - if files: - file = QuestionsFileUpload() - file.delete_files(files) + files = FileUpload.objects.filter(question_id__in=questions) + if files: + for file in files: + file.remove() questions.delete() if request.POST.get('upload') == 'upload': -- cgit