def posts_create(project_id): api = SystemUtility.attract_api() node_type = NodeType.find_first( { 'where': '{"name" : "blog"}', 'projection': '{"name": 1}' }, api=api) blog = Node.find_first( { 'where': '{"node_type" : "%s", \ "parent": "%s"}' % (node_type._id, project_id), }, api=api) node_type = NodeType.find_first({ 'where': '{"name" : "post"}', }, api=api) form = get_node_form(node_type) if form.validate_on_submit(): user_id = current_user.objectid if process_node_form(form, node_type=node_type, user=user_id): return redirect(url_for('main_blog')) form.parent.data = blog._id return render_template('nodes/custom/post/create.html', node_type=node_type, form=form, project_id=project_id)
def comments_create(): content = request.form['content'] parent_id = request.form.get('parent_id') api = SystemUtility.attract_api() node_type = NodeType.find_first({ 'where': '{"name" : "comment"}', }, api=api) node_asset_props = dict( name='Comment', #description=a.description, #picture=picture, user=current_user.objectid, node_type=node_type._id, #parent=node_parent, properties=dict( content=content, status='published', confidence=0, rating_positive=0, rating_negative=0)) if parent_id: node_asset_props['parent'] = parent_id node_asset = Node(node_asset_props) node_asset.create(api=api) return jsonify( asset_id=node_asset._id, content=node_asset.properties.content)
def projects(self): """Get list of project for the user""" # Find node_type project id (this could become static) node_type = NodeType.find_first({ 'where': '{"name" : "project"}', }, api=self.api) if not node_type: return abort(404) # Define key for querying for the project if self.is_organization: user_path = 'properties.organization' else: user_path = 'user' # Query for the project # TODO currently, this system is weak since we rely on the user property # of a node when searching for a project using the user it. This allows # us to find a project that belongs to an organization also by requesting # the user that originally created the node. This can be fixed by # introducing a 'user' property in the project node type. projects = Node.all({ 'where': '{"node_type" : "%s", "%s": "%s"}'\ % (node_type._id, user_path, self._id), }, api=self.api) for project in projects._items: attach_project_pictures(project, self.api) return projects
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 = SystemUtility.attract_api() node_type = NodeType.find_first({ 'where': '{"name" : "blog"}', 'projection': '{"name": 1}' }, api=api) blog = Node.find_first({ 'where': '{"node_type" : "%s", \ "parent": "%s"}' % (node_type._id, project_id), }, api=api) node_type = NodeType.find_first({'where': '{"name" : "post"}',}, api=api) form = get_node_form(node_type) if form.validate_on_submit(): user_id = current_user.objectid if process_node_form( form, node_type=node_type, user=user_id): return redirect(url_for('main_blog')) form.parent.data = blog._id return render_template('nodes/custom/post/create.html', node_type=node_type, form=form, project_id=project_id)
def index(node_type_name=""): """Generic function to list all nodes """ # Pagination index page = request.args.get('page', 1) max_results = 50 api = SystemUtility.attract_api() if node_type_name == "": node_type_name = "shot" node_type = NodeType.find_first( { 'where': '{"name" : "%s"}' % node_type_name, }, api=api) if not node_type: return "Empty NodeType list", 200 nodes = Node.all( { 'where': '{"node_type" : "%s"}' % (node_type._id), 'max_results': max_results, 'page': page, 'embedded': '{"picture":1}', 'sort': "order" }, api=api) # Build the pagination object pagination = Pagination(int(page), max_results, nodes._meta.total) template = '{0}/index.html'.format(node_type_name) try: return render_template(template, title=node_type_name, nodes=nodes, node_type=node_type, type_names=type_names(), pagination=pagination) except TemplateNotFound: return render_template('nodes/index.html', title=node_type_name, nodes=nodes, node_type=node_type, type_names=type_names(), pagination=pagination)
def get_projects(category): """Utility to get projects based on category. Should be moved on the API and improved with more extensive filtering capabilities. """ api = SystemUtility.attract_api() node_type = NodeType.find_first({ 'where': '{"name" : "project"}', 'projection': '{"name": 1}' }, api=api) projects = Node.all({ 'where': '{"node_type" : "%s", \ "properties.category": "%s"}' % (node_type._id, category), 'embedded': '{"picture":1}', }, api=api) for project in projects._items: attach_project_pictures(project, api) return projects
def index(node_type_name=""): """Generic function to list all nodes """ # Pagination index page = request.args.get('page', 1) max_results = 50 api = SystemUtility.attract_api() if node_type_name == "": node_type_name = "shot" node_type = NodeType.find_first({ 'where': '{"name" : "%s"}' % node_type_name, }, api=api) if not node_type: return "Empty NodeType list", 200 nodes = Node.all({ 'where': '{"node_type" : "%s"}' % (node_type._id), 'max_results': max_results, 'page': page, 'embedded': '{"picture":1}', 'sort' : "order"}, api=api) # Build the pagination object pagination = Pagination(int(page), max_results, nodes._meta.total) template = '{0}/index.html'.format(node_type_name) try: return render_template( template, title=node_type_name, nodes=nodes, node_type=node_type, type_names=type_names(), pagination=pagination) except TemplateNotFound: return render_template( 'nodes/index.html', title=node_type_name, nodes=nodes, node_type=node_type, type_names=type_names(), pagination=pagination)
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 project(self, project_name): """Get single project for one user. The project is searched by looking up project directly associated with that user or organization.""" # Find node_type project id (this could become static) node_type = NodeType.find_first({ 'where': '{"name" : "project"}', }, api=self.api) if not node_type: return abort(404) # Define key for querying for the project if self.is_organization: user_path = 'properties.organization' else: user_path = 'user' project_node = Node.find_first({ 'where': '{"node_type" : "%s", "properties.url" : "%s", "%s": "%s"}'\ % (node_type._id, project_name, user_path, self._id), }, api=self.api) if not project_node: return abort(404) return project_node
def get_projects(category): """Utility to get projects based on category. Should be moved on the API and improved with more extensive filtering capabilities. """ api = SystemUtility.attract_api() node_type = NodeType.find_first( { 'where': '{"name" : "project"}', 'projection': '{"name": 1}' }, api=api) projects = Node.all( { 'where': '{"node_type" : "%s", \ "properties.category": "%s"}' % (node_type._id, category), 'embedded': '{"picture":1}', }, api=api) for project in projects._items: attach_project_pictures(project, api) return projects
def comments_index(): parent_id = request.args.get('parent_id') if request.args.get('format'): # Get data only if we format it api = SystemUtility.attract_api() node_type = NodeType.find_first({ 'where': '{"name" : "comment"}', }, api=api) nodes = Node.all({ 'where': '{"node_type" : "%s", "parent": "%s"}' % (node_type._id, parent_id), 'embedded': '{"user":1}'}, api=api) comments = [] for comment in nodes._items: # Query for first level children (comment replies) replies = Node.all({ 'where': '{"node_type" : "%s", "parent": "%s"}' % (node_type._id, comment._id), 'embedded': '{"user":1}'}, api=api) replies = replies._items if replies._items else None if replies: replies = [format_comment(reply, is_reply=True) for reply in replies] comments.append( format_comment(comment, is_reply=False, replies=replies)) if request.args.get('format') == 'json': return_content = jsonify(items=comments) else: # Data will be requested via javascript return_content = render_template('nodes/custom/_comments.html', parent_id=parent_id) return return_content
def assets_create(): name = request.form['name'] parent_id = request.form.get('parent_id') # Detect filetype by extension (improve by detectin real file type) root, ext = os.path.splitext(name) if ext in ['.jpg', '.jpeg', '.png', '.tif', '.tiff']: filetype = 'image' elif ext in ['.blend', '.txt', '.zip']: filetype = 'file' elif ext in ['.mov', '.avi', '.mp4', '.m4v']: filetype = 'video' else: filetype = 'file' # Hash name based on file name, user id and current timestamp hash_name = name + str(current_user.objectid) + str(round(time.time())) link = hashlib.sha1(hash_name).hexdigest() link = os.path.join(link[:2], link + ext) api = SystemUtility.attract_api() node_type = NodeType.find_first({ 'where': '{"name" : "asset"}', }, api=api) # We will create the Node object later on, after creating the file object node_asset_props = dict( name=name, #description=a.description, #picture=picture, user=current_user.objectid, node_type=node_type._id, #parent=node_parent, properties=dict( content_type=filetype, #file=a.link[4:], status='processing')) src_dir_path = os.path.join(app.config['UPLOAD_DIR'], str(current_user.objectid)) # Move the file in designated location destination_dir = os.path.join(app.config['SHARED_DIR'], link[:2]) if not os.path.isdir(destination_dir): os.makedirs(destination_dir) # (TODO) Check if filename already exsits src_file_path = os.path.join(src_dir_path, name) dst_file_path = os.path.join(destination_dir, link[3:]) # (TODO) Thread this operation shutil.copy(src_file_path, dst_file_path) if filetype == 'file': mime_type = 'application' else: mime_type = filetype content_type = "{0}/{1}".format(mime_type, ext.replace(".", "")) node_file = File({ 'name': link, 'filename': name, 'user': current_user.objectid, 'backend': 'cdnsun', 'md5': '', 'content_type': content_type, 'length': 0 }) node_file.create(api=api) node_asset_props['properties']['file'] = node_file._id if parent_id: node_asset_props['parent'] = parent_id node_asset = Node(node_asset_props) node_asset.create(api=api) return jsonify( link=link, name=name, filetype=filetype, asset_id=node_asset._id)
def assets_create(): name = request.form['name'] parent_id = request.form.get('parent_id') # Detect filetype by extension (improve by detectin real file type) root, ext = os.path.splitext(name) if ext in ['.jpg', '.jpeg', '.png', '.tif', '.tiff']: filetype = 'image' elif ext in ['.blend', '.txt', '.zip']: filetype = 'file' elif ext in ['.mov', '.avi', '.mp4', '.m4v']: filetype = 'video' else: filetype = 'file' # Hash name based on file name, user id and current timestamp hash_name = name + str(current_user.objectid) + str(round(time.time())) link = hashlib.sha1(hash_name).hexdigest() link = os.path.join(link[:2], link + ext) api = SystemUtility.attract_api() node_type = NodeType.find_first({ 'where': '{"name" : "asset"}', }, api=api) # We will create the Node object later on, after creating the file object node_asset_props = dict( name=name, #description=a.description, #picture=picture, user=current_user.objectid, node_type=node_type._id, #parent=node_parent, properties=dict( content_type=filetype, #file=a.link[4:], status='processing')) src_dir_path = os.path.join(app.config['UPLOAD_DIR'], str(current_user.objectid)) # Move the file in designated location destination_dir = os.path.join(app.config['SHARED_DIR'], link[:2]) if not os.path.isdir(destination_dir): os.makedirs(destination_dir) # (TODO) Check if filename already exsits src_file_path = os.path.join(src_dir_path, name) dst_file_path = os.path.join(destination_dir, link[3:]) # (TODO) Thread this operation shutil.copy(src_file_path, dst_file_path) if filetype == 'file': mime_type = 'application' else: mime_type = filetype content_type = "{0}/{1}".format(mime_type, ext.replace(".", "")) node_file = File({ 'name': link, 'filename': name, 'user': current_user.objectid, 'backend': 'cdnsun', 'md5': '', 'content_type': content_type, 'length': 0 }) node_file.create(api=api) node_asset_props['properties']['file'] = node_file._id if parent_id: node_asset_props['parent'] = parent_id node_asset = Node(node_asset_props) node_asset.create(api=api) return jsonify(link=link, name=name, filetype=filetype, asset_id=node_asset._id)