Example #1
0
    def obj_create(self, bundle, **kwargs):
        """
        Handles adding Indicator Activity through the API.

        :param bundle: Bundle containing the information to add the Activity.
        :type bundle: Tastypie Bundle object.
        :returns: Bundle object.
        :raises BadRequest: If a campaign name is not provided or creation fails.
        """

        analyst = bundle.request.user.username
        object_id = bundle.data.get('object_id', None)
        start_date = bundle.data.get('start_date', None)
        end_date = bundle.data.get('end_date', None)
        description = bundle.data.get('description', None)

        activity = {
            'analyst': analyst,
            'start_date': start_date,
            'end_date': end_date,
            'description': description,
            'date': datetime.datetime.now()
        }
        result = activity_add(object_id, activity)
        if not result['success']:
            raise BadRequest(result['message'])
        else:
            return bundle
Example #2
0
    def obj_create(self, bundle, **kwargs):
        """
        Handles adding Indicator Activity through the API.

        :param bundle: Bundle containing the information to add the Activity.
        :type bundle: Tastypie Bundle object.
        :returns: Bundle object.
        :raises BadRequest: If a campaign name is not provided or creation fails.
        """

        analyst = bundle.request.user.username
        object_id = bundle.data.get('object_id', None)
        start_date = bundle.data.get('start_date', None)
        end_date = bundle.data.get('end_date', None)
        description = bundle.data.get('description', None)

        activity = {'analyst': analyst,
                    'start_date': start_date,
                    'end_date': end_date,
                    'description': description,
                    'date': datetime.datetime.now()}
        result = activity_add(object_id, activity)
        if not result['success']:
            raise BadRequest(result['message'])
        else:
            return bundle
Example #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'],
                '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({})
Example #4
0
    def obj_create(self, bundle, **kwargs):
        """
        Handles adding Indicator Activity through the API.

        :param bundle: Bundle containing the information to add the Activity.
        :type bundle: Tastypie Bundle object.
        :returns: HttpResponse.
        """

        analyst = bundle.request.user.username
        object_id = bundle.data.get('object_id', None)
        start_date = bundle.data.get('start_date', None)
        end_date = bundle.data.get('end_date', None)
        description = bundle.data.get('description', None)

        content = {'return_code': 1, 'type': 'Indicator'}

        if not object_id or not description:
            content['message'] = 'Must provide object_id and description.'
            self.crits_response(content)

        activity = {
            'analyst': analyst,
            'start_date': start_date,
            'end_date': end_date,
            'description': description,
            'date': datetime.datetime.now()
        }
        result = activity_add(object_id, activity)
        if not result['success']:
            content['message'] = result['message']
            self.crits_response(content)

        if result.get('message'):
            content['message'] = result.get('message')
        if result.get('id'):
            url = reverse('api_dispatch_detail',
                          kwargs={
                              'resource_name': 'indicators',
                              'api_name': 'v1',
                              'pk': result.get('id')
                          })
            content['id'] = result.get('id')
            content['url'] = url
        if result['success']:
            content['return_code'] = 0
        self.crits_response(content)
Example #5
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({})
Example #6
0
File: api.py Project: brlogan/crits
    def obj_create(self, bundle, **kwargs):
        """
        Handles adding Indicator Activity through the API.

        :param bundle: Bundle containing the information to add the Activity.
        :type bundle: Tastypie Bundle object.
        :returns: HttpResponse.
        """

        user = bundle.request.user.username
        object_id = bundle.data.get('object_id', None)
        start_date = bundle.data.get('start_date', None)
        end_date = bundle.data.get('end_date', None)
        description = bundle.data.get('description', None)

        content = {'return_code': 1,
                   'type': 'Indicator'}

        if not object_id or not description:
            content['message'] = 'Must provide object_id and description.'
            self.crits_response(content)

        activity = {'user': user,
                    'start_date': start_date,
                    'end_date': end_date,
                    'description': description,
                    'date': datetime.datetime.now()}
        result = activity_add(object_id, activity)
        if not result['success']:
            content['message'] = result['message']
            self.crits_response(content)

        if result.get('message'):
            content['message'] = result.get('message')
        if result.get('id'):
            url = reverse('api_dispatch_detail',
                          kwargs={'resource_name': 'indicators',
                                  'api_name': 'v1',
                                  'pk': result.get('id')})
            content['id'] = result.get('id')
            content['url'] = url
        if result['success']:
            content['return_code'] = 0
        self.crits_response(content)
Example #7
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({})