From 51d6b3f09684d5728bf6cfcd4f3b75e1c21f86e2 Mon Sep 17 00:00:00 2001 From: Bridger Maxwell Date: Thu, 28 Jun 2012 12:04:03 -0400 Subject: [PATCH 1/4] Changed course settings to be a json file. Moved it to its own file. --- lms/djangoapps/courseware/course_settings.py | 94 ++++++++++++++++++++ lms/djangoapps/courseware/grades.py | 51 +---------- 2 files changed, 95 insertions(+), 50 deletions(-) create mode 100644 lms/djangoapps/courseware/course_settings.py diff --git a/lms/djangoapps/courseware/course_settings.py b/lms/djangoapps/courseware/course_settings.py new file mode 100644 index 0000000000..075605f3d1 --- /dev/null +++ b/lms/djangoapps/courseware/course_settings.py @@ -0,0 +1,94 @@ +""" +Course settings module. All settings in the global_settings are +first applied, and then any settings in the settings.DATA_DIR/course_settings.json +are applied. A setting must be in ALL_CAPS. + +Settings are used by calling + +from courseware.course_settings import course_settings + +Note that courseware.course_settings.course_settings is not a module -- it's an object. So +importing individual settings is not possible: + +from courseware.course_settings.course_settings import GRADER # This won't work. + +""" +import json +import logging + +from django.conf import settings + +from xmodule import graders + +log = logging.getLogger("mitx.courseware") + +global_settings_json = """ +{ + "GRADER" : [ + { + "type" : "Homework", + "min_count" : 12, + "drop_count" : 2, + "short_label" : "HW", + "weight" : 0.15 + }, + { + "type" : "Lab", + "min_count" : 12, + "drop_count" : 2, + "category" : "Labs", + "weight" : 0.15 + }, + { + "type" : "Midterm", + "name" : "Midterm Exam", + "short_label" : "Midterm", + "weight" : 0.3 + }, + { + "type" : "Final", + "name" : "Final Exam", + "short_label" : "Final", + "weight" : 0.4 + } + ], + "GRADE_CUTOFFS" : { + "A" : 0.87, + "B" : 0.7, + "C" : 0.6 + } +} +""" + + +class Settings(object): + def __init__(self): + + #Load the global settings as a dictionary + global_settings = json.loads(global_settings_json) + + + #Load the course settings as a dictionary + course_settings = {} + try: + with open( settings.DATA_DIR + "/course_settings.json") as course_settings_file: + course_settings_string = course_settings_file.read() + course_settings = json.loads(course_settings_string) + except IOError: + log.warning("Unable to load course settings file from " + str(settings.DATA_DIR) + "/course_settings.json") + + + #First, set the properties from the global settings on ourselves + for setting in global_settings: + setting_value = global_settings[setting] + setattr(self, setting, setting_value) + + #Now, set the properties from the course settings on ourselves, possibly overriding global values + for setting in course_settings: + setting_value = course_settings[setting] + setattr(self, setting, setting_value) + + # Here is where we should parse any configurations, so that we can fail early + self.GRADER = graders.grader_from_conf(self.GRADER) + +course_settings = Settings() diff --git a/lms/djangoapps/courseware/grades.py b/lms/djangoapps/courseware/grades.py index 00bdffb697..6085239453 100644 --- a/lms/djangoapps/courseware/grades.py +++ b/lms/djangoapps/courseware/grades.py @@ -1,20 +1,3 @@ -""" -Course settings module. The settings are based of django.conf. All settings in -courseware.global_course_settings are first applied, and then any settings -in the settings.DATA_DIR/course_settings.py are applied. A setting must be -in ALL_CAPS. - -Settings are used by calling - -from courseware import course_settings - -Note that courseware.course_settings is not a module -- it's an object. So -importing individual settings is not possible: - -from courseware.course_settings import GRADER # This won't work. - -""" - from lxml import etree import random import imp @@ -25,6 +8,7 @@ import types from django.conf import settings from courseware import global_course_settings +from courseware.course_settings import course_settings from xmodule import graders from xmodule.graders import Score from models import StudentModule @@ -33,39 +17,6 @@ import xmodule _log = logging.getLogger("mitx.courseware") -class Settings(object): - def __init__(self): - # update this dict from global settings (but only for ALL_CAPS settings) - for setting in dir(global_course_settings): - if setting == setting.upper(): - setattr(self, setting, getattr(global_course_settings, setting)) - - - data_dir = settings.DATA_DIR - - fp = None - try: - fp, pathname, description = imp.find_module("course_settings", [data_dir]) - mod = imp.load_module("course_settings", fp, pathname, description) - except Exception as e: - _log.exception("Unable to import course settings file from " + data_dir + ". Error: " + str(e)) - mod = types.ModuleType('course_settings') - finally: - if fp: - fp.close() - - for setting in dir(mod): - if setting == setting.upper(): - setting_value = getattr(mod, setting) - setattr(self, setting, setting_value) - - # Here is where we should parse any configurations, so that we can fail early - self.GRADER = graders.grader_from_conf(self.GRADER) - -course_settings = Settings() - - - def grade_sheet(student,coursename=None): """ From 621680e340d619c0e95bcf85d02d2758aaa9fabe Mon Sep 17 00:00:00 2001 From: Bridger Maxwell Date: Thu, 28 Jun 2012 12:31:29 -0400 Subject: [PATCH 2/4] Removed old global course settings file. --- .../courseware/global_course_settings.py | 28 ------------------- 1 file changed, 28 deletions(-) delete mode 100644 lms/djangoapps/courseware/global_course_settings.py diff --git a/lms/djangoapps/courseware/global_course_settings.py b/lms/djangoapps/courseware/global_course_settings.py deleted file mode 100644 index f4e9696d1d..0000000000 --- a/lms/djangoapps/courseware/global_course_settings.py +++ /dev/null @@ -1,28 +0,0 @@ -GRADER = [ - { - 'type' : "Homework", - 'min_count' : 12, - 'drop_count' : 2, - 'short_label' : "HW", - 'weight' : 0.15, - }, - { - 'type' : "Lab", - 'min_count' : 12, - 'drop_count' : 2, - 'category' : "Labs", - 'weight' : 0.15 - }, - { - 'type' : "Midterm", - 'name' : "Midterm Exam", - 'short_label' : "Midterm", - 'weight' : 0.3, - }, - { - 'type' : "Final", - 'name' : "Final Exam", - 'short_label' : "Final", - 'weight' : 0.4, - } -] From 201f093981a49e60d109b972fd41d8ad2f4c0aac Mon Sep 17 00:00:00 2001 From: Bridger Maxwell Date: Thu, 28 Jun 2012 12:34:45 -0400 Subject: [PATCH 3/4] Removed old import. --- lms/djangoapps/courseware/grades.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lms/djangoapps/courseware/grades.py b/lms/djangoapps/courseware/grades.py index 6085239453..b31f4421cf 100644 --- a/lms/djangoapps/courseware/grades.py +++ b/lms/djangoapps/courseware/grades.py @@ -7,7 +7,6 @@ import types from django.conf import settings -from courseware import global_course_settings from courseware.course_settings import course_settings from xmodule import graders from xmodule.graders import Score From cbe5c612b481011ab909da4209a1819e67a4cbff Mon Sep 17 00:00:00 2001 From: Bridger Maxwell Date: Thu, 28 Jun 2012 14:04:50 -0400 Subject: [PATCH 4/4] Used dictionary update for cleaner course settings code. --- lms/djangoapps/courseware/course_settings.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lms/djangoapps/courseware/course_settings.py b/lms/djangoapps/courseware/course_settings.py index 075605f3d1..a58fc9042e 100644 --- a/lms/djangoapps/courseware/course_settings.py +++ b/lms/djangoapps/courseware/course_settings.py @@ -64,11 +64,11 @@ global_settings_json = """ class Settings(object): def __init__(self): - #Load the global settings as a dictionary + # Load the global settings as a dictionary global_settings = json.loads(global_settings_json) - #Load the course settings as a dictionary + # Load the course settings as a dictionary course_settings = {} try: with open( settings.DATA_DIR + "/course_settings.json") as course_settings_file: @@ -78,15 +78,13 @@ class Settings(object): log.warning("Unable to load course settings file from " + str(settings.DATA_DIR) + "/course_settings.json") - #First, set the properties from the global settings on ourselves + # Override any global settings with the course settings + global_settings.update(course_settings) + + # Now, set the properties from the course settings on ourselves for setting in global_settings: setting_value = global_settings[setting] setattr(self, setting, setting_value) - - #Now, set the properties from the course settings on ourselves, possibly overriding global values - for setting in course_settings: - setting_value = course_settings[setting] - setattr(self, setting, setting_value) # Here is where we should parse any configurations, so that we can fail early self.GRADER = graders.grader_from_conf(self.GRADER)