<s> from django . shortcuts import render <EOL> from django . core import serializers <EOL> from django . http import HttpResponse <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . auth import authenticate , login , logout <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . conf import settings <EOL> from registrar . models import Course <EOL> from registrar . models import Student <EOL> from registrar . models import Assignment <EOL> from registrar . models import AssignmentSubmission <EOL> from registrar . models import EssayQuestion <EOL> from registrar . models import EssaySubmission <EOL> from registrar . models import MultipleChoiceQuestion <EOL> from registrar . models import MultipleChoiceSubmission <EOL> from registrar . models import ResponseQuestion <EOL> from registrar . models import ResponseSubmission <EOL> from registrar . models import TrueFalseQuestion <EOL> from registrar . models import TrueFalseSubmission <EOL> import json <EOL> import datetime <EOL> from student . forms import EssaySubmissionForm <EOL> from student . forms import AssignmentSubmissionForm <EOL> def get_submitted_assignments ( course , student ) : <EOL> try : <EOL> assignments = Assignment . objects . filter ( course = course ) . order_by ( '<STR_LIT>' ) <EOL> except Assignment . DoesNotExist : <EOL> assignment = None <EOL> try : <EOL> submitted_assignments = AssignmentSubmission . objects . filter ( <EOL> assignment__course = course , <EOL> student = student <EOL> ) <EOL> except AssignmentSubmission . DoesNotExist : <EOL> submitted_assignments = None <EOL> if len ( assignments ) != len ( submitted_assignments ) : <EOL> for assignment in assignments : <EOL> found_assignment = False <EOL> for submitted_assignment in submitted_assignments : <EOL> if assignment . assignment_id == submitted_assignment . assignment_id : <EOL> found_assignment = True <EOL> if not found_assignment : <EOL> submission = AssignmentSubmission . objects . create ( <EOL> student = student , <EOL> assignment = assignment , <EOL> ) <EOL> submission . save ( ) <EOL> submitted_assignments = AssignmentSubmission . objects . filter ( <EOL> assignment__course = course , <EOL> student = student <EOL> ) <EOL> return submitted_assignments <EOL> @ login_required ( login_url = '<STR_LIT>' ) <EOL> def assignments_page ( request , course_id ) : <EOL> course = Course . objects . get ( id = course_id ) <EOL> student = Student . objects . get ( user = request . user ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : course , <EOL> '<STR_LIT>' : get_submitted_assignments ( course , student ) , <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : settings . APPLICATION_HAS_ADVERTISMENT , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_CSS_LIBRARY_URLS , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_JS_LIBRARY_URLS , <EOL> } ) <EOL> @ login_required ( login_url = '<STR_LIT>' ) <EOL> def assignments_table ( request , course_id ) : <EOL> course = Course . objects . get ( id = course_id ) <EOL> student = Student . objects . get ( user = request . user ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : course , <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : get_submitted_assignments ( course , student ) , <EOL> '<STR_LIT>' : settings . ESSAY_QUESTION_TYPE , <EOL> '<STR_LIT>' : settings . MULTIPLECHOICE_QUESTION_TYPE , <EOL> '<STR_LIT>' : settings . TRUEFALSE_QUESTION_TYPE , <EOL> '<STR_LIT>' : settings . RESPONSE_QUESTION_TYPE , <EOL> } ) <EOL> @ login_required ( login_url = '<STR_LIT>' ) <EOL> def assignment_page ( request , course_id , assignment_id ) : <EOL> course = Course . objects . get ( id = course_id ) <EOL> student = Student . objects . get ( user = request . user ) <EOL> assignment = Assignment . objects . get ( assignment_id = assignment_id ) <EOL> try : <EOL> e_questions = EssayQuestion . objects . filter ( assignment = assignment ) . order_by ( '<STR_LIT>' ) <EOL> except EssayQuestion . DoesNotExist : <EOL> e_questions = None <EOL> try : <EOL> e_submissions = EssaySubmission . objects . filter ( question__assignment = assignment , student = student ) <EOL> except EssayQuestion . DoesNotExist : <EOL> e_submissions = None <EOL> try : <EOL> mc_questions = MultipleChoiceQuestion . objects . filter ( assignment = assignment ) . order_by ( '<STR_LIT>' ) <EOL> except MultipleChoiceQuestion . DoesNotExist : <EOL> mc_questions = None <EOL> try : <EOL> mc_submissions = MultipleChoiceSubmission . objects . filter ( question__assignment = assignment , student = student ) <EOL> except MultipleChoiceSubmission . DoesNotExist : <EOL> mc_submissions = None <EOL> try : <EOL> tf_questions = TrueFalseQuestion . objects . filter ( assignment = assignment ) . order_by ( '<STR_LIT>' ) <EOL> except TrueFalseQuestion . DoesNotExist : <EOL> tf_questions = None <EOL> try : <EOL> tf_submissions = TrueFalseSubmission . objects . filter ( question__assignment = assignment , student = student ) <EOL> except tf_submissions . DoesNotExist : <EOL> tf_submissions = None <EOL> try : <EOL> r_questions = ResponseQuestion . objects . filter ( assignment = assignment ) . order_by ( '<STR_LIT>' ) <EOL> except ResponseQuestion . DoesNotExist : <EOL> r_questions = None <EOL> try : <EOL> r_submissions = ResponseSubmission . objects . filter ( question__assignment = assignment , student = student ) <EOL> except ResponseQuestion . DoesNotExist : <EOL> r_submissions = None <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : student , <EOL> '<STR_LIT>' : course , <EOL> '<STR_LIT>' : assignment , <EOL> '<STR_LIT>' : e_questions , <EOL> '<STR_LIT>' : e_submissions , <EOL> '<STR_LIT>' : mc_questions , <EOL> '<STR_LIT>' : mc_submissions , <EOL> '<STR_LIT>' : tf_questions , <EOL> '<STR_LIT>' : tf_submissions , <EOL> '<STR_LIT>' : r_questions , <EOL> '<STR_LIT>' : r_submissions , <EOL> '<STR_LIT>' : settings . ESSAY_QUESTION_TYPE , <EOL> '<STR_LIT>' : settings . MULTIPLECHOICE_QUESTION_TYPE , <EOL> '<STR_LIT>' : settings . TRUEFALSE_QUESTION_TYPE , <EOL> '<STR_LIT>' : settings . RESPONSE_QUESTION_TYPE , <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : settings . APPLICATION_HAS_ADVERTISMENT , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_CSS_LIBRARY_URLS , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_JS_LIBRARY_URLS , <EOL> } ) <EOL> @ login_required ( ) <EOL> def delete_assignment ( request , course_id ) : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> assignment_id = int ( request . POST [ '<STR_LIT>' ] ) <EOL> course = Course . objects . get ( id = course_id ) <EOL> student = Student . objects . get ( user = request . user ) <EOL> assignment = Assignment . objects . get ( assignment_id = assignment_id ) <EOL> try : <EOL> submission = AssignmentSubmission . objects . get ( <EOL> student = student , <EOL> assignment = assignment , <EOL> ) <EOL> submission . is_finished = False <EOL> submission . save ( ) <EOL> except AssignmentSubmission . DoesNotExist : <EOL> return HttpResponse ( json . dumps ( { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } ) , content_type = "<STR_LIT:application/json>" ) <EOL> try : <EOL> e_submissions = EssaySubmission . objects . filter ( question__assignment = assignment , student = student ) <EOL> e_submissions . delete ( ) <EOL> except EssayQuestion . DoesNotExist : <EOL> pass <EOL> try : <EOL> mc_submissions = MultipleChoiceSubmission . objects . filter ( question__assignment = assignment , student = student ) <EOL> mc_submissions . delete ( ) <EOL> except MultipleChoiceSubmission . DoesNotExist : <EOL> pass <EOL> try : <EOL> tf_submissions = TrueFalseSubmission . objects . filter ( question__assignment = assignment , student = student ) <EOL> tf_submissions . delete ( ) <EOL> except tf_submissions . DoesNotExist : <EOL> pass <EOL> try : <EOL> r_submissions = ResponseSubmission . objects . filter ( question__assignment = assignment , student = student ) <EOL> r_submissions . delete ( ) <EOL> except ResponseQuestion . DoesNotExist : <EOL> pass <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT:success>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ login_required ( ) <EOL> def submit_e_assignment_answer ( request , course_id , assignment_id ) : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> question_id = int ( request . POST [ '<STR_LIT>' ] ) <EOL> try : <EOL> file = request . FILES [ "<STR_LIT:file>" ] <EOL> except : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> course = Course . objects . get ( id = course_id ) <EOL> student = Student . objects . get ( user = request . user ) <EOL> assignment = Assignment . objects . get ( assignment_id = assignment_id ) <EOL> try : <EOL> question = EssayQuestion . objects . get ( <EOL> assignment = assignment , <EOL> question_id = question_id , <EOL> ) <EOL> except MultipleChoiceQuestion . DoesNotExist : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> try : <EOL> submission = EssaySubmission . objects . get ( <EOL> student = student , <EOL> question = question , <EOL> ) <EOL> except EssaySubmission . DoesNotExist : <EOL> submission = EssaySubmission . objects . create ( <EOL> student = student , <EOL> question = question , <EOL> ) <EOL> submission . file = file <EOL> submission . save ( ) <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT:success>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ login_required ( ) <EOL> def submit_mc_assignment_answer ( request , course_id , assignment_id ) : <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> question_id = int ( request . POST [ '<STR_LIT>' ] ) <EOL> answer = request . POST [ '<STR_LIT>' ] <EOL> course = Course . objects . get ( id = course_id ) <EOL> assignment = Assignment . objects . get ( assignment_id = assignment_id ) <EOL> student = Student . objects . get ( user = request . user ) <EOL> try : <EOL> question = MultipleChoiceQuestion . objects . get ( <EOL> assignment = assignment , <EOL> question_id = question_id , <EOL> ) <EOL> except MultipleChoiceQuestion . DoesNotExist : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> try : <EOL> submission = MultipleChoiceSubmission . objects . get ( <EOL> student = student , <EOL> question = question , <EOL> ) <EOL> except MultipleChoiceSubmission . DoesNotExist : <EOL> submission = MultipleChoiceSubmission . objects . create ( <EOL> student = student , <EOL> question = question , <EOL> ) <EOL> if answer == '<STR_LIT:A>' : <EOL> submission . a = not submission . a <EOL> if answer == '<STR_LIT:B>' : <EOL> submission . b = not submission . b <EOL> if answer == '<STR_LIT:C>' : <EOL> submission . c = not submission . c <EOL> if answer == '<STR_LIT:D>' : <EOL> submission . d = not submission . d <EOL> if answer == '<STR_LIT:E>' : <EOL> submission . e = not submission . e <EOL> if answer == '<STR_LIT:F>' : <EOL> submission . f = not submission . f <EOL> submission . save ( ) <EOL> total = <NUM_LIT:6> <EOL> correct = <NUM_LIT:0> <EOL> if submission . a == submission . question . a_is_correct : <EOL> correct += <NUM_LIT:1> ; <EOL> if submission . b == submission . question . b_is_correct : <EOL> correct += <NUM_LIT:1> ; <EOL> if submission . c == submission . question . c_is_correct : <EOL> correct += <NUM_LIT:1> ; <EOL> if submission . d == submission . question . d_is_correct : <EOL> correct += <NUM_LIT:1> ; <EOL> if submission . e == submission . question . e_is_correct : <EOL> correct += <NUM_LIT:1> ; <EOL> if submission . f == submission . question . f_is_correct : <EOL> correct += <NUM_LIT:1> ; <EOL> if total == correct : <EOL> submission . marks = submission . question . marks <EOL> else : <EOL> submission . marks = <NUM_LIT:0> <EOL> submission . save ( ) <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT:success>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ login_required ( ) <EOL> def submit_tf_assignment_answer ( request , course_id , assignment_id ) : <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> question_id = int ( request . POST [ '<STR_LIT>' ] ) <EOL> answer = request . POST [ '<STR_LIT>' ] <EOL> course = Course . objects . get ( id = course_id ) <EOL> assignment = Assignment . objects . get ( assignment_id = assignment_id ) <EOL> student = Student . objects . get ( user = request . user ) <EOL> try : <EOL> question = TrueFalseQuestion . objects . get ( <EOL> assignment = assignment , <EOL> question_id = question_id , <EOL> ) <EOL> except MultipleChoiceQuestion . DoesNotExist : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> try : <EOL> submission = TrueFalseSubmission . objects . get ( <EOL> student = student , <EOL> question_id = question_id , <EOL> ) <EOL> except TrueFalseSubmission . DoesNotExist : <EOL> submission = TrueFalseSubmission . objects . create ( <EOL> student = student , <EOL> question_id = question_id , <EOL> ) <EOL> submission . answer = answer == "<STR_LIT:true>" <EOL> submission . save ( ) <EOL> if submission . answer == submission . question . answer : <EOL> submission . marks = submission . question . marks <EOL> else : <EOL> submission . marks = <NUM_LIT:0> <EOL> submission . save ( ) <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT:success>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ login_required ( ) <EOL> def submit_r_assignment_answer ( request , course_id , assignment_id ) : <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> question_id = int ( request . POST [ '<STR_LIT>' ] ) <EOL> answer = request . POST [ '<STR_LIT>' ] <EOL> course = Course . objects . get ( id = course_id ) <EOL> assignment = Assignment . objects . get ( assignment_id = assignment_id ) <EOL> student = Student . objects . get ( user = request . user ) <EOL> try : <EOL> question = ResponseQuestion . objects . get ( <EOL> assignment = assignment , <EOL> question_id = question_id , <EOL> ) <EOL> except ResponseQuestion . DoesNotExist : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> try : <EOL> submission = ResponseSubmission . objects . get ( <EOL> student = student , <EOL> question_id = question_id , <EOL> ) <EOL> except ResponseSubmission . DoesNotExist : <EOL> submission = ResponseSubmission . objects . create ( <EOL> student = student , <EOL> question_id = question_id , <EOL> ) <EOL> submission . answer = answer <EOL> submission . save ( ) <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT:success>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ login_required ( ) <EOL> def submit_assignment ( request , course_id , assignment_id ) : <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> course = Course . objects . get ( id = course_id ) <EOL> assignment = Assignment . objects . get ( assignment_id = assignment_id ) <EOL> student = Student . objects . get ( user = request . user ) <EOL> try : <EOL> submission = AssignmentSubmission . objects . get ( <EOL> student = student , <EOL> assignment = assignment , <EOL> ) <EOL> except AssignmentSubmission . DoesNotExist : <EOL> submission = AssignmentSubmission . objects . create ( <EOL> student = student , <EOL> assignment = assignment , <EOL> ) <EOL> submission . is_finished = True <EOL> submission . save ( ) <EOL> compute_score ( student , assignment , submission ) <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT:success>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> def compute_score ( student , assignment , submission ) : <EOL> submission . total_marks = <NUM_LIT:0> <EOL> submission . earned_marks = <NUM_LIT:0> <EOL> e_submissions = EssaySubmission . objects . filter ( <EOL> student = student , <EOL> question__assignment = assignment , <EOL> ) <EOL> for e_submission in e_submissions : <EOL> submission . total_marks += e_submission . question . marks <EOL> submission . earned_marks += e_submission . marks <EOL> mc_submissions = MultipleChoiceSubmission . objects . filter ( <EOL> student = student , <EOL> question__assignment = assignment , <EOL> ) <EOL> for mc_submission in mc_submissions : <EOL> submission . total_marks += mc_submission . question . marks <EOL> submission . earned_marks += mc_submission . marks <EOL> tf_submissions = TrueFalseSubmission . objects . filter ( <EOL> student = student , <EOL> question__assignment = assignment , <EOL> ) <EOL> for tf_submission in tf_submissions : <EOL> submission . total_marks += tf_submission . question . marks <EOL> submission . earned_marks += tf_submission . marks <EOL> r_submissions = ResponseSubmission . objects . filter ( <EOL> student = student , <EOL> question__assignment = assignment , <EOL> ) <EOL> for r_submission in r_submissions : <EOL> submission . total_marks += r_submission . question . marks <EOL> submission . earned_marks += r_submission . marks <EOL> try : <EOL> submission . percent = round ( ( submission . earned_marks / submission . total_marks ) * <NUM_LIT:100> ) <EOL> except ZeroDivisionError : <EOL> submission . percent = <NUM_LIT:0> <EOL> submission . save ( ) </s>
