1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
from functools import wraps |
4
|
|
|
import logging.handlers |
5
|
|
|
|
6
|
|
|
from django.conf import settings |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class is_private(object): |
10
|
|
|
def __init__(self, is_private=True): |
11
|
|
|
self.is_private = is_private |
12
|
|
|
|
13
|
|
|
def __call__(self, test_func): |
14
|
|
|
@wraps(test_func) |
15
|
|
|
def inner(*args, **kwargs): |
16
|
|
|
if not self.is_private and not settings.IS_PRIVATE: |
17
|
|
|
return test_func(*args, **kwargs) |
18
|
|
|
elif not self.is_private and settings.IS_PRIVATE: |
19
|
|
|
return test_func(*args, **kwargs) |
20
|
|
|
elif self.is_private and settings.IS_PRIVATE: |
21
|
|
|
return test_func(*args, **kwargs) |
22
|
|
|
else: |
23
|
|
|
return |
24
|
|
|
|
25
|
|
|
return inner |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def show_toolbar(request): |
29
|
|
|
""" |
30
|
|
|
Default function to determine whether to show the toolbar on a given page. |
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
if request.is_ajax(): |
34
|
|
|
return False |
35
|
|
|
|
36
|
|
|
return bool(settings.DEBUG) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def get_client_ip(request): |
40
|
|
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') |
41
|
|
|
if x_forwarded_for: |
42
|
|
|
ip = x_forwarded_for.split(',')[0] |
43
|
|
|
else: |
44
|
|
|
ip = request.META.get('REMOTE_ADDR') |
45
|
|
|
return ip |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def add_extra_to_log_message(msg, extra): |
49
|
|
|
return msg + ' '.join(", %s=%s" % (key, val) for (key, val) in sorted(extra.items())) |
50
|
|
|
|
51
|
|
|
def get_splunk_url(params): |
52
|
|
|
SEARCH_TEMPLATE = 'http://%s/en-US/app/search/search?q=search %s' |
53
|
|
|
splunk_host = getattr(settings, 'SPLUNK_HOST', None) |
54
|
|
|
string_params = ' '.join("%s=%s" % (key, val) for (key, val) in sorted(params.items())) |
55
|
|
|
return SEARCH_TEMPLATE % (splunk_host, string_params) if splunk_host else None |
56
|
|
|
|
57
|
|
|
class CustomSysLogHandler(logging.handlers.SysLogHandler): |
58
|
|
|
def emit(self, record): |
59
|
|
|
msg = self.format(record) + '\000' |
60
|
|
|
if type(msg) is unicode: |
61
|
|
|
msg = msg.encode('utf-8') |
62
|
|
|
try: |
63
|
|
|
self.socket.sendto(msg, self.address) |
64
|
|
|
except (KeyboardInterrupt, SystemExit): |
65
|
|
|
raise |
66
|
|
|
except: |
67
|
|
|
self.handleError(record) |
68
|
|
|
|