diff --git a/kalite/topic_tools/__init__.py b/kalite/topic_tools/__init__.py index 225e3b185e..ef9561bb3b 100644 --- a/kalite/topic_tools/__init__.py +++ b/kalite/topic_tools/__init__.py @@ -46,6 +46,15 @@ logging.warning("Channel {channel} does not exist.".format(channel=django_settings.CHANNEL)) +def cache_file_path(basename): + """Consistently return path for a cache filename. This path has to be + writable for the user running kalite.""" + assert "/" not in basename, "Please use a valid filename" + cache_dir = os.path.join(django_settings.USER_DATA_ROOT, 'cache') + os.makedirs(cache_dir) + return os.path.join(cache_dir, basename + '.json') + + # Globals that can be filled TOPICS = None CACHE_VARS.append("TOPICS") @@ -75,7 +84,11 @@ def get_topic_tree(force=False, annotate=False, channel=None, language=None, par if annotate: if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force: - topics = softload_json(settings.TOPICS_FILEPATHS.get(channel) + "_" + language + ".cache", logger=logging.debug, raises=False) + topics = softload_json( + cache_file_path("topic_{0}_{1}.json".format(channel, language)), + logger=logging.debug, + raises=False + ) if topics: TOPICS[channel][language] = topics return TOPICS[channel][language] @@ -130,7 +143,7 @@ def recurse_nodes(node, parent=""): if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP: try: - with open(settings.TOPICS_FILEPATHS.get(channel) + "_" + language + ".cache", "w") as f: + with open(cache_file_path("topic_{0}_{1}.json".format(channel, language)), "w") as f: json.dump(TOPICS[channel][language], f) except IOError as e: logging.warn("Annotated topic cache file failed in saving with error {e}".format(e=e)) @@ -168,12 +181,18 @@ def get_exercise_cache(force=False, language=None): EXERCISES = {} if EXERCISES.get(language) is None: if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force: - exercises = softload_json(settings.EXERCISES_FILEPATH + "_" + language + ".cache", logger=logging.debug, raises=False) + exercises = softload_json( + cache_file_path("exercises_{1}.json".format(language)), + logger=logging.debug, + raises=False + ) if exercises: EXERCISES[language] = exercises return EXERCISES[language] EXERCISES[language] = softload_json(settings.EXERCISES_FILEPATH, logger=logging.debug, raises=False) - if language == "en": # English-language exercises live in application space, translations in user space + + # English-language exercises live in application space, translations in user space + if language == "en": exercise_root = os.path.join(settings.KHAN_EXERCISES_DIRPATH, "exercises") else: exercise_root = os.path.join(django_settings.USER_DATA_ROOT, "exercises") @@ -197,7 +216,7 @@ def get_exercise_cache(force=False, language=None): elif exercise.get("uses_assessment_items", False): available = False items = [] - for item in exercise.get("all_assessment_items","[]"): + for item in exercise.get("all_assessment_items", "[]"): item = json.loads(item) if get_assessment_item_data(request=None, assessment_item_id=item.get("id")): items.append(item) @@ -227,7 +246,7 @@ def get_exercise_cache(force=False, language=None): if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP: try: - with open(settings.EXERCISES_FILEPATH + "_" + language + ".cache", "w") as f: + with open(cache_file_path("exercises_{1}.json".format(language)), "w") as f: json.dump(EXERCISES[language], f) except IOError as e: logging.warn("Annotated exercise cache file failed in saving with error {e}".format(e=e)) @@ -272,7 +291,11 @@ def get_content_cache(force=False, annotate=False, language=None): if annotate: if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force: - content = softload_json(settings.CONTENT_FILEPATH + "_" + language + ".cache", logger=logging.debug, raises=False) + content = softload_json( + cache_file_path("content_{1}.json".format(language)), + logger=logging.debug, + raises=False + ) if content: CONTENT[language] = content return CONTENT[language] @@ -348,7 +371,7 @@ def get_content_cache(force=False, annotate=False, language=None): if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP: try: - with open(settings.CONTENT_FILEPATH + "_" + language + ".cache", "w") as f: + with open(cache_file_path("content_{1}.json".format(language)), "w") as f: json.dump(CONTENT[language], f) except IOError as e: logging.warn("Annotated content cache file failed in saving with error {e}".format(e=e))