diff --git a/pillar/api/utils/node_type_utils.py b/pillar/api/utils/node_type_utils.py index beef3f13..4e03285f 100644 --- a/pillar/api/utils/node_type_utils.py +++ b/pillar/api/utils/node_type_utils.py @@ -60,6 +60,9 @@ def add_to_project(project, node_types, replace_existing): Overwrites any existing by the same name when replace_existing=True. """ + assert isinstance(project, dict) + assert isinstance(node_types, (list, set, frozenset, tuple)) + project_id = project['_id'] for node_type in node_types: diff --git a/pillar/cli.py b/pillar/cli.py index cdf1f673..28dd7fb0 100644 --- a/pillar/cli.py +++ b/pillar/cli.py @@ -10,6 +10,7 @@ import logging from bson.objectid import ObjectId, InvalidId from eve.methods.put import put_internal +from eve.methods.post import post_internal from flask import current_app from flask_script import Manager @@ -720,3 +721,57 @@ def upgrade_attachment_schema(proj_url=None, all_projects=False): return 3 handle_project(proj) + + +@manager.command +def create_blog(proj_url): + """Adds a blog to the project.""" + + from pillar.api.utils.authentication import force_cli_user + from pillar.api.utils import node_type_utils + from pillar.api.node_types.blog import node_type_blog + from pillar.api.node_types.post import node_type_post + from pillar.api.utils import remove_private_keys + + force_cli_user() + + db = current_app.db() + + # Add the blog & post node types to the project. + projects_coll = db['projects'] + proj = projects_coll.find_one({'url': proj_url}) + if not proj: + log.error('Project url=%s not found', proj_url) + return 3 + + node_type_utils.add_to_project(proj, + (node_type_blog, node_type_post), + replace_existing=False) + + proj_id = proj['_id'] + r, _, _, status = put_internal('projects', remove_private_keys(proj), _id=proj_id) + if status != 200: + log.error('Error %i storing altered project %s: %s', status, proj_id, r) + return 4 + log.info('Project saved succesfully.') + + # Create a blog node. + nodes_coll = db['nodes'] + blog = nodes_coll.find_one({'node_type': 'blog', 'project': proj_id}) + if not blog: + blog = { + u'node_type': node_type_blog['name'], + u'name': u'Blog', + u'description': u'', + u'properties': {}, + u'project': proj_id, + } + r, _, _, status = post_internal('nodes', blog) + if status != 201: + log.error('Error %i storing blog node: %s', status, r) + return 4 + log.info('Blog node saved succesfully: %s', r) + else: + log.info('Blog node already exists: %s', blog) + + return 0 diff --git a/pillar/tests/__init__.py b/pillar/tests/__init__.py index 0d5081ea..d84ce060 100644 --- a/pillar/tests/__init__.py +++ b/pillar/tests/__init__.py @@ -329,6 +329,11 @@ class AbstractPillarTest(TestMinimal): return group_ids + def fetch_project_from_db(self, project_id=ctd.EXAMPLE_PROJECT_ID): + with self.app.app_context(): + proj_coll = self.app.db()['projects'] + return proj_coll.find_one(project_id) + @staticmethod def join_url_params(params): """Constructs a query string from a dictionary and appends it to a url. diff --git a/tests/test_api/test_cli.py b/tests/test_api/test_cli.py index c2adc2fd..7974bff5 100644 --- a/tests/test_api/test_cli.py +++ b/tests/test_api/test_cli.py @@ -267,9 +267,7 @@ class AbstractNodeReplacementTest(AbstractPillarTest): }) def fetch_project_from_db(self): - with self.app.app_context(): - proj_coll = self.app.db()['projects'] - return proj_coll.find_one(self.project_id) + return super(AbstractNodeReplacementTest, self).fetch_project_from_db(self.project_id) def add_group_permission_to_asset_node_type(self): group_perms = {u'group': ctd.EXAMPLE_PROJECT_READONLY_GROUP_ID, @@ -342,3 +340,26 @@ class UpgradeAttachmentSchemaTest(AbstractNodeReplacementTest): # Test that the permissions set previously are still there. self.assertEqual([group_perms], nt_asset['permissions']['groups']) + + +class CreateBlogTest(AbstractPillarTest): + def setUp(self, **kwargs): + AbstractPillarTest.setUp(self, **kwargs) + + self.project_id, self.proj = self.ensure_project_exists() + self.ensure_file_exists({'_id': self.proj[u'picture_header']}) + self.ensure_file_exists({'_id': self.proj[u'picture_square']}) + + def test_create_blog(self): + """Very simple test to check the create_blog CLI command.""" + + from pillar.cli import create_blog + + with self.app.test_request_context(): + create_blog(self.proj['url']) + + dbproj = self.fetch_project_from_db() + nt_blog = get_node_type(dbproj, 'blog') + self.assertIsNotNone(nt_blog) + + # I trust that the blog node has been created too.