Esempio n. 1
0
def teams(api, request=None, save_xml=None):
    """Retrieve a list of all teams this process (user) has access to.

    Parameters:
    request -  List which limits the amount of information returned. Must be a
               subset of:
                 ['parent_team__id', 'is_hidden', 'status', 'name',
                  'company_name', 'parent_team__name', 'company__reference',
                  'parent_team__reference', 'reference' , 'id']

    The teams may be from different companies.
    Results of this GET depend on the permissions granted to the authenticated
    user of this process- the oDesk user and password used to access the API.

    Returns:
    List of dict objects- one per team.
    """
    url = urls.get_API_URL('teams')
    if save_xml: save_xml = 'teams.xml'
    response = send_GET(api, url, save_xml=save_xml)
    if response is None:
        log.error("request failed: send_GET(api, {0:s}".format(url))
        return []
    teams = list_from_xml(response, 'team')
    all_info = ['parent_team__id', 'is_hidden', 'status', 'name',
                'company_name', 'parent_team__name', 'company__reference',
                'parent_team__reference', 'reference' , 'id']
    if not request:
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(list(request), all_info)
    return dict_subset(teams, requested_info)
Esempio n. 2
0
def team_users(api, team_name, request=None, eid=None,
                                                save_xml=None, debug=False):
    """Retrieve details of team members.

    Parameters:

    team_name - Name of team. Should be identical to a team returned by 
                company_teams(api, 'company name')

    request   - List which limits the amount of information returned. Must be
                a subset of:

                   ['last_name', 'first_name', 'status', 'id', 'reference',
                                'is_provider', 'timezone', 'timezone_offset']

                These are the keys in the dict object(s) returned.

    eid       - oDesk user id. This is the same as the 'id' above. If given
                a list of one dict object for a team member matching 'id' is
                returned.

    Return:
    A list of one dict object if 'eid' parameter is given, or a list of N dict
    objects where N equals the number of people on the named team.
    If 'request' is not given the dict object contains all information for each
    team member.  Otherwise each item in 'request' which exactly matchs one of
    the valid identifiers given above is returned in each dict object generated.
    """
    userlist = []
    (team_ref, parent_team_ref, company_ref) = \
                                    team_reference_IDs(api, team_name, save_xml)
    if not team_ref:
        log.warning("No results from team_users(api, '{0:s}')".format(team_name))
        return userlist
    log.info("fetching team {0:s} users".format(team_name))
    url = urls.get_API_URL('team_users', team_ref=team_ref)
    log.debug('URL: {0:s}'.format(url))
    if save_xml: save_xml = 'team_users.xml'
    response = send_GET(api, url, save_xml=save_xml)
    if response is None:
        log.error("request failed: send_GET(api, {0:s}".format(url))
        return userlist
    tmplist = list_from_xml(response, 'user', debug=debug)
    if eid:
        for user in tmplist:
            if user['id'] == eid:
                userlist.append(user)
                break
    else:
        userlist = tmplist[:]
    all_info = ['last_name', 'first_name', 'status', 'id', 'reference',
                            'is_provider', 'timezone', 'timezone_offset']
    if not request:
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(list(request), all_info)
    return dict_subset(userlist, requested_info)
Esempio n. 3
0
def jobs_managed(api, company, team, request=None, **kwargs):
    """Compile a list of open jobs managed under 'company'."""
    # Need team reference for 'buyer_team_reference' parameter to Jobs HR API
    bad_result = (0, None)
    (ref, status) = find_team(api, company, team)
    if not ref:
        log.error("Failed to find team {0:s} (company {1:s})".format(team, company))
        return bad_result
    node_name = 'job'
    params = [('buyer_team__reference', ref)]
    if kwargs:
        keys = kwargs.keys()
        for key in keys:
            param = valid_jobs_managed_parameter(key)
            if param:
                params.append((param, kwargs[key]))
            elif key == 'save_xml':
                save_xml = 'jobs_managed' if kwargs[key] else None
    url = get_API_URL('jobs_hr')
    xml = send_GET(api, url, params, save_xml=save_xml)
    if xml is None:
        e = "Error, request failed: send_GET(api, {0:s}, {1!s}".format(url, params)
        stderr("{0:s}\n".format(e))
        log.error(e)
        return bad_result
    total_items = int(xml_get_tag(xml, 'total_items'))
    all_info = ['buyer_company__name','buyer_company__reference','buyer_team__id',
        'buyer_team__name','buyer_team__reference','category','company__reference',
        'company_name','created_by','created_by_name','created_time','description',
        'duration','end_date','filled_date','id','job_type','name',
        'num_active_candidates','num_candidates','num_new_candidates',
        'owner_user_id','parent_team__id','parent_team__name',
        'parent_team__reference','public_url','reference','start_date','status',
        'subcategory','title','visibility']
    if not request:
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(request, all_info)
    if total_items > 0:
        result = dict_subset(list_from_xml(xml, node_name), requested_info)
    else:
        result = []
    return (total_items, result)
