-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.py
183 lines (156 loc) · 5.72 KB
/
utils.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import datetime
import pickle
# extjs special encoder
from django.http import Http404, HttpResponse, HttpResponseRedirect
def set_cookie(response, key, value, days_expire = 7):
if days_expire is None:
max_age = 365*24*60*60 #one year
else:
max_age = days_expire*24*60*60
expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT")
response.set_cookie(key, value, max_age=max_age, expires=expires)
return response
def set_pickle_cookie(response, key, value, days_expire = 7):
if days_expire is None:
max_age = 365*24*60*60 #one year
else:
max_age = days_expire*24*60*60
value = pickle.dumps(value)
expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT")
response.set_cookie(key, value, max_age=max_age, expires=expires)
return response
return pickle.loads(value)
def get_pickle_cookie(request, key):
value = request.COOKIES.get(key)
if value:
try:
value = pickle.loads(value)
except:
print ' * ERROR unpickling cookie %s' % key
value = None
return value
def get_cookie(request, key):
return request.COOKIES.get(key)
def datetimeFromExtDateField(indatestr):
if indatestr.count("T")>0:
(date, time) = indatestr.split("T")
(an, mois, jour) = date.split('-')
(h, m, s) = time.split(':')
return datetime.datetime(int(an), int(mois), int(jour), int(h), int(m), int(s))
elif indatestr.count("/") == '2':
if indatestr.count(' ')>0:
(date, time) = indatestr.split(" ")
(jour, mois, an) = date.split('/')
(h, m, s) = time.split(':')
return datetime.datetime(int(an), int(mois), int(jour), int(h), int(m), int(s))
else:
(jour, mois, an) = date.split('/')
return datetime.date(int(an), int(mois), int(jour))
return None
def DateFormatConverter(to_extjs = None, to_python = None):
""" convert date formats between ext and python """
f = {}
f['a'] = 'D'
f['A'] = 'l'
f['b'] = 'M'
f['B'] = 'F'
#f['c'] =
f['d'] = 'd'
f['H'] = 'H'
f['I'] = 'h'
f['j'] = 'z'
f['m'] = 'm'
f['M'] = 'i'
f['p'] = 'A'
f['S'] = 's'
f['U'] = 'W'
#f['w'] =
f['W'] = 'W'
#f['x'] =
#f['X'] =
f['y'] = 'y'
f['Y'] = 'Y'
f['Z'] = 'T'
out = ''
if to_extjs:
for char in to_extjs.replace('%',''):
out += f.get(char, char)
elif to_python:
for char in to_python:
if char in f.values():
key = [key for key, val in f.items() if f[key] == char][0]
out += '%%%s' % key
else:
out += char
return out
def JsonResponse(contents, status=200):
return HttpResponse(contents, mimetype='text/javascript', status=status)
def JsonSuccess(params = {}):
d = {"success":True}
d.update(params)
return JsonResponse(JSONserialise(d))
def JsonError(error = ''):
return JsonResponse('{"success":false, "msg":%s}' % JSONserialise(error))
def JSONserialise(obj, sep = '"', escapeStrings = True):
import decimal
from django.db import models
if type(obj)==type({}):
return JSONserialise_dict(obj)
elif type(obj)==type(True):
return obj and "true" or "false"
elif type(obj) in [type([]), type((1,2))]:
# if len(obj) > 50:
# print '*********', 'list', len(obj), type(obj)
return "[%s]" % ','.join(map(JSONserialise, obj))
# data = []
# for item in obj:
# data.append(JSONserialise(item))
# return "[%s]" % ",".join(data)
elif type(obj) in [type(0), type(0.0), long, decimal.Decimal]:
return '%s' % obj
elif type(obj) in [datetime.datetime , datetime.date]:
return u'%s%s%s' % (sep, obj, sep)
elif type(obj) in [type(''), type(u'')] or isinstance(obj, models.Model):
#print obj, isinstance(obj, str), isinstance(obj, unicode)
if obj == "False":
return "false"
elif obj == "True":
return "true"
else:
if escapeStrings:
return u'%s%s%s' % (sep, JsonCleanstr(obj), sep)
else:
return u'%s%s%s' % (sep, obj, sep)
elif not obj:
return u'%s%s' % (sep, sep)
else:
print 'JSONserialise unknown type', obj, type(obj), obj.__class__.__name__, isinstance(obj, models.Model)
return u'%s' % obj
return None
def JSONserialise_dict_item(key, value, sep = '"'):
# quote the value except for ExtJs keywords
if key in ['renderer', 'editor', 'hidden', 'sortable', 'sortInfo', 'listeners', 'view', 'failure', 'success','scope', 'fn','store','handler','callback']:
if u'%s' % value in ['True', 'False']:
value = str(value).lower()
else:
# dont escape strings inside these special values (eg; store data)
value = JSONserialise(value, sep='', escapeStrings = False)
return '"%s":%s' % (key, value)
else:
value = JSONserialise(value, sep)
return '"%s":%s' % (key, value)
def JSONserialise_dict(inDict):
data=[]
for key in inDict.keys():
data.append(JSONserialise_dict_item(key, inDict[key]))
data = ",".join(data)
return "{%s}" % data
def JsonCleanstr(inval):
try:
inval = u'%s' % inval
except:
print "ERROR nunicoding %s" % inval
pass
inval = inval.replace('"',r'\"')
inval = inval.replace('\n','\\n').replace('\r','')
return inval