Skip to content
Snippets Groups Projects
Commit 7264217a authored by Neda Moeini's avatar Neda Moeini
Browse files

Improved the export file name and log the user activity in download endpoint.

parent 3502bacd
No related branches found
No related tags found
1 merge request!4Feature/activity log
...@@ -53,16 +53,9 @@ class CSVUploadAPIView(APIView): ...@@ -53,16 +53,9 @@ class CSVUploadAPIView(APIView):
updated_data = self.update_fields(csv_data) updated_data = self.update_fields(csv_data)
request.session["validated_csv"] = updated_data request.session["validated_csv"] = updated_data
request.session["input_file_hash"] = UserActivityLog.generate_file_hash(csv_file)
request.session.modified = True request.session.modified = True
# Log the user activity
UserActivityLog.objects.create(
user=request.user,
action="upload",
name=csv_file.name,
input_file_hash=UserActivityLog.generate_file_hash(csv_file),
output_file_hash=UserActivityLog.generate_file_hash(updated_data),
timestamp=timezone.now()
)
return Response({ return Response({
"status": "success", "status": "success",
"message": "File successfully uploaded and processed.", "message": "File successfully uploaded and processed.",
...@@ -111,16 +104,25 @@ class CSVExportAPIView(APIView): ...@@ -111,16 +104,25 @@ class CSVExportAPIView(APIView):
def get(self, request: Request) -> Response: def get(self, request: Request) -> Response:
"""Return processed CSV as a downloadable response.""" """Return processed CSV as a downloadable response."""
csv_data: list[dict[str, str]] = request.session.get("validated_csv", []) csv_data: list[dict[str, str]] = request.session.get("validated_csv", [])
input_file_hash: str = request.session.get("input_file_hash", "")
if not csv_data: if not csv_data:
return Response({"status": "error", "message": "No data available for export."}, return Response({"status": "error", "message": "No data available for export."},
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
file_name = f"Validated_{csv_data[0].get('TransactionReference', 'file')}.csv"
response = HttpResponse(content_type="text/csv") response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = 'attachment; filename="updated_file.csv"' response["Content-Disposition"] = f"attachment; filename={file_name}"
writer = csv.DictWriter(response, fieldnames=csv_data[0].keys()) writer = csv.DictWriter(response, fieldnames=csv_data[0].keys())
writer.writeheader() writer.writeheader()
writer.writerows(csv_data) writer.writerows(csv_data)
# Log the user activity
UserActivityLog.objects.create(
user=request.user,
action="download",
name=file_name,
input_file_hash=input_file_hash,
output_file_hash=UserActivityLog.generate_file_hash(csv_data),
timestamp=timezone.now()
)
return response return response
...@@ -36,6 +36,7 @@ def sample_input_file() -> SimpleUploadedFile: ...@@ -36,6 +36,7 @@ def sample_input_file() -> SimpleUploadedFile:
"TaxValue", "TaxValue",
"SYSTraderGenerationReasonType", "SYSTraderGenerationReasonType",
"GoodsValueInBaseCurrency", "GoodsValueInBaseCurrency",
"TransactionReference",
# NominalAnalysis repeating columns (Example: /1 for first occurrence) # NominalAnalysis repeating columns (Example: /1 for first occurrence)
"NominalAnalysisTransactionValue/1", "NominalAnalysisTransactionValue/1",
...@@ -77,6 +78,7 @@ def sample_input_file() -> SimpleUploadedFile: ...@@ -77,6 +78,7 @@ def sample_input_file() -> SimpleUploadedFile:
"10", # TaxValue "10", # TaxValue
"1000", # SYSTraderGenerationReasonType "1000", # SYSTraderGenerationReasonType
"1200", # GoodsValueInBaseCurrency "1200", # GoodsValueInBaseCurrency
"BK123", # TransactionReference(Batch Number)
# NominalAnalysis repeating values (Example: /1) # NominalAnalysis repeating values (Example: /1)
"500.75", # NominalAnalysisTransactionValue/1 "500.75", # NominalAnalysisTransactionValue/1
......
...@@ -49,13 +49,15 @@ def test_csv_export_with_data(api_client: APIClient) -> None: ...@@ -49,13 +49,15 @@ def test_csv_export_with_data(api_client: APIClient) -> None:
# Simulate session data # Simulate session data
session = api_client.session session = api_client.session
session["validated_csv"] = [ session["validated_csv"] = [
{"AccountNumber": "12345", "TransactionDate": "01/03/2024", "NominalAnalysisNominalAccountNumber/1": "N100"} {"AccountNumber": "12345", "TransactionDate": "01/03/2024", "TransactionReference": "BK1234"},
] ]
session["input_file_hash"] = "123456"
session.save() session.save()
response = api_client.get(url) response = api_client.get(url)
assert response.status_code == 200 assert response.status_code == 200
assert response["Content-Disposition"] == 'attachment; filename="updated_file.csv"' assert response["Content-Disposition"] == "attachment; filename=Validated_BK1234.csv"
assert b"AccountNumber,TransactionDate,NominalAnalysisNominalAccountNumber/1" in response.content assert b"AccountNumber,TransactionDate,TransactionReference" in response.content
assert b"12345,01/03/2024,N100" in response.content assert b"12345,01/03/2024,BK1234" in response.content
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment