From 8ad1e7fba18320505ed2f06686bdf4c1f1d57303 Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Tue, 2 Oct 2012 11:48:16 -0400 Subject: [PATCH 1/7] Add framework for a site status banner. - still needs style - will cache for 10 seconds --- common/djangoapps/status/__init__.py | 1 + common/djangoapps/status/status.py | 43 ++++++++++++++++++++++++++++ lms/envs/common.py | 26 +++++++++++------ lms/templates/navigation.html | 22 +++++++++----- 4 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 common/djangoapps/status/__init__.py create mode 100644 common/djangoapps/status/status.py diff --git a/common/djangoapps/status/__init__.py b/common/djangoapps/status/__init__.py new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/common/djangoapps/status/__init__.py @@ -0,0 +1 @@ + diff --git a/common/djangoapps/status/status.py b/common/djangoapps/status/status.py new file mode 100644 index 0000000000..ab1624aed2 --- /dev/null +++ b/common/djangoapps/status/status.py @@ -0,0 +1,43 @@ +""" +A tiny app that checks for a status message. +""" + +from django.conf import settings +import logging +import os +import sys + +from util.cache import cache + +log = logging.getLogger(__name__) + +def get_site_status_msg(): + """ + Look for a file settings.STATUS_MESSAGE_PATH. If found, return the + contents. Otherwise, return None. Caches result for 10 seconds, per-machine. + + If something goes wrong, returns None. ("is there a status msg?" logic is + not allowed to break the entire site). + """ + cache_time = 10 + try: + key = ','.join([settings.HOSTNAME, settings.STATUS_MESSAGE_PATH]) + content = cache.get(key) + if content == '': + # cached that there isn't a status message + return None + + if content is None: + # nothing in the cache, so check the filesystem + if os.path.isfile(settings.STATUS_MESSAGE_PATH): + with open(settings.STATUS_MESSAGE_PATH) as f: + content = f.read() + else: + # remember that there isn't anything there + cache.set(key, '', cache_time) + content = None + + return content + except: + log.debug("Error while getting a status message: {0}".format(sys.exc_info())) + return None diff --git a/lms/envs/common.py b/lms/envs/common.py index d9f8a873d1..bbd36c1ae8 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -25,6 +25,7 @@ import glob2 import errno import hashlib from collections import defaultdict +import socket import djcelery from path import path @@ -95,6 +96,13 @@ GENERATE_PROFILE_SCORES = False # Used with XQueue XQUEUE_WAITTIME_BETWEEN_REQUESTS = 5 # seconds + +# Used for per-maching caching +try: + HOSTNAME = socket.gethostname() +except: + HOSTNAME = 'localhost' + ############################# SET PATH INFORMATION ############################# PROJECT_ROOT = path(__file__).abspath().dirname().dirname() # /mitx/lms REPO_ROOT = PROJECT_ROOT.dirname() @@ -103,7 +111,6 @@ ENV_ROOT = REPO_ROOT.dirname() # virtualenv dir /mitx is in ASKBOT_ROOT = REPO_ROOT / "askbot" COURSES_ROOT = ENV_ROOT / "data" -# FIXME: To support multiple courses, we should walk the courses dir at startup DATA_DIR = COURSES_ROOT sys.path.append(REPO_ROOT) @@ -127,8 +134,11 @@ node_paths = [COMMON_ROOT / "static/js/vendor", NODE_PATH = ':'.join(node_paths) +# Where to look for a status message +STATUS_MESSAGE_PATH = ENV_ROOT / "status_message.html" + ############################ OpenID Provider ################################## -OPENID_PROVIDER_TRUSTED_ROOTS = ['cs50.net', '*.cs50.net'] +OPENID_PROVIDER_TRUSTED_ROOTS = ['cs50.net', '*.cs50.net'] ################################## MITXWEB ##################################### # This is where we stick our compiled template files. Most of the app uses Mako @@ -158,7 +168,7 @@ TEMPLATE_CONTEXT_PROCESSORS = ( 'askbot.user_messages.context_processors.user_messages',#must be before auth 'django.contrib.auth.context_processors.auth', #this is required for admin 'django.core.context_processors.csrf', #necessary for csrf protection - + # Added for django-wiki 'django.core.context_processors.media', 'django.core.context_processors.tz', @@ -355,7 +365,7 @@ WIKI_CAN_ASSIGN = lambda article, user: user.is_staff or user.is_superuser WIKI_USE_BOOTSTRAP_SELECT_WIDGET = False WIKI_LINK_LIVE_LOOKUPS = False -WIKI_LINK_DEFAULT_LEVEL = 2 +WIKI_LINK_DEFAULT_LEVEL = 2 ################################# Jasmine ################################### JASMINE_TEST_DIRECTORY = PROJECT_ROOT + '/static/coffee' @@ -372,10 +382,10 @@ STATICFILES_FINDERS = ( TEMPLATE_LOADERS = ( 'mitxmako.makoloader.MakoFilesystemLoader', 'mitxmako.makoloader.MakoAppDirectoriesLoader', - + # 'django.template.loaders.filesystem.Loader', # 'django.template.loaders.app_directories.Loader', - + #'askbot.skins.loaders.filesystem_load_template_source', # 'django.template.loaders.eggs.Loader', ) @@ -393,7 +403,7 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.messages.middleware.MessageMiddleware', 'track.middleware.TrackMiddleware', 'mitxmako.middleware.MakoMiddleware', - + 'course_wiki.course_nav.Middleware', 'askbot.middleware.anon_user.ConnectToSessionMessagesMiddleware', @@ -624,7 +634,7 @@ INSTALLED_APPS = ( 'certificates', 'instructor', 'psychometrics', - + #For the wiki 'wiki', # The new django-wiki from benjaoming 'django_notify', diff --git a/lms/templates/navigation.html b/lms/templates/navigation.html index 210fd61ead..b1b2ddad60 100644 --- a/lms/templates/navigation.html +++ b/lms/templates/navigation.html @@ -8,19 +8,27 @@ from django.core.urlresolvers import reverse # App that handles subdomain specific branding import branding +# app that handles site status messages +from status.status import get_site_status_msg +site_status_msg = get_site_status_msg() %> -%if course: +% if site_status_msg: +
${site_status_msg}
+% endif + + +% if course:
-%else: +% else:
-%endif +% endif