示例#1
0
def ajax_get_fill_all_form(request, course_id, object_type, object_id):
    model_base = Item if object_type == 'item' else Demonstration
    item_or_demonstration = get_object_or_404(model_base, pk=object_id)
    course = get_object_or_404(Course, pk=course_id)
    if type(item_or_demonstration
            ) == Item and item_or_demonstration.course != course:
        raise Exception('This Item does not belong to the specified Course.')
    if type(item_or_demonstration
            ) == Demonstration and item_or_demonstration.item.course != course:
        raise Exception(
            'This Demonstration does not belong to the specified Course.')
    if type(
            item_or_demonstration
    ) == Item and item_or_demonstration.category.allow_multiple_demonstrations:
        raise Exception(
            'Marks must be assigned to Demonstrations for this Item, not directly to the Item.'
        )
    if not item_or_demonstration.mark_set.count:
        raise Exception('This {} has no Marks.'.format(
            item_or_demonstration._meta.object_name))

    if request.POST:
        form = FillAllForm(request.POST, prefix="fill_all")
        try:
            marking_period = item_or_demonstration.marking_period
        except AttributeError:
            marking_period = item_or_demonstration.item.marking_period
        if not request.user.has_perm(
                'grades.change_grade'
        ) and marking_period is not None and not marking_period.active:
            # you aren't a registrar, so you can't modify an item from an inactive marking period
            form.fields['mark'].validators.append(
                make_validationerror_raiser(
                    'This {} belongs to the inactive marking period {}.'.
                    format(object_type, marking_period)))
        if form.is_valid():
            for m in item_or_demonstration.mark_set.all():
                m.mark = form.cleaned_data['mark']
                m.save()
            messages.success(
                request, 'Marked all students {} for {}'.format(
                    form.cleaned_data['mark'], item_or_demonstration))
            return HttpResponse('SUCCESS')
    else:
        form = FillAllForm(instance=item_or_demonstration.mark_set.all()[0],
                           prefix="fill_all")
    return render_to_response(
        'benchmark_grade/fill_all_form_fragment.html',
        {
            'action': request.path,
            'form': form,
            'subtitle': unicode(item_or_demonstration),
        },
        RequestContext(request, {}),
    )
示例#2
0
def ajax_get_fill_all_form(request, course_id, object_type, object_id):
    model_base = Item if object_type == 'item' else Demonstration
    item_or_demonstration = get_object_or_404(model_base, pk=object_id)
    course = get_object_or_404(Course, pk=course_id)
    if type(item_or_demonstration
            ) == Item and item_or_demonstration.course != course:
        raise Exception('This Item does not belong to the specified Course.')
    if type(item_or_demonstration
            ) == Demonstration and item_or_demonstration.item.course != course:
        raise Exception(
            'This Demonstration does not belong to the specified Course.')
    if type(
            item_or_demonstration
    ) == Item and item_or_demonstration.category.allow_multiple_demonstrations:
        raise Exception(
            'Marks must be assigned to Demonstrations for this Item, not directly to the Item.'
        )
    if not item_or_demonstration.mark_set.count:
        raise Exception('This {} has no Marks.'.format(
            item_or_demonstration._meta.object_name))

    if request.POST:
        form = FillAllForm(request.POST, prefix="fill_all")
        if form.is_valid():
            for m in item_or_demonstration.mark_set.all():
                m.mark = form.cleaned_data['mark']
                m.save()
            messages.success(
                request, 'Marked all students {} for {}'.format(
                    form.cleaned_data['mark'], item_or_demonstration))
            return HttpResponse('SUCCESS')
    else:
        form = FillAllForm(instance=item_or_demonstration.mark_set.all()[0],
                           prefix="fill_all")
    return render_to_response(
        'benchmark_grade/fill_all_form_fragment.html',
        {
            'action': request.path,
            'form': form,
            'subtitle': unicode(item_or_demonstration),
        },
        RequestContext(request, {}),
    )