|
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
|
|
|
def show_toolbar(request): |
|
30
|
|
|
""" |
|
31
|
|
|
Default function to determine whether to show the toolbar on a given page. |
|
32
|
|
|
""" |
|
33
|
|
|
|
|
34
|
|
|
if request.is_ajax(): |
|
35
|
|
|
return False |
|
36
|
|
|
|
|
37
|
|
|
return bool(settings.DEBUG) |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def get_client_ip(request): |
|
41
|
|
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') |
|
42
|
|
|
if x_forwarded_for: |
|
43
|
|
|
ip = x_forwarded_for.split(',')[0] |
|
44
|
|
|
else: |
|
45
|
|
|
ip = request.META.get('REMOTE_ADDR') |
|
46
|
|
|
return ip |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def add_extra_to_log_message(msg, extra): |
|
50
|
|
|
return msg + ' '.join(", %s=%s" % (key, val) for (key, val) in sorted(extra.items())) |
|
51
|
|
|
|
|
52
|
|
|
def get_splunk_url(params): |
|
53
|
|
|
SEARCH_TEMPLATE = 'http://%s/en-US/app/search/search?q=search %s' |
|
54
|
|
|
splunk_host = getattr(settings, 'SPLUNK_HOST', None) |
|
55
|
|
|
string_params = ' '.join("%s=%s" % (key, val) for (key, val) in sorted(params.items())) |
|
56
|
|
|
return SEARCH_TEMPLATE % (splunk_host, string_params) if splunk_host else None |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def get_sentry_organization_slug(domain, api_key): # 1 api_key - 1 organization_slug |
|
60
|
|
|
organizations = requests.get( |
|
61
|
|
|
'http://%s/api/0/organizations/' % (domain,), |
|
62
|
|
|
auth=(api_key, '') |
|
63
|
|
|
).json() |
|
64
|
|
|
|
|
65
|
|
|
return organizations[0]['slug'] |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def get_sentry_project_slug(domain, organization, project_id, api_key): |
|
69
|
|
|
projects = requests.get( |
|
70
|
|
|
'http://%s/api/0/organizations/%s/projects/' % (domain, organization), |
|
71
|
|
|
auth=(api_key, '') |
|
72
|
|
|
).json() |
|
73
|
|
|
|
|
74
|
|
|
projects = filter(lambda x: x['id'] == project_id, projects) |
|
75
|
|
|
|
|
76
|
|
|
return projects[0]['slug'] |
|
77
|
|
|
|