Esempio n. 1
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)