Projet

Général

Profil

0001-json-data-store-allow-name_id-in-edit-delete-methods.patch

Frédéric Péters, 24 juin 2017 11:03

Télécharger (2,8 ko)

Voir les différences:

Subject: [PATCH] json data store: allow name_id in edit/delete methods
 (#17171)

 passerelle/apps/jsondatastore/models.py | 17 +++++++++++++----
 tests/test_jsondatastore.py             |  9 +++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)
passerelle/apps/jsondatastore/models.py
80 80
        data.save()
81 81
        return {'id': data.uuid, 'text': data.text}
82 82

  
83
    def get_data_object_or_404(self, uuid, name_id=None):
84
        attrs = {
85
            'uuid': uuid,
86
            'datastore': self,
87
        }
88
        if name_id is not None:
89
            attrs['name_id'] = name_id
90
        return get_object_or_404(JsonData, **attrs)
91

  
83 92
    @endpoint(perm='can_access', methods=['get', 'post'], name='data', pattern=r'(?P<uuid>\w+)/$',)
84
    def get_or_replace(self, request, uuid):
85
        data = get_object_or_404(JsonData, uuid=uuid, datastore=self)
93
    def get_or_replace(self, request, uuid, name_id=None):
94
        data = self.get_data_object_or_404(uuid, name_id)
86 95
        if request.method == 'POST':
87 96
            data.content = json.loads(request.body)
88 97
            data.save()
89 98
        return {'id': data.uuid, 'text': data.text, 'content': data.content}
90 99

  
91 100
    @endpoint(perm='can_access', methods=['post'], name='data', pattern=r'(?P<uuid>\w+)/delete$')
92
    def delete(self, request, uuid):
93
        get_object_or_404(JsonData, uuid=uuid, datastore=self).delete()
101
    def delete(self, request, uuid, name_id=None):
102
        self.get_data_object_or_404(uuid, name_id).delete()
94 103
        return {'err': 0}
tests/test_jsondatastore.py
84 84
    resp = app.get('/jsondatastore/foobar/data/?name_id=yyy')
85 85
    assert len(resp.json['data']) == 1
86 86

  
87
    # try removing an existing entry with the wrong name id
88
    resp = app.post_json('/jsondatastore/foobar/data/%s/delete?name_id=zzz' % uuid, status=404)
89

  
90
    # try removing an existing entry with a blank name id
91
    resp = app.post_json('/jsondatastore/foobar/data/%s/delete?name_id=' % uuid, status=404)
92

  
93
    # and with the right one
94
    resp = app.post_json('/jsondatastore/foobar/data/%s/delete?name_id=yyy' % uuid)
95

  
87 96
def test_jsondatastore_template(app, jsondatastore):
88 97
    jsondatastore.text_value_template = '{{foo}}'
89 98
    jsondatastore.save()
90
-