def one_row(tuple, i, totals):
    """called by stats() below.
    prints one row of the stats table.
    accumulates the total as well.
    """

    response = ''
    person,recCount,itemCount,callsNoRec,calls,starCount,recDur,callDur = tuple
    totalCalls,totalRecorded,totalItems,totalListen,totalStars,\
        totalRecDur,totalCallDur = totals
    

    #
    # mugshot column
    #
    thumb = dsh_django_utils.thumbnail(person, person.mugshot)
    href = dsh_django_config.lookup('PERSON_DETAIL_URL') + str(person.id)
    mugLink = '<a href="%s">%s</a>' % (href, thumb)

    #
    # who column
    #
    name = person.__unicode__()
    href = dsh_django_config.lookup('PERSON_URL') + str(person.id)
    nameLink = '<a href="%s">%s</a>' % (href, name)

    org = person.organization
    orgName = org.alias
    href = dsh_django_config.lookup('ORG_URL') + str(org.id)
    orgLink = '<a href="%s">%s</a>' % (href, orgName)

    messagesIcon = person.person_item_link()

    whoLinks = nameLink + '<BR><BR>' + messagesIcon + '<BR><BR>' + orgLink

    #
    # all columns of one row.
    #
    response += '<TR>\n'
    response += '<TD><p align=center>%s</p></TD>\n' % (str(i),)
    response += '<TD>%s</TD>\n' % (mugLink,)
    response += '<TD>%s</TD>\n' % (whoLinks)

    #
    # total number of calls.
    #
    response += '<TD><p align=right>%s</p></TD>\n' % (str(calls),)
    totalCalls += calls

    #
    # long recorded items.
    # eg.:
    # /admin/db/item/?owner__id__exact=51&rec_duration__gt=10
    #
    href = '/admin/db/item/?rec_duration__gt=%s&owner__id__exact=%s' %\
           (dsh_django_config.lookup('RECORDED_THRESH'),
            str(person.id))
    recLink = '<a href="%s" title="long recorded items">%s</a>' %\
              (href, str(recCount))
    response += '<TD><p align=right>%s</p></TD>\n' % (recLink,)
    totalRecorded += recCount

    #
    # total count of items.
    # eg.:
    # /admin/db/item/?owner__id__exact=16
    #
    href = '/admin/db/item/?owner__id__exact='
    href += str(person.id)
    itemLink = '<a href="%s" title="all recorded items">%s</a>' %\
               (href, str(itemCount))
    response += '<TD><p align=right>%s</p></TD>\n' % (itemLink,)
    totalItems += itemCount

    #
    # calls without recording.
    # eg.:
    # /admin/db/event/?owner__id__exact=3&action__exact=CALL
    #
    href = '/admin/db/event/?action__exact=CALL&owner__id__exact='
    href += str(person.id)
    listenLink = '<a href="%s" title="listen without recording">%s</a>' %\
                 (href, str(callsNoRec))
    response += '<TD><p align=right>%s</p></TD>\n' % (listenLink,)
    totalListen += callsNoRec

    #
    # starred.
    # eg.:
    # /admin/db/item/?starred__exact=1&owner__id__exact=16
    #
    href = '/admin/db/item/?starred__exact=1&owner__id__exact='
    href += str(person.id)
    starLink = '<a href="%s" title="starred items">%s</a>' %\
               (href, str(starCount))
    response += '<TD><p align=right>%s</p></TD>\n' % (starLink,)
    totalStars += starCount

    #
    # record duration.
    #
    response += '<TD><p align=right>%s</p></TD>\n' % (str(recDur),)
    totalRecDur += recDur

    #
    # call duration.
    #
    response += '<TD><p align=right>%s</p></TD>\n' % (str(callDur),)
    totalCallDur += callDur

    response += '</TR>\n'

    totals = totalCalls,totalRecorded,totalItems,totalListen,totalStars,\
             totalRecDur,totalCallDur
    
    return (response,totals)
def demographics(request, dshUid, personTable):
    """
    10/03/13:
    called by views.demographics().
    """

    if dsh_django_request.deny_it(request):
        return views.please_log_in()

    response = views.page_header('demographics', includeMp3Player=True)
    person = dsh_django_utils.get_foreign_key(personTable, dshUid)

    if not person:
        message = 'dsh_common_views.demographics: no person found: ' + dshUid
        response += dsh_utils.red_error_break_msg(message)
        dsh_django_utils.error_event(message, errorLevel='ERR')
        response += page_footer()
        return HttpResponse(response)

    personEditUrl = dsh_django_config.lookup('PERSON_DETAIL_URL') + \
                    str(person.id)
    thumb = dsh_django_utils.thumbnail(
        person, person.mugshot, noCenter=True)
    url = '<a href=%s title="edit person details">%s</a>' % \
          (personEditUrl, thumb)

    response += url

    response += '<BR><BR>'
    response += '<TABLE BORDER=1>'

    name = person.__unicode__()
    if name:
        response += '<TR><TD>name</TD><TD>%s</TD></TR>' % (name,)

    if person.spoken_name:
        spokenName = person.spoken_name_display_field()
        if spokenName:
            response += '<TR><TD>spoken name</TD><TD>%s</TD></TR>' % \
                        (spokenName)

    response += '<TR><TD>dsh uid</TD><TD>%s</TD></TR>' % (person.dsh_uid,)

    if person.phone_number:
        response += '<TR><TD>phone</TD><TD>%s</TD></TR>' % \
                    (person.phone_number,)

    if person.organization and person.organization.alias:
        response += '<TR><TD>org.</TD><TD>%s</TD></TR>' % \
                    (person.organization.alias,)

    if person.ptype:
        response += '<TR><TD>type</TD><TD>%s</TD></TR>' % (person.ptype,)

    if person.gender:
        response += '<TR><TD>gender</TD><TD>%s</TD></TR>' % (person.gender,)
        
    if person.date_birth:
        if person.birth_date_approximate:
            dateStr = str(person.date_birth.year)
        else:
            dateStr = person.date_birth.isoformat()
        response += '<TR><TD>birth date</TD><TD>%s</TD></TR>' % (dateStr,)

    if person.birth_date_approximate:
        response += '<TR><TD>birth date approximate</TD><TD>True</TD></TR>'

    if person.date_birth:
        years = dsh_common_db.calculate_age(person)
        if years:
            response += '<TR><TD>age</TD><TD>%s</TD></TR>' % (str(years),)

    if person.modify_datetime:
        timeStr = person.modify_datetime.strftime(
            '%#Y-%#m-%#d %#H:%#M:%#S')
        response += '<TR><TD>modify time</TD><TD>%s</TD></TR>' % (timeStr,)
    
    response += '</TABLE>'
    
    response += views.mp3_widget_control()
    response += views.page_footer()
    return HttpResponse(response)