Fix bug in history
This commit is contained in:
parent
886e682650
commit
7109142b4e
2 changed files with 31 additions and 21 deletions
|
@ -9,6 +9,8 @@ register = template.Library()
|
|||
|
||||
@register.simple_tag
|
||||
def calendar_opacity(v, vmax):
|
||||
if v is None:
|
||||
return "0%"
|
||||
return f"{math.sin(min(1, math.fabs(v/vmax))*math.pi/2):.0%}"
|
||||
|
||||
|
||||
|
@ -29,6 +31,8 @@ def empty_calendar_cells_end(n):
|
|||
|
||||
@register.simple_tag
|
||||
def up_down_icon(val):
|
||||
if val is None:
|
||||
return ""
|
||||
if val > 0:
|
||||
return remix("arrow-up-s", "green")
|
||||
elif val < 0:
|
||||
|
@ -41,15 +45,18 @@ def up_down_icon(val):
|
|||
def plot_bar(s, sum_pm, s_max):
|
||||
_res = ""
|
||||
|
||||
if sum_pm:
|
||||
_w = abs(sum_pm / s_max)
|
||||
_res += f"""<div style="width: {_w:.1%}"></div>"""
|
||||
if sum_pm is not None and s * sum_pm > 0:
|
||||
_w = abs(s / s_max)
|
||||
_res += (
|
||||
f"""<div class="tot" style="width: {_w:.1%}">"""
|
||||
f"""<span>{pmrvalue(s)}</span></div>"""
|
||||
)
|
||||
if s_max:
|
||||
if sum_pm:
|
||||
_w = abs(sum_pm / s_max)
|
||||
_res += f"""<div style="width: {_w:.1%}"></div>"""
|
||||
if sum_pm is not None and s * sum_pm > 0:
|
||||
_w = abs(s / s_max)
|
||||
_res += (
|
||||
f"""<div class="tot" style="width: {_w:.1%}">"""
|
||||
f"""<span>{pmrvalue(s)}</span></div>"""
|
||||
)
|
||||
else:
|
||||
_res += "<div></div>"
|
||||
|
||||
return mark_safe(_res)
|
||||
|
||||
|
@ -64,4 +71,4 @@ def calendar_head():
|
|||
|
||||
@register.filter
|
||||
def sum_year(y_data):
|
||||
return sum(y["sum"] for y in y_data)
|
||||
return sum(y["sum"] or 0 for y in y_data)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import datetime
|
||||
|
||||
from django.db.models import Q, Sum
|
||||
from django.db.models.functions import Abs, TruncMonth
|
||||
from django.db.models.functions import Abs, Greatest, TruncMonth
|
||||
|
||||
|
||||
def history(transaction_set):
|
||||
|
@ -14,15 +14,19 @@ def history(transaction_set):
|
|||
_first_month = _transaction_month.last()["month"]
|
||||
_last_month = _transaction_month.first()["month"]
|
||||
|
||||
_history = _transaction_month.annotate(
|
||||
sum_p=Sum("value", filter=Q(value__gt=0)),
|
||||
sum_m=Sum("value", filter=Q(value__lt=0)),
|
||||
sum=Sum("value"),
|
||||
).order_by("-month")
|
||||
_history = (
|
||||
_transaction_month.annotate(
|
||||
sum_p=Sum("value", filter=Q(value__gt=0), default=0),
|
||||
sum_m=Sum("value", filter=Q(value__lt=0), default=0),
|
||||
sum=Sum("value"),
|
||||
)
|
||||
.annotate(max_sum=Greatest("sum_p", Abs("sum_m")))
|
||||
.order_by("-month")
|
||||
)
|
||||
|
||||
_data = [
|
||||
_history.filter(month=datetime.date(y, m + 1, 1)).first()
|
||||
or {"month": datetime.date(y, m + 1, 1), "sum": 0}
|
||||
or {"month": datetime.date(y, m + 1, 1), "sum": None}
|
||||
for y in range(
|
||||
_first_month.year,
|
||||
_last_month.year + 1,
|
||||
|
@ -37,10 +41,9 @@ def history(transaction_set):
|
|||
"data": _data,
|
||||
"max": {
|
||||
"pm": 125
|
||||
* max(
|
||||
_history.order_by("-sum_p")[len(_history) // 10]["sum_p"],
|
||||
_history.order_by("-sum_m")[len(_history) // 10]["sum_m"],
|
||||
)
|
||||
* _history.order_by("-max_sum")[len(_history.exclude(max_sum=0)) // 10][
|
||||
"max_sum"
|
||||
]
|
||||
/ 100,
|
||||
"sum": 125
|
||||
* _history.annotate(abs_sum=Abs("sum")).order_by("-abs_sum")[
|
||||
|
|
Loading…
Reference in a new issue