Пример #1
0
    def __init__(self, *args, **kwargs):

        node = kwargs.pop('node')

        super(CreateMergeForm, self).__init__(*args, **kwargs)

        self.fields['free_atoms'].queryset = ConceptAtom.get_unmerged_atoms(node)
        self.fields['merged_atoms'].queryset = ConceptAtom.get_final_atoms(node)
Пример #2
0
    def __init__(self, *args, **kwargs):

        node = kwargs.pop('node')

        super(CreateMergeForm, self).__init__(*args, **kwargs)

        self.fields['free_atoms'].queryset = ConceptAtom.get_unmerged_atoms(
            node)
        self.fields['merged_atoms'].queryset = ConceptAtom.get_final_atoms(
            node)
Пример #3
0
def merge(request, node, context):
    """
    This function gets all the concepts for a specified node and
    renders a form that allows an admin to merge/unmerge concept atoms
    """

    # Hack to disable the "admin" dropdown, since it makes no sense on the merge page.
    context['staff'] = False

    # Only if we're in the merging stage.
    if node.node_type != 'M':
        return render(request, 'nodemanager/stage_error.html')

    #populate the form with all the existing concept atoms for that node
    create_form = CreateMergeForm(node=node)
    edit_formset = UpdateMergeFormSet(initial=[{'pk': atom.pk} for atom in ConceptAtom.get_final_atoms(node)])

    context['create_form'] = create_form
    context['edit_formset'] = edit_formset

    return render(request, 'nodemanager/node_merge.html', context)
Пример #4
0
def get_merge(request, node_id, merge_type=None):
    """
    This function handles a submitted form containing information
    about merging concepts.
    """

    auth = ContributorProfile.auth_status(request.user)

    if auth != 'staff':
        raise PermissionDenied

    if request.method == 'POST':
        node = get_object_or_404(ConceptNode,pk=node_id)

        #differentiate between different forms
        if merge_type == 'add merge':
            form = CreateMergeForm(request.POST, node=node)
        elif merge_type == 'subtract merge':
            form = UpdateMergeFormSet(request.POST)
        else:
            #should never get here
            print "ERROR No Merge type was called"

        # note that "form" could be either a single form or a formset
        # depending on whether the user did a 'add merge' or an
        # 'update merge'. For this reason, we have to do an additional
        # attribute check
        if form.is_valid(): #could be a form or a formset depending on request

            # user merged under a new concept atom
            if hasattr(form,'add_merge_id') and form.add_merge_id in request.POST:
                new_atom = ConceptAtom(concept_node=node,
                                       user=request.user,
                                       text=form.cleaned_data.get('new_atom_name'),
                                       final_choice=True,)
                new_atom.save()
                new_atom.add_merge_atoms(form.cleaned_data.get('free_atoms'))
            #user merged atoms to an existing concept atom
            elif hasattr(form,'subtract_merge_id') and form.subtract_merge_id in request.POST:
                curr_atom = form.cleaned_data.get('merged_atoms')

                curr_atom.add_merge_atoms(form.cleaned_data.get('free_atoms'))

            #user has edited an existing concept atom
            else:
                formset = form #small name-change to enhance clarity

                for form in formset:

                    if form.cleaned_data.get('delete'):
                        atom = ConceptAtom.objects.filter(pk=form.cleaned_data.get('pk')).get()
                        atom.delete()
                    else:
                        for atom in form.cleaned_data.get('choices'):
                            atom.merged_atoms = None
                            atom.save()

            return redirect('merge', node_id=node.id)

        #the form is invalid, and we redirect the errors to the user
        else:
            render_args = {'node': node,
                           'user': request.user}

            #need to set arguments differently based on whether it was
            #an add merge or subtract merge, since this chooses
            #between a form or formset
            if merge_type == 'add merge':
                render_args['create_form'] = form
                render_args['edit_formset'] = UpdateMergeFormSet(initial=[{'pk': atom.pk} for atom in ConceptAtom.get_final_atoms(node)])

            elif merge_type == 'subtract merge':
                render_args['create_form'] = CreateMergeForm(node)
                render_args['edit_formset'] = form
            else:
                #again, should never be here
                print "ERROR unknown merge type"

            return render(request, 'nodemanager/merge.html', context)
    else:
        return redirect('stage1 dispatch')