Esempio n. 1
0
def bulk_add_object(request):
    """
    Bulk add objects.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
    """

    formdict = form_to_dict(AddObjectForm(request.user))

    if request.method == "POST" and request.is_ajax():
        acl = get_acl_object(request.POST['otype'])
        user = request.user
        if user.has_access_to(acl.OBJECTS_ADD):
            response = parse_bulk_upload(
                request,
                parse_row_to_bound_object_form,
                add_new_handler_object_via_bulk,
                formdict)
        else:
            response = {'success':False,
                        'message':'User does not have permission to add objects'}

        return HttpResponse(json.dumps(response,
                            default=json_handler),
                            content_type="application/json")
    else:
        return render_to_response('bulk_add_default.html',
                                  {'formdict': formdict,
                                  'title': "Bulk Add Objects",
                                  'table_name': 'object'},
                                  RequestContext(request))
Esempio n. 2
0
def process_bulk_add_ip(request, formdict):
    """
    Performs the bulk add of ips by parsing the request data. Batches
    some data into a cache object for performance by reducing large
    amounts of single database queries.

    :param request: Django request.
    :type request: :class:`django.http.HttpRequest`
    :param formdict: The form representing the bulk uploaded data.
    :type formdict: dict
    :returns: :class:`django.http.HttpResponse`
    """

    ip_names = []
    cached_results = {}

    cleanedRowsData = convert_handsontable_to_rows(request)
    for rowData in cleanedRowsData:
        if rowData != None and rowData.get(form_consts.IP.IP_ADDRESS) != None:
            ip_names.append(rowData.get(form_consts.IP.IP_ADDRESS).lower())

    ip_results = IP.objects(ip__in=ip_names)

    for ip_result in ip_results:
        cached_results[ip_result.ip] = ip_result

    cache = {form_consts.IP.CACHED_RESULTS: cached_results, 'cleaned_rows_data': cleanedRowsData}

    response = parse_bulk_upload(request, parse_row_to_bound_ip_form, add_new_ip_via_bulk, formdict, cache)

    return response
Esempio n. 3
0
def bulk_add_object(request):
    """
    Bulk add objects.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
    """

    formdict = form_to_dict(AddObjectForm(request.user))

    if request.method == "POST" and request.is_ajax():
        response = parse_bulk_upload(
            request,
            parse_row_to_bound_object_form,
            add_new_handler_object_via_bulk,
            formdict)

        return HttpResponse(json.dumps(response,
                            default=json_handler),
                            content_type="application/json")
    else:
        return render_to_response('bulk_add_default.html',
                                  {'formdict': formdict,
                                  'title': "Bulk Add Objects",
                                  'table_name': 'object'},
                                  RequestContext(request))
Esempio n. 4
0
def process_bulk_add_ip(request, formdict):
    """
    Performs the bulk add of ips by parsing the request data. Batches
    some data into a cache object for performance by reducing large
    amounts of single database queries.

    :param request: Django request.
    :type request: :class:`django.http.HttpRequest`
    :param formdict: The form representing the bulk uploaded data.
    :type formdict: dict
    :returns: :class:`django.http.HttpResponse`
    """

    ip_names = []
    cached_results = {}

    cleanedRowsData = convert_handsontable_to_rows(request)
    for rowData in cleanedRowsData:
        if rowData != None and rowData.get(form_consts.IP.IP_ADDRESS) != None:
            ip_names.append(rowData.get(form_consts.IP.IP_ADDRESS).lower())

    ip_results = IP.objects(ip__in=ip_names)

    for ip_result in ip_results:
        cached_results[ip_result.ip] = ip_result

    cache = {form_consts.IP.CACHED_RESULTS: cached_results, 'cleaned_rows_data': cleanedRowsData}

    response = parse_bulk_upload(request, parse_row_to_bound_ip_form, add_new_ip_via_bulk, formdict, cache)

    return response
