diff --git a/swh/deposit/api/private/__init__.py b/swh/deposit/api/private/__init__.py index f4acbba4..59369eba 100644 --- a/swh/deposit/api/private/__init__.py +++ b/swh/deposit/api/private/__init__.py @@ -1,51 +1,50 @@ # Copyright (C) 2017-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +from swh.deposit import utils from ...config import METADATA_TYPE from ...models import DepositRequest, Deposit class DepositReadMixin: """Deposit Read mixin """ def _deposit_requests(self, deposit, request_type): """Given a deposit, yields its associated deposit_request Args: deposit (Deposit): Deposit to list requests for request_type (str): Archive or metadata type Yields: deposit requests of type request_type associated to the deposit """ if isinstance(deposit, int): deposit = Deposit.objects.get(pk=deposit) deposit_requests = DepositRequest.objects.filter( type=self.deposit_request_types[request_type], deposit=deposit).order_by('id') for deposit_request in deposit_requests: yield deposit_request def _metadata_get(self, deposit): """Given a deposit, aggregate all metadata requests. Args: deposit (Deposit): The deposit instance to extract metadata from. Returns: metadata dict from the deposit. """ - metadata = {} - for dr in self._deposit_requests(deposit, request_type=METADATA_TYPE): - metadata.update(dr.metadata) - return metadata + metadata = self._deposit_requests(deposit, request_type=METADATA_TYPE) + return utils.merge((m.metadata for m in metadata)) diff --git a/swh/deposit/tests/test_utils.py b/swh/deposit/tests/test_utils.py new file mode 100644 index 00000000..9e02c619 --- /dev/null +++ b/swh/deposit/tests/test_utils.py @@ -0,0 +1,44 @@ +# Copyright (C) 2018 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import unittest + +from nose.tools import istest + +from swh.deposit import utils + + +class UtilsTestCase(unittest.TestCase): + """Utils library + + """ + @istest + def convert(self): + d0 = { + 'author': 'someone', + 'a': 1 + } + + d1 = { + 'author': ['author0', 'author1'], + 'b': { + '1': '2' + } + } + + d2 = { + 'author': 'else', + } + + actual_merge = utils.merge([d0, d1, d2]) + + expected_merge = { + 'a': 1, + 'author': ['someone', 'author0', 'author1', 'else'], + 'b': { + '1': '2' + } + } + self.assertEquals(actual_merge, expected_merge) diff --git a/swh/deposit/utils.py b/swh/deposit/utils.py new file mode 100644 index 00000000..04c9d3b3 --- /dev/null +++ b/swh/deposit/utils.py @@ -0,0 +1,15 @@ +# Copyright (C) 2018 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + + +def merge(dicts): + """Given an iteratof of dicts, merge them losing no information. + + """ + d = {} + for data in dicts: + print(data) + d.update(data) + return d