示例#1
0
文件: views.py 项目: xuanxu11/PyLucid
def _form_submission(request):
    """ Handle a AJAX comment form submission """
    # Use django.contrib.comments.views.comments.post_comment to handle a comment post.
    response = post_comment(request)
    if isinstance(response, HttpResponseRedirect):
        # reload the page after comment saved via JavaScript
        response = HttpResponse("reload")

        if not request.user.is_authenticated():
            # Store user data for anonymous users in a secure cookie, used in _get_form() to pre fill the form
            comments_data = {
                "name": request.POST["name"],
                "email": request.POST.get("email", ""),
                "url": request.POST.get("url", ""),
            }
            # Store the user data with a security hash
            c = ClientCookieStorage(cookie_key=COOKIE_KEY)
            response = c.save_data(comments_data, response)

    return response
示例#2
0
    def test_old_api(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")  # trigger all warnings

            c = ClientCookieStorage(cookie_key="foo")

            self.assertEqual(len(w), 1)
            self.assertEqual(
                str(w[-1].message),
                "ClientCookieStorage is old API! Please change to SignedCookieStorage! This will be removed in the future!"
            )
            # self.assertIsInstance(w[-1].category, FutureWarning) # FIXME: AssertionError: <class 'FutureWarning'> is not an instance of <class 'FutureWarning'>
            self.assertTrue(issubclass(w[-1].category, FutureWarning))
示例#3
0
文件: views.py 项目: xuanxu11/PyLucid
    try:
        ctype = request.GET["content_type"].split(".", 1)
        model = models.get_model(*ctype)
    except Exception, err:
        return bad_request(APP_LABEL, "error", "Wrong content type: %s" % err)

    try:
        object_pk = request.GET["object_pk"]
        target = model._default_manager.using(None).get(pk=object_pk)
    except Exception, err:
        return bad_request(APP_LABEL, "error", "Wrong object_pk: %s" % err)

    data = {}
    if not request.user.is_authenticated() and COOKIE_KEY in request.COOKIES:
        # Get user data from secure cookie, set in the past, see _form_submission()
        c = ClientCookieStorage(cookie_key=COOKIE_KEY)
        try:
            data = c.get_data(request)
        except ClientCookieStorageError, err:
            LogEntry.objects.log_action(
                app_label=APP_LABEL, action="wrong cookie data", message="%s" % err,
            )
            if settings.DEBUG:
                return bad_request(APP_LABEL, "error", "Wrong cookie data: %s" % err)

    form = comments.get_form()(target, initial=data)
    return {"form":form}


@csrf_protect
@check_request(APP_LABEL, "_form_submission() error", must_ajax=True)