Esempio n. 1
0
def add(request):
    form = ArticleForm(request.form, obj=AutoNow(), prefix='create')
    if request.method == 'POST' and form.validate():
        key_name, kwds = prepare(form)
        # kwds['html'] = render_jinja('cache_body.html', body=kwds['body'])
        kwds['author'] = users.get_current_user()
        # lets see if we do not overwrite an existing item.
        created, post = create_entity(key_name, **kwds)
        if not created:
            return render_template('app:articles/form.html', form=form, status='error',msg='non-unique')
        return redirect(url_for('nut:articles/list'), 301)
    return render_template('app:articles/form.html', form=form)
Esempio n. 2
0
def index(request):
    latest = Post.pub().fetch(5)
    # latest.reverse()
    ns = NutSettings()
    template = ns.index_layout
    if template:
        template = 'nut:layout/%s' % template
    else:
        template = 'app:articles/index.html'
    return render_template(template, posts=latest)
    latest = Post.pub().get()
    if latest:
        return redirect(quote((u'/%s' % latest.lookup).encode('utf-8')), 301)
    return render_template('app/empty.html')
Esempio n. 3
0
def nut(request):
    from hazel import NutSettings, SettingsForm, handle_form_data
    nuts = {'hazel' : {'settings' : NutSettings,
                       'form': SettingsForm(request.form, obj=NutSettings(), \
                                            prefix='hazel_config') }}
    handler = {'hazel' : handle_form_data }
    for nut in NutSettings().nuts:
        # load the Settings, Form and data handler from the
        # nuts.
        module = __import__('hazel.nuts.%s' % nut, fromlist=['hazel.nuts'])
        ns, sf, hd = map(lambda prop: getattr(module, prop, None),
                         ['NutSettings', 'SettingsForm', 'handle_form_data'])
        # if the nut does not provide a formmodule or no data handler
        # skip it. We won't be able to configure it anyway.
        if not sf or not hd:
            continue
        nuts[nut] = {'settings' : ns(),
                       'form': sf(request.form, obj=ns(), \
                                  prefix='%s_config' % nut)}
        try:
            nuts[nut]['form'].prepopulate()
        except:
            pass
        handler[nut] = hd
    updated = False
    if request.method == 'POST' and all([nut['form'].validate()\
                                         for nut in nuts.values()]):
        # let all nuts handle their form.
        for nut in nuts:
            handler[nut](nuts[nut]['form'])
        updated = True
        return redirect(url_for('admin/configuration', message='updated'), 302)
    return render_template('app/configure.html', nuts=nuts, updated=updated)
Esempio n. 4
0
def add(request, key=None, type=FILE):
    to = key # lame but it does the trick for now
    if type == FOLDER:
        form = FolderForm(request.form)
    else:
        form = FileForm(request.form)
    if request.method == "POST" and form.validate():
        if len(form.slug.data) < 1:
            form.slug.data = slugify(form.name.data)
        if type == FOLDER:
            file = File.add(to=to,type=type, name=form.name.data,
                                            slug=form.slug.data,
                                            breadcrumb=form.breadcrumb.data,
                                            state=form.state.data,
                                            active=form.active.data,
                                            author=users.get_current_user(),
                                            updated=datetime.now())
        elif type == FILE:
            file = request.files.get('file')
            data = db.Blob(file.read())
            file = File.add(to=to,type=type, name=form.name.data,
                                            slug=form.slug.data,
                                            breadcrumb=form.breadcrumb.data,
                                            state=form.state.data,
                                            active=form.active.data,
                                            author=users.get_current_user(),
                                            updated=datetime.now(),
                                            content_type=file.content_type,
                                            data=data, size=len(data))

        if form.save.data is True:
            return redirect(url_for('nut:files/list'), 301)
        if form.cont.data is True:
            return redirect(url_for('nut:files/edit', key=file.key()), 301)
    return render_template('app:files/form.html', form=form)
Esempio n. 5
0
def search(request):
    ns = NutSettings()
    template = ns.search_layout
    if template:
        template = 'nut:layout/%s' % template
    else:
        template = 'app:articles/search.html'
    return render_template(template)
Esempio n. 6
0
def edit(request, key):
    file = File.get(key)
    form = FileEditForm(request.form, obj=file)
    if request.method == "POST" and form.validate():
        form.auto_populate(file)
        file.put()
        if form.save.data is True:
            return redirect(url_for('nut:files/list'), 301)
    return render_template('app:files/form.html', form=form, file=file)
