Beispiel #1
0
def add_update_activity(request, method, indicator_id):
    """
    Add/update an indicator's activity. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param method: Whether we are adding or updating.
    :type method: str ("add", "update")
    :param indicator_id: The ObjectId of the indicator to update.
    :type indicator_id: str
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == "POST" and request.is_ajax():
        username = request.user.username
        form = IndicatorActivityForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            add = {
                'start_date': data['start_date'] if data['start_date'] else '',
                'end_date': data['end_date'] if data['end_date'] else '',
                'description': data['description'],
                'analyst': username,
            }
            if method == "add":
                add['date'] = datetime.datetime.now()
                result = activity_add(indicator_id, add)
            else:
                date = datetime.datetime.strptime(data['date'],
                                                  settings.PY_DATETIME_FORMAT)
                date = date.replace(microsecond=date.microsecond / 1000 * 1000)
                add['date'] = date
                result = activity_update(indicator_id, add)
            if 'object' in result:
                result['html'] = render_to_string(
                    'indicators_activity_row_widget.html', {
                        'activity': result['object'],
                        'admin': is_admin(username),
                        'indicator_id': indicator_id
                    })
            return HttpResponse(json.dumps(result, default=json_handler),
                                mimetype='application/json')
        else:  #invalid form
            return HttpResponse(json.dumps({
                'success': False,
                'form': form.as_table()
            }),
                                mimetype='application/json')
    return HttpResponse({})
Beispiel #2
0
def add_update_activity(request, method, indicator_id):
    """
    Add/update an indicator's activity. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param method: Whether we are adding or updating.
    :type method: str ("add", "update")
    :param indicator_id: The ObjectId of the indicator to update.
    :type indicator_id: str
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == "POST" and request.is_ajax():
        username = request.user.username
        form = IndicatorActivityForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            add = {
                    'start_date': data['start_date'] if data['start_date'] else '',
                    'end_date': data['end_date'] if data['end_date'] else '',
                    'description': data['description'],
                    'analyst': username
                    }
            if method == "add":
                add['date'] = datetime.datetime.now()
                result = activity_add(indicator_id, add)
            else:
                date = datetime.datetime.strptime(data['date'],
                                                         settings.PY_DATETIME_FORMAT)
                date = date.replace(microsecond=date.microsecond/1000*1000)
                add['date'] = date
                result = activity_update(indicator_id, add)
            if 'object' in result:
                result['html'] = render_to_string('indicators_activity_row_widget.html',
                                                  {'activity': result['object'],
                                                   'admin': is_admin(username),
                                                   'indicator_id':indicator_id})
            return HttpResponse(json.dumps(result,
                                           default=json_handler),
                                mimetype='application/json')
        else: #invalid form
            return HttpResponse(json.dumps({'success':False,
                                            'form':form.as_table()}),
                                mimetype='application/json')
    return HttpResponse({})
Beispiel #3
0
def add_update_activity(request, method, indicator_id):
    """
    Add/update an indicator's activity. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param method: Whether we are adding or updating.
    :type method: str ("add", "update")
    :param indicator_id: The ObjectId of the indicator to update.
    :type indicator_id: str
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == "POST" and request.is_ajax():
        username = request.user.username
        form = IndicatorActivityForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            add = {
                "start_date": data["start_date"] if data["start_date"] else "",
                "end_date": data["end_date"] if data["end_date"] else "",
                "description": data["description"],
            }
            if method == "add":
                add["date"] = datetime.datetime.now()
                result = activity_add(indicator_id, add, username)
            else:
                date = datetime.datetime.strptime(data["date"], settings.PY_DATETIME_FORMAT)
                date = date.replace(microsecond=date.microsecond / 1000 * 1000)
                add["date"] = date
                result = activity_update(indicator_id, add, username)
            if "object" in result:
                result["html"] = render_to_string(
                    "indicators_activity_row_widget.html",
                    {"activity": result["object"], "admin": is_admin(username), "indicator_id": indicator_id},
                )
            return HttpResponse(json.dumps(result, default=json_handler), content_type="application/json")
        else:  # invalid form
            return HttpResponse(
                json.dumps({"success": False, "form": form.as_table()}), content_type="application/json"
            )
    return HttpResponse({})
Beispiel #4
0
def get_indicator_details(indicator_id, analyst):
    """
    Generate the data to render the Indicator details template.

    :param indicator_id: The ObjectId of the Indicator to get details for.
    :type indicator_id: str
    :param analyst: The user requesting this information.
    :type analyst: str
    :returns: template (str), arguments (dict)
    """

    template = None
    users_sources = user_sources(analyst)
    indicator = Indicator.objects(id=indicator_id,
                                  source__name__in=users_sources).first()
    if not indicator:
        error = ("Either this indicator does not exist or you do "
                 "not have permission to view it.")
        template = "error.html"
        args = {'error': error}
        return template, args
    forms = {}
    forms['new_action'] = IndicatorActionsForm(initial={
        'analyst': analyst,
        'active': "off",
        'date': datetime.datetime.now()
    })
    forms['new_activity'] = IndicatorActivityForm(
        initial={
            'analyst': analyst,
            'date': datetime.datetime.now()
        })
    forms['new_campaign'] = CampaignForm()  #'date': datetime.datetime.now(),
    forms['new_source'] = SourceForm(analyst,
                                     initial={'date': datetime.datetime.now()})
    forms['download_form'] = DownloadFileForm(initial={
        "obj_type": 'Indicator',
        "obj_id": indicator_id
    })

    indicator.sanitize("%s" % analyst)

    # remove pending notifications for user
    remove_user_from_notification("%s" % analyst, indicator_id, 'Indicator')

    # subscription
    subscription = {
        'type':
        'Indicator',
        'id':
        indicator_id,
        'subscribed':
        is_user_subscribed("%s" % analyst, 'Indicator', indicator_id),
    }

    # relationship
    relationship = {
        'type': 'Indicator',
        'value': indicator_id,
    }

    #objects
    objects = indicator.sort_objects()

    #relationships
    relationships = indicator.sort_relationships("%s" % analyst, meta=True)

    #comments
    comments = {'comments': indicator.get_comments(), 'url_key': indicator_id}

    # favorites
    favorite = is_user_favorite("%s" % analyst, 'Indicator', indicator.id)

    # services
    manager = crits.service_env.manager
    service_list = manager.get_supported_services('Indicator', True)

    args = {
        'objects': objects,
        'relationships': relationships,
        'comments': comments,
        'relationship': relationship,
        'subscription': subscription,
        "indicator": indicator,
        "forms": forms,
        "indicator_id": indicator_id,
        'service_list': service_list,
        'favorite': favorite,
        'rt_url': settings.RT_URL
    }

    return template, args