Esempio n. 5
0
File: views.py Progetto: vsbca/crits
def bulk_add_object(request):
    """
    Bulk add objects.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
    """

    formdict = form_to_dict(AddObjectForm(request.user))

    if request.method == "POST" and request.is_ajax():
        acl = get_acl_object(request.POST['otype'])
        user = request.user
        if user.has_access_to(acl.OBJECTS_ADD):
            response = parse_bulk_upload(request,
                                         parse_row_to_bound_object_form,
                                         add_new_handler_object_via_bulk,
                                         formdict)
        else:
            response = {
                'success': False,
                'message': 'User does not have permission to add objects'
            }

        return HttpResponse(json.dumps(response, default=json_handler),
                            content_type="application/json")
    else:
        return render_to_response(
            'bulk_add_default.html', {
                'formdict': formdict,
                'title': "Bulk Add Objects",
                'table_name': 'object'
            }, RequestContext(request))
Esempio n. 6
0
def bulk_add_object(request):
    """
    Bulk add objects.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
    """

    all_obj_type_choices = [(c[0],
                            c[0],
                            {'datatype':c[1].keys()[0],
                            'datatype_value':c[1].values()[0]}
                            ) for c in get_object_types(False, query={'datatype.file':{'$exists':0}})]

    formdict = form_to_dict(AddObjectForm(request.user, all_obj_type_choices))

    if request.method == "POST" and request.is_ajax():
        response = parse_bulk_upload(request, parse_row_to_bound_object_form, add_new_handler_object_via_bulk, formdict)

        return HttpResponse(json.dumps(response,
                            default=json_handler),
                            mimetype='application/json')
    else:
        return render_to_response('bulk_add_default.html',
                                  {'formdict': formdict,
                                  'title': "Bulk Add Objects",
                                  'table_name': 'object'},
                                  RequestContext(request))
Esempio n. 7
0
def bulk_add_object(request):
    """
    Bulk add objects.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
    """

    all_obj_type_choices = [
        (c[0], c[0], {"datatype": c[1].keys()[0], "datatype_value": c[1].values()[0]})
        for c in get_object_types(False, query={"datatype.file": {"$exists": 0}})
    ]

    formdict = form_to_dict(AddObjectForm(request.user, all_obj_type_choices))

    if request.method == "POST" and request.is_ajax():
        response = parse_bulk_upload(request, parse_row_to_bound_object_form, add_new_handler_object_via_bulk, formdict)

        return HttpResponse(json.dumps(response, default=json_handler), mimetype="application/json")
    else:
        return render_to_response(
            "bulk_add_default.html",
            {"formdict": formdict, "title": "Bulk Add Objects", "table_name": "object"},
            RequestContext(request),
        )
Esempio n. 8
0
def process_bulk_add_domain(request, formdict):
    """
    Performs the bulk add of domains by parsing the request data. Batches
    some data into a cache object for performance by reducing large
    amounts of single database queries.

    :param request: Django request.
    :type request: :class:`django.http.HttpRequest`
    :param formdict: The form representing the bulk uploaded data.
    :type formdict: dict
    :returns: :class:`django.http.HttpResponse`
    """

    domain_names = []
    ip_addresses = []
    cached_domain_results = {}
    cached_ip_results = {}

    cleanedRowsData = convert_handsontable_to_rows(request)
    for rowData in cleanedRowsData:
        if rowData != None:
            if rowData.get(form_consts.Domain.DOMAIN_NAME) != None:
                domain = rowData.get(
                    form_consts.Domain.DOMAIN_NAME).strip().lower()
                (root_domain, full_domain,
                 error) = get_valid_root_domain(domain)
                domain_names.append(full_domain)

                if domain != root_domain:
                    domain_names.append(root_domain)

            if rowData.get(form_consts.Domain.IP_ADDRESS) != None:
                ip_addr = rowData.get(form_consts.Domain.IP_ADDRESS)
                ip_type = rowData.get(form_consts.Domain.IP_TYPE)
                (ip_addr, error) = validate_and_normalize_ip(ip_addr, ip_type)
                ip_addresses.append(ip_addr)

    domain_results = Domain.objects(domain__in=domain_names)

    ip_results = IP.objects(ip__in=ip_addresses)

    for domain_result in domain_results:
        cached_domain_results[domain_result.domain] = domain_result

    for ip_result in ip_results:
        cached_ip_results[ip_result.ip] = ip_result

    cache = {
        form_consts.Domain.CACHED_RESULTS: cached_domain_results,
        form_consts.IP.CACHED_RESULTS: cached_ip_results,
        'cleaned_rows_data': cleanedRowsData
    }

    response = parse_bulk_upload(request, parse_row_to_bound_domain_form,
                                 add_new_domain_via_bulk, formdict, cache)

    return response
