Esempio n. 1
0
def import_form(request):
    if request.method == "POST":
        json_file = request.FILES['file']
        json_string = json_file.read()
        json_data = json.loads(json_string)

        template_options = json_data["template"]
        form_options = json_data["form"]

        template = ConfigTemplate()
        template.name = template_options["name"]
        template.description = template_options["description"]
        template.action_provider = template_options["action_provider"]
        template.action_provider_options = template_options["action_provider_options"]
        template.type = template_options["type"]
        template.template = unquote(template_options["template"])

        template.save()

        input_form = InputForm()
        input_form.name = form_options["name"]
        input_form.description = form_options["description"]
        input_form.instuctions = form_options["instructions"]
        input_form.json = unquote(form_options["json"])
        input_form.script = template

        input_form.save()

        return HttpResponseRedirect("/input_forms")
    else:
        form = ImportForm()
        context = {'form': form }
        return render(request, 'input_forms/import.html', context)
Esempio n. 2
0
def create(request):
    required_fields = set(["name", "description", "template", "type"])
    if not required_fields.issubset(request.POST):
        return render(request, "error.html",
                      {"error": "Invalid Parameters in POST"})

    template = ConfigTemplate()
    template.name = request.POST["name"]
    template.description = request.POST["description"]
    template.action_provider = request.POST["action_provider"]
    template.template = request.POST["template"].strip().replace('\r\n', '\n')
    template.type = request.POST["type"]

    if "new_template_action_options" not in request.session:
        return render(request, "error.html",
                      {"error": "Invalid session data!"})

    configured_action_options = request.session["new_template_action_options"]
    template.action_provider_options = json.dumps(configured_action_options)
    print("action options are:")
    print(configured_action_options)
    print("Saving form")
    template.save()
    return HttpResponseRedirect("/input_forms/view_from_template/%s" %
                                template.id)
Esempio n. 3
0
def clone(request, template_id):
    """
    Clones a template. Allows quickly creating new templates from existing ones.

    :param request:
    :param template_id: id of the template to be cloned
    :return: redirect to edit screen of new template!
    """
    template = get_object_or_404(ConfigTemplate, pk=template_id)

    # create our Dolly the sheep (clone)
    dolly = ConfigTemplate()
    dolly.name = template.name + " Clone"
    dolly.description = template.description
    dolly.action_provider = template.action_provider
    dolly.action_provider_options = template.action_provider_options
    dolly.template = template.template
    dolly.type = template.type

    print("Cloning template %s" % template.name)
    dolly.save()
    dollies_id = dolly.id

    # attempt to save any existing input form associations on the session
    # we'll retrieve this later when creating a new input form
    try:
        input_form = InputForm.objects.get(script=template)

        if 'cloned_templates' in request.session:
            cloned_templates = request.session['cloned_templates']
        else:
            cloned_templates = dict()

        cloned_templates[dollies_id] = dict()
        cloned_templates[dollies_id]['cloned_from'] = template.id
        cloned_templates[dollies_id]['input_form_id'] = input_form.id
        print('added a template to the cloned templates cache in the session')
        print(dollies_id)

        request.session['cloned_templates'] = cloned_templates

    except InputForm.DoesNotExist as dne:
        print('Could not find input_form for this cloned template')

    return HttpResponseRedirect('/tools/edit/%s/' % dolly.id)
Esempio n. 4
0
def import_form(jd):
    """
    Imports an input_from from serialized json data
    :param jd: json_data object generated from the export_input_form function
    :return: id of the input form or id of the input form if it already exists
    """
    template_options = jd["template"]
    form_options = jd["form"]

    if not ConfigTemplate.objects.filter(
            name=template_options['name']).exists():
        template = ConfigTemplate()
        template.name = template_options["name"]
        template.description = template_options["description"]
        template.action_provider = template_options["action_provider"]
        template.action_provider_options = template_options[
            "action_provider_options"]
        template.type = template_options["type"]
        template.template = unquote(template_options["template"])
        logger.info("Imported template: %s" % template.name)
        template.save()
    else:
        print('ConfigTemplate %s already exists' % template_options['name'])

    if not InputForm.objects.filter(name=form_options['name']).exists():
        input_form = InputForm()
        input_form.name = form_options["name"]
        input_form.description = form_options["description"]
        input_form.instructions = form_options["instructions"]
        input_form.json = unquote(form_options["json"])
        input_form.script = template

        logger.info("Import input form: %s" % input_form.name)
        input_form.save()
    else:
        print('Input form %s already exists' % form_options['name'])
        input_form = InputForm.objects.get(name=form_options['name'])

    return input_form.id
Esempio n. 5
0
def clone(request, template_id):
    """
    Clones a template. Allows quickly creating new templates from existing ones.

    :param request:
    :param template_id: id of the template to be cloned
    :return: redirect to edit screen of new template!
    """
    template = get_object_or_404(ConfigTemplate, pk=template_id)

    # create our Dolly the sheep (clone)
    dolly = ConfigTemplate()
    dolly.name = template.name + " Clone"
    dolly.description = template.description
    dolly.action_provider = template.action_provider
    dolly.action_provider_options = template.action_provider_options
    dolly.template = template.template
    dolly.type = template.type

    print "Cloning template %s" % template.name
    dolly.save()
    return HttpResponseRedirect('/tools/edit/%s/' % dolly.id)