Completed
Push — master ( 119e95...483e9e )
by Andrew
38s
created

InvalidString   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
wmc 1
1
"""
2
Django settings for myproject project.
3
4
For more information on this file, see
5
https://docs.djangoproject.com/en/1.6/topics/settings/
6
7
For the full list of settings and their values, see
8
https://docs.djangoproject.com/en/1.6/ref/settings/
9
"""
10
11
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
12
import logging.config
13
import os
14
import sys
15
from os.path import join
16
17
from django.conf import global_settings
18
import sslserver
19
20
import chat as project_module
21
22
try:
23
	from chat.production import *
24
	print('imported production.py settings')
25
except ImportError as e:
26
	print('Failed to import production.py because {}'.format(e))
27
	pass
28
29
LOGGING_CONFIG = None
30
31
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
32
33
34
# Quick-start development settings - unsuitable for production
35
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
36
37
# SECURITY WARNING: keep the secret key used in production secret!
38
SECRET_KEY = '8ou!cqb1yd)6c4h0i-cxjo&@@+04%4np6od8qn+z@5b=6)!v(o'
39
# remove this
40
41
# SECURITY WARNING: don't run with debug turned on in production!
42
43
44
DEBUG = True
45
DEBUG = False
46
TEMPLATE_DEBUG = False
47
ALLOWED_HOSTS = ["*",]
48
49
username_processor = 'chat.context_processors.add_user_name'
50
51
# Application definition
52
53
INSTALLED_APPS = (
54
	'django.contrib.admin',
55
	'django.contrib.auth',
56
	'django.contrib.contenttypes',
57
	'django.contrib.sessions',
58
	'django.contrib.messages',
59
	'django.contrib.staticfiles',
60
	'django.db.migrations',
61
	'chat',
62
	'simplejson',
63
	'redis',
64
	'tornado',
65
	"sslserver"
66
)
67
68
# TODO replace this into your keys if you want this features to be available
69
# Google recaptcha keys
70
RECAPTHCA_SITE_URL = 'https://www.google.com/recaptcha/api.js'
71
# RECAPTCHA_SECRET_KEY = 'REPLACE_THIS_WITH_KEY_FOR_RETRIEVING_RESULT'
72
# RECAPTCHA_SITE_KEY = 'REPLACE_THIS_WITH_DATA-SITEKEY_DIV_ATTRIBUTE'
73
# GOOGLE_OAUTH_2_CLIENT_ID = 'YOUR_CLIENT_ID.apps.googleusercontent.com'
74
GOOGLE_OAUTH_2_JS_URL = 'https://apis.google.com/js/platform.js'
75
FACEBOOK_JS_URL = '//connect.facebook.net/en_US/sdk.js'
76
#FACEBOOK_ACCESS_TOKEN = '!6_NUMBER_APP_ID|ALPHABET_TOKEN' # https://developers.facebook.com/tools/access_token/
77
#FACEBOOK_APP_ID = '16_NUMBER_APP_ID' # https://developers.facebook.com/apps/
78
# GOOGLE_OAUTH_2_HOST = 'pychat.org'
79
80
REDIS_PORT = int(os.environ.get('REDIS_PORT', '6379'))
81
REDIS_HOST = os.environ.get('REDIS_HOST', 'db')
82
SESSION_REDIS = {
83
	'host': REDIS_HOST,
84
	'post': REDIS_PORT,
85
	'db': 3
86
}
87
SESSION_ENGINE = 'redis_sessions.session'
88
89
90
# BROKER_URL = str(SESSION_REDIS_PORT).join(('redis://localhost:','/0'))
91
# CELERY_ACCEPT_CONTENT = ['json']
92
# CELERY_TASK_SERIALIZER = 'json'
93
# CELERY_RESULT_SERIALIZER = 'json'
94
95
CRT_PATH = os.sep.join((sslserver.__path__[0], "certs", "development.crt"))
96
KEY_PATH = os.sep.join((sslserver.__path__[0], "certs", "development.key"))
97
98
IS_HTTPS = 'CRT_PATH' in locals()
99
API_PORT = '8888'
100
CRT_PATH = '/etc/nginx/ssl/1_pychat.org_bundle.crt'
101
KEY_PATH = '/etc/nginx/ssl/server.key'
102
IS_HTTPS = 'CRT_PATH' in locals()
103
EXTENSION_ID = 'cnlplcfdldebgdlcmpkafcialnbopedn'
104
EXTENSION_INSTALL_URL = 'https://chrome.google.com/webstore/detail/pychat-screensharing-exte/' + EXTENSION_ID
105
WEBSOCKET_PROTOCOL = 'wss' if IS_HTTPS else 'ws'
106
SITE_PROTOCOL = 'https' if IS_HTTPS else 'http'
107
API_ADDRESS_PATTERN = ''.join((WEBSOCKET_PROTOCOL, '://%s:', API_PORT, '/?id='))
108
109
# GIPHY_API_KEY = 'thZMTtDfFdugqPDIAY461GzYTctuYIeIj' // TODO paste your GIPHY api key from https://developers.giphy.com/
110
GIPHY_URL= 'http://api.giphy.com/v1/gifs/search?api_key={}&limit=1&q={}'
111
GIPHY_REGEX = r"^\s*\/giphy (.+)"
112
# SESSION_COOKIE_AGE = 10
113
# SESSION_SAVE_EVERY_REQUEST = True
114
# SESSION_EXPIRE_AT_BROWSER_CLOSE = True
115
116
MIDDLEWARE_CLASSES = (
117
	'django.middleware.csrf.CsrfViewMiddleware',
118
	'django.contrib.sessions.middleware.SessionMiddleware',
119
	'django.middleware.common.CommonMiddleware',
120
	'django.contrib.auth.middleware.AuthenticationMiddleware',
121
	'django.contrib.messages.middleware.MessageMiddleware',
122
	'chat.cookies_middleware.UserCookieMiddleWare',
123
)
124
125
ROOT_URLCONF = 'chat.urls'
126
127
WSGI_APPLICATION = 'chat.wsgi.application'
128
129
AUTH_USER_MODEL = 'chat.User'
130
AUTHENTICATION_BACKENDS = ['chat.utils.EmailOrUsernameModelBackend']
131
132
LOGIN_URL = '/'
133
FIREBASE_URL = 'https://android.googleapis.com/gcm/send'
134
135
# Database
136
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
137
# pip install PyMySQL
138
# import pymysql
139
# pymysql.install_as_MySQLdb()
140
141
DATABASES = {
142
	'default': {
143
		'ENGINE': 'django.db.backends.mysql',  # django.db.backends.sqlite3
144
		'NAME': os.environ.get('MYSQL_DATABASE', 'pychat'),
145
		'USER': os.environ.get('MYSQL_USER', 'root'),
146
		'PASSWORD': os.environ.get('MYSQL_PASSWORD', ''),
147
		'HOST': os.environ.get('MYSQL_HOST', 'localhost'),
148
		'PORT': os.environ.get('MYSQL_PORT', '3306')  # mysql uses socket if host is localhost
149
		'default-character-set': 'utf8',
150
		'OPTIONS': {
151
			'autocommit': True,
152
153
		},
154
	}
155
}
156
#
157
# DATABASES = {
158
# 	'default': {
159
# 		'ENGINE': 'django.db.backends.sqlite3',
160
# 		'NAME': 'django.db',
161
# 	}
162
# }
163
164
CACHES = {
165
	'default': {
166
		'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
167
	}
168
}
169
170
# Internationalization
171
# https://docs.djangoproject.com/en/1.6/topics/i18n/
172
173
LANGUAGE_CODE = 'en-us'
174
175
TIME_ZONE = 'Europe/Kiev'
176
177
USE_I18N = True
178
179
USE_L10N = True
180
181
USE_TZ = True
182
183
DEFAULT_CHARSET = 'utf-8'
184
185
handler404 = 'chat.views.handler404'
186
187
# Static files (CSS, JavaScript, Images)
188
# https://docs.djangoproject.com/en/1.6/howto/static-files/
189
STATIC_URL = '/static/'
190
STATIC_URL = 'https://static.pychat.org/'
191
PROJECT_DIR = os.path.dirname(os.path.realpath(project_module.__file__))
192
193
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
194
SMILEYS_ROOT = os.path.join(STATIC_ROOT, 'smileys')
195
196
AUTH_PROFILE_MODULE = 'chat.UserProfile'
197
198
if 'start_tornado' in sys.argv:
199
	try:
200
		index_port = sys.argv.index('--port')
201
		port = sys.argv[index_port + 1]
202
	except (ValueError, IndexError):
203
		port = API_PORT
204
	log_file_name = 'tornado-{}.log'.format(port)
205
else:
206
	log_file_name = 'chat.log'
207
208
if DEBUG:
209
	class InvalidString(str):
210
		def __mod__(self, other):
211
			from django.template.base import TemplateSyntaxError
212
			raise TemplateSyntaxError(
213
				"Undefined variable or unknown value for: %s" % other)
214
	TEMPLATE_STRING_IF_INVALID = InvalidString("%s")
215
216
TEMPLATES = [{
217
	'BACKEND': 'django.template.backends.django.DjangoTemplates',
218
	'DIRS': [join(BASE_DIR, 'templates')],
219
	'OPTIONS': {
220
		 'loaders': [ # TORO remove in debug
221
		 	('django.template.loaders.cached.Loader', [
222
		 		'django.template.loaders.filesystem.Loader',
223
		 		'django.template.loaders.app_directories.Loader',
224
		 	])],
225
		'context_processors': global_settings.TEMPLATE_CONTEXT_PROCESSORS + [username_processor]
226
	}
227
}]
228
229
LOGGING = {
230
	'version': 1,
231
	'disable_existing_loggers': True,
232
	'filters': {
233
		'id': {
234
			'()': 'chat.log_filters.ContextFilter',
235
		}
236
	},
237
	'handlers': {
238
		'file-tornado': {
239
			'level': 'DEBUG',
240
			'class': 'logging.handlers.TimedRotatingFileHandler',
241
			'filename': join(BASE_DIR, 'log/', log_file_name),
242
			'formatter': 'django',
243
			'when': 'midnight',
244
			'interval': 1
245
		},
246
		'file': {
247
			'level': 'DEBUG',
248
			'class': 'logging.handlers.TimedRotatingFileHandler',
249
			'filename': join(BASE_DIR, 'log/', log_file_name),
250
			'formatter': 'django',
251
			'filters': ['id', ],
252
			'when': 'midnight',
253
			'interval': 1
254
		},
255
		'django-console': {
256
			'level': 'DEBUG',
257
			'class': 'logging.StreamHandler',
258
			'formatter': 'django',
259
			'filters': ['id', ]
260
		},
261
		'tornado-console': {
262
			'level': 'DEBUG',
263
			'class': 'logging.StreamHandler',
264
			'formatter': 'django',
265
		},
266
	},
267
	'loggers': {
268
		# root logger
269
		'': {
270
			'handlers': ['django-console'],
271
			'level': 'DEBUG',
272
			'propagate': False,
273
		},
274
		'chat.tornado': {
275
			'handlers': ['tornado-console'],
276
			'level': 'DEBUG',
277
			'propagate': False,
278
		},
279
	},
280
	'formatters': {
281
		'django': {
282
			'format': '%(id)s [%(asctime)s:%(msecs)03d;%(ip)s;%(module)s:%(lineno)s]: %(message)s',
283
			'datefmt': '%H:%M:%S',
284
		},
285
	},
286
}
287
288
WS_ID_CHAR_LENGTH = 4
289
290
291
logging.config.dictConfig(LOGGING)
292
293
DEFAULT_PROFILE_ID = 1
294
# for gmail or google apps
295
EMAIL_USE_TLS = True
296
EMAIL_HOST = 'smtp.gmail.com'
297
EMAIL_PORT = 587
298
EMAIL_HOST_USER = '[email protected]'
299
EMAIL_HOST_PASSWORD = 'Ilovepython'
300
301
ISSUES_REPORT_LINK = 'https://github.com/Deathangel908/djangochat/issues/new'
302
303
SESSION_COOKIE_NAME = "sessionid"
304
305
MEDIA_ROOT = os.path.join(BASE_DIR, 'photos')
306
307
MEDIA_URL = "/photo/"
308
MEDIA_URL = "https://static.pychat.org/photo/"
309
USER_COOKIE_NAME = 'user'
310
JS_CONSOLE_LOGS = True
311
312
EMAIL_USE_TLS = True
313
EMAIL_HOST = 'localhost'
314
EMAIL_PORT = 25
315
EMAIL_HOST_USER = ''
316
EMAIL_HOST_PASSWORD = ''
317
#DEFAULT_FROM_EMAIL = 'root <[email protected]>'
318
SERVER_EMAIL = '[email protected]'
319
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
320
ADMINS = [('Andrew', '[email protected]'), ]
321
322
# If this options is set, on every oncoming request chat will gather info about user location
323
324
IP_API_URL = 'http://ip-api.com/json/%s'
325
326
ALL_REDIS_ROOM = 'all'
327
WEBRTC_CONNECTION = 'webrtc_conn'
328
ALL_ROOM_ID = 1
329
330
SELECT_SELF_ROOM = """SELECT
331
	a.id as room__id,
332
	a.disabled as room__disabled
333
FROM chat_room a
334
WHERE a.id IN %s AND
335
			EXISTS
336
			(
337
					SELECT 1
338
					FROM chat_room_users b
339
					WHERE a.id = b.room_id
340
					HAVING COUNT(b.user_id) = 1
341
			)"""