Esempio n. 9
0
def process_bulk_add_domain(request, formdict):
    """
    Performs the bulk add of domains by parsing the request data. Batches
    some data into a cache object for performance by reducing large
    amounts of single database queries.

    :param request: Django request.
    :type request: :class:`django.http.HttpRequest`
    :param formdict: The form representing the bulk uploaded data.
    :type formdict: dict
    :returns: :class:`django.http.HttpResponse`
    """

    domain_names = []
    ip_addresses = []
    cached_domain_results = {}
    cached_ip_results = {}

    cleanedRowsData = convert_handsontable_to_rows(request)
    for rowData in cleanedRowsData:
        if rowData != None:
            if rowData.get(form_consts.Domain.DOMAIN_NAME) != None:
                domain = rowData.get(form_consts.Domain.DOMAIN_NAME).strip().lower()
                (root_domain, full_domain, error) = get_valid_root_domain(domain)
                domain_names.append(full_domain)

                if domain != root_domain:
                    domain_names.append(root_domain)

            if rowData.get(form_consts.Domain.IP_ADDRESS) != None:
                ip_addr = rowData.get(form_consts.Domain.IP_ADDRESS)
                ip_type = rowData.get(form_consts.Domain.IP_TYPE)
                (ip_addr, error) = validate_and_normalize_ip(ip_addr, ip_type)
                ip_addresses.append(ip_addr)

    domain_results = Domain.objects(domain__in=domain_names)

    ip_results = IP.objects(ip__in=ip_addresses)

    for domain_result in domain_results:
        cached_domain_results[domain_result.domain] = domain_result

    for ip_result in ip_results:
        cached_ip_results[ip_result.ip] = ip_result

    cache = {
        form_consts.Domain.CACHED_RESULTS: cached_domain_results,
        form_consts.IP.CACHED_RESULTS: cached_ip_results,
        "cleaned_rows_data": cleanedRowsData,
    }

    response = parse_bulk_upload(request, parse_row_to_bound_domain_form, add_new_domain_via_bulk, formdict, cache)

    return response
Esempio n. 10
0
def bulk_add_object(request):
    """
    Bulk add objects.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
    """

    formdict = form_to_dict(AddObjectForm(request.user))

    if request.method == "POST" and request.is_ajax():
        response = parse_bulk_upload(request, parse_row_to_bound_object_form,
                                     add_new_handler_object_via_bulk, formdict)

        return HttpResponse(json.dumps(response, default=json_handler),
                            mimetype='application/json')
    else:
        return render_to_response(
            'bulk_add_default.html', {
                'formdict': formdict,
                'title': "Bulk Add Objects",
                'table_name': 'object'
            }, RequestContext(request))
