Пример #1
0
def evaluate_row_action_in(action, context):
    """
    Given an action IN object and a row index:
    1) Create the form and the context
    2) Run the template with the context
    3) Return the resulting object (HTML?)

    :param action: Action object.
    :param row_values: Dictionary with pairs name/value
    :return: String with the HTML content resulting from the evaluation
    """

    # Get the active columns attached to the action
    columns = [c for c in action.columns.all() if c.is_active]

    # Get the row values.
    selected_values = [context[c.name] for c in columns]

    form = EnterActionIn(None, columns=columns, values=selected_values)

    # Render the form
    return Template("""<div align="center">
             <p class="lead">{{ description_text }}</p>
             {% load crispy_forms_tags %}{{ form|crispy }}
           </div>""").render(
        Context({
            'form': form,
            'description_text': action.description_text
        }))
Пример #2
0
def evaluate_row_action_in(action: Action, context: Mapping):
    """Evaluate an action_in in the given context.

    Given an action IN object and a row index:
    1) Create the form and the context
    2) Run the template with the context
    3) Return the resulting object (HTML?)

    :param action: Action object.
    :param context: Dictionary with pairs name/value
    :return: String with the HTML content resulting from the evaluation
    """
    # Get the active columns attached to the action
    tuples = [
        column_condition_pair
        for column_condition_pair in action.column_condition_pair.all()
        if column_condition_pair.column.is_active
    ]

    col_values = [context[colcon_pair.column.name] for colcon_pair in tuples]

    form = EnterActionIn(
        None,
        tuples=tuples,
        context=context,
        values=col_values,
    )

    # Render the form
    return Template(
        """<div align="center">
             <p class="lead">{{ description_text }}</p>
             {% load crispy_forms_tags %}{{ form|crispy }}
           </div>""", ).render(
            Context(
                {
                    'form': form,
                    'description_text': action.description_text,
                }, ))
Пример #3
0
def serve_survey_row(
    request: HttpRequest,
    action: Action,
    user_attribute_name: str,
) -> HttpResponse:
    """Serve a request for action in.

    Function that given a request, and an action IN, it performs the lookup
     and data input of values.

    :param request: HTTP request

    :param action:  Action In

    :param user_attribute_name: The column name used to check for email

    :return:
    """
    # Get the attribute value depending if the user is managing the workflow
    # User is instructor, and either owns the workflow or is allowed to access
    # it as shared
    manager = has_access(request.user, action.workflow)
    user_attribute_value = None
    if manager:
        user_attribute_value = request.GET.get('uatv')
    if not user_attribute_value:
        user_attribute_value = request.user.email

    # Get the dictionary containing column names, attributes and condition
    # valuations:
    context = get_action_evaluation_context(
        action,
        get_row_values(
            action,
            (user_attribute_name, user_attribute_value),
        ),
    )

    if not context:
        # If the data has not been found, flag
        if not manager:
            return ontask_handler404(request, None)

        messages.error(
            request,
            _('Data not found in the table'))
        return redirect(reverse('action:run', kwargs={'pk': action.id}))

    # Get the active columns attached to the action
    colcon_items = extract_survey_questions(action, request.user)

    # Bind the form with the existing data
    form = EnterActionIn(
        request.POST or None,
        tuples=colcon_items,
        context=context,
        values=[context[colcon.column.name] for colcon in colcon_items],
        show_key=manager)

    keep_processing = (
        request.method == 'POST'
        and form.is_valid()
        and not request.POST.get('lti_version'))
    if keep_processing:
        # Update the content in the DB
        row_keys, row_values  = survey_update_row_values(
            action,
            colcon_items,
            manager,
            form.cleaned_data,
            'email',
            request.user.email,
            context)

        # Log the event and update its content in the action
        log_item = Log.objects.register(
            request.user,
            Log.SURVEY_INPUT,
            action.workflow,
            {'id': action.workflow.id,
             'name': action.workflow.name,
             'action': action.name,
             'action_id': action.id,
             'new_values': json.dumps(dict(zip(row_keys, row_values)))})

        # Modify the time of execution for the action
        action.last_executed_log = log_item
        action.save()

        # If not instructor, just thank the user!
        if not manager:
            return render(request, 'thanks.html', {})

        # Back to running the action
        return redirect(reverse('action:run', kwargs={'pk': action.id}))

    return render(
        request,
        'action/run_survey_row.html',
        {
            'form': form,
            'action': action,
            'cancel_url': reverse(
                'action:run', kwargs={'pk': action.id},
            ) if manager else None,
        },
    )
