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 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 select_lectures(request, year, semester_type, slug): '''Handle selection of lectures to hide''' if request.method == 'POST': excludes = request.POST.getlist('exclude') subscriptions = Subscription.objects.get_subscriptions(year, semester_type, slug) for subscription in subscriptions: if excludes: subscription.exclude = subscription.course.lecture_set.filter(id__in=excludes) else: subscription.exclude.clear() return shortcuts.redirect( 'schedule-advanced', year, Semester.localize(semester_type), slug)
def select_lectures(request, year, semester_type, slug): """Handle selection of lectures to hide""" if request.method == "POST": with transaction.atomic(): excludes = request.POST.getlist("exclude") subscriptions = Subscription.objects.get_subscriptions(year, semester_type, slug) for subscription in subscriptions: if excludes: subscription.exclude = subscription.course.lecture_set.filter(id__in=excludes) else: subscription.exclude.clear() return shortcuts.redirect("schedule-advanced", year, Semester.localize(semester_type), slug)
def select_lectures(request, year, semester_type, slug): '''Handle selection of lectures to hide''' if request.method == 'POST': with transaction.atomic(): excludes = request.POST.getlist('exclude') subscriptions = Subscription.objects.get_subscriptions(year, semester_type, slug) for subscription in subscriptions: if excludes: subscription.exclude = subscription.course.lecture_set.filter(id__in=excludes) else: subscription.exclude.clear() return shortcuts.redirect( 'schedule-advanced', year, Semester.localize(semester_type), slug)
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)
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) ) if not lookup: localized_semester = Semester.localize(semester_type) return shortcuts.redirect("schedule-advanced", year, localized_semester, slug) 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)