Esempio n. 7
0
def add(request, key):
    """ add a new page
        to the set"""
    to = key # lame but it does the trick for now
    blocks = []
    form = PageForm(request.form)
    add  = BlockAddForm(request.form, prefix='_add')

    form.layout.choices =  Layout.get_key_to_path()
    if request.method == 'POST':
        # some logic to find __block elements.
        for key in request.form:
            if key.startswith('__block:'):
                name = key.split('__',2)[1][6:]
                blocks.append((name, BlockForm(request.form, prefix='__block:%s__' % name)))
        if add.validate() and add.add.data is True:
            blocks.append((add.name.data, BlockForm(prefix='__block:%s__' % add.name.data)))
            add = BlockAddForm(prefix='_add')
        elif form.validate() and all([block.validate() for _, block in blocks]):
            name = form.name.data
            slug = form.slug.data
            breadcrumb = form.breadcrumb.data
            state = form.state.data
            active = form.active.data
            if len(slug) < 1:
                slug = slugify(name)
            author = users.get_current_user()
            updated = datetime.now()

            description = form.description.data
            keywords = form.keywords.data
            body = form.body.data
            content_type = form.content_type.data
            if form.layout.data == 'Layout:None':
                layout = None
            else:
                layout = Layout.get(form.layout.data.split(':',1)[1])
            page = Node.add(to=to, name=name, slug=slug, breadcrumb=breadcrumb,
                            updated=updated, author=author, body=body,
                            description=description, keywords=keywords, layout=layout,
                            content_type=content_type,
                            state=state, active=active, type=PAGE)
            done = []
            try:
                for name, block in blocks:
                    b = Block(node=page, name=name, body=block.body.data)
                    b.put()
                    done.append(b)
            except:
                db.delete(done)
                Node.drop(page.get_key())
            if form.save.data is True:
                return redirect(url_for('nut:pages/list_pages'), 301)
            if form.cont.data is True:
                return redirect(url_for('nut:pages/edit', key=page.get_key()), 301)

    return render_template('app:pages/form.html', form=form, add=add, blocks=blocks)
Esempio n. 8
0
def list(request):
    files = dict([(f.get_key(), f) for f in File.all()])
    root = None
    for file in files:
        file = files[file]
        if file.path == '0':
            root = file
            break
    if root is not None:
        root = rec(root, files)
    return render_template('app:files/list.html', root=root)
Esempio n. 9
0
def show(request, key):
    prevent_cache = False
    post = Post.get_by_key_name('Published:%s' % key)
    if not post and users.is_current_user_admin():
        # admin might also try to retrieve the post by it's key!
        post = Post.get_by_key_name(key)
        prevent_cache = True
    if post:
        ns = NutSettings()
        template = ns.articles_layout
        if template:
            template = 'nut:layout/%s' % template
        else:
            template = 'app:articles/show.html'
        resp = render_template(template, post=post)
        resp.prevent_cache = prevent_cache
    else:
        resp = render_template('app/404.html')
        resp.prevent_cache = True
    return resp
Esempio n. 10
0
def list_pages(request):
    nodes = dict([(n.get_key(), n) for n in Node.all()])
    root = None
    for node in nodes:
        node = nodes[node]
        if node.path == '0':
            root = node
            break
    if root is not None:
        root = rec(root, nodes)
    return render_template('app:pages/list.html', root=root)
Esempio n. 11
0
def edit(request, key):
    layout = Layout.get(db.Key(key))
    form = LayoutForm(request.form, obj=layout)
    if request.method == "POST" and form.validate():
        form.auto_populate(layout)
        layout.put()
        # clear depending caches
        for node in layout.get_affected_nodes():
            node.invalidate_cache()
        if form.save.data is True:
            return redirect(url_for('nut:layouts/list'), 301)
    return render_template('app:layouts/form.html', form=form, layout=layout)
Esempio n. 12
0
def add_folder(request):
    form = FolderForm(request.form)
    if request.method == 'POST' and form.validate():
        layout = Layout.add(name=form.name.data,
                            slug=slugify(form.name.data),
                            author=users.get_current_user(),
                            type=FOLDER)
        if form.save.data is True:
            return redirect(url_for('nut:layouts/list'), 301)
        if form.cont.data is True:
            return redirect(url_for('nut:layouts/edit', key=layout.get_key()), 301)
    return render_template('app:layouts/form.html', form=form)
Esempio n. 13
0
def list(request):
    from hazel import NutSettings as AppSettings
    nuts = []
    _nut = {'name': '__name__',
            'title': '__title__',
            'version': '__version__',
            'admin': '__admin__',
            'doc': '__doc__'}
    for nut in AppSettings().nuts:
        module = __import__('hazel.nuts.%s' % nut, fromlist=['hazel.nuts'])
        nuts.append(dict(map(lambda (key, prop): (key, getattr(module, prop, None)),
                             _nut.items())))
    return render_template('app/admin/index.html', nuts=nuts)
