Ejemplo n.º 1
0
def preview_template(request):

    source_template = request.POST.get('template', '# Code goes here')
    source_json = request.POST.get('json_text', '{}')
    name = request.POST.get('name', 'Name')
    project_uid = request.POST.get('project_uid')
    project = Project.objects.filter(uid=project_uid).first()

    try:
        source_json = hjson.loads(source_json)
        # Fill json information by name for the preview.
        source_json = auth.fill_data_by_name(project=project,
                                             json_data=source_json)
        # Fill in the script with json data.
        context = Context(source_json)
        script_template = Template(source_template)
        script = script_template.render(context)
    except Exception as exc:
        return ajax_error(f"Error rendering code: {exc}")

    # Load the html containing the script
    tmpl = loader.get_template('widgets/preview_template.html')
    context = dict(script=script, name=name)
    template = tmpl.render(context=context)

    return ajax_success(html=template, msg="Rendered script")
Ejemplo n.º 2
0
def recipe_code_download(request, uid):
    """
    Download the raw recipe template as a file
    """

    recipe = Analysis.objects.filter(uid=uid).first()

    try:
        # Fill in the script with json data.
        json_data = auth.fill_data_by_name(project=recipe.project,
                                           json_data=recipe.json_data)
        context = Context(json_data)
        script_template = Template(recipe.template)
        script = script_template.render(context)
    except Exception as exc:
        logger.error(exc)
        script = recipe.template

    # Trigger file download with name of the recipe
    filename = "_".join(recipe.name.split()) + ".sh"

    response = HttpResponse(script, content_type='text/plain')
    response['Content-Disposition'] = f'attachment; filename={filename}'

    return response
Ejemplo n.º 3
0
def recipe_view(request, uid):
    """
    Returns a recipe view based on its id.
    """
    recipe = Analysis.objects.filter(uid=uid).first()
    project = recipe.project

    context = dict(recipe=recipe, project=project, activate='Recipe View')

    # How many results for this recipe
    rcount = Job.objects.filter(analysis=recipe, deleted=False).count()
    counts = get_counts(project)

    try:
        # Fill in the script with json data.
        json_data = auth.fill_data_by_name(project=project, json_data=recipe.json_data)
        ctx = Context(json_data)
        script_template = Template(recipe.template)
        script = script_template.render(ctx)
    except Exception as exc:
        messages.error(request, f"Error rendering code: {exc}")
        script = recipe.template

    context.update(counts, rcount=rcount, script=script)

    return render(request, "recipe_view.html", context)