def _handle_form(self, context, request): if self.post_key in request.POST: controls = request.POST.items() try: appstruct = self.form.validate(controls) except ValidationFailure, e: request.session.flash(self.error_msg, 'error') return e.render() else: if self.add is None: # edit for key, value in appstruct.items(): setattr(context, key, value) request.session.flash(self.edit_success_msg, 'success') location = resource_url(context, request, self.success_path) return HTTPFound(location=location) else: # add name = title_to_name(appstruct['title']) while name in context.keys(): name = disambiguate_name(name) item = context[name] = self.add(**appstruct) request.session.flash(self.add_success_msg, 'success') location = resource_url(item, request, self.success_path) return HTTPFound(location=location)
def paste_node(context, request): session = DBSession() id, action = request.session['kotti.paste'] item = session.query(Node).get(id) if action == 'cut': if not has_permission('edit', item, request): raise Forbidden() item.__parent__.children.remove(item) context.children.append(item) del request.session['kotti.paste'] elif action == 'copy': copy = item.copy() name = copy.name if not name: # for root name = title_to_name(copy.title) while name in context.keys(): name = disambiguate_name(name) copy.name = name context.children.append(copy) request.session.flash( _(u'${title} pasted.', mapping=dict(title=item.title)), 'success') if not request.is_xhr: location = resource_url(context, request) return HTTPFound(location=location)
def paste_node(context, request): session = DBSession() id, action = request.session['kotti.paste'] item = session.query(Node).get(id) if action == 'cut': if not has_permission('edit', item, request): raise Forbidden() item.__parent__.children.remove(item) context.children.append(item) del request.session['kotti.paste'] elif action == 'copy': copy = item.copy() name = copy.name if not name: # for root name = title_to_name(copy.title) while name in context.keys(): name = disambiguate_name(name) copy.name = name context.children.append(copy) request.session.flash(_(u'${title} pasted.', mapping=dict(title=item.title)), 'success') if not request.is_xhr: location = resource_url(context, request) return HTTPFound(location=location)
def move_node(context, request): """This view allows copying, cutting, pasting, deleting of 'context' and reordering of children of 'context'. """ P = request.POST session = DBSession() if 'copy' in P: request.session['kotti.paste'] = (context.id, 'copy') request.session.flash(u'%s copied.' % context.title, 'success') if not request.is_xhr: return HTTPFound(location=request.url) if 'cut' in P: request.session['kotti.paste'] = (context.id, 'cut') request.session.flash(u'%s cut.' % context.title, 'success') if not request.is_xhr: return HTTPFound(location=request.url) if 'paste' in P: id, action = request.session['kotti.paste'] item = session.query(Node).get(id) if action == 'cut': if not has_permission('edit', item, request): raise Forbidden() item.__parent__.children.remove(item) context.children.append(item) del request.session['kotti.paste'] elif action == 'copy': copy = item.copy() name = copy.name if not name: # for root name = title_to_name(copy.title) while name in context.keys(): name = disambiguate_name(name) copy.name = name context.children.append(copy) request.session.flash(u'%s pasted.' % item.title, 'success') if not request.is_xhr: return HTTPFound(location=request.url) if 'order-up' in P or 'order-down' in P: up, down = P.get('order-up'), P.get('order-down') id = int(down or up) if up is not None: mod = -1 else: # pragma: no cover mod = +1 child = session.query(Node).get(id) index = context.children.index(child) context.children.pop(index) context.children.insert(index+mod, child) request.session.flash(u'%s moved.' % child.title, 'success') if not request.is_xhr: return HTTPFound(location=request.url) if 'delete' in P and 'delete-confirm' in P: parent = context.__parent__ request.session.flash(u'%s deleted.' % context.title, 'success') parent.children.remove(context) location = resource_url(parent, request) if view_permitted(parent, request, 'edit'): location += '@@edit' return HTTPFound(location=location) if 'rename' in P: name = P['name'] title = P['title'] if not name or not title: request.session.flash(u'Name and title are required.', 'error') else: context.name = name context.title = title request.session.flash(u'Item renamed', 'success') location = resource_url(context, request) + '@@move' return HTTPFound(location=location) return {}
def test_disambiguate_name(self): from kotti.views.util import disambiguate_name self.assertEqual(disambiguate_name(u"foo"), u"foo-1") self.assertEqual(disambiguate_name(u"foo-3"), u"foo-4")
def test_disambiguate_name(self): from kotti.views.util import disambiguate_name self.assertEqual(disambiguate_name(u'foo'), u'foo-1') self.assertEqual(disambiguate_name(u'foo-3'), u'foo-4')
def move_node(context, request): P = request.POST session = DBSession() if 'copy' in P: request.session['kotti.paste'] = (context.id, 'copy') request.session.flash(u'%s copied.' % context.title, 'success') return HTTPFound(location=request.url) if 'cut' in P: request.session['kotti.paste'] = (context.id, 'cut') request.session.flash(u'%s cut.' % context.title, 'success') return HTTPFound(location=request.url) if 'paste' in P: id, action = request.session['kotti.paste'] item = session.query(Node).get(id) if action == 'cut': if not has_permission('edit', item, request): raise Forbidden() # XXX testme item.__parent__.children.remove(item) context.children.append(item) del request.session['kotti.paste'] elif action == 'copy': copy = item.copy() name = copy.name if not name: # for root name = title_to_name(copy.title) while name in context.keys(): name = disambiguate_name(name) copy.name = name context.children.append(copy) request.session.flash(u'%s pasted.' % item.title, 'success') return HTTPFound(location=request.url) if 'order-up' in P or 'order-down' in P: up, down = P.get('order-up'), P.get('order-down') id = int(down or up) if up is not None: # pragma: no cover mod = -1 else: mod = +1 child = session.query(Node).get(id) index = context.children.index(child) context.children.pop(index) context.children.insert(index+mod, child) request.session.flash(u'%s reordered.' % child.title, 'success') if 'delete' in P and 'delete-confirm' in P: parent = context.__parent__ parent.children.remove(context) elements = [] if view_execution_permitted(parent, request, 'edit'): elements.append('edit') location = resource_url(parent, request, *elements) request.session.flash(u'%s deleted.' % context.title, 'success') return HTTPFound(location=location) return { 'api': TemplateAPIEdit(context, request), }