import psycopg2, os
# import subprocess
from socket import gethostname
host = gethostname()

con = psycopg2.connect(host='bocian.inf.ed.ac.uk', database='alignment', user='aligner_admin', password='default99')
cur = con.cursor()
cur.execute("SELECT upload_dir FROM system_server WHERE host_name like '" + host + "'")
record = cur.fetchone()
if record == None:
  print 'Missing server settings for ' + str(host)
  cur.execute("SELECT upload_dir, host_name FROM system_server")
  record = cur.fetchone()
  print 'Having to use settings for ' + str(record[1])
  host = str(record[1])
uploadfolder = str(record[0])
cur.close()
con.close()
del cur, con, record

# Use to reset file permission only if error occurs
# for file in os.listdir(uploadfolder):
#   try:
#     # os.chmod(uploadfolder + file, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
#     subprocess.call(['chmod', '0777', uploadfolder + file])
#     print 'OK: ' + file
#   except:
#     print 'Error: ' + file
#

# Django settings for align project.

# DEBUG = True
# TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Robert Court', 'r.court@ed.ac.uk'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'django_mongodb_engine'.
        'NAME': 'alignment',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'aligner_admin',
        'PASSWORD': 'default99',
        'HOST': 'bocian.inf.ed.ac.uk',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'bocian.inf.ed.ac.uk', 'vfbaligner.inf.ed.ac.uk']



# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'Europe/London'

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

# SITE_ID=u'5395bb746c132991c57933f6'
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

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = '/disk/data/VFB/aligner/uploads/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = '/disk/data/VFB/aligner/static/'

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# 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',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'dv16bwh3f1x%p9csb3o7l9k#o8d_oqp-)aa=afq%yj+2$s96_('

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

LOGIN_URL          = '/'
LOGIN_REDIRECT_URL = '/'
LOGIN_ERROR_URL    = '/'

SOCIAL_AUTH_COMPLETE_URL_NAME  = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'socialauth_associate_complete'

SOCIAL_AUTH_GITHUB_KEY = 'e8bfae9142f86f36b391'
SOCIAL_AUTH_GITHUB_SECRET = 'b7617cf006cace2e60d90f089816924e0eabbd0f'

SOCIAL_AUTH_GITHUB_SCOPE = ['user:email']

# SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '884257168498-3gec80pdfpullsaeavbg2nqra3aflru5.apps.googleusercontent.com'
# SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'pvsqhFUx1kmBiGlVWERy_Q-b'

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ.get(
    'SOCIAL_AUTH_GOOGLE_OAUTH2_KEY',
    '884257168498-3gec80pdfpullsaeavbg2nqra3aflru5.apps.googleusercontent.com'
)

SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ.get(
    'SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET',
    'pvsqhFUx1kmBiGlVWERy_Q-b'
)

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.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'social.apps.django_app.context_processors.backends',
    'social.apps.django_app.context_processors.login_redirect',
)


# MIDDLEWARE_CLASSES = (
#     'django.middleware.common.CommonMiddleware',
#     'django.contrib.sessions.middleware.SessionMiddleware',
#     'django.middleware.csrf.CsrfViewMiddleware',
#     'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
#     'django.contrib.auth.middleware.AuthenticationMiddleware',
#     # 'permission_backend_nonrel.backends.NonrelPermissionBackend'
#     'django.contrib.messages.middleware.MessageMiddleware',
#     # Uncomment the next line for simple clickjacking protection:
#     # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
# )
MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GZipMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
)

ROOT_URLCONF = 'urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'align.wsgi.application'

TEMPLATE_DIRS = (
    "/disk/data/VFB/aligner/AlignmentPipe/align/images/templates/admin_copies" ,
    # 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.
)

# AUTHENTICATION_BACKENDS = (
#     'permission_backend_nonrel.backends.NonrelPermissionBackend',
# )
# AUTHENTICATION_BACKENDS = (
#     'social.backends.google.GoogleOAuth2',
#     'social.backends.google.GooglePlusAuth',
#     'django.contrib.auth.backends.ModelBackend',
# )

# AUTHENTICATION_BACKENDS = (
#       'social.backends.open_id.OpenIdAuth',
#       'social.backends.google.GoogleOpenId',
#       'social.backends.google.GoogleOAuth2',
#       'social.backends.google.GoogleOAuth',
#       'social.backends.twitter.TwitterOAuth',
#       'social.backends.yahoo.YahooOpenId',
#       'django.contrib.auth.backends.ModelBackend',
# )

AUTHENTICATION_BACKENDS = (
  #  'social.backends.facebook.FacebookOAuth2',
   'social.backends.google.GoogleOAuth2',
   'social.backends.google.GooglePlusAuth',
  #  'social.backends.twitter.TwitterOAuth',
   'django.contrib.auth.backends.ModelBackend',
   'social.backends.github.GithubOAuth2',
  #  'social_auth.backends.contrib.linkedin.LinkedinBackend',
)

# SOCIAL_AUTH_USER_MODEL = 'users.User'

SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ['username', 'first_name', 'email']

LOGIN_REDIRECT_URL = '/'

SOCIAL_AUTH_AUTHENTICATION_BACKENDS = (
    'social.backends.open_id.OpenIdAuth',
    'social.backends.google.GoogleOpenId',
    'social.backends.google.GoogleOAuth2',
    'social.backends.google.GoogleOAuth',
    'social.backends.google.GooglePlusAuth',
    # 'social.backends.twitter.TwitterOAuth',
    # 'social.backends.yahoo.YahooOpenId',
    'social.backends.github.GithubOAuth2',
    # 'social_auth.backends.contrib.linkedin.LinkedinBackend',
)

INSTALLED_APPS = (
    'adminactions',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 'django_mongodb_engine',
    'django.db.backends.postgresql_psycopg2',
    # 'djangotoolbox',
    # 'permission_backend_nonrel',
    'system',
    'images',
    'users',
    'bootstrap3',
    'images.templatetags.images_extras',
    'users.templatetags.backend_utils',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
    'social.apps.django_app.default',
)

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}
