Skip to content

Commit 556f01c

Browse files
Copilotnijel
andcommitted
Fix type annotations, remove test mock calls, fix template URL, update comments, remove duplicate import
Co-authored-by: nijel <[email protected]>
1 parent 3c1b184 commit 556f01c

File tree

3 files changed

+18
-34
lines changed

3 files changed

+18
-34
lines changed

weblate_web/crm/templates/crm/income.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ <h3>{% translate "Category Distribution" %}</h3>
108108
<td>{{ current_year }}/{{ month }}</td>
109109
<td class="align-right nowrap">€{{ amount|floatformat:0 }}</td>
110110
<td>
111-
<a href="{% url 'crm:income-month' current_year month %}">{% translate "View details" %}</a>
111+
<a href="{% url 'crm:income-month' current_year month|add:0 %}">{% translate "View details" %}</a>
112112
</td>
113113
</tr>
114114
{% endfor %}

weblate_web/crm/tests.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,6 @@ def test_income_yearly_view(self):
254254
cnb_mock_rates()
255255
current_year = timezone.now().year
256256

257-
# Mock rates for test dates
258-
self.mock_exchange_rates_for_date(f"{current_year}-01-15")
259-
self.mock_exchange_rates_for_date(f"{current_year}-02-15")
260-
self.mock_exchange_rates_for_date(f"{current_year}-03-15")
261-
262257
# Create test invoices
263258
self.create_test_invoice(
264259
current_year, 1, InvoiceCategory.HOSTING, Decimal(1000)
@@ -281,9 +276,6 @@ def test_income_monthly_view(self):
281276
cnb_mock_rates()
282277
current_year = timezone.now().year
283278

284-
# Mock rates for test date
285-
self.mock_exchange_rates_for_date(f"{current_year}-03-15")
286-
287279
# Create test invoices for different categories
288280
self.create_test_invoice(
289281
current_year, 3, InvoiceCategory.HOSTING, Decimal(1000)
@@ -308,9 +300,6 @@ def test_income_filters_only_invoices(self):
308300
cnb_mock_rates()
309301
current_year = timezone.now().year
310302

311-
# Mock rates for test date
312-
self.mock_exchange_rates_for_date(f"{current_year}-01-15")
313-
314303
# Create invoice
315304
invoice = Invoice.objects.create(
316305
kind=InvoiceKind.INVOICE,
@@ -364,9 +353,6 @@ def test_income_svg_chart_generation(self):
364353
cnb_mock_rates()
365354
current_year = timezone.now().year
366355

367-
# Mock rates for test date
368-
self.mock_exchange_rates_for_date(f"{current_year}-01-15")
369-
370356
self.create_test_invoice(
371357
current_year, 1, InvoiceCategory.HOSTING, Decimal(1000)
372358
)
@@ -384,12 +370,6 @@ def test_income_category_breakdown(self):
384370
cnb_mock_rates()
385371
current_year = timezone.now().year
386372

387-
# Mock rates for test dates
388-
self.mock_exchange_rates_for_date(f"{current_year}-01-15")
389-
self.mock_exchange_rates_for_date(f"{current_year}-02-15")
390-
self.mock_exchange_rates_for_date(f"{current_year}-03-15")
391-
self.mock_exchange_rates_for_date(f"{current_year}-04-15")
392-
393373
# Create invoices in different categories
394374
self.create_test_invoice(
395375
current_year, 1, InvoiceCategory.HOSTING, Decimal(1000)

weblate_web/crm/views.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import calendar
44
import math
5+
from collections.abc import Callable
56
from decimal import Decimal
67
from operator import attrgetter
78
from typing import TYPE_CHECKING, cast
@@ -26,8 +27,6 @@
2627
from .models import Interaction
2728

2829
if TYPE_CHECKING:
29-
from collections.abc import Callable
30-
3130
from django.http import HttpRequest
3231

3332
from weblate_web.payments.models import CustomerQuerySet
@@ -344,15 +343,14 @@ class IncomeView(CRMMixin, TemplateView): # type: ignore[misc]
344343
CHART_PADDING = 60
345344
MIN_CHART_VALUE = Decimal(1)
346345

347-
# Category colors shared across all charts (keyed by category value)
346+
# Category colors shared across all charts (keyed by category enum)
348347
CATEGORY_COLORS = {
349348
InvoiceCategory.HOSTING: "#417690",
350349
InvoiceCategory.SUPPORT: "#79aec8",
351350
InvoiceCategory.DEVEL: "#5b80b2",
352351
InvoiceCategory.DONATE: "#9fc5e8",
353352
}
354353

355-
# Cached label-to-enum mapping (built once on first access)
356354
def get_year(self) -> int:
357355
"""Get the year from URL kwargs or default to current year."""
358356
return self.kwargs.get("year", timezone.now().year)
@@ -466,7 +464,11 @@ def generate_svg_pie_chart(self, data: dict[InvoiceCategory, Decimal]) -> str:
466464
return "".join(svg_parts)
467465

468466
def generate_svg_stacked_bar_chart( # noqa: PLR0914
469-
self, monthly_data: dict, invoices: list, year: int, month: int | None = None
467+
self,
468+
monthly_data: dict[str, Decimal],
469+
invoices: list[Invoice],
470+
year: int,
471+
month: int | None = None,
470472
) -> str:
471473
"""
472474
Generate a stacked bar chart showing totals by category.
@@ -572,7 +574,7 @@ def filter_by_month(inv, idx):
572574

573575
def _get_invoices_and_totals(
574576
self, year: int, month: int | None = None
575-
) -> tuple[list, dict]:
577+
) -> tuple[list[Invoice], dict[int, Decimal]]:
576578
"""Fetch invoices and pre-calculate totals (shared helper)."""
577579
query = Invoice.objects.filter(kind=InvoiceKind.INVOICE, issue_date__year=year)
578580
if month:
@@ -587,9 +589,9 @@ def _aggregate_income_by_period(
587589
self,
588590
year: int,
589591
month: int | None,
590-
filter_func: Callable[[object, str], bool],
592+
filter_func: Callable[[Invoice, str], bool],
591593
period_keys: list[str],
592-
) -> tuple[dict[str, Decimal], list]:
594+
) -> tuple[dict[str, Decimal], list[Invoice]]:
593595
"""
594596
Aggregate income data filtered by a custom function.
595597
@@ -637,21 +639,23 @@ def get_income_data(
637639

638640
return category_data
639641

640-
def get_monthly_data(self, year: int) -> tuple[dict[str, Decimal], list]:
642+
def get_monthly_data(self, year: int) -> tuple[dict[str, Decimal], list[Invoice]]:
641643
"""Get monthly income data for the year."""
642644

643-
def filter_by_month(inv: object, key: str) -> bool:
645+
def filter_by_month(inv: Invoice, key: str) -> bool:
644646
return inv.issue_date.month == int(key)
645647

646648
monthly_keys = [f"{month:02d}" for month in range(1, 13)]
647649
return self._aggregate_income_by_period(
648650
year, None, filter_by_month, monthly_keys
649651
)
650652

651-
def get_daily_data(self, year: int, month: int) -> tuple[dict[str, Decimal], list]:
653+
def get_daily_data(
654+
self, year: int, month: int
655+
) -> tuple[dict[str, Decimal], list[Invoice]]:
652656
"""Get daily income data for a specific month."""
653657

654-
def filter_by_day(inv: object, key: str) -> bool:
658+
def filter_by_day(inv: Invoice, key: str) -> bool:
655659
return inv.issue_date.day == int(key)
656660

657661
num_days = calendar.monthrange(year, month)[1]
@@ -660,7 +664,7 @@ def filter_by_day(inv: object, key: str) -> bool:
660664

661665
def get_monthly_category_data(
662666
self, year: int
663-
) -> tuple[dict[str, dict[str, Decimal]], list]:
667+
) -> tuple[dict[str, dict[str, Decimal]], list[Invoice]]:
664668
"""Get monthly income data split by category for stacked chart."""
665669
invoices = list(
666670
Invoice.objects.filter(

0 commit comments

Comments
 (0)