예제 #1
0
def wiki_page(environ, start_response):
    """
    Present a single tiddler from a given tank.
    """
    tank_name = get_route_value(environ, 'bag_name')
    store = environ['tiddlyweb.store']
    usersign = environ['tiddlyweb.usersign']
    config = environ['tiddlyweb.config']

    try:
        bag = store.get(Bag(tank_name))
        bag = augment_bag(store, bag)
    except NoBagError:
        raise HTTP404('no tank found for %s' % tank_name)

    try:
        tiddler_name = get_route_value(environ, 'tiddler_name')
    except (KeyError, AttributeError):
        raise HTTP302(tank_page_uri(environ, tank_name, INDEX_PAGE))

    if tiddler_name in SPECIAL_PAGES:
        return SPECIAL_PAGES[tiddler_name](environ, start_response)

    # let permissions problems raise
    bag.policy.allows(usersign, 'read')

    editable = True
    creatable = True
    deletable = True
    try:
        bag.policy.allows(usersign, 'write')
    except PermissionsError:
        editable = False
    try:
        bag.policy.allows(usersign, 'create')
    except PermissionsError:
        creatable = False
    try:
        bag.policy.allows(usersign, 'delete')
    except PermissionsError:
        deletable = False

    try:
        tiddler = Tiddler(tiddler_name, tank_name)
        tiddler = store.get(tiddler)
    except NoTiddlerError:
        tiddler.type = 'text/x-markdown'
        tiddler.text = '### This tiddler does not yet exist\n'
        if creatable:
            editable = True
        else:
            editable = False
        deletable = False
        if tiddler.title != INDEX_PAGE:
            sisterlinks = get_sisterlinks(environ, tiddler)
            tiddler.text = (tiddler.text +
                            '\n### Other tiddlers with similar names\n' +
                            ''.join([
                                '* [[%s]]@[[%s]] @%s\n' %
                                (stiddler.title, stiddler.bag, stiddler.bag)
                                for stiddler in sisterlinks
                            ]))

    if renderable(tiddler, environ):
        backlinks = get_backlinks(environ, tiddler)
        rellinks = get_rellinks(environ, tiddler)
        compable = full_search(config, 'id:"%s:app"' % tank_name)
        html = render_wikitext(tiddler, environ)
        start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8'),
                                  ('Cache-Control', 'no-cache')])
        return send_template(
            environ, WIKI_TEMPLATE, {
                'tiddler': tiddler,
                'html': html,
                'bag': bag,
                'backlinks': backlinks,
                'create': creatable,
                'edit': editable,
                'delete': deletable,
                'compable': compable,
                'links': rellinks,
            })
    else:
        return tiddler_get(environ, start_response)
예제 #2
0
파일: wiki.py 프로젝트: pads/tank
def wiki_page(environ, start_response):
    """
    Present a single tiddler from a given tank.
    """
    tank_name = get_route_value(environ, 'bag_name')
    store = environ['tiddlyweb.store']
    usersign = environ['tiddlyweb.usersign']
    config = environ['tiddlyweb.config']

    try:
        bag = store.get(Bag(tank_name))
    except NoBagError:
        raise HTTP404('no tank found for %s' % tank_name)

    try:
        tiddler_name = get_route_value(environ, 'tiddler_name')
    except (KeyError, AttributeError):
        raise HTTP302(tank_page_uri(environ, tank_name, 'index'))

    if tiddler_name in SPECIAL_PAGES:
        return SPECIAL_PAGES[tiddler_name](environ, start_response)

    # let permissions problems raise
    bag.policy.allows(usersign, 'read')

    editable = True
    creatable = True
    deletable = True
    try:
        bag.policy.allows(usersign, 'write')
    except PermissionsError:
        editable = False
    try:
        bag.policy.allows(usersign, 'create')
    except PermissionsError:
        creatable = False
    try:
        bag.policy.allows(usersign, 'delete')
    except PermissionsError:
        deletable = False

    try:
        tiddler = Tiddler(tiddler_name, tank_name)
        tiddler = store.get(tiddler)
    except NoTiddlerError:
        tiddler.type = 'text/x-markdown'
        tiddler.text = '### This tiddler does not yet exist\n'
        if creatable:
            editable = True
        deletable = False

    if renderable(tiddler, environ):
        backlinks = get_backlinks(environ, tiddler)
        compable = full_search(config, 'id:"%s:app"' % tank_name)
        html = render_wikitext(tiddler, environ)
        wiki_template = get_template(environ, WIKI_TEMPLATE)
        start_response('200 OK', [
            ('Content-Type', 'text/html; charset=UTF-8'),
            ('Cache-Control', 'no-cache')])
        return wiki_template.generate({
            'socket_link': config.get('socket.link'),
            'csrf_token': get_nonce(environ),
            'gravatar': gravatar(environ),
            'user': usersign['name'],
            'tiddler': tiddler,
            'html': html,
            'bag': bag,
            'backlinks': backlinks,
            'edit': editable,
            'delete': deletable,
            'compable': compable,
        })
    else:
        return tiddler_get(environ, start_response)
