#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 encoding=utf-8

import os, time
import i18n


DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = ()
MANAGERS = ADMINS


# default to the system's timezone settings. this can still be
# overridden in rapidsms.ini [django], by providing one of:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
TIME_ZONE = time.tzname[0]


# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'sw'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Django i18n searches for translation files (django.po) within this dir
LOCALE_PATHS=['contrib/locale']

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '2rgmwtyq$thj49+-6u7x9t39r7jflu&1ljj3x2c0n0fl$)04_0'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
#     'django.template.loaders.eggs.load_template_source',
)

MIDDLEWARE_CLASSES = [
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
]

ROOT_URLCONF = "rapidsms.webui.urls"

TEMPLATE_CONTEXT_PROCESSORS = [
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "webapp.context_processors.base_template" # sticks the base template inside all responses
]

TEMPLATE_DIRS = [
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
]

# this is the template that is used as the base for all
# rapidsms pages.  if you want to totally restyle your pages
# you should change this in your configuration .ini file
BASE_TEMPLATE = "layout.html"
# This is a similar concept, but for templating the login
# and logout screens 
LOGIN_TEMPLATE = "webapp/login.html"
LOGGEDOUT_TEMPLATE = "webapp/loggedout.html"



# ====================
# LOAD RAPIDSMS CONFIG
# ====================

# this module will usually be called up via the django
# reloader, which is (in turn) called up by rapidsms's
# server.py, which adds RAPIDSMS_INI to the environment.
# just in case, though, check that it's defined rather
# than repeating the filename selection logic here
if not "RAPIDSMS_INI" in os.environ:
    raise(
        EnvironmentError,
        "The RAPIDSMS_INI environment variable is not "  +\
        "defined. Without it, settings.py doesn't know " +\
        "which ini file to load settings from")

# load the rapidsms configuration
from rapidsms import Config
RAPIDSMS_CONF = Config(os.environ["RAPIDSMS_INI"])

# since iterating and reading the config of apps is
# common, build a handy dict of apps and their configs
RAPIDSMS_APPS = dict([
    (app["type"], app)
    for app in RAPIDSMS_CONF["rapidsms"]["apps"]])


# this code bootstraps the i18n logic configuration, if 
# it is in the settings

def _i18n_to_django_setting(language_settings):
    languages = []
    for language in language_settings:
        if len(language) >= 2:
            languages.append( (language[0],language[1]) )
    return tuple(languages)
    
# Import i18n settings from rapidsms.ini for sms
if "i18n" in RAPIDSMS_CONF:
    RAPIDSMS_I18N = True
    if "web_languages" in RAPIDSMS_CONF["i18n"]:
        LANGUAGES = _i18n_to_django_setting( RAPIDSMS_CONF["i18n"]["web_languages"] )
    elif "languages" in RAPIDSMS_CONF["i18n"]:
        LANGUAGES = _i18n_to_django_setting( RAPIDSMS_CONF["i18n"]["languages"] )
    
    # allow you to specify the static paths for translation files
    if "locale_paths" in RAPIDSMS_CONF["i18n"]:
        LOCALE_PATHS = RAPIDSMS_CONF["i18n"]["locale_paths"]

# DATABASE SETTINGS
# 'This sets the default storage engine upon connecting to the database. 
# After your tables have been created, you should remove this option.'
# (from http://docs.djangoproject.com/en/dev/ref/databases/)
# (this solution is only for testing - the correct way to make sure
# that the engine is innodb going forward is to configure my.conf appropriately)
# 
# DATABASE_OPTIONS = {
#    "init_command": "SET storage_engine=INNODB",
# }

# ==========================
# LOAD OTHER DJANGO SETTINGS
# ==========================

# load the database settings first, since those
# keys don't correspond exactly to their eventual
# name in this module (they're missing the prefix
# in rapidsms.ini); inject them into this module
if "database" in RAPIDSMS_CONF:
    for key, val in RAPIDSMS_CONF["database"].items():
        vars()["DATABASE_%s" % key.upper()] = val

else:
    # database settings are missing, so
    # blow up. TODO: is there a way to
    # run django without a database?
    raise(
        "Your RapidSMS configuration does not contain " +\
        "a [database] section, which is required " +\
        "for the Django WebUI to function.")

# if there is a "django" section, inject
# the items as CONSTANTS in this module
if "django" in RAPIDSMS_CONF:
    for key, val in RAPIDSMS_CONF["django"].items():
        vars()[key.upper()] = val




# ====================
# INJECT RAPIDSMS APPS
# ====================

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.markup'
] + [app["module"] for app in RAPIDSMS_APPS.values()]

# ====================
# INJECT RAPIDSMS MIDDLEWARES IF PRESENT
# ====================


if "customdjango" in RAPIDSMS_CONF:
    if "middlewares" in RAPIDSMS_CONF["customdjango"]:
        middlewares = RAPIDSMS_CONF["customdjango"]["middlewares"]
        if isinstance(middlewares, basestring):
            middlewares = [middlewares]
            RAPIDSMS_CONF["customdjango"]["middlewares"] = middlewares
        MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES +\
                          [middleware for middleware in middlewares]
    if "authentications" in RAPIDSMS_CONF["customdjango"]:
        auths = RAPIDSMS_CONF["customdjango"]["authentications"]
        if isinstance(auths, basestring):
            auths = [auths]
            RAPIDSMS_CONF["customdjango"]["authentications"] = auths
        AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend'] +\
                          [backend for backend in auths]
    if "context_processors" in RAPIDSMS_CONF["customdjango"]:
        procs = RAPIDSMS_CONF["customdjango"]["context_processors"]
        if isinstance(procs, basestring):
            procs = [procs]
        TEMPLATE_CONTEXT_PROCESSORS = TEMPLATE_CONTEXT_PROCESSORS +\
                          [proc for proc in procs]

CUSTOM_MANAGERS = {}
if "managers" in RAPIDSMS_CONF:
    for model_class, method in RAPIDSMS_CONF["managers"].items():
        CUSTOM_MANAGERS[model_class] = method