def post(self): database = self.settings['db'] data = ujson.loads(self.request.body.decode("utf-8")) uids = [] if isinstance(data, list): for d in data: d = utils.default_timeuid(d) try: jsonschema.validate(d, utils.schemas['request']) except (ValidationError, SchemaError): raise utils._compose_err_msg( 400, "Invalid schema on document(s)", d) try: res = database.request.insert(d) uids.append(d['uid']) except pymongo.errors.PyMongoError: raise utils._compose_err_msg( 500, 'Validated data can not be inserted', data) database.request.create_index([('uid', pymongo.DESCENDING)], unique=True, background=True) database.request.create_index([('time', pymongo.DESCENDING)], unique=False, background=True) database.request.create_index([('sample', pymongo.DESCENDING)], unique=False, background=True, sparse=True) elif isinstance(data, dict): data = utils.default_timeuid(data) try: jsonschema.validate(data, utils.schemas['request']) except (ValidationError, SchemaError): raise utils._compose_err_msg(400, "Invalid schema on document(s)", data) try: res = database.request.insert(data) uids.append(data['uid']) except pymongo.errors.PyMongoError: raise utils._compose_err_msg( 500, 'Validated data can not be inserted', data) database.request.create_index([('uid', pymongo.DESCENDING)], unique=True, background=True) database.request.create_index([('time', pymongo.DESCENDING)], unique=False, background=True) database.request.create_index([('sample', pymongo.DESCENDING)], unique=False, background=True, sparse=True) else: raise utils._compose_err_msg( 500, status='SampleHandler expects list or dict') self.finish(ujson.dumps(uids))
def post(self): database = self.settings['db'] data = ujson.loads(self.request.body.decode("utf-8")) uids = [] if isinstance(data, list): for d in data: d = utils.default_timeuid(d) try: jsonschema.validate(d, utils.schemas['request']) except (ValidationError, SchemaError): raise utils._compose_err_msg(400, "Invalid schema on document(s)", d) try: res = database.request.insert(d) uids.append(d['uid']) except pymongo.errors.PyMongoError: raise utils._compose_err_msg(500, 'Validated data can not be inserted', data) database.request.create_index([('uid', pymongo.DESCENDING)], unique=True, background=True) database.request.create_index([('time', pymongo.DESCENDING)], unique=False, background=True) database.request.create_index([('sample', pymongo.DESCENDING)], unique=False, background=True, sparse=True) elif isinstance(data, dict): data = utils.default_timeuid(data) try: jsonschema.validate(data, utils.schemas['request']) except (ValidationError, SchemaError): raise utils._compose_err_msg(400, "Invalid schema on document(s)", data) try: res = database.request.insert(data) uids.append(data['uid']) except pymongo.errors.PyMongoError: raise utils._compose_err_msg(500, 'Validated data can not be inserted', data) database.request.create_index([('uid', pymongo.DESCENDING)], unique=True, background=True) database.request.create_index([('time', pymongo.DESCENDING)], unique=False, background=True) database.request.create_index([('sample', pymongo.DESCENDING)], unique=False, background=True, sparse=True) else: raise utils._compose_err_msg(500, status='SampleHandler expects list or dict') self.finish(ujson.dumps(uids))
def put(self): database = self.settings['db'] incoming = ujson.loads(self.request.body) try: query = incoming.pop('query') update = incoming.pop('update') except KeyError: raise utils._compose_err_msg( 500, status='filter and update are both required fields') if any(x in update.keys() for x in ['uid', 'time', 'name']): raise utils._compose_err_msg( 500, status='Time and uid cannot be updated') res = database.container.update_many(filter=query, update={'$set': update}, upsert=False) self.finish(ujson.dumps(res.raw_result))
def put(self): database = self.settings['db'] incoming = ujson.loads(self.request.body) try: query = incoming.pop('query') update = incoming.pop('update') except KeyError: raise utils._compose_err_msg(500, status='filter and update are both required fields') if any(x in update.keys() for x in ['uid', 'time', 'name']): raise utils._compose_err_msg(500, status='Time and uid cannot be updated') res = database.container.update_many(filter=query, update={'$set': update}, upsert=False) self.finish(ujson.dumps(res.raw_result))
def get(self): database = self.settings['db'] query = utils.unpack_params(self) num = query.pop("num", None) if num: try: docs = database.sample.find().sort('time', direction=pymongo.DESCENDING).limit(num) except pymongo.errors.PyMongoError: raise utils._compose_err_msg(500, '', query) else: try: docs = database.container.find(query).sort('time', direction=pymongo.DESCENDING) except pymongo.errors.PyMongoError: raise utils._compose_err_msg(500, 'Query Failed: ', query) if docs: utils.return2client(self, docs) else: raise utils._compose_err_msg(500, 'No results found!')
def get(self): database = self.settings['db'] query = utils.unpack_params(self) num = query.pop("num", None) if num: try: docs = database.sample.find().sort( 'time', direction=pymongo.DESCENDING).limit(num) except pymongo.errors.PyMongoError: raise utils._compose_err_msg(500, '', query) else: try: docs = database.container.find(query).sort( 'time', direction=pymongo.DESCENDING) except pymongo.errors.PyMongoError: raise utils._compose_err_msg(500, 'Query Failed: ', query) if docs: utils.return2client(self, docs) else: raise utils._compose_err_msg(500, 'No results found!')
def post(self): raise utils._compose_err_msg(405, status='Not allowed on server')