Exemplo n.º 1
0
def create_schedule_common(method, id_method, week_txt, day_txt=None,
                           **id_data):
    day = day_txt and int(day_txt) or -1
    try:
        group_id = id_method(**id_data)
        group_data = getattr(SOURCE, method)(group_id)
        week = week_from_txt(week_txt)
        week_txt_ru = localized_week(week_txt)
    except (KeyError, IndexError):
        raise Http404()

    def week_create(week):
        def pred(x):
            return x[0] == day
        day_data = locate(week, pred)
        if day_data is None:
            return []
        else:
            return [day_data]

    if week is None:
        if day >= 0:
            first = week_create(group_data.week(UPPER_WEEK))
            second = week_create(group_data.week(LOWER_WEEK))
        else:
            first = group_data.week(UPPER_WEEK)
            second = group_data.week(LOWER_WEEK)
        week_data = merged_weeks(first, second)
    else:
        raw_week_data = group_data.week(week)
        if day >= 0:
            week_data = [d.list() for (_, d) in week_create(raw_week_data)]
        else:
            week_data = [day.list() for (_, day) in raw_week_data]
    week_data = [d for d in week_data if d]
    if not week_data:
        lessons = [0]
    else:
        lessons = map(len, week_data)
    stats = {'days': len(week_data),
             'lessons': sum(lessons),
             'avg_lessons': sum(lessons) / len(lessons),
             'min_lessons': min(lessons),
             'max_lessons': max(lessons)}
    data = {'days': week_data,
            'week_url': week_txt,
            'day': day_txt,
            'week_ru': week_txt_ru,
            'current_week_url': current_week().name,
            'current_week_ru': localized_week(current_week().name),
            'dataset_id': group_id,
            'stats': stats,
            }
    return data
Exemplo n.º 2
0
def free_classrooms(request):
    conv = lambda x: x != '*' and x or None
    curr = current_lesson() or 1
    frm = int(request.GET.get('lesson', curr))
    len = int(request.GET.get('length', 1))
    timestamp = {
        'week': request.GET.get('week', current_week().name),
        'building': conv(request.GET.get('building', '*')),
        'day': int(request.GET.get('day', current_weekday())),
        'lessons': range(frm, frm + len)
    }
    buildings = AutoaddDict(lambda _: list())
    for classroom in SOURCE.free_classrooms(**timestamp):
        buildings[classroom.building].append(classroom)
    buildings = sorted(buildings.iteritems(), cmp=lambda a, b: cmp(a[0], b[0]))
    fbuildings = map(lambda x: {'name': x[0],
                                'classrooms': sorted(x[1], cmp=cid_cmp)},
                     buildings)
    inits = dict(request.GET.items())
    inits.update(timestamp)
    inits['lesson'] = frm
    data = {'form': FindFreeClassroomsForm(initial=inits),
            'buildings': fbuildings,
            'week_txt': timestamp['week']}
    return render_response(request, 'free_classrooms.html', data)
Exemplo n.º 3
0
def generic_today(request, method, id_factory, template, **data_id):
    week_txt = current_week().name
    day_txt = current_weekday()
    schedule = create_schedule_common(method, id_factory,
                                      week_txt, day_txt, **data_id)
    schedule['today'] = True
    return render_response(request, 'days_%s.html' % template, schedule)
Exemplo n.º 4
0
def week_from_txt(week_txt):
    try:
        return WEEK_NAME_MAP[week_txt]
    except KeyError:
        if week_txt == 'current':
            return current_week()
        else:
            raise
Exemplo n.º 5
0
def _week_type_to_number(week_type):
    if (util.current_week() == LOWER_WEEK) == bool(_rasp_vuzov_week_number(date.today()) % 2):
        # upper week is even
        week = 1 + int(week_type == UPPER_WEEK)
    else:
        # else lower week is even
        week = 1 + int(week_type == LOWER_WEEK)
    return week
Exemplo n.º 6
0
def localized_week(week_txt):
    try:
        return WEEK_NAME_MAP_RU[week_txt]
    except KeyError:
        week = current_week()
        return localized_week(week.name)