def get_user_session(): ip = get_ip() token = g.token if "token" in g else '' clazz = pydoc.locate(USER_SESSION_CLASS) with session_scope() as session: user_session = session.query(clazz).filter_by(token=token, ip=ip).first() return to_json(user_session) if user_session is not None else empty_json_object()
def post_entities(body, clazz, array_name, setup_function=None, teardown_function=None): entity_name = clazz.__name__ json_array = body[array_name] is_sensitive_data = entity_name in SENSITIVE_DATA_CLASSES if is_sensitive_data: for json_entity in json_array: body['action'] = '' body['instance'] = '' if "id" not in json_entity: json_entity[ID] = generate_uuid() json_entity, code = post_private_response(body) if code != 200 or entity_name in SENSITIVE_DATA_EXCLUDED_FROM_MERGE_CLASSES: return make_json_response(json_entity, code) with session_scope() as session: for json_entity in json_array: body['action'] = '' body['instance'] = '' if is_sensitive_data: entity_id = json_entity[ID] json_entity = empty_json_object() json_entity[ID] = entity_id elif setup_function is not None: setup_function(json_entity, session) merge_entity(clazz, json_entity, body, session) pop_token() return success('POST /' + array_name, 200, 'Success', REST_GUIDE + entity_name, body['instance'], body['action'], teardown_function)
def fault(detail, error_code, status, title, entity_type=None, instance=None, action=None): json_fault = empty_json_object() json_fault['error_code'] = error_code add_system_parameters(json_fault, detail, status, title) parameters = request_parameters() if entity_type is None: entity_type = parameters.get('type') if instance is None: instance = parameters.get('instance') if action is None: action = parameters.get('action') if entity_type is not None: json_fault['type'] = entity_type if instance is not None: json_fault['instance'] = instance if action is not None: json_fault['action'] = action error('json.fault: %s, %s', status, json_fault) if "fault" in g: json_ext_fault = g.pop('fault') json_fault['fault'] = json_ext_fault return make_json_response(json_fault, status)
def build_log_stash_extra(response): request_parameters() extra = empty_json_object() build_base_info(extra) build_request_info(extra) build_response_info(extra, response) build_data_info(extra) build_session_info(extra) return extra
def get_sensitive_data_by_ids(ids, array_name, get_relation_entity_function): request = empty_json_object() request[BYIDS_FILTER] = ids request['token'] = extract_private_token() json, code = post_private_response(request) if code == 200 and get_relation_entity_function is not None: json_array = json[array_name] get_relation_entity_function(json_array) return make_json_response(json, code)
def build_request_info(extra): extra['request'] = empty_json_object() extra['request']['full_path'] = request.full_path if "request_parameters" in g: extra['request']['raw'] = 'request.raw:\n' + g.request_parameters[:10000] extra['request']['length'] = len(g.request_parameters) if "requested" in g: extra['request']['requested'] = convert_date_with_ms_to_string(g.requested) if "requested_ms" in g: extra['request']['processed'] = int(round(time.time() * 1000)) - g.requested_ms
def build_response_info(extra, response): json_response = json_to_string(response) extra['response'] = empty_json_object() extra['response']['raw'] = 'response.raw:\n' + json_response[:10000] extra['response']['length'] = len(json_response) extra['response']['returned'] = convert_date_with_ms_to_string(now()) if "status" in response: extra['response']['status'] = response['status'] if "error_code" in json_response: extra['response']['error_code'] = response['error_code']
def get_health(): returned_server_time = convert_date_to_string(now()) query = '''select now() as current_time from dual''' returned_database_time = first(text(query))['current_time'] info("server_time: %s; database_time: %s", returned_server_time, returned_database_time) processed = int(round(time.time() * 1000)) - g.requested_ms json = empty_json_object() json['processed_time_ms'] = processed json['returned_server_time'] = returned_server_time json['returned_database_time'] = returned_database_time return make_json_response(json, 200)
def build_session_info(extra): user_session = get_user_session() extra['user_session'] = empty_json_object() extra['user_session']['raw'] = 'user_session.raw:\n' + json_to_string(user_session) extra['user_id'] = g.user_id if "user_id" in g else \ user_session.get('person_id') if "person_id" in user_session else UNDEFINED if "platform" in user_session: extra['user_session']['platform'] = user_session['platform'] if "sender" in g: extra['sender'] = g.sender if "login" in g: extra['login'] = g.login
def post_node_users(session, clazz, body, users, user_key, node_id, node_instance): new_users = [] if users is not None: for user_id in users: body['instance'] = node_instance user_json = empty_json_object() user_json['node_id'] = node_id user_json[user_key] = user_id user_db = session.query(clazz).filter_by(**{ 'node_id': node_id, user_key: user_id }) merge_entity(clazz, user_json, body, session, 'node_id', user_db) new_users.append(user_id) session.query(clazz). \ filter_by(node_id=node_id). \ filter(getattr(clazz, user_key).notin_(new_users)). \ delete(synchronize_session=False)
def make_success(detail='Success', status=200, title='Success', entity_type=None, instance=None, action=None, post_function=None): json_success = empty_json_object() add_system_parameters(json_success, detail, status, title) add_entity_details(json_success) if entity_type is not None: json_success['type'] = entity_type if "delete" == action: if instance is not None: json_success['instance'] = instance json_success['action'] = action if post_function is not None: post_function(json_success) return json_success
def merge_entity(clazz, json, json_body, session, primary_key=ID, entity_db=None): entity_name = clazz.__name__ action = '' key_field = ENTITY_KEYS[entity_name] key_value = '' try: if primary_key not in json: json[primary_key] = generate_uuid() if entity_db is None: entity_db = session.query(clazz).filter_by( id=uuid.UUID(json[primary_key])) # https://docs.sqlalchemy.org/en/13/orm/query.html?highlight=update#sqlalchemy.orm.query.Query.with_for_update entity_db = entity_db.with_for_update() entity_first = entity_db.first() if entity_first is None: action = 'insert' entity = clazz(**json) session.add(entity) key_value = entity.__getattribute__(key_field) else: action = 'update' entity_db.update(json) key_value = json.get(key_field) except: raise finally: entity_id = str(json[primary_key]) if IMPACTED_ENTITIES not in json_body: json_body[IMPACTED_ENTITIES] = empty_json_object() json_body['type'] = REST_GUIDE + entity_name + 's' json_body['instance'] = \ (json_body['instance'] if "instance" in json_body else '') + '/' + entity_name.lower() + '/' + entity_id json_body['action'] = action add_impacted_entity(key_value, entity_name, json_body)
def init_body(): body = empty_json_object() body['instance'] = '' return body