def posts_view(project_id, url=None): """View individual blogpost""" api = SystemUtility.attract_api() node_type = NodeType.find_first({ 'where': '{"name" : "blog"}', 'projection': '{"name": 1}' }, api=api) blog = Node.find_one({ 'where': '{"node_type" : "%s", \ "parent": "%s"}' % (node_type._id, project_id), }, api=api) if url: try: post = Node.find_one({ 'where': '{"parent": "%s", "properties.url": "%s"}' % (blog._id, url), 'embedded': '{"picture":1, "node_type": 1}', }, api=api) except ResourceNotFound: return abort(404) return render_template( 'nodes/custom/post/view.html', blog=blog, node=post) else: # Render all posts posts = Node.all({ 'where': '{"parent": "%s"}' % (blog._id), 'embedded': '{"picture":1}', 'sort': '-_created' }, api=api) return render_template( 'nodes/custom/blog/index.html', blog=blog, posts=posts._items)
def find_for_comment(project, node): """Returns the URL for a comment.""" api = system_util.pillar_api() parent = node while parent.node_type == 'comment': if isinstance(parent.parent, pillarsdk.Resource): parent = parent.parent continue try: parent = Node.find_one( {'where': { '_id': parent.parent, '_deleted': { '$ne': True } }}, api=api) except ResourceNotFound: log.warning( 'url_for_node(node_id=%r): Unable to find parent node %r', node['_id'], parent.parent) raise ValueError('Unable to find parent node %r' % parent.parent) # Find the redirection URL for the parent node. parent_url = find_url_for_node(parent) if '#' in parent_url: # We can't attach yet another fragment, so just don't link to # the comment for now. return parent_url return parent_url + '#{}'.format(node['_id'])
def posts_view(project_id, url=None): """View individual blogpost""" api = SystemUtility.attract_api() node_type = NodeType.find_first( { 'where': '{"name" : "blog"}', 'projection': '{"name": 1}' }, api=api) blog = Node.find_one( { 'where': '{"node_type" : "%s", \ "parent": "%s"}' % (node_type._id, project_id), }, api=api) if url: try: post = Node.find_one( { 'where': '{"parent": "%s", "properties.url": "%s"}' % (blog._id, url), 'embedded': '{"picture":1, "node_type": 1}', }, api=api) except ResourceNotFound: return abort(404) return render_template('nodes/custom/post/view.html', blog=blog, node=post) else: # Render all posts posts = Node.all( { 'where': '{"parent": "%s"}' % (blog._id), 'embedded': '{"picture":1}', 'sort': '-_created' }, api=api) return render_template('nodes/custom/blog/index.html', blog=blog, posts=posts._items)
def posts_create(project_id): api = system_util.pillar_api() try: project = Project.find(project_id, api=api) except ResourceNotFound: return abort(404) attach_project_pictures(project, api) blog = Node.find_one( {'where': { 'node_type': 'blog', 'project': project_id }}, api=api) node_type = project.get_node_type('post') # Check if user is allowed to create a post in the blog if not project.node_type_has_method('post', 'POST', api=api): return abort(403) form = get_node_form(node_type) if form.validate_on_submit(): # Create new post object from scratch post_props = dict(node_type='post', name=form.name.data, picture=form.picture.data, user=current_user.objectid, parent=blog._id, project=project._id, properties=dict(content=form.content.data, status=form.status.data, url=form.url.data)) if form.picture.data == '': post_props['picture'] = None post = Node(post_props) post.create(api=api) # Only if the node is set as published, push it to the list if post.properties.status == 'published': project_update_nodes_list(post, project_id=project._id, list_name='blog') return redirect(url_for_node(node=post)) form.parent.data = blog._id return render_template('nodes/custom/post/create.html', node_type=node_type, form=form, project=project, api=api)
def render_node_page(project_url, page_url, api): """Custom behaviour for pages, which are nodes, but accessible on a custom route base. """ # TODO: ensure this is not called for the home project, as it would # generate conflicting websites project = find_project_or_404(project_url, api=api) try: page = Node.find_one({ 'where': { 'project': project['_id'], 'node_type': 'page', 'properties.url': page_url}}, api=api) except ResourceNotFound: raise wz_exceptions.NotFound('No such node') return project, page
def view(community_url, post_shortcode, slug=None): api = system_util.pillar_api() project = Project.find_by_url(community_url, api=api) attach_project_pictures(project, api) post = Node.find_one({ 'where': { 'project': project['_id'], 'properties.shortcode': post_shortcode}, 'embedded': {'user': 1}}, api=api) if post.picture: post.picture = get_file(post.picture, api=api) return render_template( 'dillo/index.html', project=project, submit_menu=project_submit_menu(project), col_right={'post': post})
def posts_view(project_id, url=None): """View individual blogpost""" api = system_util.pillar_api() # Fetch project (for backgroud images and links generation) project = Project.find(project_id, api=api) attach_project_pictures(project, api) try: blog = Node.find_one( { 'where': { 'node_type': 'blog', 'project': project_id }, }, api=api) except ResourceNotFound: abort(404) if url: try: post = Node.find_one( { 'where': '{"parent": "%s", "properties.url": "%s"}' % (blog._id, url), 'embedded': '{"node_type": 1, "user": 1}', }, api=api) if post.picture: post.picture = get_file(post.picture, api=api) except ResourceNotFound: return abort(404) # If post is not published, check that the user is also the author of # the post. If not, return 404. if post.properties.status != "published": if current_user.is_authenticated(): if not post.has_method('PUT'): abort(403) else: abort(403) return render_template('nodes/custom/post/view.html', blog=blog, node=post, project=project, title='blog', api=api) else: node_type_post = project.get_node_type('post') status_query = "" if blog.has_method( 'PUT') else ', "properties.status": "published"' posts = Node.all( { 'where': '{"parent": "%s" %s}' % (blog._id, status_query), 'embedded': '{"user": 1}', 'sort': '-_created' }, api=api) for post in posts._items: post.picture = get_file(post.picture, api=api) return render_template('nodes/custom/blog/index.html', node_type_post=node_type_post, posts=posts._items, project=project, title='blog', api=api)
def posts_view(project_id=None, project_url=None, url=None): """View individual blogpost""" if bool(project_id) == bool(project_url): raise ValueError('posts_view(): pass either project_id or project_url') api = system_util.pillar_api() # Fetch project (for backgroud images and links generation) if project_id: project = Project.find(project_id, api=api) else: project = Project.find_one({'where': {'url': project_url}}, api=api) project_id = project['_id'] attach_project_pictures(project, api) blog = Node.find_one( { 'where': { 'node_type': 'blog', 'project': project_id }, }, api=api) status_query = "" if blog.has_method( 'PUT') else ', "properties.status": "published"' posts = Node.all( { 'where': '{"parent": "%s" %s}' % (blog._id, status_query), 'embedded': '{"user": 1}', 'sort': '-_created' }, api=api) for post in posts._items: post.picture = get_file(post.picture, api=api) post['properties'][ 'content'] = pillar.web.nodes.attachments.render_attachments( post, post['properties']['content']) # Use the *_main_project.html template for the main blog main_project_template = '_main_project' if project_id == current_app.config[ 'MAIN_PROJECT_ID'] else '' if url: post = Node.find_one( { 'where': { 'parent': blog._id, 'properties.url': url }, 'embedded': { 'node_type': 1, 'user': 1 }, }, api=api) if post.picture: post.picture = get_file(post.picture, api=api) # If post is not published, check that the user is also the author of # the post. If not, return 404. if post.properties.status != "published": if not (current_user.is_authenticated and post.has_method('PUT')): abort(403) post['properties'][ 'content'] = pillar.web.nodes.attachments.render_attachments( post, post['properties']['content']) return render_template( 'nodes/custom/post/view{0}.html'.format(main_project_template), blog=blog, node=post, posts=posts._items, project=project, title='blog', api=api) else: node_type_post = project.get_node_type('post') template_path = 'nodes/custom/blog/index.html' return render_template( 'nodes/custom/blog/index{0}.html'.format(main_project_template), node_type_post=node_type_post, posts=posts._items, project=project, title='blog', api=api)
def posts_view(project_id=None, project_url=None, url=None, *, archive=False, page=1): """View individual blogpost""" if bool(project_id) == bool(project_url): raise ValueError('posts_view(): pass either project_id or project_url') if url and archive: raise ValueError('posts_view(): cannot pass both url and archive') api = system_util.pillar_api() # Fetch project (for background images and links generation) if project_id: project = Project.find(project_id, api=api) else: project = Project.find_one({'where': {'url': project_url}}, api=api) project_id = project['_id'] attach_project_pictures(project, api) blog = Node.find_one( { 'where': { 'node_type': 'blog', 'project': project_id }, }, api=api) status_query = {} if blog.has_method('PUT') else { 'properties.status': 'published' } posts = Node.all( { 'where': { 'parent': blog._id, **status_query }, 'embedded': { 'user': 1 }, 'sort': '-_created', 'max_results': 20 if archive else 5, 'page': page, }, api=api) for post in posts._items: post.picture = get_file(post.picture, api=api) post.url = url_for_node(node=post) # Use the *_main_project.html template for the main blog is_main_project = project_id == current_app.config['MAIN_PROJECT_ID'] main_project_template = '_main_project' if is_main_project else '' main_project_template = '_main_project' index_arch = 'archive' if archive else 'index' template_path = f'nodes/custom/blog/{index_arch}{main_project_template}.html', if url: template_path = f'nodes/custom/post/view{main_project_template}.html', post = Node.find_one( { 'where': { 'parent': blog._id, 'properties.url': url }, 'embedded': { 'node_type': 1, 'user': 1 }, }, api=api) if post.picture: post.picture = get_file(post.picture, api=api) post.url = url_for_node(node=post) elif posts._items: post = posts._items[0] else: post = None if post is not None: # If post is not published, check that the user is also the author of # the post. If not, return an error. if post.properties.status != "published": if not (current_user.is_authenticated and post.has_method('PUT')): abort(403) can_create_blog_posts = project.node_type_has_method('post', 'POST', api=api) # Use functools.partial so we can later pass page=X. if is_main_project: url_func = functools.partial(url_for, 'main.main_blog_archive') else: url_func = functools.partial(url_for, 'main.project_blog_archive', project_url=project.url) project.blog_archive_url = url_func() pmeta = posts._meta seen_now = pmeta['max_results'] * pmeta['page'] if pmeta['total'] > seen_now: project.blog_archive_next = url_func(page=pmeta['page'] + 1) else: project.blog_archive_next = None if pmeta['page'] > 1: project.blog_archive_prev = url_func(page=pmeta['page'] - 1) else: project.blog_archive_prev = None title = 'blog_main' if is_main_project else 'blog' pages = Node.all( { 'where': { 'project': project._id, 'node_type': 'page' }, 'projection': { 'name': 1 } }, api=api) return render_template( template_path, blog=blog, node=post, posts=posts._items, posts_meta=pmeta, more_posts_available=pmeta['total'] > pmeta['max_results'], project=project, title=title, node_type_post=project.get_node_type('post'), can_create_blog_posts=can_create_blog_posts, pages=pages._items, api=api)