Пример #1
0
def boot(request, address):
    '''
    Boot up a component
    '''
    api = API(request)
    if api.put:
        component = Component.get(
            id=None,
            user=request.user,
            action=READ,
            address=address
        )

        action, grantor = component.rights(request.user)
        if action >= EXECUTE:
            api.user_ensure()
            session = component.activate(
                user=request.user
            )
        else:
            session = None

        return api.respond({
            'rights': {
                'action': action_string(action),
                'grantor': grantor.serialize(user=request.user) if grantor else None
            },
            'session': session.serialize(user=request.user) if session else None
        })
    return api.respond_bad()
Пример #2
0
def live(request, address):
    '''
    Open the live collaboration clone for the component.
    '''
    api = API(request)
    if api.get:
        # First, check access rights
        component = Component.get(
            id=None,
            address=address,
            user=request.user,
            action=ANNOTATE
        )
        payload = {}
        # Then, see if a live collaboration clone already exists
        # Get the snapshot data
        # TODO: In the future there may be several collaboration servers
        response = requests.get('http://10.0.1.75:7315/' + address + '@live')
        if response.status_code == 404:
            response = requests.post('http://10.0.1.75:7315/', json = {
                'schemaName': 'stencila-document',
                'documentId': address + '@live',
                'format': 'html',
                'content': component.content_get('html')['content']
            })
            if response.status_code != 200:
                raise Exception(response.text)
        payload['snapshot'] = response.json()
        # Insert the collaboration websocket URL
        if settings.MODE == 'local':
            # It's not possible to do a Websocket redirect (without Nginx's 'X-Accel-Redirect') so
            # in development just give direct WS URL of collaboration server
            ws = 'ws://10.0.1.75:7315'
        else:
            ws = 'wss://stenci.la'
        ws += '/%s@collaborate?permit=%s' % (
            address,
            hmac.new(settings.SECRET_KEY, address + ':' + request.user.username).hexdigest()
        )
        payload['collabUrl'] = ws
        # Insert the user and access rights
        action, grantor = component.rights(request.user)
        payload['user'] = request.user.username
        payload['rights'] = action_string(action)
        # Render template with snapshot data embedded in it
        return api.respond(
            template = 'components/live.html',
            context = {
                'type': 'document',
                'payload': json.dumps(payload)
            }
        )

    return api.respond_bad()