-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: csv for powerBI integration (#3327)
- Loading branch information
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
backend/benefit/applications/api/v1/power_bi_integration_views.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import logging | ||
|
||
from django.db.models import QuerySet | ||
from django.http import StreamingHttpResponse | ||
from django.utils import timezone | ||
from django_filters import DateFromToRangeFilter, rest_framework as filters | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
from rest_framework.permissions import AllowAny | ||
from rest_framework.views import APIView | ||
|
||
from applications.models import Application | ||
from applications.services.applications_power_bi_csv_report import ( | ||
ApplicationsPowerBiCsvService, | ||
) | ||
from common.authentications import RobotBasicAuthentication | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class ApplicationPowerBiFilter(filters.FilterSet): | ||
decision_date = DateFromToRangeFilter( | ||
field_name="batch__decision_date", label="Batch decision date (range)" | ||
) | ||
|
||
class Meta: | ||
model = Application | ||
fields = ["decision_date"] | ||
|
||
def filter_queryset(self, queryset): | ||
""" | ||
Custom filtering logic that ensures only applications with a batch | ||
and a non-null decision_date are returned, and then applies any | ||
additional filters such as decision_date range if provided. | ||
""" | ||
queryset = queryset.filter( | ||
batch__isnull=False, batch__decision_date__isnull=False | ||
) | ||
return super().filter_queryset(queryset) | ||
|
||
|
||
class PowerBiIntegrationView(APIView): | ||
authentication_classes = [RobotBasicAuthentication] | ||
permission_classes = [AllowAny] | ||
filter_backends = [DjangoFilterBackend] | ||
filterset_class = ApplicationPowerBiFilter | ||
|
||
def get(self, request, *args, **kwargs) -> StreamingHttpResponse: | ||
# Apply the filter | ||
filterset = ApplicationPowerBiFilter( | ||
request.GET, queryset=Application.objects.all() | ||
) | ||
|
||
if filterset.is_valid(): | ||
# Get the filtered queryset from the filter class | ||
applications_with_batch_and_decision_date = filterset.qs | ||
# Generate the CSV response from the filtered queryset | ||
response = self._csv_response( | ||
queryset=applications_with_batch_and_decision_date | ||
) | ||
return response | ||
else: | ||
# Handle invalid filters (e.g., return a default queryset or handle the error) | ||
return StreamingHttpResponse("Invalid filters", status=400) | ||
|
||
def _csv_response( | ||
self, | ||
queryset: QuerySet[Application], | ||
prune_data_for_talpa: bool = False, | ||
) -> StreamingHttpResponse: | ||
csv_service = ApplicationsPowerBiCsvService( | ||
queryset, | ||
prune_data_for_talpa, | ||
) | ||
response = StreamingHttpResponse( | ||
csv_service.get_csv_string_lines_generator(), | ||
content_type="text/csv", | ||
) | ||
|
||
response["Content-Disposition"] = "attachment; filename={filename}.csv".format( | ||
filename=self._filename() | ||
) | ||
return response | ||
|
||
@staticmethod | ||
def _filename(): | ||
return f"power_bi_data_{timezone.now().strftime('%Y%m%d_%H%M%S')}" |
27 changes: 27 additions & 0 deletions
27
backend/benefit/applications/services/applications_power_bi_csv_report.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from applications.services.applications_csv_report import ( # csv_default_column, | ||
ApplicationsCsvService, | ||
) | ||
|
||
# from applications.services.csv_export_base import CsvColumn | ||
|
||
|
||
class ApplicationsPowerBiCsvService(ApplicationsCsvService): | ||
""" | ||
This subclass customizes the CSV_COLUMNS for a different export format. | ||
""" | ||
|
||
@property | ||
def CSV_COLUMNS(self): | ||
""" | ||
Customize the CSV columns but also return the parent class's columns. | ||
""" | ||
# Get the parent class CSV_COLUMNS | ||
parent_columns = super().CSV_COLUMNS | ||
|
||
# Define custom columns to add to or modify the parent columns | ||
custom_columns = [ | ||
# CsvColumn("Custom Column 1", "custom_field_1"), | ||
# CsvColumn("Custom Column 2", "custom_field_2"), | ||
] | ||
|
||
return parent_columns + custom_columns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/benefit/applications/tests/test_power_bi_integration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import csv | ||
from io import StringIO | ||
|
||
from django.urls import reverse | ||
|
||
|
||
def test_get_power_bi_data(power_bi_client, decided_application_with_decision_date): | ||
batch = decided_application_with_decision_date.batch | ||
url = ( | ||
reverse("powerbi_integration_url") | ||
+ f"?decision_date_after={batch.decision_date}" | ||
+ f"&decision_date_before={batch.decision_date}" | ||
) | ||
|
||
response = power_bi_client.get(url) | ||
assert response["Content-Type"] == "text/csv" | ||
|
||
content = "".join([chunk.decode("utf-8") for chunk in response.streaming_content]) | ||
|
||
# Parse CSV content | ||
csv_reader = csv.reader(StringIO(content), delimiter=";") | ||
rows = list(csv_reader) | ||
|
||
# Assert CSV has a header and at least one data row | ||
assert len(rows) > 1 | ||
header = rows[0] | ||
assert "Hakemusnumero" in header | ||
assert "Työnantajan nimi" in header | ||
|
||
assert rows[1][header.index("Hakemusnumero")] == str( | ||
decided_application_with_decision_date.application_number | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters