def validate_data(request, model, partial=False, data=None): if data is None: data = validate_json_data(request) if data is None: return try: if partial and isinstance(request.context, model): initial_data = request.context.serialize() m = model(initial_data) new_patch = apply_data_patch(initial_data, data) if new_patch: m.import_data(new_patch, partial=True, strict=True) m.__parent__ = request.context.__parent__ m.validate() role = request.context.get_role() method = m.to_patch else: m = model(data) m.__parent__ = request.context m.validate() method = m.serialize role = 'create' except (ModelValidationError, ModelConversionError), e: for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422 data = None
def apply_patch(request, data=None, save=True, src=None): data = request.validated.get('data') if data is None else data patch = data and apply_data_patch(src or request.context.serialize(), data) if patch: request.context.import_data(patch) if save: return save_lot(request)
def test_apply_data_patch(self): item = Document({ u'status': u'draft', u'assetType': u'basic', u'doc_type': u'Asset' }) changes = {'status': 'pending'} expected_result = Document({ u'status': 'pending', u'assetType': u'basic', u'doc_type': u'Asset' }) result = apply_data_patch(item=item, changes=changes) self.assertEqual(result, expected_result) changes = {} result = apply_data_patch(item=item, changes=changes) self.assertEqual(result, {})
def set_status(self, status, extra=None): if extra: data.update(extra) lot = self.db.get(self.lot_id) lot.update(apply_data_patch(lot, data)) self.db.save(lot) authorization = self.app.authorization self.app.authorization = ('Basic', ('chronograph', '')) response = self.app.get('/lots/{}'.format(self.lot_id)) self.app.authorization = authorization self.assertEqual(response.status, '200 OK') self.assertEqual(response.content_type, 'application/json') return response
def set_status(self, status, extra=None): data = {'status': status} if extra: data.update(extra) resource = self.db.get(self.resource_id) resource.update(apply_data_patch(resource, data)) self.db.save(resource) response = self.app.get('/{}'.format(self.resource_id)) self.assertEqual(response.status, '200 OK') self.assertEqual(response.content_type, 'application/json') resource = response.json['data'] self.assertEqual(resource['status'], status) return resource
def set_status(self, status, extra=None): if extra: data.update(extra) asset = self.db.get(self.asset_id) asset.update(apply_data_patch(asset, data)) self.db.save(asset) authorization = self.app.authorization self.app.authorization = ('Basic', ('chronograph', '')) #response = self.app.patch_json('/assets/{}'.format(self.asset_id), {'data': {'id': self.asset_id}}) response = self.app.get('/assets/{}'.format(self.asset_id)) self.app.authorization = authorization self.assertEqual(response.status, '200 OK') self.assertEqual(response.content_type, 'application/json') return response