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 test_adds_missing_inner_dict_fields(self): item = { u'list-of-dicts': [ {}, ], } qvarn.add_missing_item_fields(u'foo-type', self.prototype, item) self.assertEqual( item, { u'type': u'foo-type', u'a-string': None, u'an-int': None, u'a-boolean': None, u'list-of-strings': [], u'list-of-dicts': [ { u'inner-string': None, u'inner-int': None, u'inner-boolean': None, u'inner-list-of-strings': [], }, ], })
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 test_adds_missing_inner_dict_fields(self): item = { u'list-of-dicts': [ { }, ], } qvarn.add_missing_item_fields(u'foo-type', self.prototype, item) self.assertEqual( item, { u'type': u'foo-type', u'a-string': None, u'an-int': None, u'a-boolean': None, u'list-of-strings': [], u'list-of-dicts': [ { u'inner-string': None, u'inner-int': None, u'inner-boolean': None, u'inner-list-of-strings': [], }, ], })
def post_item(self): """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 put_listener(self, listener_id): '''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_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): """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_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): """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 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 test_adds_missing_type_field(self): item = {} qvarn.add_missing_item_fields(u'foo-type', self.prototype, item) self.assertEqual(item[u'type'], u'foo-type')
def test_keeps_existing_type_field_even_if_wrong(self): item = { u'type': u'this-is-wrong', } qvarn.add_missing_item_fields(u'foo-type', self.prototype, item) self.assertEqual(item[u'type'], u'this-is-wrong')
def test_deals_with_non_dict(self): self.assertEqual( qvarn.add_missing_item_fields(u'foo-type', self.prototype, None), None)
def test_deals_with_non_dict(self): self.assertEqual( qvarn.add_missing_item_fields( u'foo-type', self.prototype, None), None)