Skip to content

Commit

Permalink
Changed serializer to replace null values with blank strings
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaddalena committed Sep 25, 2024
1 parent a9c23f7 commit 8dfc267
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
31 changes: 21 additions & 10 deletions ghostwriter/modules/custom_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ def __init__(self, *args, exclude=None, **kwargs):
self.fields.pop(field)
super().__init__(*args, **kwargs)

def to_representation(self, instance):
"""
Override the default method to ensure empty strings are returned for null values. The null values will
cause Jinja2 rendering errors with filters and expressions like `sort()`.
"""
data = super().to_representation(instance)
for key, value in data.items():
try:
if not value:
data[key] = ""
except KeyError:
pass
return data


class OperatorNameField(RelatedField):
"""Customize the string representation of a :model:`users.User` entry."""
Expand Down Expand Up @@ -113,7 +127,7 @@ class ExtraFieldsSerField(serializers.Field):
def __init__(self, model_name, **kwargs):
self.model_name = model_name
self.root_ser = None
kwargs['read_only'] = True
kwargs["read_only"] = True
super().__init__(**kwargs)

def bind(self, field_name, parent):
Expand All @@ -130,7 +144,9 @@ def to_representation(self, value):
if not hasattr(self.root_ser, "_extra_fields_specs") or self.root_ser._extra_fields_specs is None:
self.root_ser._extra_fields_specs = {}
if self.model_name not in self.root_ser._extra_fields_specs:
self.root_ser._extra_fields_specs[self.model_name] = ExtraFieldSpec.objects.filter(target_model=self.model_name)
self.root_ser._extra_fields_specs[self.model_name] = ExtraFieldSpec.objects.filter(
target_model=self.model_name
)

# Populate output
for field in self.root_ser._extra_fields_specs[self.model_name]:
Expand Down Expand Up @@ -514,10 +530,7 @@ class DomainHistorySerializer(CustomModelSerializer):
exclude=["id", "project", "domain"],
)

extra_fields = ExtraFieldsSerField(
Domain._meta.label,
source="domain.extra_fields"
)
extra_fields = ExtraFieldsSerField(Domain._meta.label, source="domain.extra_fields")

class Meta:
model = History
Expand Down Expand Up @@ -567,10 +580,7 @@ class ServerHistorySerializer(CustomModelSerializer):
exclude=["id", "project", "static_server", "transient_server"],
)

extra_fields = ExtraFieldsSerField(
StaticServer._meta.label,
source="server.extra_fields"
)
extra_fields = ExtraFieldsSerField(StaticServer._meta.label, source="server.extra_fields")

class Meta:
model = ServerHistory
Expand Down Expand Up @@ -756,6 +766,7 @@ class Meta:

class FullProjectSerializer(serializers.Serializer):
"""Serialize :model:`rolodex:Project` and related entries."""

project = ProjectSerializer(source="*")
client = ClientSerializer()
contacts = ProjectContactSerializer(source="projectcontact_set", many=True, exclude=["id", "project"])
Expand Down
6 changes: 3 additions & 3 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ djangorestframework-api-key==2.2.0
django-tinymce==3.4.0 # Deprecated, but kept here for legacy migrations
docutils==0.18.1
docxtpl==0.12.0
dnspython==2.2.0
jinja2==3.1.3
dnspython==2.6.1
jinja2==3.1.4
python-docx==0.8.11
python-nmap==0.7.1
python-pptx==0.6.21
requests==2.31.0
requests==2.32.0
django-timezone-field==4.2.3
pyjwt==2.7.0
psutil==5.9.4
Expand Down

0 comments on commit 8dfc267

Please sign in to comment.