Beispiel #1
0
def copy_object(request):
    """
    Add object uid or file path to sessions clipboard.
    """

    object_uid = request.POST.get('uid', '')
    project_uid = request.POST.get('project_uid', '')
    clipboard = request.POST.get('clipboard')

    project = Project.objects.filter(uid=project_uid).first()
    if not project:
        return ajax_error("Project does not exist.")

    is_readable = auth.is_readable(user=request.user, project=project)

    if not is_readable:
        return ajax_error('You do not have access to copy this object.')

    # Return current clipboard contents
    copied_uids = auth.copy_uid(request=request,
                                uid=object_uid,
                                board=clipboard)

    return ajax_success(
        f"Copied. Clipboard contains :{len(copied_uids)} objects.")
Beispiel #2
0
def copy_file(request):
    """
    Add file from import root directory into clipboard.
    """
    # Assumes incoming path is a full path
    path = request.POST.get('path')
    user = request.user

    # Project uid to check access
    uid = request.POST.get('uid') or 0
    project = Project.objects.filter(id=uid).first()

    if uid and not project:
        return ajax_error(msg="Project does not exist.")

    if project and not auth.is_readable(user=user, obj=project):
        return ajax_error(msg="You do not have access to copy this file")

    if not os.path.exists(path):
        return ajax_error(msg="File path does not exist.")

    if path.startswith(settings.IMPORT_ROOT_DIR) and not user.profile.trusted:
        return ajax_error(
            msg="Only trusted users can copy from this directory.")

    copied = auth.copy_file(request=request, fullpath=path)
    return ajax_success(msg=f"{len(copied)} files copied.")
Beispiel #3
0
def copy_object(request):
    """
    Add object uid or file path to sessions clipboard.
    """
    # Map the query parameter to a clipboard and database model.
    mapper = {
        "data": (Data, COPIED_DATA),
        "job": (Job, COPIED_RESULTS),
        "recipe": (Analysis, COPIED_RECIPES)
    }
    uid = request.POST.get('uid', '')

    # Get the clipboard to copy to
    clipboard = request.POST.get(settings.CLIPBOARD_NAME)
    klass, board = mapper.get(clipboard, (None, None))

    if not (klass and board):
        return ajax_error("Object or board does not exist.")

    obj = klass.objects.filter(uid=uid).first()
    is_readable = auth.is_readable(user=request.user, obj=obj)

    if not is_readable:
        return ajax_error('You do not have access to copy this object.')

    # Return current clipboard contents
    copied = auth.copy_uid(request=request, uid=uid, board=board)

    return ajax_success(f"Copied. Clipboard contains :{len(copied)} objects.")
Beispiel #4
0
def ajax_clipboard(request):
    """
    Displays the most recent object in clipboard.
    """

    # Get the most current item in the clipboard.
    pid = request.POST.get("id", 0)
    user = request.user

    project = Project.objects.filter(id=pid).first()
    board = auth.recent_clipboard(request=request)
    key, vals = board
    count = len(vals)

    if project and auth.is_readable(user=user, obj=project) and count:
        # Load items into clipboard
        tmpl = loader.get_template('widgets/clipboard.html')
        movable = key in [COPIED_RECIPES, COPIED_DATA]
        context = dict(count=count,
                       board=key,
                       is_recipe=key == COPIED_RECIPES,
                       movable=movable)
        template = tmpl.render(context=context)
    else:
        template = ''

    return ajax_success(html=template, msg="Refreshed clipboard")
def job_rerun(request, uid):
    # Get the job.
    job = Job.objects.filter(uid=uid).first()
    next = request.GET.get('next')
    # Get the recipe
    recipe = job.analysis
    # Get the job JSON
    json_data = job.json_data

    # Validate users can run the recipe.
    valid, msg = auth.validate_recipe_run(user=request.user, recipe=recipe)

    if not valid:
        messages.error(request, msg)
        redir = next or reverse('job_view', kwargs=dict(uid=job.uid))
        return redirect(redir)

    # Create a new job
    job = auth.create_job(analysis=recipe,
                          user=request.user,
                          json_data=json_data)

    # Spool via UWSGI or run it synchronously.
    tasks.execute_job.spool(job_id=job.id)
    if auth.is_readable(user=request.user, obj=recipe):
        url = reverse('recipe_view',
                      kwargs=dict(uid=job.analysis.uid)) + "#results"
    else:
        url = job.url()

    return redirect(url)
Beispiel #6
0
def get_part(request, name, id):
    """
    Return a template by name and with uid rendering
    """

    user = request.user

    # The recipe that needs to be edited.
    recipe = Analysis.objects.filter(id=id).annotate(
        job_count=Count("job", filter=Q(job__deleted=False))).first()

    project = recipe.project

    if not auth.is_readable(project=project, user=user):
        message = str("Recipe is not writable by current user")
        return HttpResponse(message)

    # Fills in project level counts (results, data and recipe counts).
    counts = get_counts(recipe.project)

    if name == "run":
        initial = dict(name=f"Results for: {recipe.name}")

        form = forms.RecipeInterface(request=request,
                                     analysis=recipe,
                                     json_data=recipe.json_data,
                                     initial=initial)
    else:
        # Initial form loading via a GET request.
        form = forms.RecipeForm(instance=recipe,
                                user=request.user,
                                project=project)

    remap = dict(
        info="parts/recipe_info.html",
        code="parts/recipe_code.html",
        interface="parts/recipe_interface.html",
        run="parts/recipe_run.html",
        results="parts/recipe_results.html",
    )

    name = remap.get(name, "parts/placeholder.html")

    # Check to see if this recipe is runnable by the user.
    is_runnable = auth.authorize_run(user=user, recipe=recipe)

    # Get the list of jobs required for recipe results
    jobs = recipe.job_set.filter(
        deleted=False).order_by("-lastedit_date").all()
    context = dict(recipe=recipe,
                   form=form,
                   is_runnable=is_runnable,
                   job_list=jobs,
                   rerun_btn=False)
    context.update(counts)

    html = render(request, name, context=context)
    return html
def security_label(context, analysis):
    user = context['request'].user

    if user.is_anonymous:
        is_readable = False
    else:
        is_readable = auth.is_readable(user=user, project=analysis.project)

    context.update(dict(recipe=analysis, is_readable=is_readable))

    return context
def clipboard(context, project_uid):

    request = context['request']
    user = request.user
    project = Project.objects.filter(uid=project_uid).first()
    board = auth.recent_clipboard(request=request)
    key, vals = board
    board_count = len(vals)
    movable = key in [const.COPIED_RECIPES, const.COPIED_DATA]
    if project and auth.is_readable(user=user, obj=project) and board_count:
        # Load items into clipboard
        context = dict(count=board_count,
                       board=key,
                       is_recipe=key == const.COPIED_RECIPES,
                       movable=movable)
    else:
        context = dict()

    return context