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 get_absolute_url(self, unquote_url=False): # ugly! key = self.get_key_name() if key.startswith("Published:"): key = key[10:] if not unquote_url: return url_for(self._endpoint, key=key) return unquote(url_for(self._endpoint, key=key))
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(): 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)
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)
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)
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)
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)
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)
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)
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)
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 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)
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)
def get_absolute_url(self, **kwargs): from hazel.util.globals import url_for return url_for(self._endpoint, key=self.abs_path, **kwargs)
def cache_key(self): key = self.get_key_name() if key.startswith("Published:"): key = key[10:] return url_for(self._endpoint, _external=True, key=key)
def move(request, A, mode, B): switch = { 'before': lambda x,y: File.move(x, before=y), 'after': lambda x,y: File.move(x, after=y), 'to': lambda x,y: File.move(x, to=y) } switch[mode](A,B) return redirect(url_for('nut:files/list'), 301)
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())
def cache_key(self): return url_for(self._endpoint, _external=True, key=self.abs_path)
def cache_key(self): return url_for( self._endpoint, _external=True, key=self.abs_path, type=content_type_to_file_ext[self.content_type] )
def get_absolute_url(self): if self.abs_path is not None: return url_for(self._endpoint, key=self.abs_path, type=content_type_to_file_ext[self.content_type]) return ""
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)