def post_listener(self): '''Serve POST /foos/listeners to create a new listener.''' listener = bottle.request.qvarn_json qvarn.add_missing_item_fields( u'listener', listener_prototype, listener) iv = qvarn.ItemValidator() iv.validate_item(u'listener', listener_prototype, listener) # Filling in default values sets the id field to None, if # missing. Thus we accept that and just remove it here. del listener[u'id'] del listener[u'revision'] wo = self._create_resource_wo_storage( self._listener_table, listener_prototype) with self._dbconn.transaction() as t: added = wo.add_item(t, listener) resource_path = u'%s/listeners/%s' % (self._path, added[u'id']) resource_url = urlparse.urljoin( bottle.request.url, resource_path) # FIXME: Force https scheme, until haproxy access us via https. resource_url = urlparse.urlunparse( ('https',) + urlparse.urlparse(resource_url)[1:]) bottle.response.headers['Location'] = resource_url bottle.response.status = 201 return added
def post_item(self): # pragma: no cover '''Serve POST /foos to create a new item.''' item = bottle.request.qvarn_json qvarn.add_missing_item_fields( self._item_type, self._item_prototype, item) iv = qvarn.ItemValidator() iv.validate_item(self._item_type, self._item_prototype, item) self._item_validator(item) # Filling in default values sets the fields to None, if # missing. Thus we accept that and just remove it here. del item[u'id'] del item[u'revision'] wo = self._create_wo_storage() with self._dbconn.transaction() as t: added = wo.add_item(t, item) self._listener.notify_create(added[u'id'], added[u'revision']) resource_path = u'%s/%s' % (self._path, added[u'id']) resource_url = urlparse.urljoin( bottle.request.url, resource_path) # FIXME: Force https scheme, until haproxy access us via https. resource_url = urlparse.urlunparse( ('https',) + urlparse.urlparse(resource_url)[1:]) bottle.response.headers['Location'] = resource_url bottle.response.status = 201 return added
def assertValidItem(self, item_type, prototype, item): iv = qvarn.ItemValidator() # We check that validation succeeds by making sure # validat_item returns from the call. Any validation errors # result in an exception, so a return means valid. The method # returns no value, so we compare to None. self.assertEqual(iv.validate_item(item_type, prototype, item), None)
def put_listener(self, listener_id): # pragma: no cover '''Serve PUT /foos/listeners/123 to update a listener.''' listener = bottle.request.qvarn_json qvarn.add_missing_item_fields( u'listener', listener_prototype, listener) iv = qvarn.ItemValidator() iv.validate_item(u'listener', listener_prototype, listener) listener[u'id'] = listener_id wo = self._create_resource_wo_storage( self._listener_table, listener_prototype) with self._dbconn.transaction() as t: updated = wo.update_item(t, listener) return updated
def put_item(self, item_id): # pragma: no cover '''Serve PUT /foos/123 to update an item.''' item = bottle.request.qvarn_json qvarn.add_missing_item_fields( self._item_type, self._item_prototype, item) iv = qvarn.ItemValidator() iv.validate_item(self._item_type, self._item_prototype, item) item[u'id'] = item_id self._item_validator(item) wo = self._create_wo_storage() with self._dbconn.transaction() as t: updated = wo.update_item(t, item) self._listener.notify_update(updated[u'id'], updated[u'revision']) return updated
def put_subitem(self, item_id, subitem_name): # pragma: no cover '''Serve PUT /foos/123/subitem to update a subitem.''' subitem = bottle.request.qvarn_json subitem_type = u'%s_%s' % (self._item_type, subitem_name) prototype = self._subitem_prototypes.get(self._item_type, subitem_name) qvarn.add_missing_item_fields(subitem_type, prototype, subitem) iv = qvarn.ItemValidator() revision = subitem.pop(u'revision') iv.validate_item(subitem_type, prototype, subitem) wo = self._create_wo_storage() with self._dbconn.transaction() as t: subitem[u'revision'] = wo.update_subitem( t, item_id, revision, subitem_name, subitem) updated = dict(subitem) updated.update({u'id': item_id}) self._listener.notify_update(updated[u'id'], updated[u'revision']) return subitem
def assertNotValidItem(self, item_type, prototype, item): iv = qvarn.ItemValidator() with self.assertRaises(qvarn.ValidationError): iv.validate_item(item_type, prototype, item)