Beispiel #1
0
def sqlconn_add(request):
    """
    Create a new SQL connection processing the GET/POST requests

    :param request: Request object
    :return: AJAX response
    """

    # Create the form
    form = SQLConnectionForm(request.POST or None)

    return save_conn_form(request, form,
                          'dataops/includes/partial_sqlconn_addedit.html')
Beispiel #2
0
def sqlconn_edit(request, pk):
    """

    :param request:
    :return:
    """

    # Get the connection
    conn = SQLConnection.objects.filter(pk=pk).first()
    if not conn:
        return JsonResponse({
            'form_is_valid': True,
            'html_redirect': reverse('workflow:index')
        })

    # Create the form
    form = SQLConnectionForm(request.POST or None, instance=conn)

    return save_conn_form(request, form,
                          'dataops/includes/partial_sqlconn_addedit.html')
Beispiel #3
0
def sqlconn_edit(request: HttpRequest, pk: int) -> JsonResponse:
    """Respond to the reqeust to edit a SQL CONN object.

    :param request: HTML request

    :param pk: Primary key

    :return:
    """
    # Get the connection
    conn = SQLConnection.objects.filter(pk=pk).first()
    if not conn:
        return JsonResponse({'html_redirect': reverse('home')})

    # Create the form
    form = SQLConnectionForm(request.POST or None, instance=conn)

    return _save_conn_form(
        request,
        form,
        'dataops/includes/partial_sqlconn_addedit.html')
Beispiel #4
0
def _save_conn_form(
    request: HttpRequest,
    form: SQLConnectionForm,
    template_name: str,
) -> JsonResponse:
    """Save the connection provided in the form.

    :param request: HTTP request

    :param form: form object with the collected information

    :param template_name: To render the response

    :return: AJAX response
    """
    # Type of event to record
    if form.instance.id:
        event_type = Log.SQL_CONNECTION_EDIT
        is_add = False
    else:
        event_type = Log.SQL_CONNECTION_CREATE
        is_add = True

    # If it is a POST and it is correct
    if request.method == 'POST' and form.is_valid():

        if not form.has_changed():
            return JsonResponse({'html_redirect': None})

        conn = form.save()

        # Log the event
        Log.objects.register(
            request.user,
            event_type,
            None,
            {
                'name': conn.name,
                'description': conn.description_txt,
                'conn_type': conn.conn_type,
                'conn_driver': conn.conn_driver,
                'db_user': conn.db_user,
                'db_passwd': _('<PROTECTED>') if conn.db_password else '',
                'db_host': conn.db_host,
                'db_port': conn.db_port,
                'db_name': conn.db_name,
                'db_table': conn.db_table,
            },
        )

        return JsonResponse({'html_redirect': ''})

    # Request is a GET
    return JsonResponse({
        'html_form': render_to_string(
            template_name,
            {
                'form': form,
                'id': form.instance.id,
                'add': is_add},
            request=request,
        ),
    })