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