Esempio n. 11
0
def bulk_add_object_inline(request):
    """
    Bulk add objects inline.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
    """

    formdict = form_to_dict(AddObjectForm(request.user))

    if request.method == "POST" and request.is_ajax():
        user = request.user
        acl = get_acl_object(request.POST['otype'])

        if user.has_access_to(acl.OBJECTS_ADD):
            response = parse_bulk_upload(
                request,
                parse_row_to_bound_object_form,
                add_new_handler_object_via_bulk,
                formdict)

            secondary_data_array = response.get('secondary')
            if secondary_data_array:
                latest_secondary_data = secondary_data_array[-1]
                class_type = class_from_id(
                    latest_secondary_data['type'],
                    latest_secondary_data['id'])

                subscription = {'type': latest_secondary_data['type'],
                                'id': latest_secondary_data['id'],
                                'value': latest_secondary_data['id']}

                object_listing_html = render_to_string('objects_listing_widget.html',
                                                       {'objects': class_type.sort_objects(),
                                                        'subscription': subscription},
                                                       RequestContext(request))

                response['html'] = object_listing_html

                is_relationship_made = False
                for secondary_data in secondary_data_array:
                    if secondary_data.get('relationships'):
                        is_relationship_made = True
                        break

                if is_relationship_made == True:
                    rel_html = render_to_string('relationships_listing_widget.html',
                                                {'relationship': subscription,
                                                 'relationships': class_type.sort_relationships(request.user, meta=True)},
                                                RequestContext(request))

                    response['rel_msg'] = rel_html
                    response['rel_made'] = True

        return HttpResponse(json.dumps(response,
                            default=json_handler),
                            content_type="application/json")
    else:
        is_prevent_initial_table = request.GET.get('isPreventInitialTable', False)
        is_use_item_source = request.GET.get('useItemSource', False)

        if is_use_item_source == True or is_use_item_source == "true":
            otype = request.GET.get('otype')
            oid = request.GET.get('oid')

            # Get the item with the type and ID from the database
            obj = class_from_id(otype, oid)

            if obj:
                source_field_name = get_source_field_for_class(otype)
                if source_field_name:

                    # If the item has a source, then use the source value
                    # to set as the default source
                    if hasattr(obj, "source"):
                        source_field = get_field_from_label("source", formdict)
                        earliest_source = None
                        earliest_date = None

                        # Get the earliest source, compared by date
                        for source in obj.source:
                            for source_instance in source.instances:
                                if earliest_source == None or source_instance.date < earliest_date:
                                    earliest_date = source_instance.date
                                    earliest_source = source

                        if earliest_source:
                            source_field['initial'] = earliest_source.name

        return render_to_response('bulk_add_object_inline.html',
                                  {'formdict': formdict,
                                   'title': "Bulk Add Objects",
                                   'is_prevent_initial_table': is_prevent_initial_table,
                                   'table_name': 'object_inline'},
                                  RequestContext(request))
Esempio n. 12
0
File: views.py Progetto: vsbca/crits
def bulk_add_object_inline(request):
    """
    Bulk add objects inline.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
    """

    formdict = form_to_dict(AddObjectForm(request.user))

    if request.method == "POST" and request.is_ajax():
        user = request.user
        acl = get_acl_object(request.POST['otype'])

        if user.has_access_to(acl.OBJECTS_ADD):
            response = parse_bulk_upload(request,
                                         parse_row_to_bound_object_form,
                                         add_new_handler_object_via_bulk,
                                         formdict)

            secondary_data_array = response.get('secondary')
            if secondary_data_array:
                latest_secondary_data = secondary_data_array[-1]
                class_type = class_from_id(latest_secondary_data['type'],
                                           latest_secondary_data['id'])

                subscription = {
                    'type': latest_secondary_data['type'],
                    'id': latest_secondary_data['id'],
                    'value': latest_secondary_data['id']
                }

                object_listing_html = render_to_string(
                    'objects_listing_widget.html', {
                        'objects': class_type.sort_objects(),
                        'subscription': subscription
                    }, RequestContext(request))

                response['html'] = object_listing_html

                is_relationship_made = False
                for secondary_data in secondary_data_array:
                    if secondary_data.get('relationships'):
                        is_relationship_made = True
                        break

                if is_relationship_made == True:
                    rel_html = render_to_string(
                        'relationships_listing_widget.html', {
                            'relationship':
                            subscription,
                            'relationships':
                            class_type.sort_relationships(request.user,
                                                          meta=True)
                        }, RequestContext(request))

                    response['rel_msg'] = rel_html
                    response['rel_made'] = True

        return HttpResponse(json.dumps(response, default=json_handler),
                            content_type="application/json")
    else:
        is_prevent_initial_table = request.GET.get('isPreventInitialTable',
                                                   False)
        is_use_item_source = request.GET.get('useItemSource', False)

        if is_use_item_source == True or is_use_item_source == "true":
            otype = request.GET.get('otype')
            oid = request.GET.get('oid')

            # Get the item with the type and ID from the database
            obj = class_from_id(otype, oid)

            if obj:
                source_field_name = get_source_field_for_class(otype)
                if source_field_name:

                    # If the item has a source, then use the source value
                    # to set as the default source
                    if hasattr(obj, "source"):
                        source_field = get_field_from_label("source", formdict)
                        earliest_source = None
                        earliest_date = None

                        # Get the earliest source, compared by date
                        for source in obj.source:
                            for source_instance in source.instances:
                                if earliest_source == None or source_instance.date < earliest_date:
                                    earliest_date = source_instance.date
                                    earliest_source = source

                        if earliest_source:
                            source_field['initial'] = earliest_source.name

        return render_to_response(
            'bulk_add_object_inline.html', {
                'formdict': formdict,
                'title': "Bulk Add Objects",
                'is_prevent_initial_table': is_prevent_initial_table,
                'table_name': 'object_inline'
            }, RequestContext(request))
