def get_table_from_time_spents(time_spents, detail_level='month'):
    """
    Buid a time spent table based on given time spents and given level
    of detail (week, day or month).
    """
    result = {}
    for time_spent in time_spents:
        if detail_level is 'week':
            unit = str(time_spent.date.isocalendar()[1])
        elif detail_level is 'day':
            unit = str(time_spent.date.day)
        elif detail_level is 'year':
            unit = str(time_spent.date.year)
        else:
            unit = str(time_spent.date.month)
        person_id = str(time_spent.person_id)
        if unit not in result:
            result[unit] = {}
        if person_id not in result[unit]:
            result[unit][person_id] = 0
        result[unit][person_id] += time_spent.duration
    return result
