예제 #1
0
def get_user_activity_report(request, ids, index, value, start_date, end_date, stats):
    """ CHW Group Total Activity Report - submissions per user over time

    ids: list of form id's
    index: title for the x-axis. something like, 'users', 'chws', etc. 
    value: title for the y-axis. usually corresponds to the form name(s)
    start_date: start of reporting period
    end_date: end of reporting period
    stats: any requested stats. 
    Returns a Report object populated with requested data. 
    
    """    

    domain = request.user.selected_domain
    if not ids: raise Exception("The requested form was not found")
    
    _report = Report("CHW Group Total Activity Report")
    _report.generating_url = request.path
    metadata = Metadata.objects.filter(timestart__gte=start_date)
    # the query below is used if you want to query by submission time (instead of form completion time)
    #metadata = Metadata.objects.filter(attachment__submission__submit_time__gte=start_date)
    
    # since we are working at a granularity of 'days', we want to make sure include 
    # complete days in our queries, so we round up
    timespan = get_timespan(start_date, end_date)
    delta = timedelta(days=timespan.days+1)
    metadata = metadata.filter(timeend__lt=start_date+delta)
    # the query below is used if you want to query by submission time (instead of form completion time)
    #metadata = metadata.filter(attachment__submission__submit_time__lte=end_date)
    
    dataset = DataSet( unicode(value[0]) + " per " + unicode(index) )
    dataset.indices = unicode(index)
    dataset.params = request.GET

    # when 'organization' is properly populated, we can start using that
    #       member_list = utils.get_members(organization)
    # for now, just use domain
    member_list = [r.chw_username for r in ReporterProfile.objects.filter(domain=domain).order_by("chw_username")]

    # get a sum of all forms
    visits_per_member = Values( "visits" )
    for member in member_list:
        visits_per_member.append( (member, metadata.filter(username=member).count()) )
    visits_per_member.run_stats(stats)
    visits_per_member.sort(key=operator.itemgetter(1), reverse=True) 
    dataset.valuesets.append( visits_per_member )
    
    # this report only requires the first form. you can imagine other reports doing 
    # this iteration: for id in ids:
    form_per_member = Values( unicode(value[0]) )
    form_metadata = metadata.filter(formdefmodel=ids[0])
    for member in member_list:
        # values are tuples of dates and counts
        form_per_member.append( (member, form_metadata.filter(username=member).count()) )
    form_per_member.run_stats(stats)
    form_per_member.sort(key=operator.itemgetter(1), reverse=True) 
    dataset.valuesets.append( form_per_member )
    
    _report.datasets.append(dataset)
    return _report
예제 #2
0
def get_daily_activity_report(request, ids, index, value, start_date, end_date, stats):
    """ CHW Daily Activity Report - submissions per day by user

    ids: list of form id's. this report returns the sum of all ids listed. 
    index: title for the x-axis. something like, 'day', 'session', etc. 
    value: title for the y-axis. usually corresponds to the form name(s)
    start_date: start of reporting period
    end_date: end of reporting period
    stats: any requested stats. 
    Returns a Report object populated with requested data. 
    
    """
    
    # TODO - filter returned data by user's domain
    # <HACK>
    # temporary hack to get pf api working. TODO - remove once 
    # we figure out user authentication/login from the mobile phone
    domain = Domain.objects.get(name='Pathfinder')

    # </HACK>
    # this is the correct way to do it. use this in the long term.
    # try:
    #    extuser = ExtUser.objects.get(id=request.user.id)
    # except ExtUser.DoesNotExist:
    #    return HttpResponseBadRequest( \
    #        "You do not have permission to use this API.")
    # domain = extuser.domain

    if request.GET.has_key('chw'): chw = request.GET['chw']
    else: raise Exception("This reports requires a CHW parameter")

    if not ids: raise Exception("The requested form was not found")

    # TODO - this currrently only tested for value lists of size 1. test. 
    _report = Report("CHW Daily Activity Report")
    _report.generating_url = request.path
    # when 'organization' is properly populated, we can start using that
    #       member_list = utils.get_members(organization)
    # for now, just use domain    
    member_list = [r.chw_username for r in ReporterProfile.objects.filter(domain=domain)]
    if chw not in member_list: raise Exception("No matching CHW could be identified")
    
    dataset = DataSet( unicode(value[0]) + " per " + unicode(index) )
    dataset.indices = unicode(index)
    dataset.params = request.GET
    
    values = get_daily_activity_values('Visits', None, chw, member_list, start_date, end_date, stats, domain)
    dataset.valuesets.append( values )
    
    form_list = FormDefModel.objects.filter(pk__in=ids)
    values = get_daily_activity_values(unicode(value[0]), form_list, chw, member_list, start_date, end_date, stats, domain)
    dataset.valuesets.append( values )
    
    _report.datasets.append(dataset)      
    return _report
예제 #3
0
def get_daily_activity_report(request, ids, index, value, start_date, end_date, stats):
    """ CHW Daily Activity Report - submissions per day by user

    ids: list of form id's. this report returns the sum of all ids listed. 
    index: title for the x-axis. something like, 'day', 'session', etc. 
    value: title for the y-axis. usually corresponds to the form name(s)
    start_date: start of reporting period
    end_date: end of reporting period
    stats: any requested stats. 
    Returns a Report object populated with requested data. 
    
    """
    
    domain = request.user.selected_domain
    
    if request.GET.has_key('chw'): chw = request.GET['chw']
    else: raise Exception("This reports requires a CHW parameter")

    if not ids: raise Exception("The requested form was not found")

    # TODO - this currrently only tested for value lists of size 1. test. 
    _report = Report("CHW Daily Activity Report")
    _report.generating_url = request.path
    # when 'organization' is properly populated, we can start using that
    #       member_list = utils.get_members(organization)
    # for now, just use domain    
    member_list = [r.chw_username for r in ReporterProfile.objects.filter(domain=domain)]
    if chw not in member_list: raise Exception("No matching CHW could be identified")
    
    dataset = DataSet( unicode(value[0]) + " per " + unicode(index) )
    dataset.indices = unicode(index)
    dataset.params = request.GET
    
    values = get_daily_activity_values('Visits', None, chw, member_list, start_date, end_date, stats, domain)
    dataset.valuesets.append( values )
    
    form_list = FormDefModel.objects.filter(pk__in=ids)
    values = get_daily_activity_values(unicode(value[0]), form_list, chw, member_list, start_date, end_date, stats, domain)
    dataset.valuesets.append( values )
    
    _report.datasets.append(dataset)      
    return _report