mirror of
https://github.com/wassname/pyramid_formalchemy.git
synced 2026-08-19 12:30:50 +08:00
real json api
This commit is contained in:
@@ -162,14 +162,12 @@ class ModelView(object):
|
||||
request = self.request
|
||||
request.override_renderer = 'json'
|
||||
if fs is not None:
|
||||
try:
|
||||
fields = fs.jsonify()
|
||||
except AttributeError:
|
||||
fields = dict([(field.renderer.name, field.model_value) for field in fs.render_fields.values()])
|
||||
data = dict(fields=fields)
|
||||
data = fs.to_dict(with_prefix=request.params.get('with_prefix', False))
|
||||
pk = _pk(fs.model)
|
||||
if pk:
|
||||
data['item_url'] = request.fa_url(request.model_name, 'json', pk)
|
||||
if 'id' not in data:
|
||||
data['id'] = pk
|
||||
data['absolute_url'] = request.fa_url(request.model_name, 'json', pk)
|
||||
else:
|
||||
data = {}
|
||||
data.update(kwargs)
|
||||
@@ -288,12 +286,12 @@ class ModelView(object):
|
||||
pk = _pk(item)
|
||||
fs._set_active(item)
|
||||
value = dict(id=pk,
|
||||
item_url=request.fa_url(request.model_name, pk))
|
||||
absolute_url=request.fa_url(request.model_name, pk))
|
||||
if 'jqgrid' in request.GET:
|
||||
fields = [_stringify(field.render_readonly()) for field in fs.render_fields.values()]
|
||||
value['cell'] = [pk] + fields
|
||||
else:
|
||||
value.update(dict([(field.key, field.model_value) for field in fs.render_fields.values()]))
|
||||
value.update(fs.to_dict(with_prefix=bool(request.params.get('with_prefix'))))
|
||||
values.append(value)
|
||||
return self.render_json_format(rows=values,
|
||||
records=len(values),
|
||||
@@ -339,14 +337,22 @@ class ModelView(object):
|
||||
|
||||
if request.format == 'json' and request.method == 'PUT':
|
||||
data = json.load(request.body_file)
|
||||
elif request.content_type == 'application/json':
|
||||
data = json.load(request.body_file)
|
||||
else:
|
||||
data = request.POST
|
||||
|
||||
try:
|
||||
fs = fs.bind(data=data, session=self.session, request=request)
|
||||
except Exception:
|
||||
# non SA forms
|
||||
fs = fs.bind(self.context.get_model(), data=data, session=self.session, request=request)
|
||||
with_prefix = True
|
||||
if request.format == 'json':
|
||||
with_prefix = bool(request.params.get('with_prefix'))
|
||||
|
||||
fs = fs.bind(data=data, session=self.session, request=request, with_prefix=with_prefix)
|
||||
#try:
|
||||
# fs = fs.bind(data=data, session=self.session, request=request, with_prefix=with_prefix)
|
||||
#except Exception:
|
||||
# # non SA forms
|
||||
# fs = fs.bind(self.context.get_model(), data=data, session=self.session,
|
||||
# request=request, with_prefix=with_prefix)
|
||||
|
||||
if self.validate(fs):
|
||||
fs.sync()
|
||||
@@ -384,7 +390,18 @@ class ModelView(object):
|
||||
alsoProvides(event, events.IBeforeEditRenderEvent)
|
||||
zope.component.event.objectEventNotify(event)
|
||||
|
||||
fs = fs.bind(request=request)
|
||||
if request.format == 'json' and request.method == 'PUT':
|
||||
data = json.load(request.body_file)
|
||||
elif request.content_type == 'application/json':
|
||||
data = json.load(request.body_file)
|
||||
else:
|
||||
data = request.POST
|
||||
|
||||
with_prefix = True
|
||||
if request.format == 'json':
|
||||
with_prefix = bool(request.params.get('with_prefix'))
|
||||
|
||||
fs = fs.bind(request=request, with_prefix=with_prefix)
|
||||
if self.validate(fs):
|
||||
fs.sync()
|
||||
self.sync(fs, id)
|
||||
|
||||
@@ -130,27 +130,56 @@ class Test_1_UI(unittest.TestCase):
|
||||
|
||||
# add page
|
||||
response = self.app.post('/admin/Foo/json',
|
||||
{'Foo--bar': 'value'})
|
||||
{'bar': 'value'})
|
||||
|
||||
data = response.json
|
||||
id = data['item_url'].split('/')[-1]
|
||||
id = data['absolute_url'].split('/')[-1]
|
||||
|
||||
response.mustcontain('"bar": "value"')
|
||||
|
||||
|
||||
# get data
|
||||
response = self.app.get(str(data['absolute_url']))
|
||||
response.mustcontain('"bar": "value"')
|
||||
|
||||
# edit page
|
||||
response = self.app.post(str(data['absolute_url']), {'bar': 'new value'})
|
||||
response.mustcontain('"bar": "new value"')
|
||||
|
||||
# delete
|
||||
response = self.app.delete(str(data['absolute_url']))
|
||||
self.assert_(response.json['id'] > 0)
|
||||
|
||||
def test_4_json_prefix(self):
|
||||
# index
|
||||
response = self.app.get('/admin/json')
|
||||
response.mustcontain('{"models": {', '"Foo": "http://localhost/admin/Foo/json"')
|
||||
|
||||
## Simple model
|
||||
|
||||
# add page
|
||||
response = self.app.post('/admin/Foo/json?with_prefix=True',
|
||||
{'Foo--bar': 'value', 'with_prefix': 'true'})
|
||||
|
||||
data = response.json
|
||||
id = data['absolute_url'].split('/')[-1]
|
||||
|
||||
response.mustcontain('"Foo-%s-bar": "value"' % id)
|
||||
|
||||
|
||||
# get data
|
||||
response = self.app.get(str(data['item_url']))
|
||||
response = self.app.get(str(data['absolute_url'])+'?with_prefix=True')
|
||||
response.mustcontain('"Foo-%s-bar": "value"' % id)
|
||||
|
||||
# edit page
|
||||
response = self.app.post(str(data['item_url']), {'Foo-%s-bar' % id: 'new value'})
|
||||
response = self.app.post(str(data['absolute_url']+'?with_prefix=True'), {'Foo-%s-bar' % id: 'new value', 'with_prefix': 'true'})
|
||||
response.mustcontain('"Foo-%s-bar": "new value"' % id)
|
||||
|
||||
# delete
|
||||
response = self.app.delete(str(data['item_url']))
|
||||
response = self.app.delete(str(data['absolute_url']+'?with_prefix=True'))
|
||||
self.assert_(response.json['id'] > 0)
|
||||
|
||||
def test_4_xhr(self):
|
||||
def test_5_xhr(self):
|
||||
# add page
|
||||
resp = self.app.post('/admin/Foo/', {'Foo--bar':'value'}, extra_environ={'HTTP_X_REQUESTED_WITH':'XMLHttpRequest'})
|
||||
self.assertEqual(resp.content_type, 'text/plain')
|
||||
@@ -195,6 +224,8 @@ class Test_2_Security(Test_1_UI):
|
||||
def test_2_model(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class Test_3_JQuery(Test_1_UI):
|
||||
|
||||
config = os.path.join(dirname, 'jquery.ini')
|
||||
|
||||
Reference in New Issue
Block a user