Exemplo n.º 1
0
def djangoInputs(req: WSGIRequest, questionID, answerID, commentID):
    request = HttpRequest()
    request.headers = {}
    request.body = {}

    if 'title' in req.POST: request.body['title'] = req.POST['title']
    if 'body' in req.POST: request.body['body'] = req.POST['body']
    if questionID is not None:
        request.pathParams['questionID'] = questionID
        req.path = str(req.path).replace(str(questionID), ':questionID')
    if answerID is not None:
        request.pathParams['answerID'] = answerID
        req.path = str(req.path).replace(str(answerID), ':answerID')
    if commentID is not None:
        request.pathParams['commentID'] = commentID
        req.path = str(req.path).replace(str(commentID), ':commentID')
    if req.method == 'DELETE' and req.headers.get('hard') is not None: request.headers['hard'] = 'yes'

    response = router(req.method, req.path, request)
    res = HttpResponse(status=response.status,
                       content_type='application/json',
                       content=response.body if response.body is not None else {}
                       )
    return res
Exemplo n.º 2
0
def expire_view_cache(path, meta, key_prefix=None):
    """
    This function allows you to invalidate any item from the per-view cache.
    It probably won't work with things cached using the per-site cache
    middleware (because that takes account of the Vary: Cookie header).
    This assumes you're using the Sites framework.
    Arguments:
        * path: The URL of the view to invalidate, like `/blog/posts/1234/`.
        * meta: Meta details of the request
        * key_prefix: The same as that used for the cache_page()
          function/decorator (if any).
    """
    from django.conf import settings
    from django.core.cache import cache
    from django.utils.cache import get_cache_key
    from django.core.handlers.wsgi import WSGIRequest

    # Create a fake request object
    request = WSGIRequest(meta)
    request.method = 'GET'
    request.path = path

    if 'admin' in path:
        request.META['QUERY_STRING'] = ''

    if settings.USE_I18N:
        request.LANGUAGE_CODE = settings.LANGUAGE_CODE

    # If this key is in the cache, delete it:
    try:
        cache_key = get_cache_key(request, key_prefix=key_prefix)
        if cache_key:
            if cache_key in cache:
                cache.delete(cache_key)
                return (True, 'Successfully invalidated')
            else:
                return (False, 'Cache_key does not exist in cache')
        else:
            raise ValueError('Failed to create cache_key')
    except (ValueError, Exception) as e:
        return (False, e)