-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackerutils.py
executable file
·189 lines (163 loc) · 5.71 KB
/
trackerutils.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
184
185
186
187
188
189
import datetime, tempfile, os, json
from django.shortcuts import HttpResponseRedirect, HttpResponse
from django.db import models
from django.conf import settings
SPAN_CHOICES=(('m','month'),('w','week'),('y','year'))
span2days={'w':7,'m':30,'y':365}
from django.template import RequestContext
import logging
log=logging.getLogger(__name__)
def monthago():
return datetime.datetime.now()-datetime.timedelta(days=30)
class DayModel(models.Model):
#should add default modified & created here.
def clink(self, text=None,wrap=True,skip_btn=False,klasses=None, tooltip=None):
if skip_btn:
klass=''
else:
klass='btn btn-default'
if klasses:
klass+=' '.join(klasses)
if wrap:
wrap=''
else:
wrap=' nb'
if not text:
text=self
if not tooltip:
tooltip=''
return u'<a class="%s%s" title="%s" href="%s/day/%s/?id=%d">%s</a>'%(klass, tooltip, wrap, settings.ADMIN_EXTERNAL_BASE, self.__class__.__name__.lower(), self.id, text)
def alink(self, text=None,wrap=True):
if wrap:
wrap=' nb'
else:
wrap=''
if not text:
text=self
return u'<a class="btn btn-default" href="%s/day/%s/%d/">%s</a>'%(wrap,settings.ADMIN_EXTERNAL_BASE, self.__class__.__name__.lower(), self.id, text)
class Meta:
app_label='day'
abstract=True
def gethour(hour=None):
if not hour:
hour=datetime.datetime.now().hour
if hour<2:
res= 'midnight'
elif hour<5:
res='deep night'
elif hour<7:
res='early morning'
elif hour<11:
res='morning'
elif hour<14:
res='noon'
elif hour<=20:
res='evening'
elif hour<=23:
res='night'
else:
res='midnight'
#log.info('hour %d res %s',hour, res)
return res
HOUR_CHOICES=zip([8,1,2,3,4,5], 'morning noon afternoon evening night midnight'.split())
HOUR_CHOICES.append((6, 'deep night'))
HOUR_CHOICES.append((7, 'early morning'))
hour2name={}
name2hour={}
for a in HOUR_CHOICES:
name2hour[a[1]]=a[0]
hour2name[a[0]]=a[1]
def r2r(template, request, context=None, lang=None):
from django.conf import settings
if not context:
context={}
from coffin.shortcuts import render_to_response
context['request'] = request
context['is_auth'] = request.user.is_authenticated()
return render_to_response(template, context, RequestContext(request))
def r2s(template, context=None):
from coffin.shortcuts import render_to_string
#fake=FakeRequestContext(None, dict_=context)
from django.conf import settings
return render_to_string(template, dictionary=context)
def r2j(vals):
return HttpResponse(json.dumps(vals), mimetype='text/html')
def debu(func, *args, **kwgs):
def inner(*args, **kwgs):
try:
return func(*args, **kwgs)
except Exception, e:
log.error('exception %s',e)
if settings.LOCAL:
import ipdb;ipdb.set_trace()
else:
return 'Error <contact Ernie>'
return func(*args, **kwgs)
return None
inner.__doc__=func.__doc__
inner.__name__=func.__name__
return inner
def purchase2obj(p):
return {'id':p.id,
'name':p.product.name,
'quantity':p.quantity,
'cur_symbol': p.currency.symbol,
'size':p.size,
'cost':p.cost, #ok not to use get_cost here because it includes currency.
'source':source2obj(p.source),
'who_with':[per2obj(per) for per in p.who_with.all()],
'note':p.note,
'text':p.product.name,
'product_id':p.product.id,
'hour': hour2name[p.hour],}
def measurement2obj(m):
return {'id':m.id, 'spot_id':m.spot.id, 'amount':m.amount,
'name':m.spot.name,'text':m.spot.name,'domain_id':m.spot.domain.id,'domain_name':m.spot.domain.name}
def per2obj(per):
return {'id':per.id,'first_name':per.first_name,'last_name':per.last_name,'text':'%s %s'%(per.first_name, per.last_name),}
def source2obj(source):
return {'id':source.id,'name':source.name,'text':source.name,}
def region2obj(region):
return {'id':region.id,'name':region.name,'text':region.name,}
def currency2obj(cur):
return {'id':cur.id,'name':cur.name,'text':'%s %s'%(cur.symbol, cur.name),'symbol':cur.symbol,}
def mktable(dat, rights=None, bigs=None, skip_false=False, nb=False,non_max_width=False):
rows = []
for row in dat:
if not row:
continue
if type(row) not in [list, set, tuple]:
row=[row,]
if skip_false and not row[-1]:
continue
res = '<tr>'
for ii, thing in enumerate(row):
klasses = []
if nb:
klasses.append('nb')
if rights and ii in rights:
klasses.append('right')
if bigs and ii in bigs:
klasses.append('big')
if klasses:
res += '<td class="%s">%s</td>' % (' '.join(klasses), thing)
else:
res += '<td>%s</td>' % (thing)
rows.append(res+'</tr>')
tblklass='table thintable'
tblstyle=''
if non_max_width:
tblstyle='width:inherit;'
return '<table class="%s" style="background-color:white;%s">%s</table>' % (tblklass,tblstyle,''.join(rows))
def nbspan(contents,klass=None,):
if not contents:contents=''
if not klass:
klass=''
res='<span class="nb %s">%s</span>'%(klass, contents)
return res
def div(contents=None,klass=None):
klasszone=''
if klass:
klasszone='class="%s"'%klass
res='<div %s>%s</div>'%(klasszone,contents)
return res