def create(community_url: str, post_type: str): api = system_util.pillar_api() project = Project.find_first({'where': {'url': community_url}}, api=api) if project is None: return abort(404) log.info('Creating post for user {}'.format(current_user.objectid)) dillo_post_node_type = project.get_node_type('dillo_post') dillo_post_tags_default = dillo_post_node_type['dyn_schema']['tags']['schema']['default'] post_props = dict( project=project['_id'], name='Awesome Post Title', user=current_user.objectid, node_type='dillo_post', properties=dict( tags=[dillo_post_tags_default, ], post_type=post_type) ) post = Node(post_props) post.create(api=api) embed = request.args.get('embed') return redirect(url_for( 'nodes.edit', node_id=post._id, embed=embed, _external=True, _scheme=current_app.config['SCHEME']))
def comments_create(): content = request.form['content'] parent_id = request.form.get('parent_id') api = system_util.pillar_api() parent_node = Node.find(parent_id, api=api) node_asset_props = dict( project=parent_node.project, name='Comment', user=current_user.objectid, node_type='comment', properties=dict( content=content, status='published', confidence=0, rating_positive=0, rating_negative=0)) if parent_id: node_asset_props['parent'] = parent_id # Get the parent node and check if it's a comment. In which case we flag # the current comment as a reply. parent_node = Node.find(parent_id, api=api) if parent_node.node_type == 'comment': node_asset_props['properties']['is_reply'] = True node_asset = Node(node_asset_props) node_asset.create(api=api) return jsonify( asset_id=node_asset._id, content=node_asset.properties.content)
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 groups_create(): # Use current_project_id from the session instead of the cookie name = request.form['name'] project_id = request.form['project_id'] parent_id = request.form.get('parent_id') api = system_util.pillar_api() # We will create the Node object later on, after creating the file object node_asset_props = dict( name=name, user=current_user.objectid, node_type='group', project=project_id, properties=dict( status='published')) # Add parent_id only if provided (we do not provide it when creating groups # at the Project root) if parent_id: node_asset_props['parent'] = parent_id print parent_id node_asset = Node(node_asset_props) node_asset.create(api=api) return jsonify(status='success', data=dict(name=name, asset_id=node_asset._id))
def comments_create(): content = request.form['content'] parent_id = request.form.get('parent_id') api = system_util.pillar_api() parent_node = Node.find(parent_id, api=api) node_asset_props = dict(project=parent_node.project, name='Comment', user=current_user.objectid, node_type='comment', properties=dict(content=content, status='published', confidence=0, rating_positive=0, rating_negative=0)) if parent_id: node_asset_props['parent'] = parent_id # Get the parent node and check if it's a comment. In which case we flag # the current comment as a reply. parent_node = Node.find(parent_id, api=api) if parent_node.node_type == 'comment': node_asset_props['properties']['is_reply'] = True node_asset = Node(node_asset_props) node_asset.create(api=api) return jsonify(asset_id=node_asset._id, content=node_asset.properties.content)
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 assets_create(): project_id = request.form['project_id'] 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' api = system_util.pillar_api() # We will create the Node object later on, after creating the file object node_asset_props = dict( name=name, project=project_id, user=current_user.objectid, node_type='asset', properties=dict( content_type=filetype, status='processing')) if filetype == 'file': mime_type_base = 'application' else: mime_type_base = filetype mime_type = "{0}/{1}".format(mime_type_base, ext.replace(".", "")) node_file = process_and_create_file(project_id, name, 0, mime_type) 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 comments_create(): content = request.form['content'] parent_id = request.form.get('parent_id') if not parent_id: log.warning('User %s tried to create comment without parent_id', current_user.objectid) raise wz_exceptions.UnprocessableEntity() api = system_util.pillar_api() parent_node = Node.find(parent_id, api=api) if not parent_node: log.warning( 'Unable to create comment for user %s, parent node %r not found', current_user.objectid, parent_id) raise wz_exceptions.UnprocessableEntity() log.info('Creating comment for user %s on parent node %r', current_user.objectid, parent_id) comment_props = dict(project=parent_node.project, name='Comment', user=current_user.objectid, node_type='comment', properties=dict(content=content, status='published', confidence=0, rating_positive=0, rating_negative=0)) if parent_id: comment_props['parent'] = parent_id # Get the parent node and check if it's a comment. In which case we flag # the current comment as a reply. parent_node = Node.find(parent_id, api=api) if parent_node.node_type == 'comment': comment_props['properties']['is_reply'] = True comment = Node(comment_props) comment.create(api=api) return jsonify({'node_id': comment._id}), 201
def assets_create(): project_id = request.form['project_id'] 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' api = system_util.pillar_api() # We will create the Node object later on, after creating the file object node_asset_props = dict(name=name, project=project_id, user=current_user.objectid, node_type='asset', properties=dict(content_type=filetype, status='processing')) if filetype == 'file': mime_type_base = 'application' else: mime_type_base = filetype mime_type = "{0}/{1}".format(mime_type_base, ext.replace(".", "")) node_file = process_and_create_file(project_id, name, 0, mime_type) 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 create(): """Create a node. Requires a number of params: - project id - node_type - parent node (optional) """ if request.method != 'POST': return abort(403) project_id = request.form['project_id'] parent_id = request.form.get('parent_id') node_type_name = request.form['node_type_name'] api = system_util.pillar_api() # Fetch the Project or 404 try: project = Project.find(project_id, api=api) except ResourceNotFound: return abort(404) node_type = project.get_node_type(node_type_name) node_type_name = 'folder' if node_type['name'] == 'group' else \ node_type['name'] node_props = dict( name='New {}'.format(node_type_name), project=project['_id'], user=current_user.objectid, node_type=node_type['name'], properties={} ) if parent_id: node_props['parent'] = parent_id ensure_lists_exist_as_empty(node_props, node_type) node = Node(node_props) node.create(api=api) return jsonify(status='success', data=dict(asset_id=node['_id']))
def create(): """Create a node. Requires a number of params: - project id - node_type - parent node (optional) """ if request.method != 'POST': return abort(403) project_id = request.form['project_id'] parent_id = request.form.get('parent_id') node_type_name = request.form['node_type_name'] api = system_util.pillar_api() # Fetch the Project or 404 try: project = Project.find(project_id, api=api) except ResourceNotFound: return abort(404) node_type = project.get_node_type(node_type_name) node_type_name = 'folder' if node_type['name'] == 'group' else \ node_type['name'] node_props = dict(name='New {}'.format(node_type_name), project=project['_id'], user=current_user.objectid, node_type=node_type['name'], properties={}) if parent_id: node_props['parent'] = parent_id ensure_lists_exist_as_empty(node_props, node_type) node = Node(node_props) node.create(api=api) return jsonify(status='success', data=dict(asset_id=node['_id']))
def groups_create(): # Use current_project_id from the session instead of the cookie name = request.form['name'] project_id = request.form['project_id'] parent_id = request.form.get('parent_id') api = system_util.pillar_api() # We will create the Node object later on, after creating the file object node_asset_props = dict(name=name, user=current_user.objectid, node_type='group', project=project_id, properties=dict(status='published')) # Add parent_id only if provided (we do not provide it when creating groups # at the Project root) if parent_id: node_asset_props['parent'] = parent_id print parent_id node_asset = Node(node_asset_props) node_asset.create(api=api) return jsonify(status='success', data=dict(name=name, 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)
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)