Esempio n. 4
0
def company_users(api, company_name, request=None, save_xml=None, debug=False):
    """Retrieve list of contractors working for 'company_name'

    Parameters:
    company_name - Target company of query. Should be identical to a name returned
                   by odesk.api.query.organization:companies(api, request)

    request - List which limits the amount of information returned for each user.
              Must be a subset of:
                [timezone, reference, status, timezone_offset, id,
                 is_provider, last_name, first_name]

    This ultimately depends on the success of api.query.company_details().
    That GET depends on the permissions granted to the authenticated user of
    this process- the oDesk user and password used to access the API.

    Return:
    List of dictionary objects- one per employee.
    """
    userlist = []
    details = company_details(api, company_name, save_xml=False)
    if not details:
        warn = "No results from company_details(api, '{0:s}')"
        log.warning(warn.format(company_name))
        return userlist
    log.info("fetching company {0:s} users".format(company_name))
    url = urls.get_API_URL('company_users', company_ref=details['reference'])
    log.debug('URL: {0:s}'.format(url))
    if save_xml: save_xml = 'company_users.xml'
    response = send_GET(api, url, save_xml=save_xml)
    if response is None:
        log.error("request failed: send_GET(api, {0:s}".format(url))
        return userlist
    userlist = list_from_xml(response, 'user', debug=debug)
    all_info = ['timezone', 'reference', 'status', 'timezone_offset',
                'id', 'is_provider', 'last_name', 'first_name']
    if not request:
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(request, all_info)
    return dict_subset(userlist, requested_info)
Esempio n. 5
0
def company_teams(api, company_name, request=None, save_xml=None):
    """Retrieve a list of teams within a company.

    Parameters:
    api - Instance of class oDeskAPI
    company_name - Target company of query. Should be identical to a name returned
                   by api.query.organization:companies(api, request)

    request - List which limits the amount of information returned for each team.
              Must be a subset of:

              ['parent_team__id', 'is_hidden', 'status', 'name', 'company_name',
               'parent_team__name', 'company__reference, 'parent_team__reference',
               'reference', 'id']

    Return:   
    A list of dictionary objects describing each team in the company. Default
    is to return all information. 

    Results depend on the access permission of the currently authorized user.
    """
    node_tag_name = 'team'
    details = company_details(api, company_name, request=['reference'],
                                                            save_xml=save_xml)
    if not details: return None
    refId = details['reference']
    url = urls.get_API_URL('company_teams', company_ref=refId)
    if save_xml: save_xml = 'company_teams.xml'
    xml = send_GET(api, url, save_xml=save_xml)
    if xml is None:
        log.error("request failed: send_GET(api, {0:s}".format(url))
        return None
    team_list = list_from_xml(xml, node_tag_name)
    all_info = ['parent_team__id', 'is_hidden', 'status', 'name', 'company_name',
               'parent_team__name', 'company__reference', 'parent_team__reference',
               'reference', 'id']
    if not request:               
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(request, all_info)
    return dict_subset(team_list, requested_info)
Esempio n. 6
0
def engagement_details(api, reference_no, request=None, save_xml=None):
    """Fetch details on one engagements.
    
    Parameter 'reference_no' is the engagement reference number- same as
    'reference' returned from the query api.query.engagements(api, team)

    Parameter 'request' if given limits the information returned to a subset of: 

      [buyer_team__id, buyer_team__reference, created_time, engagement_job_type,
       engagement_start_date, engagement_title, estimated_duration,
       estimated_duration_id, hourly_charge_rate, hourly_pay_rate,
       job__reference, job__title, modified_time, offer__reference, provider__id,
       provider__reference, reference, role, status, weekly_hours_limit,
       weekly_salary_charge_amount, weekly_salary_pay_amount]

    Return value is a list of dicionary objects, one for each 'engagement'
    the query returns.
    """
    all_info = ['buyer_team__id', 'buyer_team__reference', 'created_time',
        'engagement_job_type', 'engagement_start_date', 'engagement_title',
        'estimated_duration', 'estimated_duration_id', 'hourly_charge_rate',
        'hourly_pay_rate', 'job__reference', 'job__title', 'modified_time',
        'offer__reference', 'provider__id', 'provider__reference', 'reference',
        'role', 'status', 'weekly_hours_limit', 'weekly_salary_charge_amount',
        'weekly_salary_pay_amount']
    if not request:
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(request, all_info)
    node_name = 'engagement'
    url = get_API_URL('engagement', engagement_ref=reference_no)
    xml = send_GET(api, url)
    if xml is None:
        e = "Error, request failed: send_GET(api, {0:s})".format(url)
        stderr("{0:s}\n".format(e))
        log.error(e)
        return None
    result = list_from_xml(xml, node_name)
    details = dict_subset(result, requested_info)
    return details[0]
