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