settings.py 11.5 KB
"""
Django settings for pyros project.

Generated by 'django-admin startproject' using Django 1.9.4.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

''' DO NOT TOUCH THESE VARIABLES
"pyros.py simulator_development" will automatically set them to "True"
'''
# FOR SIMULATOR (TODO: remove because not used)
SIMULATOR = False

# For majordome_test.py:
# cd src/majordome/
# ./majordome_test.py
MAJORDOME_TEST = False

# Set MYSQL to False if you want to use SQLITE
# This line MUST NOT be changed at all except from changing True/False
# (or install_requirements script will become invalid)
MYSQL = True


# Dictionary containing all the versions for the different modules
# IMPORTANT : It must be updated at every commit !
MODULES_VERSIONS = {
    "Alert Manager" : "0.2.4",
    "Analyzer" : "0.1.2",
    "Dashboard" : "0.1.1",
    "Majordome" : "0.1.4",
    "Monitoring" : "0.1.3",
    "Observation Manager" : "0.1.3",
    "Routine Manager" : "0.1.2",
    "Scheduler" : "0.1.2",
    "User Manager" : "0.1.1",
    "Device" : "0.1.1"
}



import os,re,platform

# duplicate from the same function in pyros.py ...
def set_environment_variables_if_not_configured(env_path: str,env_sample_path: str)->None:
    """
    Set environment variables if they aren't defined in the current environment.
    Get the variables from .env file if it exists or create this file using a copy of the env_sample

    Args:
        env_path (str): path to .env file
        env_sample_path (str): path to .env-sample file
    """   
    is_environment_variables_not_defined = os.environ.get("MYSQL_PYROS_LOGIN") == None or os.environ.get("MYSQL_PYROS_PWD") == None or os.environ.get("MYSQL_TCP_PORT") == None
    if(is_environment_variables_not_defined):
        print("Some environment variables are not configured...")
        try:
            with open(env_path,"r") as env_file:
                print("Reading env file")
                for line in env_file:
                    if(line.startswith("#") and not line.strip()):
                        continue
                    else:
                        key,value = line.split("=")
                        # setting variables as environment variables
                        os.environ[key] = value
        except:
            print(f".env not found at {ENV_PATH}, creating a file at this path from the .env-sample file stored at {ENV_SAMPLE_PATH}\n \
                values from .env-sample will be used as environment variables")
            with open(env_sample_path,'r') as env_sample_file:
                with open(env_path,"w") as env_file:
                    for env_sample_line in env_sample_file:
                        if(env_sample_line.startswith("#") or not env_sample_line.strip()):
                            continue
                        key,value = env_sample_line.split("=")
                        os.environ[key] = value
                        env_file.write(env_sample_line)
    else:
        print("The environment variables are already configured, skipping this step...")

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Output folder for images
#TODO: c'est quoi ce dossier ? existe-t-il vraiment ???
OUTPUT_FOLDER = os.path.join(BASE_DIR, "../images_folder")

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '0*@w)$rq4x1c2w!c#gn58*$*u$w=s8uw2zpr_c3nj*u%qlxc23'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

try:
    WITH_DOCKER=os.environ['WITH_DOCKER']
except KeyError:
    WITH_DOCKER = False


if type(WITH_DOCKER) is str and re.match("^y$|^Y$|^yes$|^Yes$",WITH_DOCKER.rstrip()) != None : 
    WITH_DOCKER = True 
else : 
    WITH_DOCKER = False

HTTP_PORT = ""
ENV_PATH="../../../../docker/.env"
ENV_SAMPLE_PATH="../../../../docker/.env-sample"
# default value of mysql port
MYSQL_PORT = "3306"
SQL_USER = ""
SQL_PWD = ""
ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'pyros.irap.omp.eu']
# defining variables when using Docker
if WITH_DOCKER:
    ALLOWED_HOSTS.append('0.0.0.0')
    HTTP_PORT = ":8000"

try:
    MYSQL_PORT = os.environ['MYSQL_TCP_PORT'].strip()
    SQL_USER = os.environ["MYSQL_PYROS_LOGIN"].strip()
    SQL_PWD = os.environ["MYSQL_PYROS_PWD"].strip()
except:
    set_environment_variables_if_not_configured(ENV_PATH,ENV_SAMPLE_PATH)
# TODO : Change ALLOWED_HOSTS depending if you are using docker with windows (the domain is "localhost") or docker with another OS (the domain is "0.0.0.0"). If you are running PyROS without Docker, the domain is "localhost".
DEFAULT_DOMAIN = f'{ALLOWED_HOSTS[0]}{HTTP_PORT}'
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',

    # (EP) For debug_toolbar
    'django.contrib.staticfiles',
    'debug_toolbar',

    # for using "./manage.py graph_models" with graphviz:
    # (https://projects.irap.omp.eu/projects/pyros/wiki/Project_Development#django-extensions-and-graphviz-useful-for-generating-an-image-of-all-the-models-and-their-relationships)
    'django_extensions',
    'test_without_migrations',
    'bootstrap3',

    # PYROS APPS
    'dashboard',
    'scheduler',
    'common',
    'alert_manager',
    'analyzer',
    'majordome',
    'monitoring',
    'observation_manager',
    'routine_manager',
    'user_manager',
    'devices',
    #'kombu.transport.django'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
    # For debug_toolbar
    'debug_toolbar.middleware.DebugToolbarMiddleware',

]

ROOT_URLCONF = 'pyros.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'misc/templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'pyros.wsgi.application'

FIXTURE_DIRS = (
    'misc/fixtures/',
)

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'pyrostnc'
EMAIL_HOST_PASSWORD = 'PyROSTNC7!'
EMAIL_USE_TLS = True
LOGIN_URL = "/"

# DATABASE CONFIG
'''
From MySQL 5.7 onwards and on fresh installs of MySQL 5.6,
the default value of the sql_mode option contains STRICT_TRANS_TABLES.
That option escalates warnings into errors when data are truncated upon insertion,
so Django highly recommends activating a strict mode for MySQL to prevent data loss
(either STRICT_TRANS_TABLES or STRICT_ALL_TABLES)
'''
mysql_options = { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" }

# DEFAULT (NORMAL) RUN MODE, use pyros (normal) database 
if MYSQL:
    DATABASES = {
        'default': {
            'OPTIONS': mysql_options, 
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'pyros',
            'USER': SQL_USER,
            'PASSWORD': SQL_PWD,
            'PORT': MYSQL_PORT,
            ''' 
                (See https://docs.djangoproject.com/fr/2.1/topics/testing/overview/#the-test-database)
                Optional, but this allows to remember the default django test database name 
                (and even to rename it if needed).
                For this DB, you need to do this in mysql :
                GRANT ALL PRIVILEGES ON test_pyros.* TO 'pyros_user'@'localhost';
            '''
            'TEST': {
                'NAME': 'test_pyros',
            },
        }
    }
    if WITH_DOCKER:
        # add host and port to connect to the database
        DATABASES['default']['HOST'] = 'db'
        DATABASES['default']['PORT'] = '3306'
        
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

# SIMULATOR==True ==> 'TEST (simu)' RUN MODE, use pyros_test database
if SIMULATOR:
    DATABASES = {
        'default': {
            'OPTIONS': mysql_options, 
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'pyros_test',
            'USER': 'pyros',
            'PASSWORD': 'DjangoPyros'
        }
    }

if MAJORDOME_TEST: DATABASES['default']['NAME'] = 'pyros_test'


AUTH_USER_MODEL = 'common.PyrosUser'

# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/


LANGUAGE_CODE = 'en-us'
#LANGUAGE_CODE = 'fr-FR'

# UTC = FR - 2h in summer
# UTC = FR - 1h in winter
TIME_ZONE = 'UTC'
#TIME_ZONE = 'Europe/Paris'

# 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

# EP change
# If you set this to False, Django will not use timezone-aware datetimes.
# If true => "updated" fields in weatherwatch or sitewatch will be saved as UTC time
# If false => "updated" fields in weatherwatch or sitewatch will be saved as UTC+1 time (French time)
# (Was) Necessary for "pyros test" (no more necessary now because fixtures dates have been converted to naive format without time zone) :
# Necessary for ENV monitoring :
#USE_TZ = False
USE_TZ = True

# To find the media files {{ MEDIA_URL }}
MEDIA_URL = '/public/static/media/'


# To find the static files in the app/static/app/... folders
STATIC_URL = '/public/static/'
#STATIC_URL = '/static/'

# To find the static files in src/static/. Any local directory can be added to this list.
STATICFILES_DIRS = (
                    os.path.join(BASE_DIR, "misc", "static"),
                    )

# Used for deployment (DEBUG = False). Need to run "python manage.py collectstatic" to fill it.
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'public', 'static')


# EP added
if not DEBUG:
    '''
    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
            'LOCATION': '127.0.0.1:11211',
        }
    }
    '''
    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
            'LOCATION': '/var/tmp/django_cache',
        }
    }

else:
    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        }
    }

# from django.core.cache import cache
# cache.clear()