コード例 #1
0
ファイル: views.py プロジェクト: lakiw/cripts
def add_update_comment(request, method, obj_type, obj_id):
    """
    Add/update a comment for a top-level object. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param method: If this is a new comment or an update (set to "update").
    :type method: str
    :param obj_type: The type of the top-level object.
    :type obj_type: str
    :param obj_id: The ObjectId of the top-level object.
    :type obj_id: str
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == "POST" and request.is_ajax():
        form = AddCommentForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            subscr = cleaned_data.get('subscribable', False)
            analyst = request.user.username
            if method == "update":
                return comment_update(cleaned_data, obj_type, obj_id,
                                      subscr, analyst)
            else:
                return comment_add(cleaned_data, obj_type, obj_id, method,
                                      subscr, analyst)
        return HttpResponse(json.dumps({'success':False,
                                        'form':form.as_table()}),
                            content_type="application/json")
    return render_to_response("error.html", {'error':'Expected AJAX/POST'})
コード例 #2
0
def add_update_comment(request, method, obj_type, obj_id):
    """
    Add/update a comment for a top-level object. Should be an AJAX POST.

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :param method: If this is a new comment or an update (set to "update").
    :type method: str
    :param obj_type: The type of the top-level object.
    :type obj_type: str
    :param obj_id: The ObjectId of the top-level object.
    :type obj_id: str
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == "POST" and request.is_ajax():
        form = AddCommentForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            subscr = cleaned_data.get('subscribable', False)
            analyst = request.user.username
            if method == "update":
                return comment_update(cleaned_data, obj_type, obj_id, subscr,
                                      analyst)
            else:
                return comment_add(cleaned_data, obj_type, obj_id, method,
                                   subscr, analyst)
        return HttpResponse(json.dumps({
            'success': False,
            'form': form.as_table()
        }),
                            content_type="application/json")
    return render_to_response("error.html", {'error': 'Expected AJAX/POST'})
コード例 #3
0
ファイル: api.py プロジェクト: lakiw/cripts
    def obj_create(self, bundle, **kwargs):
        """
        Handles creating Comments through the API.

        :param bundle: Bundle containing the information to create the Comment.
        :type bundle: Tastypie Bundle object.
        :returns: HttpResponse.
        """

        analyst = bundle.request.user.username
        comment = bundle.data.get('comment', None)
        obj_type = bundle.data.get('object_type', None)
        obj_id = bundle.data.get('object_id', None)

        content = {'return_code': 1, 'type': 'Comment', 'success': False}

        if not obj_type:
            content['message'] = 'Must provide an object type.'
            self.cripts_response(content)
        if not obj_id:
            content['message'] = 'Must provide an object id.'
            self.cripts_response(content)
        if not comment:
            content['message'] = 'Must provide a comment.'
            self.cripts_response(content)

        data = {
            'comment': comment,
            'object_type': obj_type,
            'object_id': obj_id,
            'url_key': obj_id
        }

        retVal = comment_add(data, obj_type, obj_id, '', {}, analyst)

        if "Comment added successfully!" in retVal.content:
            content['success'] = True
            content['return_code'] = 0
            content['message'] = 'Comment added successfully!'

        self.cripts_response(content)
コード例 #4
0
ファイル: api.py プロジェクト: lakiw/cripts
    def obj_create(self, bundle, **kwargs):
        """
        Handles creating Comments through the API.

        :param bundle: Bundle containing the information to create the Comment.
        :type bundle: Tastypie Bundle object.
        :returns: HttpResponse.
        """

        analyst = bundle.request.user.username
        comment = bundle.data.get("comment", None)
        obj_type = bundle.data.get("object_type", None)
        obj_id = bundle.data.get("object_id", None)

        content = {"return_code": 1, "type": "Comment", "success": False}

        if not obj_type:
            content["message"] = "Must provide an object type."
            self.cripts_response(content)
        if not obj_id:
            content["message"] = "Must provide an object id."
            self.cripts_response(content)
        if not comment:
            content["message"] = "Must provide a comment."
            self.cripts_response(content)

        data = {"comment": comment, "object_type": obj_type, "object_id": obj_id, "url_key": obj_id}

        retVal = comment_add(data, obj_type, obj_id, "", {}, analyst)

        if "Comment added successfully!" in retVal.content:
            content["success"] = True
            content["return_code"] = 0
            content["message"] = "Comment added successfully!"

        self.cripts_response(content)