Projet

Général

Profil

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

Frédéric Péters, 30 juin 2017 10:02

Télécharger (3,02 ko)

Voir les différences:

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

 passerelle/apps/jsondatastore/models.py | 20 ++++++++++++++------
 tests/test_jsondatastore.py             |  9 +++++++++
 2 files changed, 23 insertions(+), 6 deletions(-)
passerelle/apps/jsondatastore/models.py
18 18
import uuid
19 19

  
20 20
from django.db import models
21
from django.shortcuts import get_object_or_404
22 21
from django.template import Context, Template
23 22
from django.utils.translation import ugettext_lazy as _
24 23

  
......
80 79
        data.save()
81 80
        return {'id': data.uuid, 'text': data.text}
82 81

  
82
    def get_data_object(self, uuid, name_id=None):
83
        attrs = {
84
            'uuid': uuid,
85
            'datastore': self,
86
        }
87
        if name_id is not None:
88
            attrs['name_id'] = name_id
89
        return JsonData.objects.get(**attrs)
90

  
83 91
    @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)
92
    def get_or_replace(self, request, uuid, name_id=None):
93
        data = self.get_data_object(uuid, name_id)
86 94
        if request.method == 'POST':
87 95
            data.content = json.loads(request.body)
88 96
            data.save()
89 97
        return {'id': data.uuid, 'text': data.text, 'content': data.content}
90 98

  
91 99
    @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()
94
        return {'err': 0}
100
    def delete(self, request, uuid, name_id=None):
101
        self.get_data_object(uuid, name_id).delete()
102
        return {}
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
-