Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order by class when writing bikes's yearly report #304

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions comptages/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from operator import itemgetter
import os

from datetime import datetime
from typing import Any

from qgis.core import Qgis
from qgis.PyQt.uic import loadUiType
Expand Down Expand Up @@ -65,3 +67,18 @@ def connect_to_db():
db.open()

return db


def partial_order_with_blanks(
container: dict[int, Any], default_missing=None
) -> dict[int, Any]:
container_copy = container.copy()
blanks = []
prev = 0
for k in container_copy:
if k > prev + 1:
blanks.append(k - 1)
prev = k
for b in blanks:
container_copy[b] = default_missing
return dict(sorted(container_copy.items(), key=itemgetter(0)))
23 changes: 16 additions & 7 deletions comptages/report/yearly_report_bike.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os


from django.db.models import Sum, Count
from django.db.models import Sum, Count, F
from django.db.models.functions import Cast
from django.db.models.fields import DateField
from django.db.models.functions import (
Expand All @@ -13,6 +12,7 @@
from openpyxl import load_workbook

from comptages.core import definitions
from comptages.core.utils import partial_order_with_blanks
from comptages.datamodel.models import CountDetail, Section, Lane


Expand Down Expand Up @@ -153,13 +153,14 @@ def values_by_class(self):
import_status=definitions.IMPORT_STATUS_DEFINITIVE,
)

result = (
return (
qs.annotate(res=Sum("times"))
.values("res")
.values("id_category__code")
.annotate(tjm=Count("id_category__code"))
.annotate(code=F("id_category__code"))
.order_by("code")
)
return result

def tjm_direction_bike(self, categories, direction, weekdays=[0, 1, 2, 3, 4, 5, 6]):
qs = CountDetail.objects.filter(
Expand Down Expand Up @@ -384,10 +385,18 @@ def run(self):
row_offset = 4
column_offset = 2

data = self.values_by_class()
row = row_offset
for i in data:
ws.cell(row=row, column=column_offset, value=i["tjm"])
data = self.values_by_class()
# construct dict by class id and
data = {i["code"]: i for i in data}
# ensure all 6 rows / class ids are there, whether filled with data or with a blank
data = partial_order_with_blanks(data)
for value in data.values():
ws.cell(
row=row,
column=column_offset,
value=value["tjm"] if value is not None else "",
)
row += 1

ws = workbook["AN_GR"]
Expand Down