Ejemplo n.º 1
0
def add_template_post(request, parent_oid, template_oid):
    pm = helpers.PageManager(request, 'stweb/templates/apply_set.html')
    parent = pm.setVar('parent', pm.object_store.getOID(parent_oid))
    pm.path(parent)

    template = pm.object_store.getOID(template_oid)
    pm.render_var['template'] = template

    url = '/device/add/template/post/%s/%s/' % (parent_oid, template.oid)
    form = TemplateSetForm(template, request.POST)
    pm.addForm(form, url, 'create device')
    if not pm.form.is_valid():
        return pm.error('')

    apply_rules = []
    for key, value in request.POST.iteritems():
        if not key.startswith('apply-argument'):
            continue
        _, _, rule_oid = key.split('-')
        apply_rules.append(rule_oid)

    arguments = {}
    for key in pm.form.cleaned_data:
        if not key.startswith('argument-'):
            continue
        prefix, rule_oid = key.split('-', 1)
        arguments[rule_oid] = [pm.form.cleaned_data[key]]

    skip_rules = []
    for rule in template.combinedRules():
        if rule.oid not in apply_rules:
            skip_rules.append(rule.oid)
        if rule.apply_arguments == 0 and rule.oid in arguments:
            del arguments[rule.oid]
        if rule.class_name == 'template rule password':
            arguments[rule.oid] = [helpers.generate_password()]
        if rule.class_name == 'template rule subdevice':
            arguments[rule.oid] = [{}, arguments[rule.oid][0]]

    device = parent.add('device')
    overwrite = True
    try:
        template.apply(device, arguments, overwrite, skip_rules)
    except siptracklib.errors.SiptrackError, e:
        device.delete()
        return pm.error(str(e))
Ejemplo n.º 2
0
def add_template_post(request, parent_oid, template_oid):
    pm = helpers.PageManager(request, 'stweb/templates/apply_set.html')
    parent = pm.setVar('parent', pm.object_store.getOID(parent_oid))
    pm.path(parent)

    template = pm.object_store.getOID(template_oid)
    pm.render_var['template'] = template

    url = '/device/add/template/post/%s/%s/' % (parent_oid, template.oid)
    form = TemplateSetForm(template, request.POST)
    pm.addForm(form, url, 'create device')
    if not pm.form.is_valid():
        return pm.error('')

    apply_rules = []
    for key, value in request.POST.iteritems():
        if not key.startswith('apply-argument'):
            continue
        _, _, rule_oid = key.split('-')
        apply_rules.append(rule_oid)

    arguments = {}
    for key in pm.form.cleaned_data:
        if not key.startswith('argument-'):
            continue
        prefix, rule_oid = key.split('-', 1)
        arguments[rule_oid] = [pm.form.cleaned_data[key]]

    skip_rules = []
    for rule in template.combinedRules():
        if rule.oid not in apply_rules:
            skip_rules.append(rule.oid)
        if rule.apply_arguments == 0 and rule.oid in arguments:
            del arguments[rule.oid]
        if rule.class_name == 'template rule password':
            arguments[rule.oid] = [helpers.generate_password()]
        if rule.class_name == 'template rule subdevice':
            arguments[rule.oid] = [{}, arguments[rule.oid][0]]

    device = parent.add('device')
    overwrite = True
    try:
        template.apply(device, arguments, overwrite, skip_rules)
    except siptracklib.errors.SiptrackError, e:
        device.delete()
        return pm.error(str(e))