From 16edb973586b53e171795a4b92398ea03c42f15d Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Sat, 16 Oct 2021 14:58:48 +0200
Subject: [PATCH 1/5] plone_restapi: adapt record id field (#57904)

---
 passerelle/apps/plone_restapi/models.py | 33 +++++++++++++------------
 tests/test_plone_restapi.py             |  5 ++--
 2 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/passerelle/apps/plone_restapi/models.py b/passerelle/apps/plone_restapi/models.py
index 57bea1e4..27f3229a 100644
--- a/passerelle/apps/plone_restapi/models.py
+++ b/passerelle/apps/plone_restapi/models.py
@@ -98,27 +98,30 @@ class PloneRestApi(BaseResource):
                 self.adapt_id_and_type_plone_attributes(value)
         elif isinstance(data, dict):
             for key, value in list(data.items()):
                 self.adapt_id_and_type_plone_attributes(value)
                 if key in self.plone_keys_to_rename and key[0] == '@':
                     data['PLONE_%s' % key[1:]] = value
                     del data[key]
 
-    def adapt_record(self, text_template, record):
+    def adapt_record(
+        self,
+        record,
+        text_template='{{ id }}',
+        id_key='UID',
+    ):
         self.adapt_id_and_type_plone_attributes(record)
-        data = {}
-        for key, value in record.items():
+        for key, value in list(record.items()):
             # backup original id and text fields
             if key in ('id', 'text'):
                 key = 'original_%s' % key
-            data[key] = value
-        data['id'] = record.get('UID')
-        data['text'] = render_to_string(text_template, data).strip()
-        return data
+            record[key] = value
+        record['id'] = record.get(id_key)
+        record['text'] = render_to_string(text_template, record).strip()
 
     def get_token(self, renew=False):
         token_key = 'plone-restapi-%s-token' % self.id
         if not renew and cache.get(token_key):
             return cache.get(token_key)
         payload = {
             'grant_type': 'password',
             'client_id': str(self.client_id),
@@ -187,38 +190,36 @@ class PloneRestApi(BaseResource):
                 if order:
                     params['sort_order'] = 'ascending'
                 else:
                     params['sort_order'] = 'descending'
             if limit:
                 params['b_size'] = limit
             params.update(parse_qsl(filter_expression))
         params['fullobjects'] = 'y'
-        json_response = self.request(uri=uri, uid='@search', method='GET', params=params)
+        response = self.request(uri=uri, uid='@search', method='GET', params=params)
 
-        result = []
-        for record in json_response.get('items'):
-            data = self.adapt_record(text_template, record)
-            result.append(data)
-        return result
+        for record in response.get('items') or []:
+            self.adapt_record(record, text_template)
+        return response.get('items') or []
 
     @endpoint(
         perm='can_access',
         description=_('Fetch'),
         parameters={
             'uri': {'description': _('Uri')},
             'uid': {'description': _('Uid')},
             'text_template': {'description': _('Text template')},
         },
         display_order=1,
     )
     def fetch(self, request, uid, uri='', text_template=''):
-        json_response = self.request(uri=uri, uid=uid, method='GET')
-        data = self.adapt_record(text_template, json_response)
-        return {'data': data}
+        response = self.request(uri=uri, uid=uid, method='GET')
+        self.adapt_record(response, text_template)
+        return {'data': response}
 
     @endpoint(
         perm='can_access',
         description=_('Creates'),
         parameters={
             'uri': {'description': _('Uri')},
         },
         methods=['post'],
diff --git a/tests/test_plone_restapi.py b/tests/test_plone_restapi.py
index 43115768..74f5c718 100644
--- a/tests/test_plone_restapi.py
+++ b/tests/test_plone_restapi.py
@@ -185,24 +185,25 @@ def test_adapt_id_and_type_plone_attributes(connector):
     connector.adapt_id_and_type_plone_attributes(plone_response)
     assert plone_response == {
         'PLONE_type': '@value',
         '@dict': {'@array': [{'PLONE_id': '123', 'PLONE_type': '@value'}]},
     }
 
 
 def test_adapt_record(connector, token):
-    data = {
+    record = {
         '@id': 'plone id',
         'UID': 'plone uid',
         'id': 'foo',
         'text': 'bar',
     }
     template = '{{ PLONE_id }}, {{ id }}, {{original_id }}, {{ original_text }}'
-    assert connector.adapt_record(template, data) == {
+    connector.adapt_record(record, template)
+    assert record == {
         'PLONE_id': 'plone id',
         'UID': 'plone uid',
         'id': 'plone uid',
         'text': 'plone id, plone uid, foo, bar',
         'original_id': 'foo',
         'original_text': 'bar',
     }
 
-- 
2.30.2

