Skip to content

Commit

Permalink
feat: logged in user to alteration csv (#3223)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke authored Aug 26, 2024
1 parent 0eb0ee8 commit 15e1869
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
8 changes: 6 additions & 2 deletions backend/benefit/applications/api/v1/application_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
from shared.audit_log import audit_logging
from shared.audit_log.enums import Operation
from shared.audit_log.viewsets import AuditLoggingModelViewSet
from users.models import User
from users.utils import get_company_from_request

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -511,7 +512,9 @@ def update_with_csv(self, request):
account_number=alteration_fields.data["account_number"],
billing_department=alteration_fields.data["billing_department"],
)
return self._alterations_csv_response(queryset, configurable_fields)
return self._alterations_csv_response(
queryset, configurable_fields, request.user
)
except ObjectDoesNotExist:
raise ImproperlyConfigured(
"application_alteration_fields fields not found in the ahjo_settings table"
Expand All @@ -521,9 +524,10 @@ def _alterations_csv_response(
self,
queryset: QuerySet[ApplicationAlteration],
config: AlterationCsvConfigurableFields,
user: User,
) -> StreamingHttpResponse:
"""Generate a response with a CSV file containing application alteration data."""
csv_service = ApplicationAlterationCsvService(queryset, config)
csv_service = ApplicationAlterationCsvService(queryset, config, user)

response = HttpResponse(
csv_service.get_csv_string(True).encode("utf-8"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from dataclasses import dataclass
from typing import Union

from django.conf import settings
from django.db.models.query import QuerySet

from applications.models import ApplicationAlteration
from applications.services.csv_export_base import CsvColumn, CsvExportBase
from users.models import User


@dataclass
Expand All @@ -22,10 +24,12 @@ def __init__(
self,
application_alterations: QuerySet[ApplicationAlteration],
config: AlterationCsvConfigurableFields,
current_user: User,
):
self.application_alterations = application_alterations
self.billing_department = config.billing_department
self.account_number = config.account_number
self.current_user = current_user

def get_recovery_period(
self, alteration: ApplicationAlteration
Expand All @@ -46,9 +50,9 @@ def get_account_number(self, alteration: ApplicationAlteration) -> str:
return self.account_number

def get_handler_name(self, alteration: ApplicationAlteration) -> Union[str, None]:
if alteration.handled_by:
return f"{alteration.handled_by.get_full_name()}, {alteration.handled_by.email}"
return ""
email = settings.DEFAULT_SYSTEM_EMAIL

return f"{self.current_user.get_full_name()}, {email}"

def get_company_address(self, alteration: ApplicationAlteration) -> str:
return alteration.application.company.get_full_address()
Expand Down
1 change: 1 addition & 0 deletions backend/benefit/applications/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ def application_alteration_csv_service():
application_id__in=[application_1.id, application_2.id]
),
config=config,
current_user=handled_by,
)


Expand Down
15 changes: 11 additions & 4 deletions backend/benefit/applications/tests/test_applications_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,15 @@ def test_sensitive_data_removed_csv_output(sanitized_csv_service_with_one_applic
assert col_heading not in csv_lines[0]


def test_application_alteration_csv_output(application_alteration_csv_service):
def test_application_alteration_csv_output(
application_alteration_csv_service, settings
):
csv_lines = split_lines_at_semicolon(
application_alteration_csv_service.get_csv_string()
)

settings.DEFAULT_SYSTEM_EMAIL = "helsinkilisa@hel.fi"

alteration_1 = application_alteration_csv_service.get_alterations()[0]
alteration_2 = application_alteration_csv_service.get_alterations()[1]

Expand Down Expand Up @@ -409,10 +413,13 @@ def test_application_alteration_csv_output(application_alteration_csv_service):
csv_lines[1][9]
== f'"{application_alteration_csv_service.get_account_number(alteration_1)}"'
)
assert (
csv_lines[1][10]
== f'"{application_alteration_csv_service.get_handler_name(alteration_1)}"'

handler_string = (
f'"{application_alteration_csv_service.get_handler_name(alteration_1)}"'
)
assert csv_lines[1][10] == handler_string

assert settings.DEFAULT_SYSTEM_EMAIL in handler_string
assert (
csv_lines[1][11]
== f'"{application_alteration_csv_service.get_title(alteration_1)}"'
Expand Down
2 changes: 2 additions & 0 deletions backend/benefit/helsinkibenefit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
EMAIL_PORT=(int, 25),
EMAIL_TIMEOUT=(int, 15),
DEFAULT_FROM_EMAIL=(str, "Helsinki-lisä <noreply-helsinkilisa@hel.fi>"),
DEFAULT_SYSTEM_EMAIL=(str, "helsinkilisa@hel.fi"),
WKHTMLTOPDF_BIN=(str, "/usr/bin/wkhtmltopdf"),
DUMMY_COMPANY_FORM_CODE=(
int,
Expand Down Expand Up @@ -556,3 +557,4 @@
ENABLE_CLAMAV = env.bool("ENABLE_CLAMAV")
CLAMAV_URL = env.str("CLAMAV_URL")
ENABLE_AHJO_AUTOMATION = env.bool("ENABLE_AHJO_AUTOMATION")
DEFAULT_SYSTEM_EMAIL = env.str("DEFAULT_SYSTEM_EMAIL")

0 comments on commit 15e1869

Please sign in to comment.