342
343
UPDATE_LAST_READ_MESSAGE = """
344
UPDATE chat_room_users out_cru
345
	INNER JOIN
346
		(SELECT
347
			max(chat_message.id) message_id,
348
			chat_room_users.id rooms_users_id
349
		 FROM chat_room_users
350
			JOIN chat_message ON chat_message.room_id = chat_room_users.room_id
351
		WHERE chat_room_users.user_id = %s and chat_room_users.room_id != {}
352
		GROUP BY chat_message.room_id) last_message ON out_cru.id = last_message.rooms_users_id
353
SET out_cru.last_read_message_id = last_message.message_id 
354
""".format(ALL_ROOM_ID)
355
356
# ---------------JAVASCRIPT CONSTANTS --------------------
357
358
VALIDATION_IS_OK = 'ok'
359
MAX_USERNAME_LENGTH = 16
360
MAX_MESSAGE_SIZE = 100000
361
GENDERS = {0: 'Secret', 1: 'Male', 2: 'Female', }
362
#
363
DATE_INPUT_FORMATS = ('%Y-%m-%d',)  # html5 input date default format, see also Pikaday in js
364
DATE_INPUT_FORMATS_JS = 'YYYY-MM-DD'  # html5 input date default format, see also Pikaday in js, TODO webrtc.js
365
USE_L10N = False  # use DATE_INPUT_FORMATS as main format for date rendering
366