Skip to content

Commit

Permalink
Merge pull request #3854 from allegro/fix-not-json-serializable-data-…
Browse files Browse the repository at this point in the history
…to-external-service

Fix data not JSON-serializable
  • Loading branch information
hipek8 authored Oct 23, 2024
2 parents 91e3d05 + 087ba90 commit 3b8929b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/ralph/assets/invoice_report.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import datetime
import json
import logging

from django.contrib import messages
Expand Down Expand Up @@ -116,7 +117,7 @@ def _get_report_data(self, request, queryset):
'model': queryset.model._meta.model_name,
'base_info': {
'invoice_no': first_item.invoice_no,
'invoice_date': first_item.invoice_date,
'invoice_date': str(first_item.invoice_date),
'provider': first_item.provider,
'datetime': datetime.datetime.now().strftime(
self._invoice_report_datetime_format
Expand Down Expand Up @@ -148,6 +149,9 @@ def _get_pdf_content(self, data):
template_content = f.read()

service_pdf = ExternalService('PDF')
# Make sure data is JSON-serializable
# Will throw otherwise
data = json.loads(json.dumps(data))
result = service_pdf.run(
template=template_content,
data=data,
Expand Down
18 changes: 12 additions & 6 deletions src/ralph/reports/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import json
import os
import tempfile

Expand All @@ -25,17 +26,22 @@ def generate_report(name, requester, instances, language, context):
service_pdf = ExternalService('PDF')

for n in range(0, len(context), items_per_attachment):
result = service_pdf.run(
template=template_content,
data={
# Make sure data is JSON-serializable
# Will throw otherwise
data = json.loads(json.dumps(
{
'id': ', '.join([str(obj.id) for obj in
instances[n:n+items_per_attachment]]),
'now': datetime.datetime.now(),
instances[n:n + items_per_attachment]]),
'now': str(datetime.datetime.now()),
'logged_user': obj_to_dict(requester),
'affected_user': obj_to_dict(instances[0].user),
'owner': obj_to_dict(instances[0].owner),
'assets': context[n:n+items_per_attachment],
'assets': context[n:n + items_per_attachment],
}
))
result = service_pdf.run(
template=template_content,
data=data
)

filename = "_".join([
Expand Down

0 comments on commit 3b8929b

Please sign in to comment.