Пример #1
0
def list_spaces(environ, start_response):
    """
    List all the spaces on the service, as a JSON list.

    If a "mine" parameter is present, just get the current
    user's spaces.
    """
    store = environ['tiddlyweb.store']
    mine = environ['tiddlyweb.query'].get('mine', [None])[0]
    current_user = environ['tiddlyweb.usersign']['name']
    if mine:
        spaces = []
        try:
            recipe_names = store.storage.cached_storage.user_spaces(
                    current_user)
        except AttributeError:
            recipe_names = store.storage.user_spaces(current_user)
        for recipe in recipe_names:
            spaces.append(Space.name_from_recipe(recipe))
    else:
        spaces = [Space.name_from_recipe(recipe.name) for
                recipe in store.list_recipes() if
                Space.recipe_is_public(recipe.name)]
    start_response('200 OK', [
        ('Cache-Control', 'no-cache'),
        ('Content-Type', 'application/json; charset=UTF-8')])
    return simplejson.dumps([{'name': space, 'uri': space_uri(environ, space)}
        for space in sorted(spaces)])
Пример #2
0
def list_spaces(environ, start_response):
    """
    List all the spaces on the service, as a JSON list.

    If a "mine" parameter is present, just get the current
    user's spaces.
    """
    store = environ['tiddlyweb.store']
    mine = environ['tiddlyweb.query'].get('mine', [None])[0]
    current_user = environ['tiddlyweb.usersign']['name']
    if mine:
        spaces = []
        try:
            recipe_names = store.storage.cached_storage.user_spaces(
                current_user)
        except AttributeError:
            recipe_names = store.storage.user_spaces(current_user)
        for recipe in recipe_names:
            spaces.append(Space.name_from_recipe(recipe))
    else:
        spaces = [
            Space.name_from_recipe(recipe.name)
            for recipe in store.list_recipes()
            if Space.recipe_is_public(recipe.name)
        ]
    start_response('200 OK',
                   [('Cache-Control', 'no-cache'),
                    ('Content-Type', 'application/json; charset=UTF-8')])
    return simplejson.dumps([{
        'name': space,
        'uri': space_uri(environ, space)
    } for space in sorted(spaces)])
Пример #3
0
def web_tiddler_url(environ, tiddler, container='bags', full=True):
    """
    Override default tiddler_url to be space+host aware.

    If the bag or recipe of the tiddler is of a space, switch to
    that space's host for the duration of uri creation.

    Do this all the time, so that we get the right URIs even
    when working around ControlView.

    If the bag does not fit in a space, then make is URI be at
    the server_host domain. If/when auxbags are made to work this
    will need to be reviewed.
    """
    if '_canonical_uri' in tiddler.fields:
        return tiddler.fields['_canonical_uri']

    saved_host = environ.get('HTTP_HOST', '')
    try:
        if container == 'recipes':
            space_name = Space.name_from_recipe(tiddler.recipe)
        else:
            space_name = Space.name_from_bag(tiddler.bag)
        space_name = space_name + '.'
    except ValueError, exc:
        space_name = ''
Пример #4
0
def web_tiddler_url(environ,
                    tiddler,
                    container='bags',
                    full=True,
                    friendly=False):
    """
    Override default tiddler_url to be space+host aware.

    If the bag or recipe of the tiddler is of a space, switch to
    that space's host for the duration of uri creation.

    Do this all the time, so that we get the right URIs even
    when working around ControlView.

    If the bag does not fit in a space, then make is URI be at
    the server_host domain. If/when auxbags are made to work this
    will need to be reviewed.
    """
    saved_host = environ.get('HTTP_HOST', '')
    try:
        if container == 'recipes':
            space_name = Space.name_from_recipe(tiddler.recipe)
        else:
            space_name = Space.name_from_bag(tiddler.bag)
        space_name = space_name + '.'
    except ValueError:
        space_name = ''

    host = environ['tiddlyweb.config']['server_host']['host']
    port = environ['tiddlyweb.config']['server_host']['port']
    if port is '443' or port is '80':
        port = ''
    else:
        port = ':%s' % port
    environ['HTTP_HOST'] = '%s%s%s' % (space_name.encode('utf-8'), host, port)

    if friendly and space_name:
        url = '%s/%s' % (tiddlyweb.web.util.server_base_url(environ),
                         tiddlyweb.web.util.encode_name(tiddler.title))
    else:
        url = original_tiddler_url(environ, tiddler, container, full)
    if saved_host:
        environ['HTTP_HOST'] = saved_host
    elif 'HTTP_HOST' in environ:
        del environ['HTTP_HOST']
    return url
Пример #5
0
def web_tiddler_url(environ, tiddler, container='bags', full=True,
        friendly=False):
    """
    Override default tiddler_url to be space+host aware.

    If the bag or recipe of the tiddler is of a space, switch to
    that space's host for the duration of uri creation.

    Do this all the time, so that we get the right URIs even
    when working around ControlView.

    If the bag does not fit in a space, then make is URI be at
    the server_host domain. If/when auxbags are made to work this
    will need to be reviewed.
    """
    saved_host = environ.get('HTTP_HOST', '')
    try:
        if container == 'recipes':
            space_name = Space.name_from_recipe(tiddler.recipe)
        else:
            space_name = Space.name_from_bag(tiddler.bag)
        space_name = space_name + '.'
    except ValueError:
        space_name = ''

    host = environ['tiddlyweb.config']['server_host']['host']
    port = environ['tiddlyweb.config']['server_host']['port']
    if port is '443' or port is '80':
        port = ''
    else:
        port = ':%s' % port
    environ['HTTP_HOST'] = '%s%s%s' % (space_name.encode('utf-8'),
        host, port)

    if friendly and space_name:
        url = '%s/%s' % (tiddlyweb.web.util.server_base_url(environ),
                tiddlyweb.web.util.encode_name(tiddler.title))
    else:
        url = original_tiddler_url(environ, tiddler, container, full)
    if saved_host:
        environ['HTTP_HOST'] = saved_host
    elif 'HTTP_HOST' in environ:
        del environ['HTTP_HOST']
    return url
Пример #6
0
def web_tiddler_url(environ, tiddler, container='bags', full=True):
    """
    Override default tiddler_url to be space+host aware.

    If the bag or recipe of the tiddler is of a space, switch to
    that space's host for the duration of uri creation.

    Do this all the time, so that we get the right URIs even
    when working around ControlView.
    """
    if '_canonical_uri' in tiddler.fields:
        return tiddler.fields['_canonical_uri']

    saved_host = environ.get('HTTP_HOST', '')
    try:
        if container == 'recipes':
            space_name = Space.name_from_recipe(tiddler.recipe)
        else:
            space_name = Space.name_from_bag(tiddler.bag)

        host = environ['tiddlyweb.config']['server_host']['host']
        port = environ['tiddlyweb.config']['server_host']['port']
        if port is '443' or port is '80':
            port = ''
        else:
            port = ':%s' % port
        environ['HTTP_HOST'] = '%s.%s%s' % (space_name.encode('utf-8'),
            host, port)
    except ValueError:
        pass
    url = original_tiddler_url(environ, tiddler, container, full)
    if saved_host:
        environ['HTTP_HOST'] = saved_host
    elif 'HTTP_HOST' in environ:
        del environ['HTTP_HOST']
    return url
Пример #7
0
def test_name_from_recipe():
    assert Space.name_from_recipe('cat_private') == 'cat'
    py.test.raises(ValueError, 'Space.name_from_recipe("cat_ball")')
Пример #8
0
    def list_tiddlers(self, tiddlers):
        """
        List the tiddlers from a container. Include a link
        to the container if it can be viewed by the current
        user. List the available serializations for the tiddlers.
        """
        tiddlers_url = (self.environ.get('SCRIPT_NAME', '')
                + self.environ.get('PATH_INFO', ''))
        if tiddlers_url.startswith('/tiddlers'):
            tiddlers.link = '/tiddlers'

        template_name = 'friendlytiddlers.html'
        if '/bags/' in tiddlers.link or '/recipes/' in tiddlers.link:
            template_name = 'tiddlers.html'

        container_name = ''
        container_type = 'bags'
        container_url = ''
        container_policy = False
        store = self.environ['tiddlyweb.store']
        user = self.environ['tiddlyweb.usersign']
        space_name = ''
        if not (tiddlers.is_search or tiddlers.is_revisions):
            if tiddlers.recipe:
                name = tiddlers.recipe
                try:
                    space_name = Space.name_from_recipe(name)
                    tiddlers.title = 'Tiddlers in %s' % space_name
                except ValueError:
                    pass
                container_url = '/recipes/%s' % name
                container_name = 'Recipe %s' % name
                container_type = 'recipes'
                try:
                    store.get(Recipe(name)).policy.allows(user, 'read')
                    container_policy = True
                except PermissionsError:
                    pass
            elif tiddlers.bag:
                name = tiddlers.bag
                try:
                    space_name = Space.name_from_recipe(name)
                    tiddlers.title = 'Tiddlers in %s' % space_name
                except ValueError:
                    pass
                container_url = '/bags/%s' % name
                container_name = 'Bag %s' % name
                try:
                    store.get(Bag(name)).policy.allows(user, 'manage')
                    container_policy = True
                except PermissionsError:
                    pass

        if tiddlers.is_revisions:
            container_policy = True
            container_url = tiddlers.link.rsplit('/revisions')[0]
            container_name = 'Head'

        try:
            query_string = self.environ.get('QUERY_STRING', '').decode('utf-8')
        except UnicodeDecodeError:
            query_string = u'invalid+query+string+encoding'

        links = self.environ.get('tiddlyweb.config',
                {}).get('extension_types', {}).keys()

        if query_string:
            query_string = '?%s' % query_string

        if tiddlers.is_search:
            template_name = 'search.html'
            if 'tiddlyweb.query.original' in self.environ:
                tiddlers.title = ('Search for %s'
                        % self.environ['tiddlyweb.query.original'])

        return send_template(self.environ, template_name, {
            'meta_keywords': 'tiddlers, tiddlyspace',
            'meta_description': 'A list of tiddlers on TiddlySpace',
            'title': tiddlers.title,
            'tiddler_url': tiddler_url,
            'environ': self.environ,
            'revisions': tiddlers.is_revisions,
            'tiddlers_url': tiddlers.link,
            'space_uri': space_uri,
            'space_bag': space_bag,
            'query_string': query_string,
            'container_type': container_type,
            'container_name': container_name,
            'container_url': container_url,
            'container_policy': container_policy,
            'links': links,
            'space_name': space_name,
            'tiddlers': tiddlers})
Пример #9
0
def test_name_from_recipe():
    assert Space.name_from_recipe('cat_private') == 'cat'
    py.test.raises(ValueError, 'Space.name_from_recipe("cat_ball")')