config.py 3.34 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1 2 3
# -*- coding: utf-8 -*-

import os
Nicolas Joyard's avatar
Nicolas Joyard committed
4
from random import SystemRandom
Nicolas Joyard's avatar
Nicolas Joyard committed
5 6 7 8 9


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


Nicolas Joyard's avatar
Nicolas Joyard committed
10 11 12 13 14 15 16
def get_secret_key(data_dir):
    secret_file = os.path.join(data_dir, 'secret.txt')

    if not os.path.exists(secret_file):
        chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'

        rnd = SystemRandom()
Nicolas Joyard's avatar
Nicolas Joyard committed
17
        key = ''.join([chars[rnd.randint(1, len(chars)) - 1]
Nicolas Joyard's avatar
Nicolas Joyard committed
18 19 20 21 22 23 24 25 26
                       for i in range(1, 50)])

        with open(secret_file, 'w+') as f:
            f.write(key)

    with open(secret_file, 'r') as f:
        return f.read()


Nicolas Joyard's avatar
Nicolas Joyard committed
27 28 29 30 31 32 33 34 35 36
class DefaultConfig(object):
    """
    Default irfm config file for standard environment
    """
    DEBUG = False
    SQLALCHEMY_DATABASE_URI = \
        'postgresql://irfm:irfm@localhost:5432/irfm'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ECHO = False
    DATA_DIR = os.path.join(BASE_DIR, 'data')
37
    MAX_CONTENT_LENGTH = 4 * 1024 * 1024
Nicolas Joyard's avatar
Nicolas Joyard committed
38 39 40
    PIWIK_HOST = None
    PIWIK_ID = None

Nicolas Joyard's avatar
Nicolas Joyard committed
41 42 43
    ADMIN_EMAIL = 'none@example.com'
    ADMIN_PASSWORD = None

Nicolas Joyard's avatar
Nicolas Joyard committed
44 45 46 47 48 49
    MAIL_SERVER = 'localhost'
    MAIL_PORT = 25
    MAIL_USE_TLS = False
    MAIL_USE_SSL = False
    MAIL_USERNAME = None
    MAIL_PASSWORD = None
50
    MAIL_SUPPRESS_SEND = False
Nicolas Joyard's avatar
Nicolas Joyard committed
51

Nicolas Joyard's avatar
Nicolas Joyard committed
52
    PERMANENT_SESSION_LIFETIME = 6 * 31 * 24 * 60 * 60
Nicolas Joyard's avatar
Nicolas Joyard committed
53
    SERVER_NAME = 'irfm.regardscitoyens.org'
Nicolas Joyard's avatar
Nicolas Joyard committed
54

Nicolas Joyard's avatar
Nicolas Joyard committed
55 56 57 58 59 60 61 62 63

class DebugConfig(DefaultConfig):
    """
    Debug-enabled default config
    """
    DEBUG = True
    SQLALCHEMY_ECHO = True


Nicolas Joyard's avatar
Nicolas Joyard committed
64
class EnvironmentConfig(DefaultConfig):
Nicolas Joyard's avatar
Nicolas Joyard committed
65 66 67 68 69 70 71 72
    """
    Config for environment-based setup.
    - IRFM_DEBUG: 'True' to enable
    - IRFM_DEBUG_SQL: 'True' to enable
    - IRFM_DB_URL: database connection URL
    - IRFM_DATA_DIR: directory for data files
    - IRFM_PIWIK_HOST: piwik hostname
    - IRFM_PIWIK_ID: piwik site ID
Nicolas Joyard's avatar
Nicolas Joyard committed
73 74
    - IRFM_ADMIN_EMAIL: admin email
    - IRFM_ADMIN_PASSWORD: admin password as hashed by `irfm password`
Nicolas Joyard's avatar
Nicolas Joyard committed
75 76 77 78 79 80
    - IRFM_MAIL_SERVER:
    - IRFM_MAIL_PORT:
    - IRFM_MAIL_USE_TLS: 'True' to enable
    - IRFM_MAIL_USE_SSL: 'True' to enable
    - IRFM_MAIL_USERNAME:
    - IRFM_MAIL_PASSWORD:
Nicolas Joyard's avatar
Nicolas Joyard committed
81 82 83 84 85 86 87 88 89 90 91
    """
    DEBUG = os.environ.get('IRFM_DEBUG', 'False') == 'True'
    SQLALCHEMY_ECHO = os.environ.get('IRFM_DEBUG_SQL', 'False') == 'True'

    SQLALCHEMY_DATABASE_URI = os.environ.get(
        'IRFM_DB_URL', DefaultConfig.SQLALCHEMY_DATABASE_URI)

    DATA_DIR = os.environ.get('IRFM_DATA_DIR', DefaultConfig.DATA_DIR)

    PIWIK_HOST = os.environ.get('IRFM_PIWIK_HOST', DefaultConfig.PIWIK_HOST)
    PIWIK_ID = os.environ.get('IRFM_PIWIK_ID', DefaultConfig.PIWIK_ID)
Nicolas Joyard's avatar
Nicolas Joyard committed
92 93 94 95

    ADMIN_EMAIL = os.environ.get('IRFM_ADMIN_EMAIL', DefaultConfig.ADMIN_EMAIL)
    ADMIN_PASSWORD = os.environ.get('IRFM_ADMIN_PASSWORD',
                                    DefaultConfig.ADMIN_PASSWORD)
Nicolas Joyard's avatar
Nicolas Joyard committed
96 97 98 99 100 101 102 103 104

    MAIL_SERVER = os.environ.get('IRFM_MAIL_SERVER', DefaultConfig.MAIL_SERVER)
    MAIL_PORT = os.environ.get('IRFM_MAIL_PORT', DefaultConfig.MAIL_PORT)
    MAIL_USE_TLS = os.environ.get('IRFM_MAIL_USE_TLS', 'False') == 'True'
    MAIL_USE_SSL = os.environ.get('IRFM_MAIL_USE_SSL', 'False') == 'True'
    MAIL_USERNAME = os.environ.get('IRFM_MAIL_USERNAME',
                                   DefaultConfig.MAIL_USERNAME)
    MAIL_PASSWORD = os.environ.get('IRFM_MAIL_PASSWORD',
                                   DefaultConfig.MAIL_PASSWORD)
105 106

    MAIL_SUPPRESS_SEND = os.environ.get('IRFM_MAIL_SUPPRESS_SEND', 'False') \
Nicolas Joyard's avatar
Nicolas Joyard committed
107
        == 'True'