def handle(self, readfrom, update_lm=True, base="", owner="", **options): contentxml = os.path.join(readfrom, "content.xml") mediadir = os.path.join(readfrom, "media") if not os.access(settings.MEDIA_ROOT, os.W_OK): print "%s may not be writable. Continue (y/n)?" % settings.MEDIA_ROOT if raw_input().lower().strip() != "y": print "Exiting" return data = open(contentxml).read() tree = ElementTree.fromstring(data) basenode = Node.get(base) if basenode is None: basenode = Node.root() for part in base.strip("/").split("/"): sub = basenode.child(part) if sub is None: sub = basenode.add(part) basenode = sub Importer(basenode, update_lm=update_lm).run(tree) ## check writability if os.path.exists(mediadir): copy_tree(mediadir, settings.MEDIA_ROOT)
def test_update_post(self, client): root = Node.root() Type1(node=root, title="Hello").save() request = superuser_request("/edit", method="POST", title="Test", slug="", language="en") handler = MainHandler(request=request, post=True, instance=root) pytest.raises(Redirect, handler.update) root = Node.root() assert root.content().title == "Test"
def test_create_post(self, client): request = superuser_request("/create", method="POST", title="Test", slug="test", language="en") root = Node.root() handler = MainHandler(request=request, post=True, instance=dict(instance=root)) pytest.raises(Redirect, handler.create, type=Type1.get_name()) node = Node.get("/test") assert node.content().title == "Test"
def test_create_attach_post(self, client): """ post the form for attaching content """ request = superuser_request("/create", method="POST", title="Test", language="en") root = Node.root() handler = MainHandler(request=request, post=True, instance=dict(instance=root)) pytest.raises(Redirect, handler.create, type=Type1.get_name(), attach=True) root = Node.root() # pytest.set_trace() assert root.content().title == "Test"
def test_create_post_unicode(self, client): """ issue #693 - unicode enoding issue """ request = superuser_request("/create", method="POST", title=u"Testing «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!", slug="test", language="en") root = Node.root() handler = MainHandler(request=request, post=True, instance=dict(instance=root)) pytest.raises(Redirect, handler.create, type=Type1.get_name()) node = Node.get("/test") assert node.content().title == u"Testing «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!"
def test_update_post_unicode(self, client): """ update content with unicode with new unicode title """ root = Node.root() Type1(node=root, title=u"Testing «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!").save() request = superuser_request("/edit", method="POST", title="TTesting «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!", slug="", language="en") root = Node.root() handler = MainHandler(request=request, post=True, instance=root) pytest.raises(Redirect, handler.update) root = Node.root() assert root.content().title == u"TTesting «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!"
def test_create_image(self, client): request = superuser_request("/create", method="POST", title="Test", slug="test", language="en", storage=filedata) root = Node.root() handler = MainHandler(request=request, post=True, instance=dict(instance=root)) pytest.raises(Redirect, handler.create, type=TestImage.get_name()) node = Node.get("/test") filedata.seek(0) assert node.content().storage.read() == filedata.read()
def test_update_image(self, client): root = Node.root() node = root.add("test") TestImage(node=node, title="image", storage=filedata).save() request = superuser_request("/test/edit", method="POST", title="Test", slug="", language="en", storage=filedata2) handler = MainHandler(request=request, post=True, instance=node) pytest.raises(Redirect, handler.update) node = Node.get("/test") filedata2.seek(0) assert node.content().storage.read() == filedata2.read()
def test_form_choices(self): """ the form should get its choices from the workflow """ form = Type1Type.form(parent=Node.root()) wfclass = core.workflow[Type1Type] assert form.workflow_choices() == wfclass.states assert form.workflow_default() == wfclass.default
def test_handler_create(self, client): """ The handler *can* set the user """ request = create_request("POST", "/create", data=dict(title="Test", slug="test", language="en")) request.user = self.user root = Node.root() handler = MainHandler(request=request, post=True, instance=dict(parent=root)) pytest.raises(Redirect, handler.create, type=Type1.get_name()) node = Node.get("/test") assert node.content().title == "Test" assert node.content().owner == self.user
def test_paste_cut_action(self, client): """ paste after cut moves content, must be in different node to have any effect """ root = Node.root() target = root.add("target") t1 = Type1(node=root.add("t1"), title="t1").save() t2 = Type1(node=root.add("t2"), title="t2").save() request = superuser_request("/contents_actions_cutcopypaste", method="POST", action="paste", selection=[t1.node.tree_path, t2.node.tree_path]) request.session['clipboard_cut'] = [t2.node.tree_path, t1.node.tree_path] handler = MainHandlerTestable(request=request, instance=target, post=True) pytest.raises(Redirect, handler.handle_contents_actions_cutcopypaste) assert len(target.children()) == 2 assert set(x.content().title for x in target.children()) == \ set(('t1', 't2')) assert len(root.children()) == 1 assert root.child('target')
def handle(self, writeto=None, path="", *args, **options): if writeto is None: raise CommandError("You must specificy a writable directory where the export can be written to") verbose = options.get('verbose', True) mediadir = os.path.join(writeto, "media") if not os.path.exists(mediadir): os.makedirs(mediadir) base = path # "" # /sub5_2/sub4_2/sub3_2" root = Node.get(base) xml, files = Exporter().run(root, base) open(os.path.join(writeto, "content.xml"), "w" ).write(prettify(xml).encode("utf8")) for file in set(files): dirname = os.path.dirname(file) if dirname: dir = os.path.join(mediadir, dirname) if not os.path.exists(dir): os.makedirs(dir) dest = os.path.join(mediadir, file) source = os.path.join(settings.MEDIA_ROOT, file) if verbose: print "Copy %s to %s" % (source, dest) shutil.copy(source, dest) if verbose: print files
def test_coerce_parent(self, client): """ coerce a dict holding an parent path """ root = Node.root() a = root.add("a") res = MainHandler.coerce(dict(parent="a")) assert 'parent' in res assert res['parent'] == a assert 'instance' not in res
def test_attached_root(self, client): """ A root node with content attached. Its name should not be its title but 'Home' """ root = Node.root() Type1(node=root, title="The rootnode of this site").save() request = create_request("GET", "/") handler = MainHandlerTestable(request=request, instance=root) assert handler.breadcrumb() == [("Home", '')]
def test_create_attach_get(self, client): """ get the form for attaching content """ root = Node.root() Type1(node=root).save() request = superuser_request("/", type=Type1.get_name()) handler = MainHandlerTestable(request=request, instance=root) create = handler.create(type=Type1.get_name(), attach=True) assert create['path'] == "wheelcms_axle/create.html" assert 'form' in create['context']
def test_create_get_root(self, client): """ test create on root - get """ root = Node.root() Type1(node=root).save() request = superuser_request("/", type=Type1.get_name()) handler = MainHandlerTestable(request=request, instance=root) create = handler.create() assert create['path'] == "wheelcms_axle/create.html" assert 'form' in create['context']
def test_create_translation_content_post(self, client): """ test case where root has content but current language is not translated """ root = Node.root() node = root.add("content") Type1(node=node, title="Hello", language="en").save() request = superuser_request("/edit", method="POST", title="hello", language="nl") request.session['admin_language'] = 'nl' root = Node.root() handler = MainHandler(request=request, post=True, instance=node) pytest.raises(Redirect, handler.update) assert node.content(language='nl') assert node.content(language='en') assert node.content(language='nl') != node.content(language='en')
def test_parent_instance(self, client): """ handler initialized with a parent but no instance. Should mean edit mode, but for now assume custom breadcrumb context """ root = Node.root() Type1(node=root, title="Root").save() request = create_request("GET", "/") handler = MainHandlerTestable(request=request, kw=dict(parent=root)) assert handler.breadcrumb() == []
def test_not_visible(self, client): """ content that's published but not in navigation """ root = Node.root() testable = root.add("sub") child = testable.add("child") content = Type1(node=child, state="published") content.save() assert get_visible_children(testable).count() == 0
def test_update_root(self, client): """ test /edit """ root = Node.root() Type1(node=root).save() request = superuser_request("/edit", method="POST", type=Type1.get_name()) instance = MainHandlerTestable.coerce(dict(instance="")) handler = MainHandlerTestable(request=request, instance=instance) update = handler.update() assert update['path'] == "wheelcms_axle/update.html" assert 'form' in update['context']
def test_published(self, client): """ content that's in navigation but not published """ root = Node.root() testable = root.add("sub") child = testable.add("child") content = Type1(node=child, navigation=True, state="published") content.save() assert get_visible_children(testable).count() == 1 assert get_visible_children(testable).get() == content.node
def test_coerce_instance_parent(self, client): """ coerce a dict holding both instance and parent """ root = Node.root() a = root.add("a") b = a.add("b") res = MainHandler.coerce(dict(instance="b", parent="a")) assert 'instance' in res assert 'parent' in res assert res['instance'] == b assert res['parent'] == a
def test_create_translation_root_get(self, client): """ test case where root has content but current language is not translated """ root = Node.root() Type1(node=root, title="Hello", language="en").save() request = superuser_request("/edit", method="GET", language="nl") handler = MainHandler(request=request, post=False, instance=root) handler.update() assert 'slug' not in handler.context['form'].fields
def test_attached_form(self, client): """ The form when attaching should not contain a slug field since it will be attached to an existing node """ root = Node.root() Type1(node=root).save() request = superuser_request("/") handler = MainHandlerTestable(request=request, instance=root) create = handler.create(type=Type1.get_name(), attach=True) form = create['context']['form'] assert 'slug' not in form.fields
def setup_handler(self, user=None, method="GET", with_instance=False, state="private"): """ setup the mainhandler """ ins = None if with_instance: cont = Type1(node=Node.root(), state=state).save() ins = cont.node request = create_request(method, "/") if self.provide_user(): request.user = self.provide_user() handler = MainHandler(request=request, instance=ins) return handler
def test_change_slug_available(self, client): root = Node.root() Type1(node=root.add("inuse"), title="InUse").save() other = Type1(node=root.add("other"), title="Other").save() request = superuser_request("/other/update", method="POST", title="Other", slug="inuse2", language="en") handler = MainHandlerTestable(request=request, post=True, instance=other.node) pytest.raises(Redirect, handler.update) form = handler.context['form'] assert form.is_valid()
def test_subsub_unattached(self, client): """ a child with content under the root, lowest child unattached """ root = Node.root() Type1(node=root, title="Root").save() child = root.add("child") Type1(node=child, title="Child").save() child2 = child.add("child2") request = create_request("GET", "/") handler = MainHandlerTestable(request=request, instance=child2) assert handler.breadcrumb() == [("Home", root.get_absolute_url()), ("Child", child.get_absolute_url()), ("Unattached node /child/child2", "")]
def test_create_post_unicode(self, client, root): """ issue #693 - unicode enoding issue """ request = superuser_request("/create", method="POST", title=u"Testing «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!", slug="test", language="en", type=Type1.get_name()) handler = MainHandlerTestable() res = handler.dispatch(request, nodepath="", handlerpath="create") assert res.status_code == 302 node = Node.get("/test") assert node.content().title == u"Testing «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!"
def test_create_translation(self, client): root = Node.root() Type1(node=root, language="en").save() request = superuser_request("/edit", method="POST", type=Type1.get_name()) request.session = {'admin_language':'nl'} instance = MainHandlerTestable.coerce(dict(instance="")) handler = MainHandlerTestable(request=request, instance=instance) update = handler.update() assert update['path'] == "wheelcms_axle/update.html" assert 'form' in update['context'] f = update['context']['form'] assert f.initial['language'] == 'nl'
def test_create_post(self, client, root): request = superuser_request("/create", method="POST", title="Test", slug="test", language="en", type=Type1.get_name()) handler = MainHandlerTestable() res = handler.dispatch(request, nodepath="", handlerpath="create") assert res.status_code == 302 node = Node.get("/test") assert node.content().title == "Test"