Projet

Général

Profil

0001-general-allow-POST-to-a-cell-to-return-the-raw-servi.patch

Frédéric Péters, 12 septembre 2018 21:42

Télécharger (3,84 ko)

Voir les différences:

Subject: [PATCH] general: allow POST to a cell to return the raw service
 response (#26373)

 combo/data/models.py  |  8 ++++++++
 combo/public/views.py |  8 +++++++-
 tests/test_public.py  | 20 +++++++++++++++++++-
 3 files changed, 34 insertions(+), 2 deletions(-)
combo/data/models.py
1194 1194
        if self.cache_duration:
1195 1195
            self.get_cell_extra_context(context, invalidate_cache=True)
1196 1196

  
1197
        if self.actions[action].get('response', 'cell') == 'raw':
1198
            # response: raw in the config will get the response directly sent
1199
            # to the client.
1200
            return json_response
1201

  
1202
        # default behaviour, the cell will render itself.
1203
        return None
1204

  
1197 1205
    def render(self, context):
1198 1206
        if self.force_async and not context.get('synchronous'):
1199 1207
            raise NothingInCacheException()
combo/public/views.py
112 112
        raise PermissionDenied()
113 113

  
114 114
    exception = None
115
    action_response = None
115 116
    if request.method == 'POST':
116 117
        if not hasattr(cell, 'post'):
117 118
            raise PermissionDenied()
118 119
        try:
119
            cell.post(request)
120
            action_response = cell.post(request)
120 121
        except PostException as e:
121 122
            exception = e
122 123
            if not request.is_ajax():
123 124
                messages.error(request, force_text(e) if force_text(e) != 'None' else _('Error sending data.'))
124 125

  
126
        if action_response:
127
            return HttpResponse(
128
                    action_response.content,
129
                    content_type=action_response.headers['Content-Type'])
130

  
125 131
        if not request.is_ajax():
126 132
            return HttpResponseRedirect(cell.page.get_online_url())
127 133

  
tests/test_public.py
402 402
                    'update': {
403 403
                        'url': 'http://test-post-cell/update/',
404 404
                        'method': 'PATCH',
405
                    }
405
                    },
406
                    'search': {
407
                        'url': 'http://test-post-cell/search/',
408
                        'method': 'GET',
409
                        'response': 'raw'
410
                    },
406 411
                }
407 412
            }},
408 413
            TEMPLATES=templates_settings
......
473 478
                assert requests_patch.call_args[1]['json'] == {'value': 'plop', 'items': ['1']}
474 479
                assert urlparse.urlparse(resp2.location).path == '/'
475 480

  
481
            # check raw result
482
            with mock.patch('combo.utils.requests.request') as requests_search:
483
                resp.form['action'] = 'search'
484
                requests_search.return_value = mock.Mock(
485
                        content=json.dumps({'foo': 'bar'}),
486
                        status_code=200,
487
                        headers={'Content-Type': 'application/json'})
488
                resp2 = resp.form.submit()
489
                assert requests_search.call_args[0][0] == 'GET'
490
                assert requests_search.call_args[0][1] == 'http://test-post-cell/search/'
491
                assert resp2.json == {'foo': 'bar'}
492

  
493

  
476 494
def test_familyinfos_cell_with_placeholders(app, admin_user):
477 495
    Page.objects.all().delete()
478 496
    page = Page(title='Family', slug='index', template_name='standard')
479
-