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 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)