# Django settings for pbp_com project.
import os
import sys

import logging
import warnings

warnings.simplefilter("ignore")

## Directories
CONF_ROOT = os.path.dirname(os.path.realpath(os.path.dirname(__file__)))
PROJECT_ROOT = os.path.dirname(CONF_ROOT)
SITE_ROOT = os.path.dirname(PROJECT_ROOT)
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "media/")

STATIC_ROOT = PROJECT_ROOT + "/static_collected/"
STATIC_URL = "/static/"
STATICFILES_DIRS = (os.path.join(PROJECT_ROOT, "static/"),)


VIRTUALENV = "/var/lib/virtualenvs/pbp/lib/python2.5/site-packages"

sys.path.append(SITE_ROOT)

DEBUG = True
TEMPLATE_DEBUG = DEBUG

# Django settings for pbp project.
ADMINS = ("Jeremiah Savage", "jeremiahs@pbp.me")

STOREFRONT_HELP_TO = ["subscriptions@pbp.me", "web@pbp.me"]
# change of address email
NOTIFICATIONS_EMAIL = ["subscriptions@pbp.me", "web@pbp.me"]

LANGUAGE_CODE = "en-us"
MANAGERS = ADMINS
USE_I18N = True
APPEND_SLASH = False
SMART_APPEND_SLASH = True
SEND_BROKEN_LINK_EMAILS = False
IGNORABLE_404_STARTS = (
    "/vault/loginout.html",
    "/calendar/submitanevent.html",
    "robots.txt",
)

TIME_ZONE = "America/New_York"
LANGUAGE_CODE = "en-us"

# Our site ID numbers, set to constants for easy reference
PBP = 2
IA = 3
CP = 4
WP = 5

# We're going to try setting this in the manage.py script.
SITE_ID = PBP  # pbp.com

# import logging

# LOG_DATE_FORMAT = '%d %b %Y %H:%M:%S'
# LOG_FORMATTER = logging.Formatter('%(asctime)s %(name)-7s: %(levelname)-8s %(message)s',
#    datefmt=LOG_DATE_FORMAT)

# CONSOLE_HANDLER = logging.StreamHandler() # defaults to stderr
# CONSOLE_HANDLER.setFormatter(LOG_FORMATTER)
# CONSOLE_HANDLER.setLevel(logging.DEBUG)

LOGGING = {
    "version": 1,
    "disable_existing_loggers": True,
    "formatters": {
        "verbose": {
            "format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s"
        },
        "simple": {"format": "%(levelname)s %(message)s"},
    },
    "handlers": {
        "null": {"level": "DEBUG", "class": "django.utils.log.NullHandler",},
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "log_file": {
            "level": "DEBUG",
            "class": "logging.handlers.RotatingFileHandler",
            "formatter": "verbose",
            "filename": "/var/log/uwsgi/pbp_com.log",
            "maxBytes": 1024 * 1024 * 25,  # 25 MB
            "backupCount": 5,
        },
        "mail_admins": {
            "level": "ERROR",
            "class": "django.utils.log.AdminEmailHandler",
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console", "log_file", "mail_admins"],
            "level": "INFO",
            "propagate": True,
        },
        "django.request": {
            "handlers": ["console", "log_file", "mail_admins"],
            "level": "ERROR",
            "propagate": False,
        },
        "django.db.backends": {
            "handlers": ["console", "log_file", "mail_admins"],
            "level": "INFO",
            "propagate": False,
        },
        # Catch All Logger -- Captures any other logging
        "": {
            "handlers": ["console", "log_file", "mail_admins"],
            "level": "DEBUG",
            "propagate": True,
        },
    },
}


AUTH_PROFILE_MODULE = "profiles.UserProfile"

LOGIN_REDIRECT_URL = "/store/"

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
)

SECRET_KEY = "l(4_rf+z#5oj7ik0j$ax9+zl)3yfkh8-ige$=!eoq=hydfbm7k"

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    "pbp_core.context_processors.pbp_store",
    "adzone.context_processors.get_source_ip",
)

MIDDLEWARE_CLASSES = (
    "django.middleware.cache.UpdateCacheMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.middleware.locale.LocaleMiddleware",
    "ipfilter.middleware.FilterIPs",
    "pbp_com.middleware.SmartAppendSlashMiddleware",
    "pbp_com.middleware.SetRemoteAddrMiddleware",
    "django.contrib.flatpages.middleware.FlatpageFallbackMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.cache.FetchFromCacheMiddleware",
)

CACHE_MIDDLEWARE_ALIAS = "default"
CACHE_MIDDLEWARE_SECONDS = 8600
CACHE_MIDDLEWARE_KEY_PREFIX = "pbp_"

AUTHENTICATION_BACKENDS = ("django.contrib.auth.backends.ModelBackend",)

ROOT_URLCONF = "urls.pbp"

# Django Merchant settings
MERCHANT_TEST_MODE = True
MERCHANT_SETTINGS = None  # needs to set explicitly

# if SITE_ID == PBP:
HAYSTACK_SITECONF = "pbp_com.search_sites"
HAYSTACK_SEARCH_ENGINE = "whoosh"
HAYSTACK_WHOOSH_PATH = os.path.join(PROJECT_ROOT, "search-index")
CLASSIFIED_LENGTH = 200
INSTALLED_APPS = (
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.staticfiles",
    "django.contrib.admin",
    "django.contrib.markup",
    "django.contrib.humanize",
    "django.contrib.comments",
    "django.contrib.admindocs",
    "django.contrib.sitemaps",
    "django.contrib.flatpages",
    "django_extensions",
    "registration",
    "profiles",
    "debug_toolbar",
    "typogrify",
    "taggit",
    "markitup",
    "django_inlines",
    "convert",
    "ipfilter",
    "documents",
    "newsroom",
    "darkroom",
    "adzone",
    "directory",
    "marketplace",
    "remember",
    "classifieds",
    "historical",
    "almanac",
    "visitors",
    "haystack",
)

PASSWORD_HASHERS = (
    "django.contrib.auth.hashers.PBKDF2PasswordHasher",
    "django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher",
    "django.contrib.auth.hashers.BCryptPasswordHasher",
    "django.contrib.auth.hashers.SHA1PasswordHasher",
    "django.contrib.auth.hashers.MD5PasswordHasher",
    "django.contrib.auth.hashers.CryptPasswordHasher",
)
