settings.py 10.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 SIMULATOR (and TESTS)
CELERY_TEST = False

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

# (EP) Remove this the day we succeed to use no more of Celery...
# Set this to False if you want to run pyros without using Celery (and RabbitMQ)
USE_CELERY = False
#USE_CELERY = True

# 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

# 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
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

ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'pyros.irap.omp.eu']


# Application definition

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

    # 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',
    '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',
]

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 = 'colibricontrolcenter'
EMAIL_HOST_PASSWORD = '!ColibriCC31!'
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': 'pyros',
            'PASSWORD': 'DjangoPyros',
        }
    }

else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

# CELERY_TEST==True ==> 'TEST' RUN MODE, use pyros_test database
if CELERY_TEST:
    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
#if USE_CELERY: USE_TZ = True

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


# To find the static files in the app/static/ap/... folders
STATIC_URL = '/public/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()

# CELERY CONFIG

CELERY_RESULT_BACKEND = 'amqp'
#CELERY_RESULT_BACKEND = 'rpc://'
#CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',

# (EP) TODO: This should also be added (according to https://simpleisbetterthancomplex.com/tutorial/2017/08/20/how-to-use-celery-with-django.html)
#CELERY_BROKER_URL = 'amqp://localhost'
#CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672/'
#CELERY_BROKER_URL = 'django://'

CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

# EP TODO: no more necessary, to be removed
CELERY_IMPORTS = (
    "alert_manager.tasks",
    "analyzer.tasks",
    "majordome.tasks",
    "monitoring.tasks",
    "observation_manager.tasks",
    "scheduler.tasks",
)
    # "userSimulator.tasks",

# This config allows only 1 process / queue. We replace it by the -c option at celery workers creation.
# CELERYD_CONCURRENCY = 1

''' Following config is needed for manual purge '''
CELERY_ACKS_LATE = False
CELERYD_PREFETCH_MULTIPLIER = 1

CELERY_QUEUES = {
    "alert_listener_q": {"exchange": "alert_listener_q", "routing_key": "alert_listener_q"},
    "monitoring_q": {"exchange": "monitoring_q", "routing_key": "monitoring_q"},
    "majordome_q": {"exchange": "majordome_q", "routing_key": "majordome_q"},
    "analysis_q": {"exchange": "analysis_q", "routing_key": "analysis_q"},
    "scheduling_q": {"exchange": "scheduling_q", "routing_key": "scheduling_q"},
    "create_calibrations_q": {"exchange": "create_calibrations_q", "routing_key": "create_calibrations_q"},
    "execute_plan_vis_q": {"exchange": "execute_plan_vis_q", "routing_key": "execute_plan_vis_q"},
    "execute_plan_nir_q": {"exchange": "execute_plan_nir_q", "routing_key": "execute_plan_nir_q"},
    "night_calibrations_q": {"exchange": "night_calibrations_q", "routing_key": "night_calibrations_q"},
}

CELERY_ROUTES = {
    "alert_manager.tasks.AlertListener": {"queue": "alert_listener_q"},
    "majordome.tasks.Majordome": {"queue": "majordome_q"},
    "monitoring.tasks.Monitoring": {"queue": "monitoring_q"},
    "analyzer.tasks.analysis": {"queue": "analysis_q"},
    "observation_manager.tasks.execute_plan_vis": {"queue": "execute_plan_vis_q"},
    "observation_manager.tasks.execute_plan_nir": {"queue": "execute_plan_nir_q"},
    "observation_manager.tasks.create_calibrations": {"queue": "create_calibrations_q"},
    "observation_manager.tasks.night_calibrations": {"queue": "night_calibrations_q"},
    "scheduler.tasks.scheduling": {"queue": "scheduling_q"}
}
    # "userSimulator.tasks.simulator": {"queue": "simulator_q"},

''' Removes pickle warning '''
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']