Esempio n. 14
0
def delete(request, key):
    form = FileConfirmDeleteForm(request.form)
    if request.method == "POST" and form.validate():
        if form.drop.data is True:
            File.drop(key)
            return redirect(url_for('nut:files/list'), 301)
        if form.cascade.data is True:
            File.drop(key, cascade=True)
            return redirect(url_for('nut:files/list'), 301)
    file = File.get(key)
    files = dict([(n.get_key(), n) for n in File.all().filter("ancestors = ", key)])
    file = rec(file, files)

    return render_template('app:files/confirm_delete.html', file=file, form=form)
Esempio n. 15
0
def add(request, key):
    form = LayoutForm(request.form)
    if request.method == 'POST' and form.validate():
        layout = Layout.add(to=key, type=LAYOUT,
                            name=form.name.data,
                            slug=slugify(form.name.data),
                            body=form.body.data,
                            author=users.get_current_user())
        layout.update_extends()
        layout.put()
        if form.save.data is True:
            return redirect(url_for('nut:layouts/list'), 301)
        if form.cont.data is True:
            return redirect(url_for('nut:layouts/edit', key=layout.get_key()), 301)
    return render_template('app:layouts/form.html', form=form)
Esempio n. 16
0
def edit(request, key):
    blocks = {}
    node = Node.get(key)
    add  = BlockAddForm(request.form, prefix='_add')
    if node.type == FOLDER:
        form = FolderForm(request.form, obj=node)
    else:
        form = PageForm(request.form, obj=node)
        form.layout.choices =  Layout.get_key_to_path()
        for block in node.blocks:
            blocks[block.name] = BlockForm(request.form, obj=block, prefix='__block:%s__' % block.name)

    if request.method == 'POST':
        blocks = dict(blocks)
        for key in request.form:
            if key.startswith('__block:'):
                name = key.split('__',2)[1][6:]
                if name not in blocks:
                    blocks[name] = BlockForm(request.form, prefix='__block:%s__' % name)
        if add.validate() and add.add.data is True:
            blocks[add.name.data] = BlockForm(prefix='__block:%s__' % add.name.data)
            add = BlockAddForm(prefix='_add')
        elif form.validate() and all([blocks[block].validate() for block in blocks]):
            layout_val = None
            if node.type == PAGE:
                layout_val = form.layout.data
                keys = blocks.keys()
                if form.layout.data == 'Layout:None':
                    form.layout.data = None
                else:
                    form.layout.data = Layout.get(form.layout.data.split(':',1)[1])
                for block in node.blocks:
                    blocks[block.name].auto_populate(block)
                    keys.remove(block.name)
                    block.put()
                for block in keys:
                    block = Block(node=node, name=block, body=blocks[block].body.data)
                    block.put()
            form.auto_populate(node)
            node.update()
            # invalidate cache
            node.invalidate_cache()

            if form.save.data is True:
                return redirect(url_for('nut:pages/list_pages'), 301)
            if layout_val is not None:
                form.layout.data = layout_val
    return render_template('app:pages/form.html', form=form, add=add, blocks=blocks.items(), mode='edit', node=node)
Esempio n. 17
0
def topic(request, tag):
    qs = Post.pub()
    rqs = Post.rpub()
    if tag is not None:
        qs = qs.filter('topics = ', tag)
        rqs = rqs.filter('topics = ', tag)
    prev, posts, next = pager(qs,
                              lambda bm: qs.filter('sort_key <', bm),
                              lambda bm: rqs.filter('sort_key >', bm),
                              bookmark=request.args.get('bookmark', None))
    ns = NutSettings()
    template = ns.archive_layout
    if template:
        template = 'nut:layout/%s' % template
    else:
        template = 'app:articles/archive.html'    
    return render_template(template,
                           prev=prev, next=next,
                           posts = posts,
                           tag=tag)
