コード例 #1
0
ファイル: views.py プロジェクト: OpenCSI/lbe
def modifyObject(request, obj_id=None, instance_id=None):
    objectForm = None
    lbeObjectTemplate = LBEObjectTemplate.objects.get(id=obj_id)
    if request.method == 'POST':
        # we can't modify the Synced_at value
        POST = request.POST.copy()
        POST['synced_at'] = lbeObjectTemplate.synced_at
        # POST modification
        objectForm = LBEObjectTemplateForm(POST, instance=lbeObjectTemplate)
        oldNAttribute = lbeObjectTemplate.instanceNameAttribute.name
        oldDNAttribute = lbeObjectTemplate.instanceDisplayNameAttribute.id
        if objectForm.is_valid():
            # change the _id value if changed:
            if not oldNAttribute == request.POST['instanceNameAttribute']:
                changeID = True
            else:
                changeID = False
                # change the displayName value if changed:
            if not oldDNAttribute == int(request.POST['instanceDisplayNameAttribute']):
                DN = True
            else:
                DN = False
            if changeID or DN:
                if changeID is True:
                    objectForm.instance.instanceNameBeforeAttribute = LBEAttribute.objects.get(
                        name__iexact=oldNAttribute)
                    objectForm.instance.needReconciliationRDN = True
                backend = BackendHelper()
                ob = backend.searchObjects(lbeObjectTemplate)
                try:
                    for o in ob:
                        if changeID:
                            # change the _id value
                            backend.update_id(lbeObjectTemplate, o,
                                              o.attributes[request.POST['instanceNameAttribute']][0])
                            # the RDN Attribute from Target Server is replace into the Reconciliation
                        if DN:
                            attribute = LBEAttribute.objects.get(id=request.POST['instanceDisplayNameAttribute'])
                            backend.modifyDisplayName(lbeObjectTemplate, o.name, o.attributes[attribute.name][0])
                    # Groups
                    if changeID:
                        groups = LBEGroup.objects.filter(objectTemplate=lbeObjectTemplate)
                        for group in groups:
                            InstanceHelper = GroupInstanceHelper(group)
                            InstanceHelper.changeIDObjects()
                except KeyError:
                    messages.add_message(request, messages.ERROR, 'Error while saving object, "' + request.POST[
                        'instanceNameAttribute'] + '" does not exist for the Object.')
                    return redirect('/config/object/modify/' + obj_id)
            objectForm.save()
            messages.add_message(request, messages.SUCCESS, 'Object saved')
            return redirect('/config/object/modify/' + obj_id)
        else:
            messages.add_message(request, messages.ERROR, 'Error while saving object.')
    else:
        if obj_id is None:
            messages.add_message(request, messages.INFO, 'Object id is missing.')
            return render_to_response('config/object/list.html', {'objects': LBEObjectTemplate.objects.all()})
        else:
            objectForm = LBEObjectTemplateForm(instance=lbeObjectTemplate)
    attForm = LBEAttributeInstanceForm()
    instances = LBEAttributeInstance.objects.filter(lbeObjectTemplate=lbeObjectTemplate).order_by('position')
    # which attribute have ajax request:
    ajaxAttribute = 'instanceNameAttribute'
    defaultValue = lbeObjectTemplate.instanceNameAttribute.name
    # Ajax function to call (js):
    ajaxFunction = 'selectFrom(\'' + reverse('config.views.showAttributeAJAX')[:-1] + '\',\'' + ajaxAttribute + '\');'
    info_missing_policy = "Variable used for setting if the Object is deleted into the Target or <br> if we need to add "
    info_missing_policy += " to the Backend"
    info_different_policy = "Variable enables to set which Server, we need to upgrade values:<br> If the value is TARGET"
    info_different_policy += ", then the Backend object will replace the Target object <br>else, the opposite."
    if lbeObjectTemplate.instanceNameBeforeAttribute is not None:
        attributeBefore = lbeObjectTemplate.instanceNameBeforeAttribute.name
    else:
        attributeBefore = lbeObjectTemplate.instanceNameAttribute.name
    return render_to_response('config/object/modify.html',
                              {'attributeInstances': instances, 'lbeObject': lbeObjectTemplate,
                               'objectForm': objectForm, 'attributeForm': attForm, 'ajaxAttribute': ajaxAttribute,
                               'ajaxFunction': ajaxFunction, 'defaultValue': defaultValue,
                               'info_missing_policy': info_missing_policy,
                               'info_different_policy': info_different_policy,
                               'attributeInstanceBefore': attributeBefore},
                              context_instance=RequestContext(request))
