def add_update_action(request, method, indicator_id): """ Add/update an indicator's action. 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 = IndicatorActionsForm(request.POST) if form.is_valid(): data = form.cleaned_data add = { 'action_type': data['action_type'], 'begin_date': data['begin_date'] if data['begin_date'] else '', 'end_date': data['end_date'] if data['end_date'] else '', 'performed_date': data['performed_date'] if data['performed_date'] else '', 'active': data['active'], 'reason': data['reason'], 'analyst': username, } if method == "add": add['date'] = datetime.datetime.now() result = action_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 = action_update(indicator_id, add) if 'object' in result: result['html'] = render_to_string( 'indicators_action_row_widget.html', { 'action': 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({})
def add_update_action(request, method, indicator_id): """ Add/update an indicator's action. 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 = IndicatorActionsForm(request.POST) if form.is_valid(): data = form.cleaned_data add = { 'action_type': data['action_type'], 'begin_date': data['begin_date'] if data['begin_date'] else '', 'end_date': data['end_date'] if data['end_date'] else '', 'performed_date': data['performed_date'] if data['performed_date'] else '', 'active': data['active'], 'reason': data['reason'], 'analyst': username } if method == "add": add['date'] = datetime.datetime.now() result = action_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 = action_update(indicator_id, add) if 'object' in result: result['html'] = render_to_string('indicators_action_row_widget.html', {'action': 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({})
def add_update_action(request, method, indicator_id): """ Add/update an indicator's action. 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 = IndicatorActionsForm(request.POST) if form.is_valid(): data = form.cleaned_data add = { "action_type": data["action_type"], "begin_date": data["begin_date"] if data["begin_date"] else "", "end_date": data["end_date"] if data["end_date"] else "", "performed_date": data["performed_date"] if data["performed_date"] else "", "active": data["active"], "reason": data["reason"], "analyst": username, } if method == "add": add["date"] = datetime.datetime.now() result = action_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 = action_update(indicator_id, add) if "object" in result: result["html"] = render_to_string( "indicators_action_row_widget.html", {"action": 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({})