status.py 1.18 KB
Newer Older
1 2 3 4 5
"""
A tiny app that checks for a status message.
"""

from django.conf import settings
6
import json
7 8 9 10 11 12
import logging
import os
import sys

log = logging.getLogger(__name__)

Calen Pennington committed
13

14
def get_site_status_msg(course_id):
15
    """
16 17 18 19 20 21 22 23
    Look for a file settings.STATUS_MESSAGE_PATH.  If found, read it,
    parse as json, and do the following:

    * if there is a key 'global', include that in the result list.
    * if course is not None, and there is a key for course.id, add that to the result list.
    * return "<br/>".join(result)

    Otherwise, return None.
24 25 26 27 28

    If something goes wrong, returns None.  ("is there a status msg?" logic is
    not allowed to break the entire site).
    """
    try:
29 30 31
        if os.path.isfile(settings.STATUS_MESSAGE_PATH):
            with open(settings.STATUS_MESSAGE_PATH) as f:
                content = f.read()
32 33 34 35 36
        else:
            return None

        status_dict = json.loads(content)
        msg = status_dict.get('global', None)
37
        if course_id in status_dict:
38
            msg = msg + "<br>" if msg else ''
39
            msg += status_dict[course_id]
40

41
        return msg
42
    except:
43
        log.exception("Error while getting a status message.")
44
        return None