Esempio n. 18
0
def list(request):
    from logging import info
    from datetime import datetime

    if request.method == 'POST':
        handle_backup(request.files.get('backup'))
    
    pub_bm, upc_bm, unp_bm = None, None, None
    if request.args.get('list','') == 'published':
        pub_bm = request.args.get('bookmark', None)
    elif request.args.get('list','') == 'upcoming':
        upc_bm = request.args.get('bookmark', None)
    elif request.args.get('list','') == 'unpublished':
        unp_bm = request.args.get('bookmark', None)

    pub_prev, pub, pub_next = pager(Post.pub(),
                                   lambda bm: Post.pub().filter('sort_key <', bm),
                                   lambda bm: Post.rpub().filter('sort_key >', bm),
                                    bookmark=pub_bm)
    upc_prev, upc, upc_next = pager(Post.upcoming(),
                                   lambda bm: Post.upcoming()\
                                                  .filter('sort_key <', bm),
                                   lambda bm: Post.rupcoming()\
                                                  .filter('sort_key >', bm),
                                   bookmark=upc_bm)
    unp_prev, unp, unp_next = pager(Post.unpub(),
                                   lambda bm: Post.unpub()\
                                                  .filter('sort_key <', bm),
                                   lambda bm: Post.runpub()\
                                                  .filter('sort_key >', bm),
                                   bookmark=unp_bm)
    return render_template('app:articles/list.html',
                           unpublished_prev = unp_prev,
                           unpublished      = unp,
                           unpublished_next = unp_next,
                           upcoming_prev    = upc_prev,
                           upcoming         = upc,
                           upcoming_next    = upc_next,
                           published_prev   = pub_prev,
                           published        = pub,
                           published_next   = pub_next)
Esempio n. 19
0
def add_folder(request):
    form = FolderForm(request.form)
    if request.method == 'POST' and form.validate():
        name = form.name.data
        slug = form.slug.data
        breadcrumb = form.breadcrumb.data
        state = form.state.data
        active = form.active.data
        if len(slug) < 1:
            slug = slugify(name)
        author = users.get_current_user()
        updated = datetime.now()

        page = Node.add(name=name, slug=slug, breadcrumb=breadcrumb,
                         updated=updated, author=author,
                         state=state, active=active, type=FOLDER)
        if form.save.data is True:
            return redirect(url_for('nut:pages/list_pages'), 301)
        if form.cont.data is True:
            return redirect(url_for('nut:pages/edit', key=page.get_key()), 301)
    return render_template('app:pages/form.html', form=form)
Esempio n. 20
0
def edit(request, key):
    post = Post.get_by_key_name(key)
    form = ArticleForm(request.form, obj=PostProxy(post), prefix='edit')
    status = False
    if request.method == 'POST' and form.validate():
        key_name, kwds = prepare(form)
#        kwds['html'] = render_jinja('cache_body.html', body=kwds['body'])
        if post.key().name() == key_name:
            post = update_entity(key_name, **kwds)
            status = 'Updated'
        else:
            kwds['author'] = users.get_current_user()
            created, entity = create_entity(key_name, **kwds)
            if not created:
                return Response('sorry, post with that pub_date and title'\
                                    + 'exists already')
            post.invalidate_cache()
            post.delete()
            post = entity
            status = 'Updated'
        post.invalidate_cache()
        if form.save.data:
            return redirect(url_for('nut:articles/list'), 301)
    return render_template('app:articles/form.html', form=form, post=post, status=status)
Esempio n. 21
0
def delete(request, key):
    form = ConfirmDeleteForm(request.form)
    if request.method == 'POST' and form.validate():
        if form.drop.data is True:
            node = Node.get(key)
            db.delete(node.blocks)
            Node.drop(key)
            return redirect('/admin/pages/', 301)
        if form.cascade.data is True:
            # cascade callback
            # inject a cascade callback that does
            #            # try to delete the key
            #            for i in range(2):
            #                if memcache.delete(node.get_absolute_url()) > 0:
            #                    break
            Node.drop(key, cascade=True)
            # FIXME: remove blocks from dropped Nodes
            return redirect(url_for('nut:pages/list_pages'), 301)

    node = Node.get(key)
    nodes = dict([(n.get_key(), n) for n in Node.all().filter("ancestors = ", key)])
    node = rec(node, nodes)
    return render_template('app:pages/confirm_delete.html', form=form,
                           node=node)
Esempio n. 22
0
def delete(request, key):
    post = Post.get_by_key_name(key)
    if request.method == 'POST':
        post.delete()
        return redirect(url_for('nut:articles/list'), 301)
    return render_template('app:articles/post_confirm_delete.html', post=post)
Esempio n. 23
0
def list(request):
    if request.args.get('flush',False):
        memcache.flush_all()
    return render_template('app:cache/list.html', memcache=memcache.get_stats())
Esempio n. 24
0
def delete(request, key):
    layout = Layout.get(db.Key(key))
    if request.method == 'POST':
        Layout.drop(key)
        return redirect(url_for('nut:layouts/list'), 301)
    return render_template('app:layouts/confirm_delete.html', layout=layout, form=DeleteForm())