def getting_started(request, year, semester_type): '''Intial top level page that greets users''' try: semester = Semester.objects.get(year=year, type=semester_type) except Semester.DoesNotExist: raise http.Http404 try: next_semester = Semester.objects.next() except Semester.DoesNotExist: next_semester = None if next_semester and next_semester == semester: next_semester = None # Redirect user to their timetable if request.method == 'POST': schedule_form = forms.ScheduleForm(request.POST) if schedule_form.is_valid(): slug = schedule_form.cleaned_data['slug'] # TODO(adamcik): what should we do if current is empty? return schedule_current(request, semester.year, semester.type, slug) else: schedule_form = forms.ScheduleForm() context = Course.get_stats(semester=semester) context.update({ 'color_map': utils.ColorMap(hex=True), 'current': semester, 'next_semester': next_semester, 'schedule_form': schedule_form, }) return shortcuts.render(request, 'start.html', context)
def select_groups(request, year, semester_type, slug): '''Form handler for selecting groups to use in schedule''' courses = Course.objects.get_courses(year, semester_type, slug) course_groups = Course.get_groups(year, semester_type, [c.id for c in courses]) if request.method == 'POST': with transaction.atomic(): for c in courses: try: groups = course_groups[c.id] except KeyError: # Skip courses without groups continue group_form = forms.GroupForm(groups, request.POST, prefix=c.id) if group_form.is_valid(): subscription = Subscription.objects.get_subscriptions( year, semester_type, slug).get(course=c) subscription.groups = group_form.cleaned_data['groups'] return shortcuts.redirect('schedule-advanced', year, Semester.localize(semester_type), slug) color_map = utils.ColorMap(hex=True) subscription_groups = Subscription.get_groups(year, semester_type, slug) all_subscripted_groups = set() for groups in subscription_groups.values(): for group in groups: all_subscripted_groups.add(group) for c in courses: color_map[c.id] subscription_id = c.subscription_set.get(student__slug=slug).pk try: groups = course_groups[c.id] except KeyError: # Skip courses without groups continue initial_groups = subscription_groups.get(subscription_id, all_subscripted_groups) c.group_form = forms.GroupForm(groups, prefix=c.id, initial={'groups': initial_groups}) return shortcuts.render( request, 'select_groups.html', { 'semester': Semester(year=year, type=semester_type), 'slug': slug, 'courses': courses, 'color_map': color_map, })
def test_course_get_stats(self): semester = Semester.objects.get(year=2009, type=Semester.SPRING) actual = Course.get_stats(semester) self.assertEquals(3, actual.pop('slug_count')) self.assertEquals(3, actual.pop('course_count')) self.assertEquals(6, actual.pop('subscription_count')) stats = actual.pop('stats') self.assertEquals((3, 2, u'COURSE2', u'Course 2 full name'), stats[0]) self.assertEquals((2, 1, u'COURSE1', u'Course 1 full name'), stats[1]) self.assertEquals((1, 3, u'COURSE3', u'Course 3 full name'), stats[2])
def select_groups(request, year, semester_type, slug): '''Form handler for selecting groups to use in schedule''' courses = Course.objects.get_courses(year, semester_type, slug) course_groups = Course.get_groups(year, semester_type, [c.id for c in courses]) if request.method == 'POST': with transaction.atomic(): for c in courses: try: groups = course_groups[c.id] except KeyError: # Skip courses without groups continue group_form = forms.GroupForm(groups, request.POST, prefix=c.id) if group_form.is_valid(): subscription = Subscription.objects.get_subscriptions(year, semester_type, slug).get(course=c) subscription.groups = group_form.cleaned_data['groups'] return shortcuts.redirect( 'schedule-advanced', year, Semester.localize(semester_type), slug) color_map = utils.ColorMap(hex=True) subscription_groups = Subscription.get_groups(year, semester_type, slug) all_subscripted_groups = set() for groups in subscription_groups.values(): for group in groups: all_subscripted_groups.add(group) for c in courses: color_map[c.id] subscription_id = c.subscription_set.get(student__slug=slug).pk try: groups = course_groups[c.id] except KeyError: # Skip courses without groups continue initial_groups = subscription_groups.get(subscription_id, all_subscripted_groups) c.group_form = forms.GroupForm(groups, prefix=c.id, initial={'groups': initial_groups}) return shortcuts.render(request, 'select_groups.html', { 'semester': Semester(year=year, type=semester_type), 'slug': slug, 'courses': courses, 'color_map': color_map, })