Esempio n. 13
0
def bulk_add_object_inline(request):
    """
    Bulk add objects inline.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
    """
    all_obj_type_choices = [
        (c[0], c[0], {"datatype": c[1].keys()[0], "datatype_value": c[1].values()[0]})
        for c in get_object_types(False, query={"datatype.file": {"$exists": 0}})
    ]

    formdict = form_to_dict(AddObjectForm(request.user, all_obj_type_choices))

    if request.method == "POST" and request.is_ajax():
        response = parse_bulk_upload(request, parse_row_to_bound_object_form, add_new_handler_object_via_bulk, formdict)

        secondary_data_array = response.get("secondary")
        if secondary_data_array:
            latest_secondary_data = secondary_data_array[-1]
            class_type = class_from_id(latest_secondary_data["type"], latest_secondary_data["id"])

            subscription = {
                "type": latest_secondary_data["type"],
                "id": latest_secondary_data["id"],
                "value": latest_secondary_data["id"],
            }

            object_listing_html = render_to_string(
                "objects_listing_widget.html",
                {"objects": class_type.sort_objects(), "subscription": subscription},
                RequestContext(request),
            )

            response["html"] = object_listing_html

            is_relationship_made = False
            for secondary_data in secondary_data_array:
                if secondary_data.get("relationships"):
                    is_relationship_made = True
                    break

            if is_relationship_made == True:
                rel_html = render_to_string(
                    "relationships_listing_widget.html",
                    {
                        "relationship": subscription,
                        "relationships": class_type.sort_relationships(request.user, meta=True),
                    },
                    RequestContext(request),
                )

                response["rel_msg"] = rel_html
                response["rel_made"] = True

        return HttpResponse(json.dumps(response, default=json_handler), mimetype="application/json")
    else:
        is_prevent_initial_table = request.GET.get("isPreventInitialTable", False)
        is_use_item_source = request.GET.get("useItemSource", False)

        if is_use_item_source == True or is_use_item_source == "true":
            otype = request.GET.get("otype")
            oid = request.GET.get("oid")

            # Get the item with the type and ID from the database
            obj = class_from_id(otype, oid)

            if obj:
                source_field_name = get_source_field_for_class(otype)
                if source_field_name:

                    # If the item has a source, then use the source value
                    # to set as the default source
                    if hasattr(obj, "source"):
                        source_field = get_field_from_label("source", formdict)
                        earliest_source = None
                        earliest_date = None

                        # Get the earliest source, compared by date
                        for source in obj.source:
                            for source_instance in source.instances:
                                if earliest_source == None or source_instance.date < earliest_date:
                                    earliest_date = source_instance.date
                                    earliest_source = source

                        if earliest_source:
                            source_field["initial"] = earliest_source.name

        return render_to_response(
            "bulk_add_object_inline.html",
            {
                "formdict": formdict,
                "title": "Bulk Add Objects",
                "is_prevent_initial_table": is_prevent_initial_table,
                "table_name": "object_inline",
            },
            RequestContext(request),
        )