summaryrefslogtreecommitdiff
path: root/yaksh
diff options
context:
space:
mode:
authoradityacp2016-06-20 14:50:58 +0530
committeradityacp2016-07-28 15:56:06 +0530
commitd27b168e6fa0789891acf8952a60a7c200caef44 (patch)
treeb99332a7937a14789e5a3e4ba556e840898bf51f /yaksh
parent542f862445b65f56704009e324ed39a7eec23dad (diff)
downloadonline_test-d27b168e6fa0789891acf8952a60a7c200caef44.tar.gz
online_test-d27b168e6fa0789891acf8952a60a7c200caef44.tar.bz2
online_test-d27b168e6fa0789891acf8952a60a7c200caef44.zip
added files upload for questions
Diffstat (limited to 'yaksh')
-rw-r--r--yaksh/forms.py7
-rw-r--r--yaksh/models.py42
-rw-r--r--yaksh/views.py73
3 files changed, 98 insertions, 24 deletions
diff --git a/yaksh/forms.py b/yaksh/forms.py
index a443e34..1226fe2 100644
--- a/yaksh/forms.py
+++ b/yaksh/forms.py
@@ -175,6 +175,11 @@ class QuestionForm(forms.ModelForm):
exclude = ['user']
+class FileForm(forms.Form):
+ file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}),
+ required=False)
+
+
class RandomQuestionForm(forms.Form):
question_type = forms.CharField(max_length=8, widget=forms.Select\
(choices=question_types))
@@ -211,7 +216,7 @@ class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['first_name', 'last_name', 'institute',
- 'department', 'roll_number', 'position', 'timezone']
+ 'department', 'roll_number', 'position', 'timezone']
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
diff --git a/yaksh/models.py b/yaksh/models.py
index 4ee6141..20d8716 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -11,6 +11,8 @@ from django.contrib.contenttypes.models import ContentType
from taggit.managers import TaggableManager
from django.utils import timezone
import pytz
+import os
+import shutil
languages = (
("python", "Python"),
@@ -62,6 +64,9 @@ def has_profile(user):
""" check if user has profile """
return True if hasattr(user, 'profile') else False
+def get_upload_dir(instance, filename):
+ return "question_%s/%s" % (instance.question.id, filename)
+
###############################################################################
class CourseManager(models.Manager):
@@ -216,6 +221,10 @@ class Question(models.Model):
question_data['test_case_data'] = test_case_data
question_data['user_answer'] = user_answer
+ files = QuestionsFileUpload.objects.filter(question=self)
+ if files:
+ question_data['file_paths'] = [(file.files.path, file.extract)
+ for file in files]
return json.dumps(question_data)
@@ -252,7 +261,7 @@ class Question(models.Model):
model=self.test_case_type
)
test_cases = test_case_ctype.get_all_objects_for_this_type(
- question=self,
+ question=self,
**kwargs
)
@@ -263,7 +272,7 @@ class Question(models.Model):
model=self.test_case_type
)
test_case = test_case_ctype.get_object_for_this_type(
- question=self,
+ question=self,
**kwargs
)
@@ -274,6 +283,35 @@ class Question(models.Model):
###############################################################################
+class QuestionsFileUpload(models.Model):
+ files = models.FileField(upload_to=get_upload_dir, blank=True)
+ question = models.ForeignKey(Question, related_name="question")
+ extract = models.BooleanField(default=False)
+
+ def delete_all_files(self, files):
+ for file in files:
+ if os.path.exists(file.files.path):
+ shutil.rmtree(os.path.dirname(file.files.path))
+ file.delete()
+
+ def delete_selected_files(self, files):
+ for file in files:
+ if os.path.exists(file.files.path):
+ os.remove(file.files.path)
+ if os.listdir(os.path.dirname(file.files.path)) == []:
+ os.rmdir(os.path.dirname(file.files.path))
+ file.delete()
+
+ def extract_files(self, files):
+ for file in files:
+ if file.extract:
+ file.extract = False
+ else:
+ file.extract = True
+ file.save()
+
+
+###############################################################################
class Answer(models.Model):
"""Answers submitted by the users."""
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)