def gen_sponsor_schedule(user, sponsor=None, num_blocks=6, surrounding_blocks=None, given_date=None):
    r"""Return a list of :class:`EighthScheduledActivity`\s in which the
    given user is sponsoring.

    Returns:
        Dictionary with:
            activities
            no_attendance_today
            num_acts
    """

    no_attendance_today = None
    acts = []

    if sponsor is None:
        sponsor = user.get_eighth_sponsor()

    if surrounding_blocks is None:
        surrounding_blocks = EighthBlock.objects.get_upcoming_blocks(num_blocks)

    activities_sponsoring = (EighthScheduledActivity.objects.for_sponsor(sponsor).select_related("block").filter(block__in=surrounding_blocks))
    sponsoring_block_map = {}
    for sa in activities_sponsoring:
        bid = sa.block.id
        if bid in sponsoring_block_map:
            sponsoring_block_map[bid] += [sa]
        else:
            sponsoring_block_map[bid] = [sa]

    num_acts = 0

    for b in surrounding_blocks:
        num_added = 0
        sponsored_for_block = sponsoring_block_map.get(b.id, [])

        for schact in sponsored_for_block:
            acts.append(schact)
            if schact.block.is_today():
                if not schact.attendance_taken and schact.block.locked:
                    no_attendance_today = True

            num_added += 1

        if num_added == 0:
            # fake an entry for a block where there is no sponsorship
            acts.append({"block": b, "id": None, "fake": True})
        else:
            num_acts += 1

    logger.debug(acts)

    cur_date = surrounding_blocks[0].date if acts else given_date if given_date else datetime.now().date()

    last_block = surrounding_blocks[len(surrounding_blocks) - 1] if surrounding_blocks else None
    last_block_date = last_block.date + timedelta(days=1) if last_block else cur_date
    next_blocks = list(last_block.next_blocks(1)) if last_block else None
    next_date = next_blocks[0].date if next_blocks else last_block_date

    first_block = surrounding_blocks[0] if surrounding_blocks else None
    if cur_date and not first_block:
        first_block = EighthBlock.objects.filter(date__lte=cur_date).last()
    first_block_date = first_block.date + timedelta(days=-7) if first_block else cur_date
    prev_blocks = list(first_block.previous_blocks(num_blocks - 1)) if first_block else None
    prev_date = prev_blocks[0].date if prev_blocks else first_block_date
    return {
        "sponsor_schedule": acts,
        "no_attendance_today": no_attendance_today,
        "num_attendance_acts": num_acts,
        "sponsor_schedule_cur_date": cur_date,
        "sponsor_schedule_next_date": next_date,
        "sponsor_schedule_prev_date": prev_date
    }
def gen_sponsor_schedule ( user , sponsor = None , num_blocks = 6 , surrounding_blocks = None , given_date = None ) : no_attendance_today = None acts = [ ] if sponsor is None : sponsor = user . get_eighth_sponsor ( ) if surrounding_blocks is None : surrounding_blocks = EighthBlock . objects . get_upcoming_blocks ( num_blocks ) activities_sponsoring = ( EighthScheduledActivity . objects . for_sponsor ( sponsor ) . select_related ( "block" ) . filter ( block__in = surrounding_blocks ) ) sponsoring_block_map = { } for sa in activities_sponsoring : bid = sa . block . id if bid in sponsoring_block_map : sponsoring_block_map [ bid ] += [ sa ] else : sponsoring_block_map [ bid ] = [ sa ] num_acts = 0 for b in surrounding_blocks : num_added = 0 sponsored_for_block = sponsoring_block_map . get ( b . id , [ ] ) for schact in sponsored_for_block : acts . append ( schact ) if schact . block . is_today ( ) : if not schact . attendance_taken and schact . block . locked : no_attendance_today = True num_added += 1 if num_added == 0 : acts . append ( { "block" : b , "id" : None , "fake" : True } ) else : num_acts += 1 logger . debug ( acts ) cur_date = surrounding_blocks [ 0 ] . date if acts else given_date if given_date else datetime . now ( ) . date ( ) last_block = surrounding_blocks [ len ( surrounding_blocks ) - 1 ] if surrounding_blocks else None last_block_date = last_block . date + timedelta ( days = 1 ) if last_block else cur_date next_blocks = list ( last_block . next_blocks ( 1 ) ) if last_block else None next_date = next_blocks [ 0 ] . date if next_blocks else last_block_date first_block = surrounding_blocks [ 0 ] if surrounding_blocks else None if cur_date and not first_block : first_block = EighthBlock . objects . filter ( date__lte = cur_date ) . last ( ) first_block_date = first_block . date + timedelta ( days = - 7 ) if first_block else cur_date prev_blocks = list ( first_block . previous_blocks ( num_blocks - 1 ) ) if first_block else None prev_date = prev_blocks [ 0 ] . date if prev_blocks else first_block_date return { "sponsor_schedule" : acts , "no_attendance_today" : no_attendance_today , "num_attendance_acts" : num_acts , "sponsor_schedule_cur_date" : cur_date , "sponsor_schedule_next_date" : next_date , "sponsor_schedule_prev_date" : prev_date }
r"""Return a list of :class:`EighthScheduledActivity`\s in which the
    given user is sponsoring.

    Returns:
        Dictionary with:
            activities
            no_attendance_today
            num_acts