<s> import random <EOL> from django . contrib . auth . decorators import login_required , permission_required <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . shortcuts import get_object_or_404 , render <EOL> from django . utils . decorators import method_decorator <EOL> from django . views . generic import DetailView , ListView , TemplateView , FormView <EOL> from . forms import QuestionForm , EssayForm <EOL> from . models import Quiz , Category , Progress , Sitting , Question <EOL> from essay . models import Essay_Question <EOL> class QuizMarkerMixin ( object ) : <EOL> @ method_decorator ( login_required ) <EOL> @ method_decorator ( permission_required ( '<STR_LIT>' ) ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( QuizMarkerMixin , self ) . dispatch ( * args , ** kwargs ) <EOL> class SittingFilterTitleMixin ( object ) : <EOL> def get_queryset ( self ) : <EOL> queryset = super ( SittingFilterTitleMixin , self ) . get_queryset ( ) <EOL> quiz_filter = self . request . GET . get ( '<STR_LIT>' ) <EOL> if quiz_filter : <EOL> queryset = queryset . filter ( quiz__title__icontains = quiz_filter ) <EOL> return queryset <EOL> class QuizListView ( ListView ) : <EOL> model = Quiz <EOL> def get_queryset ( self ) : <EOL> queryset = super ( QuizListView , self ) . get_queryset ( ) <EOL> return queryset . filter ( draft = False ) <EOL> class QuizDetailView ( DetailView ) : <EOL> model = Quiz <EOL> slug_field = '<STR_LIT:url>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . object = self . get_object ( ) <EOL> if self . object . draft and not request . user . has_perm ( '<STR_LIT>' ) : <EOL> raise PermissionDenied <EOL> context = self . get_context_data ( object = self . object ) <EOL> return self . render_to_response ( context ) <EOL> class CategoriesListView ( ListView ) : <EOL> model = Category <EOL> class ViewQuizListByCategory ( ListView ) : <EOL> model = Quiz <EOL> template_name = '<STR_LIT>' <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . category = get_object_or_404 ( <EOL> Category , <EOL> category = self . kwargs [ '<STR_LIT>' ] <EOL> ) <EOL> return super ( ViewQuizListByCategory , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ViewQuizListByCategory , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = self . category <EOL> return context <EOL> def get_queryset ( self ) : <EOL> queryset = super ( ViewQuizListByCategory , self ) . get_queryset ( ) <EOL> return queryset . filter ( category = self . category , draft = False ) <EOL> class QuizUserProgressView ( TemplateView ) : <EOL> template_name = '<STR_LIT>' <EOL> @ method_decorator ( login_required ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( QuizUserProgressView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( QuizUserProgressView , self ) . get_context_data ( ** kwargs ) <EOL> progress , c = Progress . objects . get_or_create ( user = self . request . user ) <EOL> context [ '<STR_LIT>' ] = progress . list_all_cat_scores <EOL> context [ '<STR_LIT>' ] = progress . show_exams ( ) <EOL> return context <EOL> class QuizMarkingList ( QuizMarkerMixin , SittingFilterTitleMixin , ListView ) : <EOL> model = Sitting <EOL> def get_queryset ( self ) : <EOL> queryset = super ( QuizMarkingList , self ) . get_queryset ( ) . filter ( complete = True ) <EOL> user_filter = self . request . GET . get ( '<STR_LIT>' ) <EOL> if user_filter : <EOL> queryset = queryset . filter ( user__username__icontains = user_filter ) <EOL> return queryset <EOL> class QuizMarkingDetail ( QuizMarkerMixin , DetailView ) : <EOL> model = Sitting <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> sitting = self . get_object ( ) <EOL> q_to_toggle = request . POST . get ( '<STR_LIT>' , None ) <EOL> if q_to_toggle : <EOL> q = Question . objects . get_subclass ( id = int ( q_to_toggle ) ) <EOL> if int ( q_to_toggle ) in sitting . get_incorrect_questions : <EOL> sitting . remove_incorrect_question ( q ) <EOL> else : <EOL> sitting . add_incorrect_question ( q ) <EOL> return self . get ( request ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( QuizMarkingDetail , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = context [ '<STR_LIT>' ] . get_questions ( with_answers = True ) <EOL> return context <EOL> class QuizTake ( FormView ) : <EOL> form_class = QuestionForm <EOL> template_name = '<STR_LIT>' <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . quiz = get_object_or_404 ( Quiz , url = self . kwargs [ '<STR_LIT>' ] ) <EOL> if self . quiz . draft and not request . user . has_perm ( '<STR_LIT>' ) : <EOL> raise PermissionDenied <EOL> self . logged_in_user = self . request . user . is_authenticated ( ) <EOL> if self . logged_in_user : <EOL> self . sitting = Sitting . objects . user_sitting ( request . user , <EOL> self . quiz ) <EOL> else : <EOL> self . sitting = self . anon_load_sitting ( ) <EOL> if self . sitting is False : <EOL> return render ( request , '<STR_LIT>' ) <EOL> return super ( QuizTake , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def get_form ( self , form_class ) : <EOL> if self . logged_in_user : <EOL> self . question = self . sitting . get_first_question ( ) <EOL> self . progress = self . sitting . progress ( ) <EOL> else : <EOL> self . question = self . anon_next_question ( ) <EOL> self . progress = self . anon_sitting_progress ( ) <EOL> if self . question . __class__ is Essay_Question : <EOL> form_class = EssayForm <EOL> return form_class ( ** self . get_form_kwargs ( ) ) <EOL> def get_form_kwargs ( self ) : <EOL> kwargs = super ( QuizTake , self ) . get_form_kwargs ( ) <EOL> return dict ( kwargs , question = self . question ) <EOL> def form_valid ( self , form ) : <EOL> if self . logged_in_user : <EOL> self . form_valid_user ( form ) <EOL> if self . sitting . get_first_question ( ) is False : <EOL> return self . final_result_user ( ) <EOL> else : <EOL> self . form_valid_anon ( form ) <EOL> if not self . request . session [ self . quiz . anon_q_list ( ) ] : <EOL> return self . final_result_anon ( ) <EOL> self . request . POST = { } <EOL> return super ( QuizTake , self ) . get ( self , self . request ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( QuizTake , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = self . question <EOL> context [ '<STR_LIT>' ] = self . quiz <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> context [ '<STR_LIT>' ] = self . previous <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> context [ '<STR_LIT>' ] = self . progress <EOL> return context <EOL> def form_valid_user ( self , form ) : <EOL> progress , c = Progress . objects . get_or_create ( user = self . request . user ) <EOL> guess = form . cleaned_data [ '<STR_LIT>' ] <EOL> is_correct = self . question . check_if_correct ( guess ) <EOL> if is_correct is True : <EOL> self . sitting . add_to_score ( <NUM_LIT:1> ) <EOL> progress . update_score ( self . question , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> else : <EOL> self . sitting . add_incorrect_question ( self . question ) <EOL> progress . update_score ( self . question , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> if self . quiz . answers_at_end is not True : <EOL> self . previous = { '<STR_LIT>' : guess , <EOL> '<STR_LIT>' : is_correct , <EOL> '<STR_LIT>' : self . question , <EOL> '<STR_LIT>' : self . question . get_answers ( ) , <EOL> '<STR_LIT>' : { self . question <EOL> . __class__ . __name__ : True } } <EOL> else : <EOL> self . previous = { } <EOL> self . sitting . add_user_answer ( self . question , guess ) <EOL> self . sitting . remove_first_question ( ) <EOL> def final_result_user ( self ) : <EOL> results = { <EOL> '<STR_LIT>' : self . quiz , <EOL> '<STR_LIT>' : self . sitting . get_current_score , <EOL> '<STR_LIT>' : self . sitting . get_max_score , <EOL> '<STR_LIT>' : self . sitting . get_percent_correct , <EOL> '<STR_LIT>' : self . sitting , <EOL> '<STR_LIT>' : self . previous , <EOL> } <EOL> self . sitting . mark_quiz_complete ( ) <EOL> if self . quiz . answers_at_end : <EOL> results [ '<STR_LIT>' ] = self . sitting . get_questions ( with_answers = True ) <EOL> results [ '<STR_LIT>' ] = self . sitting . get_incorrect_questions <EOL> if self . quiz . exam_paper is False : <EOL> self . sitting . delete ( ) <EOL> return render ( self . request , '<STR_LIT>' , results ) <EOL> def anon_load_sitting ( self ) : <EOL> if self . quiz . single_attempt is True : <EOL> return False <EOL> if self . quiz . anon_q_list ( ) in self . request . session : <EOL> return self . request . session [ self . quiz . anon_q_list ( ) ] <EOL> else : <EOL> return self . new_anon_quiz_session ( ) <EOL> def new_anon_quiz_session ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . session . set_expiry ( <NUM_LIT> ) <EOL> questions = self . quiz . get_questions ( ) <EOL> question_list = [ question . id for question in questions ] <EOL> if self . quiz . random_order is True : <EOL> random . shuffle ( question_list ) <EOL> if self . quiz . max_questions and ( self . quiz . max_questions <EOL> < len ( question_list ) ) : <EOL> question_list = question_list [ : self . quiz . max_questions ] <EOL> self . request . session [ self . quiz . anon_score_id ( ) ] = <NUM_LIT:0> <EOL> self . request . session [ self . quiz . anon_q_list ( ) ] = question_list <EOL> self . request . session [ self . quiz . anon_q_data ( ) ] = dict ( <EOL> incorrect_questions = [ ] , <EOL> order = question_list , <EOL> ) <EOL> return self . request . session [ self . quiz . anon_q_list ( ) ] <EOL> def anon_next_question ( self ) : <EOL> next_question_id = self . request . session [ self . quiz . anon_q_list ( ) ] [ <NUM_LIT:0> ] <EOL> return Question . objects . get_subclass ( id = next_question_id ) <EOL> def anon_sitting_progress ( self ) : <EOL> total = len ( self . request . session [ self . quiz . anon_q_data ( ) ] [ '<STR_LIT>' ] ) <EOL> answered = total - len ( self . request . session [ self . quiz . anon_q_list ( ) ] ) <EOL> return ( answered , total ) <EOL> def form_valid_anon ( self , form ) : <EOL> guess = form . cleaned_data [ '<STR_LIT>' ] <EOL> is_correct = self . question . check_if_correct ( guess ) <EOL> if is_correct : <EOL> self . request . session [ self . quiz . anon_score_id ( ) ] += <NUM_LIT:1> <EOL> anon_session_score ( self . request . session , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> else : <EOL> anon_session_score ( self . request . session , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> self . request . session [ self . quiz . anon_q_data ( ) ] [ '<STR_LIT>' ] . append ( self . question . id ) <EOL> self . previous = { } <EOL> if self . quiz . answers_at_end is not True : <EOL> self . previous = { '<STR_LIT>' : guess , <EOL> '<STR_LIT>' : is_correct , <EOL> '<STR_LIT>' : self . question , <EOL> '<STR_LIT>' : self . question . get_answers ( ) , <EOL> '<STR_LIT>' : { self . question <EOL> . __class__ . __name__ : True } } <EOL> self . request . session [ self . quiz . anon_q_list ( ) ] = self . request . session [ self . quiz . anon_q_list ( ) ] [ <NUM_LIT:1> : ] <EOL> def final_result_anon ( self ) : <EOL> score = self . request . session [ self . quiz . anon_score_id ( ) ] <EOL> q_order = self . request . session [ self . quiz . anon_q_data ( ) ] [ '<STR_LIT>' ] <EOL> max_score = len ( q_order ) <EOL> percent = int ( round ( ( float ( score ) / max_score ) * <NUM_LIT:100> ) ) <EOL> session , session_possible = anon_session_score ( self . request . session ) <EOL> if score is <NUM_LIT:0> : <EOL> score = "<STR_LIT:0>" <EOL> results = { <EOL> '<STR_LIT>' : score , <EOL> '<STR_LIT>' : max_score , <EOL> '<STR_LIT>' : percent , <EOL> '<STR_LIT>' : session , <EOL> '<STR_LIT>' : session_possible <EOL> } <EOL> del self . request . session [ self . quiz . anon_q_list ( ) ] <EOL> if self . quiz . answers_at_end : <EOL> results [ '<STR_LIT>' ] = sorted ( <EOL> self . quiz . question_set . filter ( id__in = q_order ) <EOL> . select_subclasses ( ) , <EOL> key = lambda q : q_order . index ( q . id ) ) <EOL> results [ '<STR_LIT>' ] = ( <EOL> self . request <EOL> . session [ self . quiz . anon_q_data ( ) ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> results [ '<STR_LIT>' ] = self . previous <EOL> del self . request . session [ self . quiz . anon_q_data ( ) ] <EOL> return render ( self . request , '<STR_LIT>' , results ) <EOL> def anon_session_score ( session , to_add = <NUM_LIT:0> , possible = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" not in session : <EOL> session [ "<STR_LIT>" ] , session [ "<STR_LIT>" ] = <NUM_LIT:0> , <NUM_LIT:0> <EOL> if possible > <NUM_LIT:0> : <EOL> session [ "<STR_LIT>" ] += to_add <EOL> session [ "<STR_LIT>" ] += possible <EOL> return session [ "<STR_LIT>" ] , session [ "<STR_LIT>" ] </s>
