def myspark(self, obj): purch=Purchase.objects.filter(product=obj) if not purch: spark= '' else: mindate=settings.LONG_AGO_STR res={} costres = {} for pu in purch: date=pu.created.strftime(DATE) res[date]=res.get(date, 0)+pu.quantity costres[date] = costres.get(date, 0) + pu.get_cost() if not mindate or date<mindate: mindate=date res2=group_day_dat(res, by='month',mindate=mindate) costres2=group_day_dat(costres, by='month',mindate=mindate) #first=datetime.datetime.strptime(mindate, DATE) #now=datetime.datetime.now() #trying=first #res2=[] #costres2 = [] #while trying<now: #dt = trying.strftime(DATE) #res2.append((res.get(dt, 0))) #costres2.append((costres.get(dt, 0))) #trying=datetime.timedelta(days=1)+trying counts = sparkline(labelresults=res2, width= 400, height= 100, kind = 'bar') costs = sparkline(labelresults=costres2, width= 400, height= 100, kind = 'bar') return 'Counts %s<br>Costs%s' % (counts, costs)
def myproducts(self, obj): total=sum([pp.get_cost() for pp in Purchase.objects.filter(product__domain=obj)]) ear=Purchase.objects.filter(product__domain=obj).order_by('created') earliest=None if ear: earliest=datetime.datetime.combine(ear[0].created, datetime.time()) else: total= '' if total and ear: now=datetime.datetime.now() dayrange= abs((now-earliest).days)+1 total='%s$<br>%s$/day<br>(%d days)'%(rstripz(total), rstripz(total/dayrange), dayrange) purch=Purchase.objects.filter(product__domain=obj) if not purch: costs='' else: mindate=settings.LONG_AGO_STR res={} for pu in purch: date=pu.created.strftime(DATE) res[date]=res.get(date, 0)+pu.get_cost() res2=group_day_dat(res, by='month',mindate=mindate) costs= sparkline(labelresults=res2, width=5, height=100) summary=obj.summary() return '<h2>%s</h2>%s<br>%s<br>%s<br>'%(obj.name, total, costs, summary)
def mysources(self, obj): purch=Purchase.objects.filter(product=obj) sources=Source.objects.filter(purchases__product=obj).distinct() dat=[(ss.total_spent(product=obj), ss, ss.purchases.filter(product=obj).count()) for ss in sources] dat.sort(key=lambda x:-1*x[0]) labelresults = [(d[0], (d[1].name)) for d in dat] if sources.count() < 3: height = 100 else: height = 200 from utils import sparkline pie = sparkline(labelresults = labelresults, height = height, kind = 'pie') res = '<h3>Sources</h3>%s' % pie res=[] countsum=0 alltotal=0 for total, source, counts in dat: filterlink='<a class="nb" href="/admin/day/purchase/?product__id=%d&source=%d">view</a>'%(obj.id, source.id) res.append([source.clink(), '%0.1f$'%total, counts, filterlink]) countsum+=counts alltotal+=total lastrow=['all','%0.1f'%alltotal,countsum,'',] res.append(lastrow) tbl = mktable(res) return pie + '<br>' + tbl
def myhistory(self, obj): measurements=obj.measurements.all() if obj.exclude_zeros: measurements=measurements.exclude(amount=0) if not measurements: return mindate=None res={} #{date:value} for measurement in measurements: date=measurement.created.strftime(DATE) res[date]=measurement.amount if not mindate or date<mindate: mindate=date if not obj.exclude_leading_zeros: mindate=settings.LONG_AGO_STR first=datetime.datetime.strptime(mindate, DATE) now=datetime.datetime.now() trying=first label2value=[] lastt=None val = 0 barwidth = 2 while trying<= now: dd = trying.strftime(DATE) if dd in res: val =res.get(dd) label2value.append((val, dd)) else: if obj.interpolate: pass else: val = 0 if obj.exclude_zeros: barwidth = 4 else: label2value.append((val, dd)) trying=datetime.timedelta(days=1)+trying if obj.interpolate: rendered = sparkline(labelresults = label2value, width=600, height= 150, kind = 'line') else: rendered = sparkline(labelresults = label2value, width= 600, height= 150, kind = 'bar', barwidth = barwidth) return '<div>%s</div>'% (rendered)
def myproducts(self, obj): products=Product.objects.filter(purchases__source=obj).distinct() labelresults = [(oo.total_spent(source=obj), str(oo)) for oo in products] height = products.count < 4 and 100 or 200 pie = sparkline(labelresults = labelresults, height = height, kind = 'pie') return pie