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)
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)
def prepopulate(self): layouts = [(v,v) for k,v in Layout.get_key_to_path()] layouts[0] = ('','---') self.index_layout.choices = layouts self.articles_layout.choices = layouts self.search_layout.choices = layouts self.archive_layout.choices = layouts