Esempio n. 1
0
def edit_page(context, request):
    params = request.params

    if 'save' in params:
        context.title = params['title']
        context.body = params['body']
        image = params.get('image')
        if hasattr(image, 'file'):
            mimetype = guess_type(image.filename)[0]
            context.upload_image(mimetype, image.file)
        index_doc(context)
        return HTTPFound(request.resource_url(context))

    elif 'cancel' in params:
        return HTTPFound(request.resource_url(context))

    # Show form
    if context.image:
        img_url = request.resource_url(context, 'image')
    else:
        img_url = None

    return {
        'title': 'Edit Page',
        'img_url': img_url,
        'page': context,
    }
Esempio n. 2
0
def add_page(context, request):
    params = request.params

    if 'save' in params:
        # Construct page
        page = Page(params['title'], params['body'])

        # Insert page into document tree
        path = params['path']
        try:
            prev = find_model(context, path)

            # Replace an existing node
            for name, child in prev.items():
                page[name] = child
            folder, page_name = prev.__parent__, prev.__name__
            del folder[page_name]
            folder[page_name] = page
        except KeyError:
            # Insert page at new location
            path = filter(None, path.split('/'))
            page_name = path.pop(-1)
            folder = context
            for name in path:
                if name not in folder:
                    folder[name] = Folder()
                folder = folder[name]
            folder[page_name] = page

        # Handle image upload
        image = params.get('image')
        if hasattr(image, 'file'):
            mimetype = guess_type(image.filename)[0]
            page.upload_image(mimetype, image.file)

        index_doc(page)
        return HTTPFound(request.resource_url(page))

    elif 'cancel' in params:
        redirect_to = params['backto']
        if not redirect_to:
            redirect_to = request.resource_url(context)
        return HTTPFound(redirect_to)

    # Show form
    backto = request.referer
    if backto is None:
        backto = ''
    return {
        'title': 'New Page',
        'img_url': None,
        'page': Page('Page Title', ''),
        'backto': backto,
    }
def root_factory(request):
    connection = get_connection(request)
    root = connection.root()
    if not "site" in root:
        root["site"] = site = Site(u"Sure, Bro!", front_page)
        site.upload_image("image/gif", pkg_resources.resource_stream("surebro", "static/pyramid-ufo.gif"))
        site["example_page"] = Page("Example Page", example_page)
        site.catalog = make_catalog()
        index_doc(site)
        index_doc(site["example_page"])
        transaction.commit()

    return root["site"]