Esempio n. 7
0
def user_roles(api, request=None, save_xml=None):
    """Retrieve permission information of user.

    For the currently authenticated user return a list of permission information
    for each company/team the user has access to.

    Parameter 'request' is a list which limits the information returned. Must
    be a subset of:

        ['affiliation_status', 'company__name', 'company__reference',
         'engagement__reference', 'has_team_room_access', 'parent_team__id',
         'parent_team__name', 'parent_team__reference', 'permission',
         'reference', 'role', 'team__id', 'team__name', 'team__reference',
         'user__first_name', 'user__id', 'user__is_provider', 'user__last_name',
         'user__reference']

    """
    url = urls.get_API_URL('user_roles')
    xmlnode = 'userrole'
    if save_xml: save_xml = 'user_roles'
    xml = send_GET(api, url, save_xml=save_xml)
    if xml is None:
        e = "Error, request failed: send_GET(api, {0:s}".format(url)
        stderr("{0:s}\n".format(e))
        log.error(e)
        return None
    roles = list_from_xml(xml, xmlnode)
    all_info = ['affiliation_status', 'company__name', 'company__reference',
         'engagement__reference', 'has_team_room_access', 'parent_team__id',
         'parent_team__name', 'parent_team__reference', 'permission',
         'reference', 'role', 'team__id', 'team__name', 'team__reference',
         'user__first_name', 'user__id', 'user__is_provider', 'user__last_name',
         'user__reference']
    if not request:
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(list(request), all_info)
    return dict_subset(roles, requested_info)
Esempio n. 8
0
def job_details(api, job_reference, request=None, **kwargs):
    """Find all available information for a particular job."""
    # Need team reference for 'buyer_team_reference' parameter to Jobs HR API
    (ref, status) = find_team(api, company, team)
    if not ref:
        log.error("Failed to find team {0:s} (company {1:s})".format(team, company))
        return None
    node_name = 'job'
    save_xml = None
    params = [('buyer_team__reference', ref)]
    if kwargs:
        keys = kwargs.keys()
        for key in keys:
            param = valid_jobs_managed_parameter(key)
            if param:
                params.append((param, kwargs[key]))
            elif key == 'save_xml':
                save_xml = kwargs[key]
    url = get_API_URL('jobs_hr')
    xml = send_GET(api, url, params, save_xml=save_xml)
    if xml is None:
        e = "Error, request failed: send_GET(api, {0:s}, {1!s}".format(url, params)
        stderr("{0:s}\n".format(e))
        log.error(e)
        return None
    result = list_from_xml(xml, node_name)
    all_info= ['reference','title','job_type','description','public_url',
        'created_time','created_by','start_date','end_date','filled_date',
        'cancelled_date','buyer_team__reference','buyer_team__id',
        'buyer_team__name','buyer_company__reference','buyer_company__name',
        'visibility','budget','duration','category','subcategory','num_candidates',
        'num_active_candidates','num_new_candidates','last_candidacy_access_time',
        'status'] 
    if not rquest:
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(request, all_info)
    return dict_subset(result, all_info)
Esempio n. 9
0
def company_details(api, company_name, request=None, save_xml=None):
    """Retrieve details of a specific company.

    Return a dictionary object containing the details for 'company_name'. If
    currently authenticated user (this process) does not have access to this
    company return None.

    Parameters:
    company_name - 'My Company', not company id or reference

    request - List which limits the amount of information returned.
              Must be a subset of:
                ['reference', 'status', 'name', 'owner_user_id']

    Return:
    A dictionary object describing the company. 
    Default is to return all information.
    """
    refId = company_reference_ID(api, company_name, save_xml=save_xml)
    if refId == -1: return None
    node_name = 'company'
    url = urls.get_API_URL('company_details', company_ref=refId)
    if save_xml: save_xml = 'company_details.xml'
    xml = send_GET(api, url, save_xml=save_xml)
    if xml is None:
        log.error("request failed: send_GET(api, {0:s}".format(url))
        return None
    details = list_from_xml(xml, node_name)
    if len(details) != 1:
        return None
    all_info = ('reference', 'status', 'name', 'owner_user_id')
    if not request:
        requested_info = all_info
        log.debug('requested_info: {0!r}')
    else:
        requested_info = list_intersect(request, all_info)
    details = dict_subset(details, requested_info)
    return details[0]