예제 #3
0
파일: wiki.py 프로젝트: BillSeitz/tank
def wiki_page(environ, start_response):
    """
    Present a single tiddler from a given tank.
    """
    tank_name = get_route_value(environ, "bag_name")
    store = environ["tiddlyweb.store"]
    usersign = environ["tiddlyweb.usersign"]
    config = environ["tiddlyweb.config"]

    try:
        bag = store.get(Bag(tank_name))
        bag = augment_bag(store, bag)
    except NoBagError:
        raise HTTP404("no tank found for %s" % tank_name)

    try:
        tiddler_name = get_route_value(environ, "tiddler_name")
    except (KeyError, AttributeError):
        raise HTTP302(tank_page_uri(environ, tank_name, INDEX_PAGE))

    if tiddler_name in SPECIAL_PAGES:
        return SPECIAL_PAGES[tiddler_name](environ, start_response)

    # let permissions problems raise
    bag.policy.allows(usersign, "read")

    editable = True
    creatable = True
    deletable = True
    try:
        bag.policy.allows(usersign, "write")
    except PermissionsError:
        editable = False
    try:
        bag.policy.allows(usersign, "create")
    except PermissionsError:
        creatable = False
    try:
        bag.policy.allows(usersign, "delete")
    except PermissionsError:
        deletable = False

    try:
        tiddler = Tiddler(tiddler_name, tank_name)
        tiddler = store.get(tiddler)
    except NoTiddlerError:
        tiddler.type = "text/x-markdown"
        tiddler.text = "### This tiddler does not yet exist\n"
        if creatable:
            editable = True
        else:
            editable = False
        deletable = False
        if tiddler.title != INDEX_PAGE:
            sisterlinks = get_sisterlinks(environ, tiddler)
            tiddler.text = (
                tiddler.text
                + "\n### Other tiddlers with similar names\n"
                + "".join(
                    ["* [[%s]]@[[%s]] @%s\n" % (stiddler.title, stiddler.bag, stiddler.bag) for stiddler in sisterlinks]
                )
            )

    if renderable(tiddler, environ):
        backlinks = get_backlinks(environ, tiddler)
        rellinks = get_rellinks(environ, tiddler)
        compable = full_search(config, 'id:"%s:app"' % tank_name)
        html = render_wikitext(tiddler, environ)
        start_response("200 OK", [("Content-Type", "text/html; charset=UTF-8"), ("Cache-Control", "no-cache")])
        return send_template(
            environ,
            WIKI_TEMPLATE,
            {
                "tiddler": tiddler,
                "html": html,
                "bag": bag,
                "backlinks": backlinks,
                "create": creatable,
                "edit": editable,
                "delete": deletable,
                "compable": compable,
                "links": rellinks,
            },
        )
    else:
        return tiddler_get(environ, start_response)
예제 #4
0
def get_handler(environ, start_response):
    """
    selector comes to this function when a url is found. 
    
    retrieve the recipe/serialization details and pass to 
    tiddlyweb.web.handler.recipe.get_tiddlers
    """
    selector_variables = extract_variables(environ['wsgiorg.routing_args'][1])
    #add the username to be more compliant with recipe variables
    selector_variables['user'] = environ['tiddlyweb.usersign']['name']
    
    potential_matches = get_urls(environ['tiddlyweb.config']['url_bag'], \
        environ['tiddlyweb.store'])
    match = match_url(environ['tiddlyweb.config']['selector'], \
        environ['selector.matches'][0], potential_matches, \
        environ['tiddlyweb.store'])
    
    destination_url = replace_url_patterns(selector_variables, match.text)
    
    if is_redirect(match):
        if destination_url.startswith('www.'):
            destination_url = 'http://' + destination_url
        #redirect to the url and return
        start_response('301 Moved Permanently', [
            ('Location', str(destination_url))
            ])
        return_link = '''<html>
<head>
<title>URL Redirect</title>
</head>
<body>
Please see <a href="%s">%s</a>
</body>
</html>''' % (destination_url, destination_url)
        return return_link
    
    try:
        url_part, custom_filters = destination_url.split('?', 1)
    except ValueError:
        url_part = destination_url
        custom_filters = None
    
    mime_type = 'default'
    destination_parts = figure_destination(url_part)
    for part, value in destination_parts.iteritems():
        if part == 'extension':
            environ['tiddlyweb.extension'] = str(value)
            mime_type = \
                environ['tiddlyweb.config']['extension_types'].get(value)
        else:
            try:
                environ['wsgiorg.routing_args'][1][part] = str(value)
            except UnicodeEncodeError:
                environ['wsgiorg.routing_args'][1][part] = str(value.encode('utf8'))

    for key, value in selector_variables.iteritems():
        if key not in destination_parts:
            destination_parts[key] = value

    #special handling of unicode is required here so that recipe templates will work properly
    environ['tiddlyweb.recipe_template'] = {}
    for key, value in destination_parts.iteritems():
        try:
            environ['tiddlyweb.recipe_template'][key] = value.decode('utf8')
        except UnicodeEncodeError:
            environ['tiddlyweb.recipe_template'][key] = value
            
    filters = figure_filters(environ['tiddlyweb.filters'], custom_filters)
    environ['tiddlyweb.filters'] = filters
    
    #set tiddlyweb.type to make sure we call the correct serializer
    environ['tiddlyweb.type'] = [mime_type]
    
    if 'tiddler_name' in environ['wsgiorg.routing_args'][1]:
        return tiddler_get(environ, start_response)
    elif 'recipe_name' in environ['wsgiorg.routing_args'][1]:
        return recipe_tiddlers(environ, start_response)
    elif 'bag_name' in environ['wsgiorg.routing_args'][1]:
        return bag_tiddlers(environ, start_response)
    
    raise InvalidDestinationURL('URL \'%s\' is incorrectly formatted' % \
        destination_url)