コード例 #1
0
ファイル: views.py プロジェクト: akarambir/django-sis
def ajax_get_demonstration_form(request, course_id, demonstration_id=None):
    ''' the transaction decorator helps, but people can still hammer the submit button
    and create tons of assignments. for some reason, only one shows up right away, and the rest
    don't appear until reload '''
    course = get_object_or_404(Course, pk=course_id)
    lists = None
    
    if request.POST:
        if demonstration_id:
            # modifying an existing demonstration
            demonstration = get_object_or_404(Demonstration, pk=demonstration_id)
            form = DemonstrationForm(request.POST, instance=demonstration, prefix="demonstration")
            if not request.user.has_perm('grades.change_grade') and not demonstration.item.marking_period.active:
                # you aren't a registrar, so you can't modify a demonstration from an inactive marking period
                form.fields['item'].validators.append(
                    make_validationerror_raiser('This demonstration belongs to the inactive marking period {}.'.format(demonstration.item.marking_period))
                )
        else:
            # creating a new demonstration
            form = DemonstrationForm(request.POST, prefix="demonstration")
            if not request.user.has_perm('grades.add_grade'):
                # you aren't a registrar, so make sure you can only select items in active marking periods
                form.fields['item'].validators.append(require_item_in_active_marking_period)
        if form.is_valid():
            demonstration = form.save()
            if demonstration_id is None:
                # a new demonstration; must create blank marks for each student
                for student in Student.objects.filter(course=course):
                    mark, created = Mark.objects.get_or_create(item=demonstration.item, demonstration=demonstration, student=student)

            # Should I use the django message framework to inform the user?
            # This would not work in ajax unless we make some sort of ajax
            # message handler.
            messages.success(request, '%s saved' % (demonstration,))
            return HttpResponse('SUCCESS')
        
    else:
        if demonstration_id:
            demonstration = get_object_or_404(Demonstration, pk=demonstration_id)
            form = DemonstrationForm(instance=demonstration, prefix="demonstration")
            # TODO: remove TC hard-coding
            if demonstration.item.category.name == 'Standards':
                students_missing = Student.objects.filter(mark__demonstration=demonstration, mark__mark__lt=3)
                if not students_missing: students_missing = ('None',)
                lists = ({'heading':'Students Missing This Demonstration', 'items':students_missing},)
        else:
            form = DemonstrationForm(initial={'course': course}, prefix="demonstration")
    
    form.fields['item'].queryset = Item.objects.filter(course=course,
                                                       category__display_in_gradebook=True, category__allow_multiple_demonstrations=True)

    return render_to_response('benchmark_grade/demonstration_form_fragment.html', {
        'form': form,
        'demonstration_id': demonstration_id,
        'lists': lists,
    }, RequestContext(request, {}),)
コード例 #2
0
def ajax_get_demonstration_form(request, course_id, demonstration_id=None):
    ''' the transaction decorator helps, but people can still hammer the submit button
    and create tons of assignments. for some reason, only one shows up right away, and the rest
    don't appear until reload '''
    course = get_object_or_404(Course, pk=course_id)
    lists = None

    if request.POST:
        if demonstration_id:
            demonstration = get_object_or_404(Demonstration,
                                              pk=demonstration_id)
            form = DemonstrationForm(request.POST,
                                     instance=demonstration,
                                     prefix="demonstration")
        else:
            form = DemonstrationForm(request.POST, prefix="demonstration")
        if form.is_valid():
            demonstration = form.save()
            if demonstration_id is None:
                # a new demonstration; must create blank marks for each student
                for student in Student.objects.filter(course=course):
                    mark, created = Mark.objects.get_or_create(
                        item=demonstration.item,
                        demonstration=demonstration,
                        student=student)
                    if created:
                        mark.save()

            # Should I use the django message framework to inform the user?
            # This would not work in ajax unless we make some sort of ajax
            # message handler.
            messages.success(request, '%s saved' % (demonstration, ))
            return HttpResponse('SUCCESS')

    else:
        if demonstration_id:
            demonstration = get_object_or_404(Demonstration,
                                              pk=demonstration_id)
            form = DemonstrationForm(instance=demonstration,
                                     prefix="demonstration")
            # TODO: remove TC hard-coding
            if demonstration.item.category.name == 'Standards':
                students_missing = Student.objects.filter(
                    mark__demonstration=demonstration, mark__mark__lt=3)
                if not students_missing: students_missing = ('None', )
                lists = ({
                    'heading': 'Students Missing This Demonstration',
                    'items': students_missing
                }, )
        else:
            form = DemonstrationForm(initial={'course': course},
                                     prefix="demonstration")

    form.fields['item'].queryset = Item.objects.filter(
        course=course,
        category__display_in_gradebook=True,
        category__allow_multiple_demonstrations=True)

    return render_to_response(
        'benchmark_grade/demonstration_form_fragment.html',
        {
            'form': form,
            'demonstration_id': demonstration_id,
            'lists': lists,
        },
        RequestContext(request, {}),
    )
