From a4c6a0a3c013f02c7cdd1d365a180ff6de121d6e Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Fri, 4 Jan 2019 16:33:26 +0530 Subject: Code refactor and resolves requested changes. --- yaksh/models.py | 36 +++++++++++++++++------------ yaksh/static/yaksh/css/exam.css | 3 ++- yaksh/templates/exam.html | 40 ++++---------------------------- yaksh/views.py | 51 ----------------------------------------- 4 files changed, 28 insertions(+), 102 deletions(-) diff --git a/yaksh/models.py b/yaksh/models.py index 98a63b0..0f20605 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -6,7 +6,8 @@ import ruamel.yaml from ruamel.yaml.scalarstring import PreservedScalarString from ruamel.yaml.comments import CommentedMap from random import sample -from collections import Counter +from collections import Counter, defaultdict + from django.db import models from django.contrib.auth.models import User, Group, Permission from django.contrib.contenttypes.models import ContentType @@ -80,6 +81,17 @@ string_check_type = ( ("exact", "Case Sensitive"), ) +legend_display_types = { + "mcq": {"category": "objective", "label": "Objective Type"}, + "mcc": {"category": "objective", "label": "Objective Type"}, + "code": {"category": "programming", "label": "Programming"}, + "upload": {"category": "upload", "label": "Upload"}, + "integer": {"category": "objective", "label": "Objective Type"}, + "string": {"category": "objective", "label": "Objective Type"}, + "float": {"category": "objective", "label": "Objective Type"}, + "arrange": {"category": "objective", "label": "Objective Type"}, + } + attempts = [(i, i) for i in range(1, 6)] attempts.append((-1, 'Infinite')) @@ -2091,20 +2103,14 @@ class AnswerPaper(models.Model): def get_previous_answers(self, question): return self.answers.filter(question=question).order_by('-id') - def get_question_by_index(self, all_ordered_questions, questions_by_type): - return [index+1 for index, item in enumerate(all_ordered_questions) - if item in set(questions_by_type)] - - def get_questions_by_type(self, all_ordered_questions, objective_types): - questions_by_type = [] - for types in objective_types: - for question in all_ordered_questions: - if question.type == types: - questions_by_type.append(question) - return self.get_question_by_index( - all_ordered_questions, - questions_by_type - ) + def get_categorized_question_indices(self): + + category_question_map = defaultdict(lambda: []) + for index, question in enumerate(self.get_all_ordered_questions(), 1): + category_question_map[ + legend_display_types[question.type]["label"] + ].append(index) + return dict(category_question_map) def validate_answer(self, user_answer, question, json_data=None, uid=None, server_port=SERVER_POOL_PORT): diff --git a/yaksh/static/yaksh/css/exam.css b/yaksh/static/yaksh/css/exam.css index e420166..a246141 100644 --- a/yaksh/static/yaksh/css/exam.css +++ b/yaksh/static/yaksh/css/exam.css @@ -5,8 +5,9 @@ #stdio, #assertion { table-layout: fixed } -.table{ +.legend_table{ border: 1px solid; + width: 15em; background-color: white; border-spacing: 5px; border-collapse: collapse; diff --git a/yaksh/templates/exam.html b/yaksh/templates/exam.html index 8c65e2b..29ad167 100644 --- a/yaksh/templates/exam.html +++ b/yaksh/templates/exam.html @@ -78,7 +78,7 @@

Question(s) left: {{ paper.questions_left }}


- +
@@ -86,46 +86,16 @@ - {% if index.objectives_index %} + {% for category, question_number in paper.get_categorized_question_indices.items %} - {% endif %} - {% if index.blanks_index %} - - - - - {% endif %} - {% if index.upload_index %} - - - - - {% endif %} - {% if index.programming_index %} - - - - - {% endif %} + {% endfor %}
Category
- Objectives: + {{category}} - {{index.objectives_index|join:", "}} + {{question_number| join:", "}}
- Blanks: - - {{index.blanks_index|join:", "}} -
- Upload: - - {{index.upload_index|join:", "}} -
- Programming: - - {{index.programming_index|join:", "}} -
diff --git a/yaksh/views.py b/yaksh/views.py index 4c23ba6..3eaa165 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -616,55 +616,6 @@ def show_question(request, question, paper, error_message=None, course = Course.objects.get(id=course_id) module = course.learning_module.get(id=module_id) all_modules = course.get_learning_modules() - all_question_types = [types[0] for types in question_types] - types = {} - categorized_questions = {} - categorized_questions["objectives"] = questions.filter(Q(type="mcq") | - Q(type="mcc") | - Q(type="arrange")) - categorized_questions["blanks"] = questions.filter(Q(type="integer") | - Q(type="string") | - Q(type="float")) - categorized_questions["programming"] = questions.filter(Q(type="code")) - categorized_questions["upload"] = questions.filter(Q(type="upload")) - - types["objective_types"] = set([_type.type - for _type in - categorized_questions["objectives"]]) - types["blank_types"] = set([_type.type - for _type in - categorized_questions["blanks"]]) - types["programming_types"] = set([_type.type - for _type in - categorized_questions["programming"]]) - types["upload_types"] = set([_type.type - for _type in categorized_questions["upload"]]) - - all_ordered_questions = paper.get_all_ordered_questions() - - objectives_index = paper.get_questions_by_type( - all_ordered_questions, - types["objective_types"] - ) - blanks_index = paper.get_questions_by_type( - all_ordered_questions, - types["blank_types"] - ) - programming_index = paper.get_questions_by_type( - all_ordered_questions, - types["programming_types"] - ) - upload_index = paper.get_questions_by_type( - all_ordered_questions, - types["upload_types"] - ) - - index = { - 'objectives_index': objectives_index, - 'blanks_index': blanks_index, - 'programming_index': programming_index, - 'upload_index': upload_index - } context = { 'question': question, 'paper': paper, @@ -680,8 +631,6 @@ def show_question(request, question, paper, error_message=None, 'delay_time': delay_time, 'quiz_type': quiz_type, 'all_modules': all_modules, - "question_types": all_question_types, - "index": index, } answers = paper.get_previous_answers(question) if answers: -- cgit