コード例 #1
0
ファイル: satori_api.py プロジェクト: drorgarti/SatoriLab
def person_to_person():
    """
    Get a paths from source person to target person (by IDs)
    This endpoint returns all paths leading from source person to target company via a referral
    ---
    tags:
      - paths
    parameters:
      - name: source_id
        in: query
        type: string
        description: source id of path
      - name: target_id
        in: query
        type: string
        description: target id of path
    responses:
      200:
        description: A list of paths sorted by strength. Each path contains array of segments. Each segment is made of [seg-start, relation-type, seg-end]
        schema:
          type: array
          items:
              properties:
                source_id:
                  type: string
                  description: The source id of the relation
                relation_type:
                  type: string
                  description: The type of the relation (e.g. EMPLOYEE_OF, TWITTER_FRIEND, etc.)
                target_id:
                  type: string
                  description: The target id of the relation
    """
    # Get source/target ids from request
    source_id = request.args.get('source_id', None)
    if source_id is None:
        return 'Missing source id parameter', 400
    target_id = request.args.get('target_id', None)
    if target_id is None:
        return 'Missing target id parameter', 400

    # Check that source/target exist
    if Store.get_person_by_aid(source_id) is None:
        return 'No person matching source id', 400
    if Store.get_person_by_aid(target_id) is None:
        return 'No person matching target id', 400

    try:
        paths = Store.get_paths_to_person(source_id, target_id)
    except Exception as e:
        tb = traceback.format_exc()
        return 'Exception %s raised trying to get path. %s' % (e, tb), 500

    # Return the paths as json with code 200
    response = app.response_class(response=json.dumps(paths),
                                  status=200,
                                  mimetype='application/json')
    return response
コード例 #2
0
ファイル: satori_api.py プロジェクト: drorgarti/SatoriLab
def person_relations_by_id(person_id):
    """
    Get a person relations by ID
    This endpoint returns all relations of a person by a given ID in a list format
    ---
    tags:
      - person
    parameters:
      - name: person_id
        in: path
        type: string
        description: id of person
        required: true
    responses:
      200:
        description: Returns a list of relations
        schema:
          type: array
          items:
              properties:
                source_id:
                  type: string
                  description: The source id of the relation
                relation_type:
                  type: string
                  description: The type of the relation (e.g. EMPLOYEE_OF, TWITTER_FRIEND, etc.)
                target_id:
                  type: string
                  description: The target id of the relation
                reltion_properties:
                  type: string
                  description: String with comma-separated key:value properties of this relation
      400:
        description: Bad request
      404:
        description: Person not found
    """
    person = Store.get_person_by_aid(person_id)
    if person is None:
        return 'Person with id %s not found' % person_id, 404

    relations = person.get_relations()
    data = []
    for source_aid, relation_type, target_aid, relation_properties in relations:
        # TODO: move relation_properties from string to array
        data_element = {
            'relation_type': relation_type,
            'relation_properties': relation_properties,
            'source_id': source_aid,
            'target_id': target_aid
        }
        data.append(data_element)
    response = app.response_class(response=json.dumps(data),
                                  status=200,
                                  mimetype='application/json')
    return response
コード例 #3
0
ファイル: satori_api.py プロジェクト: drorgarti/SatoriLab
def person_properties_by_id(person_id):
    """
    Get a person properties by ID
    This endpoint returns all properties of a person by a given ID in a key/value fashion
    ---
    tags:
      - person
    parameters:
      - name: person_id
        in: path
        type: string
        description: id of person
        required: true
    responses:
      200:
        description: A single user item
        schema:
          properties:
            property-1:
              type: string
              description: A property
              default: 'value-1'
            property-2:
              type: string
              description: A property
              default: 'value-2'
            property-N:
              type: string
              description: A property
              default: 'value-N'
      400:
        description: Bad request
      404:
        description: Person not found
    """
    if len(person_id) == 0:
        return 'Missing person id', 400

    person = Store.get_person_by_aid(person_id)
    if person is None:
        return 'Person with id %s not found' % person_id, 404

    data = person.get_properties()
    response = app.response_class(response=json.dumps(data),
                                  status=200,
                                  mimetype='application/json')
    return response