Пример #4
0
def serve_action_in(request, action, user_attribute_name, is_inst):
    """
    Function that given a request, and an action IN, it performs the lookup
     and data input of values.
    :param request: HTTP request
    :param action:  Action In
    :param user_attribute_name: The column name used to check for email
    :param is_inst: Boolean stating if the user is instructor
    :return:
    """

    # Get the attribute value
    if is_inst:
        user_attribute_value = request.GET.get('uatv', None)
    else:
        user_attribute_value = request.user.email

    # Get the active columns attached to the action
    columns = [c for c in action.columns.all() if c.is_active]
    #print( "user_attribute_name" + user_attribute_name )
    #print(action.workflow)
    #print(user_attribute_name)
    #print(user_attribute_value)
    #print( [c.name for c in columns] )
    # Get the row values. User_instance has the record used for verification
    row_pairs = pandas_db.get_table_row_by_key(
        action.workflow, None, (user_attribute_name, user_attribute_value),
        [c.name for c in columns])
    #print( row_pairs )
    #print(columns)
    # If the data has not been found, flag
    if not row_pairs:
        if not is_inst:
            return render(request, '404.html', {})

        messages.error(request, 'Data not found in the table')
        return redirect(reverse('action:run', kwargs={'pk': action.id}))

    # Bind the form with the existing data
    form = EnterActionIn(request.POST or None,
                         columns=columns,
                         values=row_pairs.values(),
                         show_key=is_inst)

    cancel_url = None
    if is_inst:
        cancel_url = reverse('action:run', kwargs={'pk': action.id})

    # Create the context
    context = {'form': form, 'action': action, 'cancel_url': cancel_url}

    if request.method == 'GET' or not form.is_valid():
        return render(request, 'action/run_row.html', context)

    # Correct POST request!
    if not form.has_changed():
        if not is_inst:
            return redirect(reverse('action:thanks'))

        return redirect(reverse('action:run', kwargs={'pk': action.id}))

    # Post with different data. # Update content in the DB
    set_fields = []
    set_values = []
    where_field = None
    where_value = None
    log_payload = []
    # Create the SET name = value part of the query
    #print(columns)
    for idx, column in enumerate(columns):
        try:
            #print(field_prefix + '%s' % idx)
            value = form.cleaned_data[field_prefix + '%s' % idx]
            #print( " value:" + value )
            if column.is_key:
                #print( column )
                #print( " is key" )
                if not where_field:
                    # Remember one unique key for selecting the row
                    where_field = column.name
                    where_value = value
                continue

            set_fields.append(column.name)
            set_values.append(value)
            log_payload.append((column.name, value))
        except:
            pass
    ##Wen patch for where_field and where_vlue is None##
    if not where_field:
        where_field = user_attribute_name
    if not where_value:
        where_value = user_attribute_value
    pandas_db.update_row(action.workflow.id, set_fields, set_values,
                         [where_field], [where_value])

    # Recompute all the values of the conditions in each of the actions
    for act in action.workflow.actions.all():
        act.update_n_rows_selected()

    # Log the event
    logs.ops.put(
        request.user, 'tablerow_update', action.workflow, {
            'id': action.workflow.id,
            'name': action.workflow.name,
            'new_values': log_payload
        })

    # If not instructor, just thank the user!
    if not is_inst:
        return render(request, 'thanks.html', {})

    # Back to running the action
    return redirect(reverse('action:run', kwargs={'pk': action.id}))
Пример #5
0
def serve_action_in(request, action, user_attribute_name, is_inst):
    """
    Function that given a request, and an action IN, it performs the lookup
     and data input of values.
    :param request: HTTP request
    :param action:  Action In
    :param user_attribute_name: The column name used to check for email
    :param is_inst: Boolean stating if the user is instructor
    :return:
    """

    # Get the attribute value
    if is_inst:
        user_attribute_value = request.GET.get('uatv', None)
    else:
        user_attribute_value = request.user.email

    # Get the active columns attached to the action
    columns = [c for c in action.columns.all() if c.is_active]
    if action.shuffle:
        # Shuffle the columns if needed
        random.seed(request.user)
        random.shuffle(columns)

    # Get the row values. User_instance has the record used for verification
    row_pairs = pandas_db.get_table_row_by_key(
        action.workflow, None, (user_attribute_name, user_attribute_value),
        [c.name for c in columns])

    # If the data has not been found, flag
    if not row_pairs:
        if not is_inst:
            return render(request, '404.html', {})

        messages.error(request, _('Data not found in the table'))
        return redirect(reverse('action:run', kwargs={'pk': action.id}))

    # Bind the form with the existing data
    form = EnterActionIn(request.POST or None,
                         columns=columns,
                         values=list(row_pairs.values()),
                         show_key=is_inst)

    cancel_url = None
    if is_inst:
        cancel_url = reverse('action:run', kwargs={'pk': action.id})

    # Create the context
    context = {'form': form, 'action': action, 'cancel_url': cancel_url}

    if request.method == 'GET' or not form.is_valid() or \
            request.POST.get('lti_version', None):
        return render(request, 'action/run_survey_row.html', context)

    # Post with different data. # Update content in the DB
    set_fields = []
    set_values = []
    where_field = 'email'
    where_value = request.user.email
    log_payload = []
    # Create the SET name = value part of the query
    for idx, column in enumerate(columns):
        if not is_inst and column.is_key:
            # If it is a learner request and a key column, skip
            continue

        value = form.cleaned_data[field_prefix + '%s' % idx]
        if column.is_key:
            # Remember one unique key for selecting the row
            where_field = column.name
            where_value = value
            continue

        set_fields.append(column.name)
        set_values.append(value)
        log_payload.append((column.name, value))

    pandas_db.update_row(action.workflow.id, set_fields, set_values,
                         [where_field], [where_value])

    # Recompute all the values of the conditions in each of the actions
    for act in action.workflow.actions.all():
        act.update_n_rows_selected()

    # Log the event and update its content in the action
    log_item = Log.objects.register(
        request.user, Log.TABLEROW_UPDATE, action.workflow, {
            'id': action.workflow.id,
            'name': action.workflow.name,
            'new_values': log_payload
        })

    # Modify the time of execution for the action
    action.last_executed_log = log_item
    action.save()

    # If not instructor, just thank the user!
    if not is_inst:
        return render(request, 'thanks.html', {})

    # Back to running the action
    return redirect(reverse('action:run', kwargs={'pk': action.id}))