Пример #1
0
 def process_request(self, request):
     # Check if maintenance mode is activated, if not set default to False
     if getattr(settings, 'MAINTENANCE_MODE', False):
         if request.path.startswith('/theme/') or request.path.startswith('/static/'):
             return
         # Check if the user doing the request is logged in and a staff member
         if request.user.is_anonymous():
             # Let the user log in
             return
         if request.user.is_staff:
             # Let Django normally handle the request
             return
         # Otherwise show the user the 503 page
         return service_unavailable(request)
Пример #2
0
def handle_xmlrpc(request):
    """ Handle XML-RPC requests

    request
        The HttpRequest object that carries the XML-RPC call. If this is a
        GET request, nothing will happen (we only accept POST requests)
    """
    if request.method == 'POST':
        # Limit the mumber of XML-RPC calls...
        key = request.META['REMOTE_ADDR']
        calls = cache.get('xmlrpc:calls:%s' % key, default = 0)
        if calls > 5:
            # Return '503'
            return service_unavailable(request)
        else:
            # Increment the limit counter
            cache.set('xmlrpc:calls:%s' % key, calls + 1, 600)

        # The XML-RPC response
        response = HttpResponse(mimetype = 'text/xml')

        # Try to process the request
        try:
            result = xmlrpc_interface.process(request.raw_post_data)
        except:
            # Decrement the limit counter
            calls = cache.get('xmlrpc:calls:%s' % key)
            cache.set('xmlrpc:calls:%s' % key, calls - 1, 600)
            # Return '500'
            return server_error(request)
        else:
            # Decrement the limit counter
            calls = cache.get('xmlrpc:calls:%s' % key)
            cache.set('xmlrpc:calls:%s' % key, calls - 1, 600)
            # Write the result to the XML-RPC response
            response.write(result)
            # Return the response
            return response
    else:
        # Return '404'
        return page_not_found(request)