Example #1
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}))
Example #2
0
def row_update(request):
    """
    Receives a POST request to update a row in the data table
    :param request: Request object with all the data.
    :return:
    """

    # If there is no workflow object, go back to the index
    workflow = get_workflow(request)
    if not workflow:
        return redirect('workflow:index')

    # If the workflow has no data, something went wrong, go back to the
    # dataops home
    if workflow.nrows == 0:
        return redirect('dataops:list')

    # Get the pair key,value to fetch the row from the table
    update_key = request.GET.get('update_key', None)
    update_val = request.GET.get('update_val', None)

    if not update_key or not update_val:
        # Malformed request
        return render(request, 'error.html',
                      {'message': 'Unable to update table row'})

    # Get the rows from the table
    rows = pandas_db.execute_select_on_table(workflow.id, [update_key],
                                             [update_val])

    row_form = RowForm(request.POST or None,
                       workflow=workflow,
                       initial_values=list(rows[0]))

    if request.method == 'GET' or not row_form.is_valid():
        return render(
            request, 'dataops/row_filter.html', {
                'workflow': workflow,
                'row_form': row_form,
                'cancel_url': reverse('table:display')
            })

    # This is a valid POST request

    # Create the query to update the row
    set_fields = []
    set_values = []
    columns = workflow.get_columns()
    unique_names = [c.name for c in columns if c.is_key]
    unique_field = None
    unique_value = None
    log_payload = []
    for idx, col in enumerate(columns):
        value = row_form.cleaned_data[field_prefix + '%s' % idx]
        set_fields.append(col.name)
        set_values.append(value)
        log_payload.append((col.name, str(value)))

        if not unique_field and col.name in unique_names:
            unique_field = col.name
            unique_value = value

    # If there is no unique key, something went wrong.
    if not unique_field:
        raise Exception('Key value not found when updating row')

    pandas_db.update_row(workflow.id, set_fields, set_values, [unique_field],
                         [unique_value])

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

    return redirect('table:display')
Example #3
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}))