def _report_object_exists(typename, **kwargs): """ Generate standard log message + request error for warning: Trying to POST an object that already exists :param typename: name of type involved :param **kwargs: arbitrary keyword parameters :return: Connexion Error() type to return """ report = typename + ': Attempt to modify with a POST' message = 'Attempt to modify '+typename+' with a POST' logger().warning(struct_log(action=report, **kwargs)) return Error(message=message, code=405)
def _report_conversion_error(typename, exception, **kwargs): """ Generate standard log message + request error for warning: Trying to POST an object that already exists :param typename: name of type involved :param exception: exception thrown by ORM :param **kwargs: arbitrary keyword parameters :return: Connexion Error() type to return """ report = 'Could not convert '+typename+' to ORM model' message = typename + ': failed validation - could not convert to internal representation' logger().error(struct_log(action=report, exception=str(exception), **kwargs)) return Error(message=message, code=400)
def _report_update_failed(typename, exception, **kwargs): """ Generate standard log message + request error for error: Internal error performing update (PUT) :param typename: name of type involved :param exception: exception thrown by ORM :param **kwargs: arbitrary keyword parameters :return: Connexion Error() type to return """ report = typename + ' updated failed' message = 'Internal error updating '+typename+'s' logger().error(struct_log(action=report, exception=str(exception), **kwargs)) return Error(message=message, code=500)
def _report_write_error(typename, exception, **kwargs): """ Generate standard log message + request error for error: Error writing to DB :param typename: name of type involved :param exception: exception thrown by ORM :param **kwargs: arbitrary keyword parameters :return: Connexion Error() type to return """ report = 'Internal error writing '+typename+' to DB' message = typename + ': internal error saving ORM object to DB' logger().error(struct_log(action=report, exception=str(exception), **kwargs)) err = Error(message=message, code=500) return err
def post_variant(variant): """ Add a new variant """ db_session = orm.get_session() # Does this variant already exist, by ID or by content? try: found_variant = variant_exists(**variant) except orm.ORMException as e: err = _report_search_failed('variant', e, **variant) return err if found_variant: err = _report_object_exists('variant', **variant) return err, 405 vid = uuid.uuid1() variant['id'] = vid variant['created'] = datetime.datetime.utcnow() variant['updated'] = variant['created'] # convert to ORM representation try: orm_variant = models.Variant(**variant) except orm.ORMException as e: err = _report_conversion_error('variant', e, **variant) return err, 400 try: db_session.add(orm_variant) db_session.commit() except orm.ORMException as e: err = _report_write_error('variant', e, **variant) return err, 400 logger().info(struct_log(action='variant_created', **variant)) return variant, 201, {'Location': BASEPATH+'/variants/'+str(vid)}
def post_call(call): """ Add a new call """ db_session = orm.get_session() try: found_call = call_exists(**call) except orm.ORMException as e: err = _report_search_failed('call', e, **call) return err if found_call: err = _report_object_exists('call', **call) return err, 405 cid = uuid.uuid1() call['id'] = cid call['created'] = datetime.datetime.utcnow() call['updated'] = call['created'] try: orm_call = orm.models.Call(**call) except orm.ORMException as e: err = _report_conversion_error('call', e, **call) return err try: db_session.add(orm_call) db_session.commit() except orm.ORMException as e: err = _report_write_error('call', e, **call) return err logger().info(struct_log(action='call_post', status='created', call_id=str(cid), **call)) # noqa501 return call, 201, {'Location': BASEPATH+'/calls/'+str(cid)}
def post_individual(individual): """ Add a new individual """ db_session = orm.get_session() try: found_individual = individual_exists(db_session, **individual) except orm.ORMException as e: err = _report_search_failed('variant', e, **individual) return err if found_individual: err = _report_object_exists('individual', **individual) return err, 405 iid = uuid.uuid1() individual['id'] = iid individual['created'] = datetime.datetime.utcnow() individual['updated'] = individual['created'] try: orm_ind = orm.models.Individual(**individual) except orm.ORMException as e: err = _report_conversion_error('individual', e, **individual) return err, 400 try: db_session.add(orm_ind) db_session.commit() except orm.ORMException as e: err = _report_write_error('individual', e, **individual) return err, 500 logger().info(struct_log(action='individual_created', ind_id=str(iid), **individual)) return individual, 201, {'Location': BASEPATH+'/individuals/'+str(iid)}