-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest.py
145 lines (112 loc) · 4.05 KB
/
request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import djucsvlog.settings as my_settings
from djucsvlog.fields.tools import json_dump_line, readable_dict, readable_list
def path(request):
return request.path
def language_code(request):
return getattr(request,'LANGUAGE_CODE','')
def get(request):
return readable_dict(request.GET)
def post(request):
return readable_dict(request.POST)
def files(request):
if not request.FILES:
return '{}'
ret = ''
for field,file in request.FILES.items():
ret += '\n%s:%s,' %(json_dump_line(field),readable_list([file.name,file.content_type,file.size]))
return '{\n'+ret[:-1]+'\n}'
def cookies(request):
return readable_dict(request.COOKIES)
def request_form_data(request):
ret = ''
r_get = get(request)
if r_get != '{}':
ret+='\n\'G\':'+r_get+','
r_post = post(request)
if r_post != '{}':
ret+='\n\'P\':'+r_post+','
r_files = files(request)
if r_files != '{}':
ret+='\n\'F\':'+r_files+','
if not ret:
return '{}'
return '{'+ret[:-1]+'\n}'
def request_data(request):
ret = request_form_data(request)
r_cookies = cookies(request)
if r_cookies == '{}':
return ret
if ret == '{}':
return '{\'C\':'+r_cookies+'}'
return ret[:-1] + ',\'C\':'+r_cookies+'}'
def sessionid(request):
if hasattr(request, 'session'):
return request.session._session_key
return ''
def browser_uuid(request):
from djucsvlog.glog import glog
import uuid
browser_uuid = request.COOKIES.get(my_settings.BROWSER_UUID_COOKIE)
if not browser_uuid:
browser_uuid = uuid.uuid4().get_hex()
glog.browser_uuid = browser_uuid
return glog.browser_uuid
def remote_addr(request):
return request.META.get(my_settings.REQ_REMOTE_ADDR_REAL_IP,request.META.get('REMOTE_ADDR', my_settings.REQ_REMOTE_ADDR_ANONYMOUSE))
def http_host(request):
return request.META.get('HTTP_HOST',my_settings.REQ_HTTP_HOST_NOHOST)
def http_user_agent(request):
meta = request.META
ret = meta.get('HTTP_USER_AGENT','NO USER AGENT')
if 'HTTP_ACCEPT_LANGUAGE' in meta:
ret+= ' Accept Language:'+meta['HTTP_ACCEPT_LANGUAGE']
if 'HTTP_ACCEPT_ENCODING' in meta:
ret += ' Accept Encoding:'+meta['HTTP_ACCEPT_ENCODING']
return ret
def read_in_chunks(file_object, chunk_size=1024):
"""Lazy function (generator) to read a file piece by piece.
Default chunk size: 1k.
Stolen from http://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python
"""
while True:
data = file_object.read(chunk_size)
if not data:
file_object.seek(0)
break
yield data
import os
def find_place_to_store(name):
from random import randint
while True:
full_path = os.path.join(my_settings.REQ_SAVE_FILES_FOLDER,name)
if not os.path.exists(full_path):
return full_path
point_cunks = name.split('.')
if len(point_cunks) == 1:
#without exstension
name += str(randint(0,9))
else:
#with extenstion add randint after name and before extension
name = '%s%s.%s' % ('.'.join(point_cunks[:-1]), randint(0,9),point_cunks[-1])
def save_files(request):
if not my_settings.REQ_SAVE_FILES_FOLDER:
return
if not request.FILES:
return '{}'
ret = ''
for field,file in request.FILES.items():
store_filename = find_place_to_store(file.name)
ret += '\n%s:%s,' %(json_dump_line(field),readable_list([file.name,store_filename,file.content_type,file.size]))
fh = open(store_filename,'wb')
for piece in read_in_chunks(file.file):
fh.write(piece)
fh.close()
return '{\n'+ret+'\n}'
def http_referer(request):
return request.META.get('HTTP_REFERER','')
def http_accept_language(request):
return request.META.get('HTTP_ACCEPT_LANGUAGE','')
def is_ajax(request):
return int(request.is_ajax())
def is_secure(request):
return int(request.is_secure())