コード例 #3
0
ファイル: views.py プロジェクト: fluentglobe/django-sis
def ajax_get_demonstration_form(request, course_id, demonstration_id=None):
    ''' the transaction decorator helps, but people can still hammer the submit button
    and create tons of assignments. for some reason, only one shows up right away, and the rest
    don't appear until reload '''
    course = get_object_or_404(Course, pk=course_id)
    lists = None

    if request.POST:
        if demonstration_id:
            # modifying an existing demonstration
            demonstration = get_object_or_404(Demonstration,
                                              pk=demonstration_id)
            old_demonstration = get_object_or_404(Demonstration,
                                                  pk=demonstration_id)
            form = DemonstrationForm(request.POST,
                                     instance=demonstration,
                                     prefix="demonstration")
            if not request.user.has_perm(
                    'grades.change_grade'
            ) and not demonstration.item.marking_period.active:
                # you aren't a registrar, so you can't modify a demonstration from an inactive marking period
                form.fields['item'].validators.append(
                    make_validationerror_raiser(
                        'This demonstration belongs to the inactive marking period {}.'
                        .format(demonstration.item.marking_period)))
        else:
            # creating a new demonstration
            form = DemonstrationForm(request.POST, prefix="demonstration")
            if not request.user.has_perm('grades.add_grade'):
                # you aren't a registrar, so make sure you can only select items in active marking periods
                form.fields['item'].validators.append(
                    require_item_in_active_marking_period)
        if form.is_valid():
            demonstration = form.save()
            if demonstration_id is None:
                # a new demonstration; must create blank marks for each student
                for student in Student.objects.filter(course=course):
                    mark, created = Mark.objects.get_or_create(
                        item=demonstration.item,
                        demonstration=demonstration,
                        student=student)
            else:
                # do we belong to a different Item?
                if old_demonstration.item_id != demonstration.item_id:
                    # update all our Marks to reference the new Item
                    for mark in Mark.objects.filter(
                            demonstration=demonstration):
                        mark.item = demonstration.item
                        mark.save()
                    # recalculate both Items
                    gradebook_recalculate_on_item_change(
                        demonstration.item, old_item=old_demonstration.item)
                    # is the old Item totally abandoned now?
                    if not old_demonstration.item.demonstration_set.count():
                        if old_demonstration.item.mark_set.count():
                            raise Exception(
                                'Stray Marks found after attempting to reassign last Demonstration.'
                            )
                        else:
                            # no Demonstrations are left. kill the Item.
                            old_demonstration.item.delete()

            # Should I use the django message framework to inform the user?
            # This would not work in ajax unless we make some sort of ajax
            # message handler.
            messages.success(request, '%s saved' % (demonstration, ))
            return HttpResponse('SUCCESS')

    else:
        if demonstration_id:
            demonstration = get_object_or_404(Demonstration,
                                              pk=demonstration_id)
            form = DemonstrationForm(instance=demonstration,
                                     prefix="demonstration")
            # TODO: remove TC hard-coding
            if demonstration.item.category.name == 'Standards':
                students_missing = Student.objects.filter(
                    mark__demonstration=demonstration, mark__mark__lt=3)
                if not students_missing: students_missing = ('None', )
                lists = ({
                    'heading': 'Students Missing This Demonstration',
                    'items': students_missing
                }, )
        else:
            form = DemonstrationForm(initial={'course': course},
                                     prefix="demonstration")

    form.fields['item'].queryset = Item.objects.filter(
        course=course,
        category__display_in_gradebook=True,
        category__allow_multiple_demonstrations=True)

    return render_to_response(
        'benchmark_grade/demonstration_form_fragment.html',
        {
            'form': form,
            'demonstration_id': demonstration_id,
            'lists': lists,
        },
        RequestContext(request, {}),
    )