コード例 #4
0
ファイル: satori_api.py プロジェクト: drorgarti/SatoriLab
def import_contacts():
    """
    Import contacts from file
    Provide path to file, encoding and contacts are imported and enriched
    ---
    tags:
      - importer
    consumes:
      - application/x-www-form-urlencoded
      - multipart/form-data
      - application/json
    produces:
      - application/x-www-form-urlencoded
      - multipart/form-data
    parameters:
      - name: contacts_file
        in: formData
        type: file
        required: true
      - name: user_id
        in: formData
        type: string
        required: true
      - name: encoding
        in: formData
        type: string
        required: true
        default: "utf-8"
      - name: test_mode
        in: formData
        type: boolean
        required: true
        default: true
    responses:
      200:
        description: Please wait the calculation, you'll receive an email with results
    """

    user_id = request.form.get('user_id', None)
    if user_id is None:
        return 'Missing user_id in form parameters', 400
    encoding = request.form.get('encoding', None)
    if encoding is None:
        return 'Missing encoding in form parameters', 400
    test_mode = request.form.get('test_mode', None)
    if test_mode is None:
        return 'Missing test_mode in form parameters', 400
    else:
        test_mode = test_mode in ['True', 'true']

    try:
        file = request.files['contacts_file']
        extension = os.path.splitext(file.filename)[1]
        if extension != '.csv':
            return 'Not a CSV file. Contacts not uploaded', 400
        f_name = str(uuid.uuid4()) + extension
        upload_folder = GeneralConfig.UPLOAD_FOLDER
        file.save(os.path.join(upload_folder, f_name))
        contacts_file_json = json.dumps({'filename': f_name})
    except Exception as e:
        return 'Failed to upload contacts file. Server error: %s' % e, 500

    # Check if user is in DB and has full name
    user_person = Store.get_person_by_aid(user_id)
    if user_person is None:
        return 'Person with id %s not found. Import aborted.' % user_id, 400
    if P.FULL_NAME not in user_person.deduced:
        return 'Person with id %s has no full-name property. Import aborted.' % user_id, 400

    contacts_file_name = '%s\%s' % (GeneralConfig.UPLOAD_FOLDER, f_name)

    print('test_mode = %s, type(test_mode) = %s' %
          (test_mode, type(test_mode)))

    ci = CSVContactsImporter(path=contacts_file_name,
                             encoding=encoding,
                             source="GoogleContacts",
                             attribution_id=user_person.aid,
                             attribution_name=user_person.deduced[P.FULL_NAME],
                             mapping=CSVContactsImporter.google_mapping2,
                             test_import=test_mode)

    # TODO: have this done async
    ci.import_now()

    return 'Contacts imported successfully', 200
コード例 #5
0
ファイル: satori_api.py プロジェクト: drorgarti/SatoriLab
def person_to_company():
    """
    Get a paths from source person to target company (by IDs)
    This endpoint returns all paths leading from source person to target company via a referral
    ---
    tags:
      - paths
    parameters:
      - name: source_id
        in: query
        type: string
        description: source id of path
      - name: target_id
        in: query
        type: string
        description: target id of path
      - name: seniority
        in: query
        type: string
        enum:
          - C-Level
          - Senior
          - Not Senior
        description: Level of seniority of people leading to company
      - name: area
        in: query
        type: string
        enum:
          - Board
          - G&A
          - Communications
          - Consulting
          - Customer Service
          - Education
          - Engineering
          - Finance
          - Health Professional
          - Human Resources
          - Information Technology
          - Legal
          - Marketing
          - Operations
          - Product
          - Public Relations
          - Real Estate
          - Recruiting
          - Research
          - Sales
          - Business Development
        description: area of people we want to reach in target company
    responses:
      200:
        description: A list of paths sorted by strength. Each path contains array of segments. Each segment is made of [seg-start, relation-type, seg-end]
        schema:
          type: array
          items:
              properties:
                source_id:
                  type: string
                  description: The source id of the relation
                relation_type:
                  type: string
                  description: The type of the relation (e.g. EMPLOYEE_OF, TWITTER_FRIEND, etc.)
                target_id:
                  type: string
                  description: The target id of the relation
    """
    # Get source/target ids from request
    source_id = request.args.get('source_id', None)
    if source_id is None:
        return 'Missing source id parameter', 400
    target_id = request.args.get('target_id', None)
    if target_id is None:
        return 'Missing target id parameter', 400

    # Check that source/target exist
    if Store.get_person_by_aid(source_id) is None:
        return 'No person matching source id', 400
    if Store.get_company_by_aid(target_id) is None:
        return 'No company matching target id', 400

    # Extract seniority/area filters
    seniority = request.args.get('seniority', None)
    area = request.args.get('area', None)

    try:
        # TODO: instead of 'seniority' & 'area', we may have here a generic k/v property filter
        paths = Store.get_paths_to_company(source_id, target_id, seniority,
                                           area)
    except Exception as e:
        tb = traceback.format_exc()
        return 'Exception %s raised trying to get path. %s' % (e, tb), 500

    # Return the paths as json with code 200
    response = app.response_class(response=json.dumps(paths),
                                  status=200,
                                  mimetype='application/json')
    return response