Esempio n. 10
0
def companies(api, request=None, save_xml=None):
    """Retrieve a list of all companies this process (user) has access to.

    Parameters:
    request - List which limits the amount of information returned for each
              company the user has access to.  Must be a subset of:
                [reference, status, name, owner_user_id]

    """
    node_name = 'company'
    url = urls.get_API_URL('companies')
    if save_xml: save_xml = 'companies.xml'
    xml = send_GET(api, url, save_xml=save_xml)
    if xml is None:
        log.error("request failed: send_GET(api, {0:s}".format(url))
        return None
    company_list = list_from_xml(xml, node_name)
    all_info = ['reference', 'status', 'name', 'owner_user_id']
    if not request:
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(request, all_info)
    return dict_subset(company_list, requested_info)
Esempio n. 11
0
def team_engagements(api, team, request=None, **kwargs):
    """Fetch details on one or more engagements for given team.
    
    Parameter  'team' is the team name, not integer reference or team Id.

    Parameter 'request', if given, limits the information returned to a
    subset of: 

    [buyer_team__id, buyer_team__reference, created_time, 
     engagement_job_type, engagement_start_date,
     engagement_title, estimated_duration, estimated_duration_id,
     hourly_charge_rate, hourly_pay_rate, job__reference, job__title,
     offer__reference, provider__id, provider__reference, 
     provider_team__reference, reference, role,
     status, weekly_hours_limit, weekly_salary_charge_amount,
     weekly_salary_pay_amount, weekly_stipend_hours]

    Parameter kwargs contains one or more of the following API parameters:
        'job__reference'
        'include_sub_teams'         # 0|1
        'provider__reference'
        'agency_team__reference'
        'status'                    # 'active' or 'closed'
        'created_time_from'         # yyyy-mm-dd HH:MM:SS
        'created-time_to'           # yyyy-mm-dd HH:MM:SS
        'page'                      # page=offset;count
        'order_by'                  # order_by=
     
    Return value is a list of dicionary objects, one for each 'engagement'
    the query returns.
    """
    iam = func()
    from odapi.query.organization import team_reference_ID
    ref = team_reference_ID(api, team)
    if not ref:
        log.error("Failed to find team {0:s}".format(team))
        return None
    node_name = 'engagement'
    save_xml = None

    # These are all the fields returned by this query
    # Note: 'reference' is the number needed to make a bonus payment to
    #       a provider.
    all_info = ['buyer_team__id', 'buyer_team__reference', 'created_time',
       'description', 'engagement_end_date', 'engagement_job_type',
       'engagement_start_date', 'engagement_title', 'estimated_duration',
       'estimated_duration_id', 'hourly_charge_rate', 'hourly_pay_rate',
       'job__reference', 'job__title', 'offer__reference', 'provider__id',
       'provider__reference', 'provider_team__id', 'provider_team__reference',
       'reference', 'role', 'status', 'weekly_hours_limit',
       'weekly_salary_charge_amount', 'weekly_salary_pay_amount',
       'weekly_stipend_hours']

    if not request:
        requested_info = all_info[:]
    else:
        requested_info = list_intersect(request, all_info)
    params = [('buyer_team__reference', ref)]
    if kwargs:
        keys = kwargs.keys()
        for key in keys:
            param = valid_engagement_parameter(key)
            if param:
                params.append((param, kwargs[key]))
            elif key == 'save_xml':
                if kwargs[key]:
                    save_xml = '{0:s}.xml'.format(iam)
    url = get_API_URL('engagements')
    offset=0
    count=10
    engagements = []
    while 1:
        # page parameter is page=offset;count
        params.append(('page', '{0:d};{1:d}'.format(offset, count)))
        xml = send_GET(api, url, params, save_xml=save_xml)
        if xml is None:
            e = "Error, request failed: send_GET(api, {0:s}, {1!s})"
            estr = e.format(url, params)
            stderr("{0:s}\n".format(estr))
            log.error(estr)
            return None
        if save_xml:
            with open(save_xml, 'w') as f:
                f.write(xml)   
        result = list_from_xml(xml, node_name)
        if len(result) == 0: break
        engagements.extend(result)
        if len(result) < count: break
        params.pop()
        offset += count
    return dict_subset(engagements, requested_info)