0001-jsondatastore-attach-data-to-store-17166.patch
passerelle/apps/jsondatastore/migrations/0003_jsondata_datastore.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import migrations, models |
|
5 | ||
6 | ||
7 |
class Migration(migrations.Migration): |
|
8 | ||
9 |
dependencies = [ |
|
10 |
('jsondatastore', '0002_auto_20170623_1844'), |
|
11 |
] |
|
12 | ||
13 |
operations = [ |
|
14 |
migrations.AddField( |
|
15 |
model_name='jsondata', |
|
16 |
name='datastore', |
|
17 |
field=models.ForeignKey(to='jsondatastore.JsonDataStore', null=True), |
|
18 |
), |
|
19 |
] |
passerelle/apps/jsondatastore/models.py | ||
---|---|---|
33 | 33 | |
34 | 34 | |
35 | 35 |
class JsonData(models.Model): |
36 |
datastore = models.ForeignKey('JsonDataStore', null=True) |
|
37 | ||
36 | 38 |
uuid = models.CharField(_('uuid'), max_length=32, |
37 | 39 |
default=get_hex_uuid, editable=False, unique=True) |
38 | 40 |
name_id = models.CharField(max_length=256, blank=True) |
... | ... | |
61 | 63 | |
62 | 64 |
@endpoint(perm='can_access', name='data', pattern=r'$') |
63 | 65 |
def list(self, request, name_id=None, **kwargs): |
64 |
objects = JsonData.objects.all()
|
|
66 |
objects = JsonData.objects.filter(datastore=self)
|
|
65 | 67 |
if name_id: |
66 | 68 |
objects = objects.filter(name_id=name_id) |
67 | 69 |
return {'data': [{'id': x.uuid, 'text': x.text, 'content': x.content} for x in objects]} |
68 | 70 | |
69 | 71 |
@endpoint(perm='can_access', methods=['post'], name='data', pattern=r'create$') |
70 | 72 |
def create(self, request, name_id=None, **kwargs): |
71 |
attrs = {'content': json.loads(request.body)} |
|
73 |
attrs = { |
|
74 |
'content': json.loads(request.body), |
|
75 |
'datastore': self, |
|
76 |
} |
|
72 | 77 |
if name_id: |
73 | 78 |
attrs['name_id'] = name_id |
74 | 79 |
data = JsonData(**attrs) |
... | ... | |
78 | 83 | |
79 | 84 |
@endpoint(perm='can_access', methods=['get', 'post'], name='data', pattern=r'(?P<uuid>\w+)/$',) |
80 | 85 |
def get_or_replace(self, request, uuid): |
81 |
data = get_object_or_404(JsonData, uuid=uuid) |
|
86 |
data = get_object_or_404(JsonData, uuid=uuid, datastore=self)
|
|
82 | 87 |
if request.method == 'POST': |
83 | 88 |
data.content = json.loads(request.body) |
84 | 89 |
self.update_text(data) |
... | ... | |
87 | 92 | |
88 | 93 |
@endpoint(perm='can_access', methods=['post'], name='data', pattern=r'(?P<uuid>\w+)/delete$') |
89 | 94 |
def delete(self, request, uuid): |
90 |
get_object_or_404(JsonData, uuid=uuid).delete() |
|
95 |
get_object_or_404(JsonData, uuid=uuid, datastore=self).delete()
|
|
91 | 96 |
return {'err': 0} |
tests/test_jsondatastore.py | ||
---|---|---|
15 | 15 |
resource_type=obj_type, resource_pk=datastore.pk) |
16 | 16 |
return datastore |
17 | 17 | |
18 |
@pytest.fixture |
|
19 |
def jsondatastore2(db): |
|
20 |
datastore = JsonDataStore.objects.create(slug='foobar2') |
|
21 |
api = ApiUser.objects.create(username='all2', keytype='', key='') |
|
22 |
obj_type = ContentType.objects.get_for_model(datastore) |
|
23 |
AccessRight.objects.create(codename='can_access', apiuser=api, |
|
24 |
resource_type=obj_type, resource_pk=datastore.pk) |
|
25 |
return datastore |
|
18 | 26 | |
19 |
def test_jsondatastore(app, jsondatastore): |
|
27 | ||
28 |
def test_jsondatastore(app, jsondatastore, jsondatastore2): |
|
20 | 29 |
resp = app.get('/jsondatastore/foobar/data/') |
21 | 30 |
assert resp.json == {'data': []} |
22 | 31 | |
... | ... | |
44 | 53 |
resp = app.get('/jsondatastore/foobar/data/') |
45 | 54 |
assert len(resp.json['data']) == 0 |
46 | 55 | |
56 |
resp = app.post_json('/jsondatastore/foobar/data/create', params={'foo': 'bar'}) |
|
57 |
uuid = resp.json['id'] |
|
58 |
resp = app.get('/jsondatastore/foobar2/data/') |
|
59 |
assert len(resp.json['data']) == 0 |
|
60 | ||
61 |
resp = app.post_json('/jsondatastore/foobar2/data/%s/delete' % uuid, status=404) |
|
47 | 62 | |
48 | 63 |
def test_jsondatastore_name_id(app, jsondatastore): |
49 | 64 |
resp = app.get('/jsondatastore/foobar/data/') |
50 |
- |