Exemplo n.º 1
0
def page(request, address, component=None):
    '''
    Serve a component page.
    Not a view, but used by views below
    '''
    if component and not request.user_agent.is_bot:
        # Record the visit
        Visit.record(request, address, 'view')
        # Increment view count for this component
        component.views += 1
        component.save()
    # Return the page of the component
    # If the user is authenticated, check if they have an active session for this component
    if request.user.is_authenticated():
        session = component.session(
            user=request.user,
            required=False
        )
        if session:
            # Return the current live page in the session
            # Ensure a trailing slash so that there is no redirection to the 
            # trailing slash URL by the embedded server in the session
            location = '%s:%s/%s/' % (session.worker.ip, session.port, address)
            if settings.MODE == 'local':
                # Proxy to session
                response = requests.get('http://%s' % location)
                return HttpResponse(response.text, status=response.status_code)
            else:
                # Get Nginx to proxy from session
                response = django.http.HttpResponse()
                response['X-Accel-Redirect'] = '/internal-component-session/%s' % location
                return response
    # Otherwise, return the `index.html` for the compooent
    if settings.MODE == 'local':
        # In `local` mode serve the static file
        return django.views.static.serve(request, '%s/index.html' % address, document_root='/srv/stencila/store')
    else:
        # Otherwise, ask Nginx to serve it
        url = '/internal-component-file/%s/index.html' % address
        response = HttpResponse()
        response['X-Accel-Redirect'] = url
        return response