1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
from functools import wraps |
4
|
|
|
from django.core.files.storage import DefaultStorage |
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
|
|
|
# Link the message and extras into a '|' separated list. |
50
|
|
|
def add_extra_to_log_message(msg, extra): |
51
|
|
|
return msg + '|' + '|'.join("%s=%s" % (key, val) for (key, val) in sorted(extra.items())) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def get_splunk_url(params): |
55
|
|
|
SEARCH_TEMPLATE = 'http://%s/en-US/app/search/search?q=search %s' |
56
|
|
|
FILEBEAT_HOST = getattr(settings, 'FILEBEAT_HOST', None) |
57
|
|
|
string_params = ' '.join("%s=%s" % (key, val) for (key, val) in sorted(params.items())) |
58
|
|
|
return SEARCH_TEMPLATE % (FILEBEAT_HOST, string_params) if FILEBEAT_HOST else None |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def get_sentry_organization_slug(domain, api_key): # 1 api_key - 1 organization_slug |
62
|
|
|
organizations = requests.get( |
63
|
|
|
'http://%s/api/0/organizations/' % (domain,), |
64
|
|
|
auth=(api_key, '') |
65
|
|
|
).json() |
66
|
|
|
|
67
|
|
|
return organizations[0]['slug'] |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
def get_sentry_project_slug(domain, organization, project_id, api_key): |
71
|
|
|
projects = requests.get( |
72
|
|
|
'http://%s/api/0/organizations/%s/projects/' % (domain, organization), |
73
|
|
|
auth=(api_key, '') |
74
|
|
|
).json() |
75
|
|
|
|
76
|
|
|
projects = filter(lambda x: x['id'] == project_id, projects) |
77
|
|
|
|
78
|
|
|
return projects[0]['slug'] |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
class StorageWithSpaces(DefaultStorage): |
82
|
|
|
def get_valid_name(self, name): |
83
|
|
|
return name |
84
|
|
|
|
85
|
|
|
storage_with_spaces_instance = StorageWithSpaces() |
86
|
|
|
|