Exemplo n.º 1
0
def serve_action_out(user, action, user_attribute_name):
    """
    Function that given a user and an Action Out
    searches for the appropriate data in the table with the given
    attribute name equal to the user email and returns the HTTP response.
    :param user: User object making the request
    :param action: Action to execute (action out)
    :param user_attribute_name: Column to check for email
    :return:
    """
    # User_instance has the record used for verification
    action_content = evaluate_row(action, (user_attribute_name, user.email))

    payload = {'action': action.name, 'action_id': action.id}

    # If the action content is empty, forget about it
    response = action_content
    if action_content is None:
        response = render_to_string('action/action_unavailable.html', {})
        payload['error'] = 'Action not enabled for user ' + user.email

    # Log the event
    logs.ops.put(user,
                 'action_served_execute',
                 workflow=action.workflow,
                 payload=payload)

    # Respond the whole thing
    return HttpResponse(response)
Exemplo n.º 2
0
def preview_response(request, pk, idx, template, prelude=None):
    """
    HTML request and the primary key of an action to preview one of its
    instances. The request must provide and additional parameter idx to
    denote which instance to show.

    :param request: HTML request object
    :param pk: Primary key of the an action for which to do the preview
    :param idx: Index of the reponse to preview
    :param template: Path to the template to use for the render.
    :param prelude: Optional text to include at the top of the rencering
    :return:
    """

    # To include in the JSON response
    data = dict()

    # Action being used
    try:
        action = Action.objects.get(id=pk)
    except ObjectDoesNotExist:
        data['form_is_valid'] = True
        data['html_redirect'] = reverse('workflow:index')
        return JsonResponse(data)

    # Get the workflow to obtain row numbers
    workflow = get_workflow(request, action.workflow.id)
    if not workflow:
        data['form_is_valid'] = True
        data['html_redirect'] = reverse('workflow:index')
        return JsonResponse(data)

    # If the request has the 'action_content' field, update the action
    action_content = request.GET.get('action_content', None)
    if action_content:
        action.content = action_content
        action.save()

    # Turn the parameter into an integer
    idx = int(idx)

    # Get the total number of items
    n_items = action.n_selected_rows
    if n_items == -1:
        n_items = workflow.nrows

    # Set the idx to a legal value just in case
    if not 1 <= idx <= n_items:
        idx = 1

    prv = idx - 1
    if prv <= 0:
        prv = n_items

    nxt = idx + 1
    if nxt > n_items:
        nxt = 1

    action_content = evaluate_row(action, idx)
    if action_content is None:
        action_content = \
            "Error while retrieving content for row {0}".format(idx)

    data['html_form'] = \
        render_to_string(template,
                         {'action': action,
                          'action_content': action_content,
                          'index': idx,
                          'nxt': nxt,
                          'prv': prv,
                          'prelude': prelude},
                         request=request)

    return JsonResponse(data)