From c672baed238dcf2de50470ce7da9368d5121159b Mon Sep 17 00:00:00 2001 From: Pablo Panero Date: Wed, 16 Mar 2022 16:04:14 +0100 Subject: [PATCH] dumpers: support for different load type --- invenio_records/dumpers/elasticsearch.py | 15 +++++++-------- invenio_records/systemfields/model.py | 11 ++++++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/invenio_records/dumpers/elasticsearch.py b/invenio_records/dumpers/elasticsearch.py index b503e5fc..868a8d7b 100644 --- a/invenio_records/dumpers/elasticsearch.py +++ b/invenio_records/dumpers/elasticsearch.py @@ -151,7 +151,7 @@ def _dump_model_field(self, record, model_field_name, dump, dump_key, dump[dump_key] = self._serialize(val, dump_type) def _load_model_field(self, record_cls, model_field_name, dump, dump_key, - dump_type): + load_type): """Helper method to load model fields from dump. :param record_cls: The record class being used for loading. @@ -169,12 +169,11 @@ def _load_model_field(self, record_cls, model_field_name, dump, dump_key, return val # Determine dump data type if not provided - if dump_type is None: - sa_field = getattr(record_cls.model_cls, model_field_name) - dump_type = self._sa_type(record_cls.model_cls, model_field_name) + if load_type is None: + load_type = self._sa_type(record_cls.model_cls, model_field_name) # Deserialize the value - return self._deserialize(val, dump_type) + return self._deserialize(val, load_type) @staticmethod def _iter_modelfields(record_cls): @@ -247,15 +246,15 @@ def load(self, dump_data, record_cls): # Load explicitly defined model fields. model_data = {} it = self._model_fields.items() - for model_field_name, (dump_key, dump_type) in it: + for model_field_name, (dump_key, load_type) in it: model_data[model_field_name] = self._load_model_field( - record_cls, model_field_name, dump_data, dump_key, dump_type) + record_cls, model_field_name, dump_data, dump_key, load_type) # Load model fields defined as system fields for systemfield in self._iter_modelfields(record_cls): model_data[systemfield.model_field_name] = self._load_model_field( record_cls, systemfield.model_field_name, dump_data, - systemfield.dump_key, systemfield.dump_type) + systemfield.dump_key, systemfield.load_type) # Initialize model if an id was provided. if model_data.get('id') is not None: diff --git a/invenio_records/systemfields/model.py b/invenio_records/systemfields/model.py index efc74c3c..3b410a84 100644 --- a/invenio_records/systemfields/model.py +++ b/invenio_records/systemfields/model.py @@ -16,7 +16,7 @@ class ModelField(SystemField): """Model field for providing get and set access on a model field.""" def __init__(self, model_field_name=None, dump=True, dump_key=None, - dump_type=None): + dump_type=None, load_type=None): """Initialize the field. :param model_field_name: Name of field on the database model. @@ -29,6 +29,7 @@ def __init__(self, model_field_name=None, dump=True, dump_key=None, self.dump = dump self._dump_key = dump_key self._dump_type = dump_type + self._load_type = load_type # # Helpers @@ -58,6 +59,14 @@ def dump_type(self): """ return self._dump_type + @property + def load_type(self): + """The data type used to determine how to deserialize the model field. + + Defaults to dump_type. + """ + return self._load_type or self._dump_type + def _set(self, model, value): """Internal method to set value on the model's field.""" setattr(model, self.model_field_name, value)