mirror of https://github.com/snachodog/mybuddy.git
Sum amounts and account for timezone in "Feeding Amounts" report. (#68)
This commit is contained in:
parent
2f8bc831d7
commit
c45e8ef53b
|
@ -1,6 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from django.db.models import Sum
|
from django.utils import timezone
|
||||||
from django.db.models.functions import TruncDate
|
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
import plotly.offline as plotly
|
import plotly.offline as plotly
|
||||||
|
@ -15,17 +14,18 @@ def feeding_amounts(instances):
|
||||||
:param instances: a QuerySet of Feeding instances.
|
:param instances: a QuerySet of Feeding instances.
|
||||||
:returns: a tuple of the the graph's html and javascript.
|
:returns: a tuple of the the graph's html and javascript.
|
||||||
"""
|
"""
|
||||||
totals = instances.annotate(date=TruncDate('start')) \
|
totals = {}
|
||||||
.values('date') \
|
for instance in instances:
|
||||||
.annotate(sum=Sum('amount')) \
|
end = timezone.localtime(instance.end)
|
||||||
.order_by('-date')
|
date = end.date()
|
||||||
|
if date not in totals.keys():
|
||||||
dates = [value['date'] for value in totals.values('date')]
|
totals[date] = 0
|
||||||
amounts = [value['amount'] or 0 for value in totals.values('amount')]
|
totals[date] += instance.amount or 0
|
||||||
|
|
||||||
|
amounts = [round(amount, 2) for amount in totals.values()]
|
||||||
trace = go.Bar(
|
trace = go.Bar(
|
||||||
name=_('Total feeding amount'),
|
name=_('Total feeding amount'),
|
||||||
x=dates,
|
x=list(totals.keys()),
|
||||||
y=amounts,
|
y=amounts,
|
||||||
hoverinfo='text',
|
hoverinfo='text',
|
||||||
textposition='outside',
|
textposition='outside',
|
||||||
|
|
Loading…
Reference in New Issue