Пример #1
0
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)
Пример #2
0
def post_private_response(body, url=None):
    private_url = url if url is not None else get_private_token_url()
    auth_headers = build_auth_headers({'id': generate_uuid()}, CLIENT_ROLE)
    body['token'] = extract_private_token()
    response = requests.post(private_url,
                             data=json.dumps(body),
                             headers=auth_headers)
    return response.json(), response.status_code
Пример #3
0
def setup_node(json):
    if 'node_type' not in json:
        raise BadRequest("'node_type' must be specified")
    if json['node_type'] == NODE_TYPE_FILE and '.' not in json.get('name'):
        raise StorageBadRequest('please specify correct file name')
    if 'parent_id' not in json:
        json['parent_id'] = None
    if 'provider_id' not in json:
        json['provider_id'] = SERVER_DATA_PROVIDER_ID
    if 'description' not in json:
        json['description'] = ''
    if 'id' not in json:
        json['id'] = generate_uuid()
    with session_scope() as session:
        prepare_default_rights(json, session)
    return json
Пример #4
0
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)
Пример #5
0
def generate_order_number():
    return generate_uuid().replace('-', '')
Пример #6
0
def delete_private_response(private_url=None):
    private_url = private_url if private_url is not None else get_private_token_url(
    )
    auth_headers = build_auth_headers({'id': generate_uuid()}, CLIENT_ROLE)
    response = requests.delete(private_url, headers=auth_headers)
    return response.json(), response.status_code