コード例 #2
0
ファイル: views.py プロジェクト: OpenCSI/lbe
def modifyObject(request, obj_id=None, instance_id=None):
    objectForm = None
    lbeObjectTemplate = LBEObjectTemplate.objects.get(id=obj_id)
    if request.method == 'POST':
        # we can't modify the Synced_at value
        POST = request.POST.copy()
        POST['synced_at'] = lbeObjectTemplate.synced_at
        # POST modification
        objectForm = LBEObjectTemplateForm(POST, instance=lbeObjectTemplate)
        oldNAttribute = lbeObjectTemplate.instanceNameAttribute.name
        oldDNAttribute = lbeObjectTemplate.instanceDisplayNameAttribute.id
        if objectForm.is_valid():
            # change the _id value if changed:
            if not oldNAttribute == request.POST['instanceNameAttribute']:
                changeID = True
            else:
                changeID = False
                # change the displayName value if changed:
            if not oldDNAttribute == int(
                    request.POST['instanceDisplayNameAttribute']):
                DN = True
            else:
                DN = False
            if changeID or DN:
                if changeID is True:
                    objectForm.instance.instanceNameBeforeAttribute = LBEAttribute.objects.get(
                        name__iexact=oldNAttribute)
                    objectForm.instance.needReconciliationRDN = True
                backend = BackendHelper()
                ob = backend.searchObjects(lbeObjectTemplate)
                try:
                    for o in ob:
                        if changeID:
                            # change the _id value
                            backend.update_id(
                                lbeObjectTemplate, o, o.attributes[
                                    request.POST['instanceNameAttribute']][0])
                            # the RDN Attribute from Target Server is replace into the Reconciliation
                        if DN:
                            attribute = LBEAttribute.objects.get(
                                id=request.POST['instanceDisplayNameAttribute']
                            )
                            backend.modifyDisplayName(
                                lbeObjectTemplate, o.name,
                                o.attributes[attribute.name][0])
                    # Groups
                    if changeID:
                        groups = LBEGroup.objects.filter(
                            objectTemplate=lbeObjectTemplate)
                        for group in groups:
                            InstanceHelper = GroupInstanceHelper(group)
                            InstanceHelper.changeIDObjects()
                except KeyError:
                    messages.add_message(
                        request, messages.ERROR,
                        'Error while saving object, "' +
                        request.POST['instanceNameAttribute'] +
                        '" does not exist for the Object.')
                    return redirect('/config/object/modify/' + obj_id)
            objectForm.save()
            messages.add_message(request, messages.SUCCESS, 'Object saved')
            return redirect('/config/object/modify/' + obj_id)
        else:
            messages.add_message(request, messages.ERROR,
                                 'Error while saving object.')
    else:
        if obj_id is None:
            messages.add_message(request, messages.INFO,
                                 'Object id is missing.')
            return render_to_response(
                'config/object/list.html',
                {'objects': LBEObjectTemplate.objects.all()})
        else:
            objectForm = LBEObjectTemplateForm(instance=lbeObjectTemplate)
    attForm = LBEAttributeInstanceForm()
    instances = LBEAttributeInstance.objects.filter(
        lbeObjectTemplate=lbeObjectTemplate).order_by('position')
    # which attribute have ajax request:
    ajaxAttribute = 'instanceNameAttribute'
    defaultValue = lbeObjectTemplate.instanceNameAttribute.name
    # Ajax function to call (js):
    ajaxFunction = 'selectFrom(\'' + reverse(
        'config.views.showAttributeAJAX'
    )[:-1] + '\',\'' + ajaxAttribute + '\');'
    info_missing_policy = "Variable used for setting if the Object is deleted into the Target or <br> if we need to add "
    info_missing_policy += " to the Backend"
    info_different_policy = "Variable enables to set which Server, we need to upgrade values:<br> If the value is TARGET"
    info_different_policy += ", then the Backend object will replace the Target object <br>else, the opposite."
    if lbeObjectTemplate.instanceNameBeforeAttribute is not None:
        attributeBefore = lbeObjectTemplate.instanceNameBeforeAttribute.name
    else:
        attributeBefore = lbeObjectTemplate.instanceNameAttribute.name
    return render_to_response('config/object/modify.html', {
        'attributeInstances': instances,
        'lbeObject': lbeObjectTemplate,
        'objectForm': objectForm,
        'attributeForm': attForm,
        'ajaxAttribute': ajaxAttribute,
        'ajaxFunction': ajaxFunction,
        'defaultValue': defaultValue,
        'info_missing_policy': info_missing_policy,
        'info_different_policy': info_different_policy,
        'attributeInstanceBefore': attributeBefore
    },
                              context_instance=RequestContext(request))