Skip to content

Commit

Permalink
Fix None values getting exported as empty strings
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
hstct committed Oct 4, 2023
1 parent 642bc82 commit 4a54dae
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pulp_deb/app/modelresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pulpcore.plugin.importexport import BaseContentResource
from pulpcore.plugin.modelresources import RepositoryResource
from pulp_deb.app.constants import NULL_VALUE
from pulp_deb.app.models import (
AptRepository,
GenericContent,
Expand Down Expand Up @@ -53,6 +54,38 @@ def set_up_queryset(self):

return content

def dehydrate_field(self, obj, field_name):
"""
Assigns fields that don't have a value (e.g. None) with the special `NULL_VALUE`.
This ensures that the import can set it correctly to None rather than an empty string.
Args:
obj: exporting object.
field_name (str): name of the field that gets exported.
Returns:
str: the field value or NULL_VALUE.
"""
field = self.fields[field_name]
value = field.attribute and field.get_value(obj) or None
return value if value is not None else NULL_VALUE


def before_import_row(self, row, **kwargs):
"""
Ensures that all fields that got exported with the `NULL_VALUE` will be imported as None.
Args:
row (tablib.Dataset row): incoming import-row representing a single PRC.
kwargs: args passed along from the import() call.
"""
super().before_import_row(row, **kwargs)

for key, value in row.items():
if value == NULL_VALUE:
row[key] = None


class InstallerFileIndexResource(DebContentResource):
"""
Expand Down

0 comments on commit 4a54dae

Please sign in to comment.