Skip to content

Commit

Permalink
Improve the presentation of "multi-values" in DejaCode Reports #10
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Feb 15, 2024
1 parent 846b513 commit 88fcdf9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 17 additions & 4 deletions reporting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

LICENSE_TAG_PREFIX = "tag: "

MULTIVALUE_SEPARATOR = ", "
MULTIVALUE_SEPARATOR = "\n"

ERROR_STR = "Error"

Expand Down Expand Up @@ -674,7 +674,12 @@ def get_value_for_instance(self, instance, user=None):
for field_name in self.field_name.split("__"):
objects = self._get_objects_for_field_name(objects, field_name, user)

return [str(val) for val in objects if not (len(objects) < 2 and val is None)]
return [
# The .strip() ensure the SafeString types are casted to regular str
str(val).strip()
for val in objects
if not (len(objects) < 2 and val is None)
]


class ReportQuerySet(DataspacedQuerySet):
Expand Down Expand Up @@ -746,7 +751,7 @@ def save(self, *args, **kwargs):
def get_absolute_url(self):
return reverse("reporting:report_details", args=[self.uuid])

def get_output(self, queryset=None, user=None, include_view_link=False):
def get_output(self, queryset=None, user=None, include_view_link=False, multi_as_list=False):
# Checking if the parameter is given rather than the boolean value of the QuerySet
if queryset is None:
queryset = self.query.get_qs(user=user)
Expand All @@ -762,7 +767,15 @@ def get_output(self, queryset=None, user=None, include_view_link=False):

for field in self.column_template.fields.all():
value = field.get_value_for_instance(instance, user=user)
cells.append(MULTIVALUE_SEPARATOR.join(value))
if not multi_as_list:
cells.append(MULTIVALUE_SEPARATOR.join(value))
else:
if value == []:
cells.append("")
elif len(value) > 1:
cells.append(value)
else:
cells.append(value[0])

rows.append(cells)

Expand Down
2 changes: 2 additions & 0 deletions reporting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ def get_context_data(self, **kwargs):
# Only available in the UI since the link is relative to the current URL
include_view_link = not self.format and hasattr(model_class, "get_absolute_url")
interpolated_report_context = self.get_interpolated_report_context(request, report)
multi_as_list = True if self.format in ["json", "yaml"] else False
output = report.get_output(
queryset=context["object_list"],
user=request.user,
include_view_link=include_view_link,
multi_as_list=multi_as_list,
)

context.update(
Expand Down

0 comments on commit 88fcdf9

Please sign in to comment.