Ejemplo n.º 1
0
def select_course(request, year, semester_type, slug, add=False):
    '''Handle selecting of courses from course list, change of names and
       removeall of courses'''

    # FIXME split ut three sub functions into seperate functions?

    try:
        semester = Semester.objects.get(year=year, type=semester_type)
    except Semester.DoesNotExist:
        return shortcuts.redirect(
            'schedule', year, Semester.localize(semester_type), slug)

    if request.method == 'POST':
        if 'submit_add' in request.POST or add:
            lookup = []

            for l in request.POST.getlist('course_add'):
                lookup.extend(l.replace(',', '').split())

            subscriptions = set(Subscription.objects.get_subscriptions(year,
                semester_type, slug).values_list('course__code', flat=True))

            errors = []
            to_many_subscriptions = False

            student, created = Student.objects.get_or_create(slug=slug)

            for l in lookup:
                try:
                    if len(subscriptions) > settings.TIMETABLE_MAX_COURSES:
                        to_many_subscriptions = True
                        break

                    course = Course.objects.get(
                            code__iexact=l.strip(),
                            semester__year__exact=year,
                            semester__type__exact=semester_type,
                        )

                    Subscription.objects.get_or_create(
                            student=student,
                            course=course,
                        )
                    subscriptions.add(course.code)

                except Course.DoesNotExist:
                    errors.append(l)

            if errors or to_many_subscriptions:
                return shortcuts.render(request, 'error.html', {
                        'courses': errors,
                        'max': settings.TIMETABLE_MAX_COURSES,
                        'slug': slug,
                        'year': year,
                        'type': semester_type,
                        'to_many_subscriptions': to_many_subscriptions,
                    })

            return shortcuts.redirect(
                'change-groups', year, Semester.localize(semester_type), slug)

        elif 'submit_remove' in request.POST:
            with transaction.atomic():
                courses = []
                for c in request.POST.getlist('course_remove'):
                    if c.strip():
                        courses.append(c.strip())

                Subscription.objects.get_subscriptions(year, semester_type, slug). \
                        filter(course__id__in=courses).delete()

                if Subscription.objects.filter(student__slug=slug).count() == 0:
                    Student.objects.filter(slug=slug).delete()

        elif 'submit_name' in request.POST:
            subscriptions = Subscription.objects.get_subscriptions(year, semester_type, slug)

            for u in subscriptions:
                form = forms.CourseAliasForm(request.POST, prefix=u.course_id)

                if form.is_valid():
                    alias = form.cleaned_data['alias'].strip()

                    if alias.upper() == u.course.code.upper() or alias == "":
                        # Leave as blank if we match the current course name
                        alias = ""

                    u.alias = alias
                    u.save()

    return shortcuts.redirect(
        'schedule-advanced', year, Semester.localize(semester_type), slug)
Ejemplo n.º 2
0
def schedule(request, year, semester_type, slug, advanced=False,
             week=None, all=False):
    '''Page that handels showing schedules'''
    current_week = get_current_week()
    if week:
        week = int(week)
        max_week = utils.max_number_of_weeks(year)
    if week is not None:
        if (week <= 0 or week > max_week):
            raise http.Http404

    # Color mapping for the courses
    color_map = utils.ColorMap(hex=True)

    try:
        semester = Semester.objects.get(year=year, type=semester_type)
    except Semester.DoesNotExist:
        raise http.Http404

    try:
        student = Student.objects.distinct().get(slug=slug, subscription__course__semester=semester)
    except Student.DoesNotExist:
        student = None

    # Start setting up queries
    courses = Course.objects.get_courses(year, semester.type, slug)
    lectures = Lecture.objects.get_lectures(year, semester.type, slug, week)
    exams = {}
    for exam in Exam.objects.get_exams(year, semester.type, slug):
        exams.setdefault(exam.course_id, []).append(exam)

    # Use get_related to cut query counts
    lecturers = Lecture.get_related(Lecturer, lectures)
    groups = Lecture.get_related(Group, lectures, fields=['code'])
    rooms = Lecture.get_related(Room, lectures, fields=['name', 'url'])
    weeks = Lecture.get_related(Week, lectures, fields=['number'], use_extra=False)

    schedule_weeks = set()
    for lecture_week_set in weeks.values():
        for lecture_week in lecture_week_set:
            schedule_weeks.add(lecture_week)

    schedule_weeks = list(schedule_weeks)
    schedule_weeks.sort()

    if schedule_weeks:
        schedule_weeks = range(schedule_weeks[0], schedule_weeks[-1]+1)

    next_week = None
    prev_week = None

    # TODO(adamcik): lookup actuall valid weeks.
    if week and week < max_week:
        next_week = week+1

    if week and week > 1:
        prev_week = week-1

    # Init colors in predictable maner
    for c in courses:
        color_map[c.id]

    # Create Timetable
    table = timetable.Timetable(lectures)

    if week:
        table.set_week(semester.year, week)

    if lectures:
        table.place_lectures()
        table.do_expansion()

    table.insert_times()
    table.add_markers()

    if advanced:
        subscriptions = Subscription.objects.get_subscriptions(year, semester.type, slug)

        # Set up and course name forms
        for course in courses:
            alias = course.alias or ''
            course.alias_form = forms.CourseAliasForm(
                initial={'alias': alias}, prefix=course.id)

    try:
        next_semester = Semester.objects.next()
        next_message = Subscription.objects.get_subscriptions(
            next_semester.year, next_semester.type, slug).count() == 0
    except Semester.DoesNotExist:
        next_semester = None
        next_message = False

    week_is_current = semester.year == today().year and week == current_week

    return shortcuts.render(request, 'schedule.html', {
            'advanced': advanced,
            'all': all,
            'color_map': color_map,
            'courses': courses,
            'current': (week == current_week),
            'current_week': current_week,
            'exams': exams,
            'next_message': next_message,
            'lectures': lectures,
            'semester': semester,
            'week_is_current': week_is_current,
            'next_semester': next_semester,
            'slug': slug,
            'timetable': table,
            'week': week,
            'next_week': next_week,
            'prev_week': prev_week,
            'rooms': rooms,
            'weeks': schedule_weeks,
            'groups': groups,
            'lecturers': lecturers,
            'lecture_weeks': weeks,
            'student': student,
        })