code refactor for genericparser
[modeling/etsicatalog.git] / genericparser / settings.py
1 # Copyright 2017 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import os
16 import sys
17 import platform
18
19 import redisco
20
21 from genericparser.pub.config.config import REDIS_HOST, REDIS_PORT, REDIS_PASSWD
22 from genericparser.pub.config.config import DB_NAME, DB_IP, DB_USER, DB_PASSWD, DB_PORT
23 from genericparser.pub.config import config as pub_config
24 from logging import config as log_config
25 from onaplogging import monkey
26
27 monkey.patch_all()
28
29 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
30 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
31
32 # Quick-start development settings - unsuitable for production
33 # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
34
35 # SECURITY WARNING: keep the secret key used in production secret!
36 SECRET_KEY = '3o-wney!99y)^h3v)0$j16l9=fdjxcb+a8g+q3tfbahcnu2b0o'
37
38 # SECURITY WARNING: don't run with debug turned on in production!
39 DEBUG = True
40
41 ALLOWED_HOSTS = ['*']
42
43 # Application definition
44
45 INSTALLED_APPS = [
46     'django.contrib.auth',
47     'django.contrib.contenttypes',
48     'django.contrib.sessions',
49     'django.contrib.messages',
50     'django.contrib.staticfiles',
51     'django.contrib.admin',
52     'rest_framework',
53     'genericparser.pub.database',
54     'genericparser.samples',
55     'genericparser.swagger',
56     'drf_yasg',
57 ]
58
59 # drf-yasg
60 SWAGGER_SETTINGS = {
61     'LOGIN_URL': '/admin/login',
62     'LOGOUT_URL': '/admin/logout',
63     'DEFAULT_INFO': 'genericparser.swagger.urls.swagger_info'
64 }
65
66 TEMPLATES = [
67     {
68         'BACKEND': 'django.template.backends.django.DjangoTemplates',
69         'DIRS': [],
70         'APP_DIRS': True,
71         'OPTIONS': {
72             'context_processors': [
73                 'django.template.context_processors.debug',
74                 'django.template.context_processors.request',
75                 'django.contrib.auth.context_processors.auth',
76                 'django.contrib.messages.context_processors.messages',
77             ],
78         },
79     },
80 ]
81
82 MIDDLEWARE_CLASSES = [
83     'django.middleware.security.SecurityMiddleware',
84     'django.contrib.sessions.middleware.SessionMiddleware',
85     'django.middleware.common.CommonMiddleware',
86     'django.middleware.csrf.CsrfViewMiddleware',
87     'django.contrib.auth.middleware.AuthenticationMiddleware',
88     'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
89     'django.contrib.messages.middleware.MessageMiddleware',
90     'django.middleware.clickjacking.XFrameOptionsMiddleware',
91     'genericparser.middleware.LogContextMiddleware',
92 ]
93
94 ROOT_URLCONF = 'genericparser.urls'
95
96 WSGI_APPLICATION = 'genericparser.wsgi.application'
97
98 REST_FRAMEWORK = {
99     'DEFAULT_RENDERER_CLASSES': (
100         'rest_framework.renderers.JSONRenderer',
101     ),
102
103     'DEFAULT_PARSER_CLASSES': (
104         'rest_framework.parsers.JSONParser',
105         'rest_framework.parsers.MultiPartParser',
106     )
107 }
108
109 DATABASES = {
110     'default': {
111         'ENGINE': 'django.db.backends.mysql',
112         'NAME': DB_NAME,
113         'HOST': DB_IP,
114         'PORT': DB_PORT,
115         'USER': DB_USER,
116         'PASSWORD': DB_PASSWD,
117     },
118 }
119
120 redisco.connection_setup(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWD, db=0)
121 # CACHE_BACKEND = 'redis_cache.cache://%s@%s:%s' % (REDIS_PASSWD, REDIS_HOST, REDIS_PORT)
122
123 TIME_ZONE = 'UTC'
124
125 # Static files (CSS, JavaScript, Images)
126 # https://docs.djangoproject.com/en/1.6/howto/static-files/
127
128 STATIC_URL = '/static/'
129
130 STATICFILES_DIRS = [
131     os.path.join(BASE_DIR, "static")
132 ]
133 # change
134 pub_config.GENERICPARSER_ROOT_PATH = os.path.join(STATICFILES_DIRS[0], "genericparser")
135 pub_config.GENERICPARSER_URL_PATH = "static/genericparser"
136 pub_config.SDC_BASE_URL = "http://%s:%s/api" % (pub_config.MSB_SERVICE_IP, pub_config.MSB_SERVICE_PORT)
137
138 if platform.system() == 'Windows' or 'test' in sys.argv:
139     LOGGING = {
140         'version': 1,
141         'disable_existing_loggers': True,
142         'formatters': {
143             'standard': {
144                 'format': '%(asctime)s:[%(name)s]:[%(filename)s]-[%(lineno)d] [%(levelname)s]:%(message)s',
145             },
146         },
147         'filters': {
148         },
149         # change
150         'handlers': {
151             'genericparser_handler': {
152                 'level': 'DEBUG',
153                 'class': 'logging.handlers.RotatingFileHandler',
154                 'filename': os.path.join(BASE_DIR, 'logs/runtime_genericparser.log'),
155                 'formatter': 'standard',
156                 'maxBytes': 1024 * 1024 * 50,
157                 'backupCount': 5,
158             },
159         },
160
161         'loggers': {
162             # change
163             'genericparser': {
164                 'handlers': ['genericparser_handler'],
165                 'level': 'DEBUG',
166                 'propagate': False
167             },
168         }
169     }
170 else:
171     LOGGING_CONFIG = None
172     # yaml configuration of logging
173     LOGGING_FILE = os.path.join(BASE_DIR, 'genericparser/log.yml')
174     log_config.yamlConfig(filepath=LOGGING_FILE, watchDog=True)
175
176 if 'test' in sys.argv:
177     pub_config.REG_TO_MSB_WHEN_START = False
178
179     DATABASES = {}
180     DATABASES['default'] = {
181         'ENGINE': 'django.db.backends.sqlite3',
182         'NAME': ':memory:',
183     }
184     REST_FRAMEWORK = {}
185
186     if platform.system() == 'Linux':
187         TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'
188         TEST_OUTPUT_VERBOSE = True
189         TEST_OUTPUT_DESCRIPTIONS = True
190         TEST_OUTPUT_DIR = 'test-reports'
191
192     import mock
193     from genericparser.pub.utils import idutil
194
195     idutil.get_auto_id = mock.Mock()
196     idutil.get_auto_id.return_value = 1