-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
149 lines (133 loc) · 4.95 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
from threading import Thread
from bson.objectid import ObjectId
from django.db.models.base import ModelBase
from django.http import HttpResponse
from django.utils import simplejson
from django.template.loader import get_template
from django.template import Context
from django.utils.encoding import force_unicode
import datetime
def async(gen):
def func(*args, **kwargs):
it = gen(*args, **kwargs)
result = it.next()
Thread(target=lambda: list(it)).start()
return result
return func
def get_pretty_date(time=False):
"""
Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc
"""
now = datetime.datetime.now()
if type(time) is int:
diff = now - datetime.datetime.fromtimestamp(time)
elif type(time) is float:
diff = now - datetime.datetime.fromtimestamp(int(time))
elif isinstance(time,datetime.datetime):
diff = now - time
elif not time:
diff = now - now
second_diff = diff.seconds
day_diff = diff.days
if day_diff < 0:
return ''
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds ago"
if second_diff < 120:
return "a minute ago"
if second_diff < 3600:
return str( second_diff / 60 ) + " minutes ago"
if second_diff < 7200:
return "an hour ago"
if second_diff < 86400:
return str( second_diff / 3600 ) + " hours ago"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
return str(day_diff) + " days ago"
if day_diff < 31:
return str(day_diff/7) + " weeks ago"
if day_diff < 365:
return str(day_diff/30) + " months ago"
return str(day_diff/365) + " years ago"
################################################################################
# DYNAMIC MODULE LOADED FUNCTIONS #
################################################################################
def my_import(name):
mod = __import__(name)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
################################################################################
# JSON Response: http://chronosbox.org/blog/jsonresponse-in-django?lang=en #
################################################################################
class LazyJSONEncoder(simplejson.JSONEncoder):
""" a JSONEncoder subclass that handle querysets and models objects. Add
your code about how to handle your type of object here to use when dumping
json """
def default(self,o):
# this handles querysets and other iterable types
try:
iterable = iter(o)
except TypeError:
pass
else:
return list(iterable)
# this handlers Models
try:
isinstance(o.__class__,ModelBase)
except Exception:
pass
else:
return force_unicode(o)
return super(LazyJSONEncoder,self).default(o)
def serialize_to_json(obj,*args,**kwargs):
""" A wrapper for simplejson.dumps with defaults as:
ensure_ascii=False
cls=LazyJSONEncoder
All arguments can be added via kwargs
"""
kwargs['ensure_ascii'] = kwargs.get('ensure_ascii',False)
kwargs['cls'] = kwargs.get('cls',LazyJSONEncoder)
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
def handler(obj):
if isinstance(obj, datetime.datetime): return obj.isoformat()
if isinstance(obj, ObjectId): return '%s' % obj
return None
return simplejson.dumps(obj, default=handler, *args,**kwargs)
class JSONResponse(HttpResponse):
""" JSON response class """
def __init__(self,content='',json_opts={},mimetype="application/json",
*args,**kwargs):
"""
This returns a object that we send as json content using
utils.serialize_to_json, that is a wrapper to simplejson.dumps
method using a custom class to handle models and querysets. Put your
options to serialize_to_json in json_opts, other options are used by
response.
"""
if content:
content = serialize_to_json(content,**json_opts)
else:
content = serialize_to_json([],**json_opts)
super(JSONResponse,self).__init__(content,mimetype,*args,**kwargs)
def format_template_and_json_response(template, context, data):
template = get_template(template)
html = template.render(Context(context))
try:
data = data.__dict__
except AttributeError:
data = data
return_data = { "template":html, "data": data }
return JSONResponse(return_data)
def empty(str):
"""
Returns true if the given string is empty.
"""
return not str or not str.strip()