def create_document(name: str, doc_id: int): """Add a document""" data = request.get_json() if name == 'businesses': business = Business(**data) doc = BusinessElastic(**business.dehydrate()) elif name == 'tips': tip = Tip(**data) doc = TipElastic(**tip.dehydrate()) elif name == 'checkins': checkin = Checkin(**data) doc = CheckinElastic(**checkin.dehydrate()) elif name == 'users': user = User(**data) doc = UserElastic(**user.dehydrate()) elif name == 'reviews': review = Review(**data) doc = ReviewElastic(**review.dehydrate()) else: raise GenericException('unknown index {}'.format(name), 400) doc.meta.id = doc_id try: doc.save() except ValueError as e: message = str(e) logger.error(message) raise GenericException(message=message, status_code=400, payload=request.get_json()) return build_response(doc.to_dict())
def get_business_reviews(doc_id: int): try: doc = BusinessElastic.get(id=doc_id, index='businesses') except TransportError as e: raise GenericException( message='error getting document {}'.format(doc_id), status_code=e.status_code, payload=e.info) results = business_reviews(business_id=doc.business_id) return build_response(hydrate_models(results.hits))
def delete_document(name: str, doc_id: int): try: if name == 'businesses': BusinessElastic.get(id=doc_id, index=name).delete(index=name) elif name == 'tips': TipElastic.get(id=doc_id, index=name).delete(index=name) elif name == 'checkins': CheckinElastic.get(id=doc_id, index=name).delete(index=name) elif name == 'users': UserElastic.get(id=doc_id, index=name).delete(index=name) elif name == 'reviews': ReviewElastic.get(id=doc_id, index=name).delete(index=name) else: raise GenericException('unknown index {}'.format(name), 400) except TransportError as e: message = 'error deleting document {}'.format(doc_id) logger.error(message) raise GenericException(message=message, status_code=e.status_code, payload=e.info) build_response({'acknowledged': True})
def get(name: str, doc_id: int): """Get a document by ID""" try: if name == 'businesses': resp = Business.hydrate(BusinessElastic.get(id=doc_id, index=name)) elif name == 'tips': resp = Tip.hydrate(TipElastic.get(id=doc_id, index=name)) elif name == 'users': resp = User.hydrate(UserElastic.get(id=doc_id, index=name)) elif name == 'checkins': resp = Checkin.hydrate(CheckinElastic.get(id=doc_id, index=name)) elif name == 'reviews': resp = Review.hydrate(ReviewElastic.get(id=doc_id, index=name)) else: raise GenericException('unknown index {}'.format(name), 400) except TransportError as e: raise GenericException( message='error getting document {}'.format(doc_id), status_code=e.status_code, payload=e.info) return build_response(resp.serialize())
def get(): query = request.args.get('q') offset = int(request.args.get('offset', default=0)) page_size = int(request.args.get('page_size', default=10)) results = generic_search(query, offset=offset, page_size=page_size) return build_response(hydrate_models(results.hits))
def heartbeat(): return build_response({'time': str(datetime.now()), 'env': ENVIRONMENT})
def malformed_payload(e: BadRequest): return build_response({'message': e.description}), 400
def method_not_allowed(e): return build_response({'message': e.description}), 405
def page_not_found(e: NotFound): return build_response({'message': e.description}), 404