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({})
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({})
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({})