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