diff options
author | hardythe1 | 2012-04-14 06:28:44 +0530 |
---|---|---|
committer | hardythe1 | 2012-04-14 06:28:44 +0530 |
commit | 5d7ab20a710e2374a3345b07ed62154456dbd2c8 (patch) | |
tree | c553204df559a8c71e7b462a18b48f548d7fe193 /testapp/exam | |
parent | c0fcc1181e209192f6d11f2ca521e55e33a4ead7 (diff) | |
download | online_test-5d7ab20a710e2374a3345b07ed62154456dbd2c8.tar.gz online_test-5d7ab20a710e2374a3345b07ed62154456dbd2c8.tar.bz2 online_test-5d7ab20a710e2374a3345b07ed62154456dbd2c8.zip |
model changes as per requirnment
Diffstat (limited to 'testapp/exam')
-rw-r--r-- | testapp/exam/models.py | 11 | ||||
-rw-r--r-- | testapp/exam/urls.py | 1 | ||||
-rw-r--r-- | testapp/exam/views.py | 197 |
3 files changed, 183 insertions, 26 deletions
diff --git a/testapp/exam/models.py b/testapp/exam/models.py index ad3dad8..dd0c033 100644 --- a/testapp/exam/models.py +++ b/testapp/exam/models.py @@ -2,7 +2,7 @@ import datetime from django.db import models from django.contrib.auth.models import User from taggit_autocomplete_modified.managers import TaggableManagerAutocomplete as TaggableManager - +from django.http import HttpResponse ################################################################################ class Profile(models.Model): """Profile for a user to store roll number and other details.""" @@ -120,7 +120,10 @@ class AnswerPaper(models.Model): # The user's profile, we store a reference to make it easier to access the # data. profile = models.ForeignKey(Profile) - + + # All questions that remains to attempt to perticular Student + questions = models.CharField(max_length=128) + # The Quiz to which this question paper is attached to. question_paper = models.ForeignKey(QuestionPaper) @@ -157,7 +160,7 @@ class AnswerPaper(models.Model): def completed_question(self, question_id): """Removes the question from the list of questions and returns - the next.""" +the next.""" qa = self.questions_answered if len(qa) > 0: self.questions_answered = '|'.join([qa, str(question_id)]) @@ -193,7 +196,7 @@ class AnswerPaper(models.Model): except AttributeError: # total_seconds is new in Python 2.7. :( secs = dt.seconds + dt.days*24*3600 - total = self.quiz.duration*60.0 + total = self.question_paper.quiz.duration*60.0 remain = max(total - secs, 0) return int(remain) diff --git a/testapp/exam/urls.py b/testapp/exam/urls.py index 4651204..e7284f2 100644 --- a/testapp/exam/urls.py +++ b/testapp/exam/urls.py @@ -33,6 +33,7 @@ urlpatterns = patterns('exam.views', url(r'^manage/designquestionpaper/automatic/(?P<questionpaper_id>\d+)/$','automatic_questionpaper'), url(r'^manage/designquestionpaper/automatic$','automatic_questionpaper'), url(r'^manage/designquestionpaper/manual$','manual_questionpaper'), + url(r'^manage/designquestionpaper/manual/(?P<questionpaper_id>\d+)/$','manual_questionpaper'), ) diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 464bc3c..b742785 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -14,7 +14,7 @@ from django.db.models import Sum from taggit.models import Tag from itertools import chain # Local imports. -from exam.models import Quiz, Question, QuestionPaper, Profile, Answer, User +from exam.models import Quiz, Question, QuestionPaper, Profile, Answer, AnswerPaper, User from exam.forms import UserRegisterForm, UserLoginForm, QuizForm , QuestionForm from exam.xmlrpc_clients import code_server from settings import URL_ROOT @@ -308,7 +308,19 @@ def show_all_questionpapers(request,questionpaper_id=None): if request.method=="POST" and request.POST.get('add') == "add": return my_redirect("/exam/manage/designquestionpaper/" + questionpaper_id) - + + if request.method=="POST" and request.POST.get('delete') == "delete": + data = request.POST.getlist('papers') + for i in data: + q_paper = QuestionPaper.objects.get(id=i).delete() + question_paper= QuestionPaper.objects.all() + context = {'papers': question_paper, } + return my_render_to_response('exam/showquestionpapers.html', context, + context_instance=RequestContext(request)) + qu_papers = QuestionPaper.objects.all() + context = {'papers':qu_papers} + return my_render_to_response('exam/showquestionpapers.html',context,context_instance=RequestContext(request)) + if questionpaper_id == None: qu_papers = QuestionPaper.objects.all() context = {'papers':qu_papers} @@ -340,7 +352,6 @@ def automatic_questionpaper(request,questionpaper_id=None): quest_paper.quiz = quiz quest_paper.save() for i in set2: - print str(i.id) + " " + i.summary q = Question.objects.get(summary=i) quest_paper.questions.add(q) return my_redirect('/exam/manage/showquiz') @@ -385,13 +396,152 @@ def automatic_questionpaper(request,questionpaper_id=None): return my_render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) else: - return HttpResponse("eni mane pochi gaya ayan... ") + if request.method=="POST": + if request.POST.get('save') == 'save' : + quest_paper = QuestionPaper.objects.get(id=questionpaper_id) + for i in set2: + print str(i.id) + " " + i.summary + q = Question.objects.get(summary=i) + quest_paper.questions.add(q) + return my_redirect('/exam/manage/showquiz') + else: + set1 = set() + set2 = set() + no_questions = int(request.POST.get('questions')) + first_tag = request.POST.get('first_tag') + first_condition = request.POST.get('first_condition') + second_tag = request.POST.get('second_tag') + second_condition = request.POST.get('second_condition') + third_tag = request.POST.get('third_tag') + question1 = set(Question.objects.filter(tags__name__in=[first_tag])) + question2 = set(Question.objects.filter(tags__name__in=[second_tag])) + question3 = set(Question.objects.filter(tags__name__in=[third_tag])) + if first_condition == 'and': + set1 = question1.intersection(question2) + if second_condition == 'and': + set2 = set1.intersection(question3) + else: + set2 = set1.union(question3) + else: + set1 = question1.union(question2) + if second_condition == 'and': + set2 = set1.intersection(question3) + else: + set2 = set1.union(question3) + n = len(set2) + msg = '' + if (no_questions < n ) : + i = n - no_questions + for i in range(0,i): + set2.pop() + elif( no_questions > n): + msg = 'The given Criteria does not satisfy the number of Questions...' + tags = Tag.objects.all() + context = {'data':{'questions':set2,'tags':tags,'msg':msg}} + return my_render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) + else: + tags = Tag.objects.all() + context = {'data':{'tags':tags}} + return my_render_to_response('exam/automatic_questionpaper.html',context,context_instance=RequestContext(request)) -def manual_questionpaper(request): +def manual_questionpaper(request,questionpaper_id=None): user=request.user + global set1 + global set2 if not user.is_authenticated() or user.groups.filter(name='moderator').count() == 0 : raise Http404('You are not allowed to view this page!') - return my_render_to_response('exam/manual_questionpaper.html',{},context_instance=RequestContext(request)) + + if questionpaper_id == None: + if request.method=="POST": + if request.POST.get('save') == 'save' : + questions = request.POST.getlist('questions') + quiz = Quiz.objects.order_by("-id")[0] + quest_paper = QuestionPaper() + quest_paper.quiz = quiz + quest_paper.save() + for i in questions: + q = Question.objects.get(id=i) + quest_paper.questions.add(q) + return my_redirect('/exam/manage/showquiz') + else: + set1 = set() + set2 = set() + first_tag = request.POST.get('first_tag') + first_condition = request.POST.get('first_condition') + second_tag = request.POST.get('second_tag') + second_condition = request.POST.get('second_condition') + third_tag = request.POST.get('third_tag') + question1 = set(Question.objects.filter(tags__name__in=[first_tag])) + question2 = set(Question.objects.filter(tags__name__in=[second_tag])) + question3 = set(Question.objects.filter(tags__name__in=[third_tag])) + if first_condition == 'and': + set1 = question1.intersection(question2) + if second_condition == 'and': + set2 = set1.intersection(question3) + else: + set2 = set1.union(question3) + else: + set1 = question1.union(question2) + if second_condition == 'and': + set2 = set1.intersection(question3) + else: + set2 = set1.union(question3) + n = len(set2) + msg = '' + if (n == 0) : + msg = 'No matching Question found...' + tags = Tag.objects.all() + context = {'data':{'questions':set2,'tags':tags,'msg':msg}} + return my_render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) + else: + tags = Tag.objects.all() + context = {'data':{'tags':tags}} + return my_render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) + + else: + if request.method=="POST": + if request.POST.get('save') == 'save' : + quest_paper = QuestionPaper.objects.get(id=questionpaper_id) + questions = request.POST.getlist('questions') + for i in questions: + q = Question.objects.get(id=i) + quest_paper.questions.add(q) + return my_redirect('/exam/manage/showquiz') + else: + set1 = set() + set2 = set() + first_tag = request.POST.get('first_tag') + first_condition = request.POST.get('first_condition') + second_tag = request.POST.get('second_tag') + second_condition = request.POST.get('second_condition') + third_tag = request.POST.get('third_tag') + question1 = set(Question.objects.filter(tags__name__in=[first_tag])) + question2 = set(Question.objects.filter(tags__name__in=[second_tag])) + question3 = set(Question.objects.filter(tags__name__in=[third_tag])) + if first_condition == 'and': + set1 = question1.intersection(question2) + if second_condition == 'and': + set2 = set1.intersection(question3) + else: + set2 = set1.union(question3) + else: + set1 = question1.union(question2) + if second_condition == 'and': + set2 = set1.intersection(question3) + else: + set2 = set1.union(question3) + n = len(set2) + msg = '' + if (n == 0) : + msg = 'No matching Question found...' + tags = Tag.objects.all() + context = {'data':{'questions':set2,'tags':tags,'msg':msg}} + return my_render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) + else: + tags = Tag.objects.all() + context = {'data':{'tags':tags}} + return my_render_to_response('exam/manual_questionpaper.html',context,context_instance=RequestContext(request)) + @@ -438,17 +588,17 @@ def start(request): try: # Right now the app is designed so there is only one active quiz # at a particular time. - quiz = Quiz.objects.get(active=True) - except Quiz.DoesNotExist: + questionpaper = QuestionPaper.objects.all()[0] + except QuestionPaper.DoesNotExist: msg = 'Quiz not found, please contact your '\ 'instructor/administrator. Please login again thereafter.' return complete(request, reason=msg) try: - old_paper = QuestionPaper.objects.get(user=user, quiz=quiz) + old_paper = AnswerPaper.objects.get(user=user, question_paper=questionpaper) q = old_paper.current_question() return show_question(request, q) - except QuestionPaper.DoesNotExist: + except AnswerPaper.DoesNotExist: ip = request.META['REMOTE_ADDR'] key = gen_key(10) try: @@ -457,16 +607,18 @@ def start(request): msg = 'You do not have a profile and cannot take the quiz!' raise Http404(msg) - new_paper = QuestionPaper(user=user, user_ip=ip, key=key, - quiz=quiz, profile=profile) + new_paper = AnswerPaper(user=user, user_ip=ip, + question_paper=questionpaper, profile=profile) new_paper.start_time = datetime.datetime.now() # Make user directory. user_dir = get_user_dir(user) - questions = [ str(_.id) for _ in Question.objects.filter(active=True) ] + questions = [ str(_.id) for _ in questionpaper.questions.all() ] random.shuffle(questions) - + + #questions = questionpaper.questions + #random.shuffle(questions) new_paper.questions = "|".join(questions) new_paper.save() @@ -484,16 +636,17 @@ def question(request, q_id): return my_redirect('/exam/login/') q = get_object_or_404(Question, pk=q_id) try: - paper = QuestionPaper.objects.get(user=request.user, quiz__active=True) - except QuestionPaper.DoesNotExist: + q_paper = QuestionPaper.objects.get(quiz__active=True) + paper = AnswerPaper.objects.get(user=request.user, question_paper=q_paper) + except AnswerPaper.DoesNotExist: return my_redirect('/exam/start') - if not paper.quiz.active: + if not paper.question_paper.quiz.active: return complete(request, reason='The quiz has been deactivated!') time_left = paper.time_left() if time_left == 0: return complete(request, reason='Your time is up!') - quiz_name = paper.quiz.description + quiz_name = paper.question_paper.quiz.description context = {'question': q, 'paper': paper, 'user': user, 'quiz_name': quiz_name, 'time_left': time_left} @@ -503,7 +656,6 @@ def question(request, q_id): def show_question(request, q_id): """Show a question if possible.""" - if len(q_id) == 0: msg = 'Congratulations! You have successfully completed the quiz.' return complete(request, msg) @@ -517,7 +669,8 @@ def check(request, q_id): if not user.is_authenticated(): return my_redirect('/exam/login/') question = get_object_or_404(Question, pk=q_id) - paper = QuestionPaper.objects.get(user=user, quiz__active=True) + q_paper = QuestionPaper.objects.get(quiz__active=True) + paper = AnswerPaper.objects.get(user=request.user,question_paper = q_paper) answer = request.POST.get('answer') skip = request.POST.get('skip', None) @@ -557,12 +710,12 @@ def check(request, q_id): time_left = paper.time_left() if time_left == 0: return complete(request, reason='Your time is up!') - if not paper.quiz.active: + if not paper.question_paper.quiz.active: return complete(request, reason='The quiz has been deactivated!') context = {'question': question, 'error_message': err_msg, 'paper': paper, 'last_attempt': answer, - 'quiz_name': paper.quiz.description, + 'quiz_name': paper.question_paper.quiz.description, 'time_left': time_left} ci = RequestContext(request) |