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 edit(node_id): """Generic node editing form """ node = Node.query.get_or_404(node_id) form = get_node_form(node.node_type.url) if form.validate_on_submit(): if process_node_form(form, node_id): return redirect(url_for('node.edit', node_id=node_id)) form.name.data = node.name form.description.data = node.description # We populate the form, basing ourselves on the default node properties for node_property in node.properties: for field in form: if field.name == node_property.custom_field.name_url: value = node_property.value # We cast values into the right type if node_property.custom_field.field_type == 'integer': value = int(value) if node_property.custom_field.field_type == 'select': value = int(value) field.data = value return render_template('nodes/edit.html', node=node, form=form)
def add(node_type): """Generic function to add a node of any type """ form = get_node_form(node_type) if form.validate_on_submit(): if process_node_form(form): return redirect('/') else: print form.errors return render_template('nodes/add.html', node_type=node_type, form=form)
def add(node_type_id): """Generic function to add a node of any type """ api = SystemUtility.attract_api() ntype = NodeType.find(node_type_id, api=api) form = get_node_form(ntype) user_id = current_user.objectid if form.validate_on_submit(): if process_node_form(form, node_type=ntype, user=user_id): return redirect(url_for('nodes.index', node_type_name=ntype['name'])) else: print form.errors return render_template('nodes/add.html', node_type=ntype, form=form, errors=form.errors, type_names=type_names())
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 add(node_type_id): """Generic function to add a node of any type """ api = SystemUtility.attract_api() ntype = NodeType.find(node_type_id, api=api) form = get_node_form(ntype) user_id = current_user.objectid if form.validate_on_submit(): if process_node_form(form, node_type=ntype, user=user_id): return redirect( url_for('nodes.index', node_type_name=ntype['name'])) else: print form.errors return render_template('nodes/add.html', node_type=ntype, form=form, errors=form.errors, type_names=type_names())
def posts_edit(post_id): api = system_util.pillar_api() try: post = Node.find(post_id, {'embedded': '{"user": 1}'}, api=api) except ResourceNotFound: return abort(404) # Check if user is allowed to edit the post if not post.has_method('PUT'): return abort(403) project = Project.find(post.project, api=api) attach_project_pictures(project, api) node_type = project.get_node_type(post.node_type) form = get_node_form(node_type) if form.validate_on_submit(): if process_node_form(form, node_id=post_id, node_type=node_type, user=current_user.objectid): # The the post is published, add it to the list if form.status.data == 'published': project_update_nodes_list(post, project_id=project._id, list_name='blog') return redirect(url_for_node(node=post)) form.parent.data = post.parent form.name.data = post.name form.content.data = post.properties.content form.status.data = post.properties.status form.url.data = post.properties.url if post.picture: form.picture.data = post.picture # Embed picture file post.picture = get_file(post.picture, api=api) if post.properties.picture_square: form.picture_square.data = post.properties.picture_square return render_template('nodes/custom/post/edit.html', node_type=node_type, post=post, form=form, project=project, api=api)
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 edit(node_id): """Generic node editing form """ def set_properties(dyn_schema, form_schema, node_properties, form, prefix="", set_data=True): """Initialize custom properties for the form. We run this function once before validating the function with set_data=False, so that we can set any multiselect field that was originally specified empty and fill it with the current choices. """ for prop in dyn_schema: schema_prop = dyn_schema[prop] form_prop = form_schema[prop] prop_name = "{0}{1}".format(prefix, prop) if schema_prop['type'] == 'dict': set_properties(schema_prop['schema'], form_prop['schema'], node_properties[prop_name], form, "{0}__".format(prop_name)) continue if prop_name not in form: continue try: db_prop_value = node_properties[prop] except KeyError: log.debug('%s not found in form for node %s', prop_name, node_id) continue if schema_prop['type'] == 'datetime': db_prop_value = datetime.strptime( db_prop_value, app.config['RFC1123_DATE_FORMAT']) if isinstance(form[prop_name], SelectMultipleField): # If we are dealing with a multiselect field, check if # it's empty (usually because we can't query the whole # database to pick all the choices). If it's empty we # populate the choices with the actual data. if not form[prop_name].choices: form[prop_name].choices = [(d, d) for d in db_prop_value] # Choices should be a tuple with value and name # Assign data to the field if set_data: if prop_name == 'attachments': for attachment_collection in db_prop_value: for a in attachment_collection['files']: attachment_form = ProceduralFileSelectForm() attachment_form.file = a['file'] attachment_form.slug = a['slug'] attachment_form.size = 'm' form[prop_name].append_entry(attachment_form) elif prop_name == 'files': schema = schema_prop['schema']['schema'] # Extra entries are caused by min_entries=1 in the form # creation. field_list = form[prop_name] if len(db_prop_value) > 0: while len(field_list): field_list.pop_entry() for file_data in db_prop_value: file_form_class = build_file_select_form(schema) subform = file_form_class() for key, value in file_data.iteritems(): setattr(subform, key, value) field_list.append_entry(subform) # elif prop_name == 'tags': # form[prop_name].data = ', '.join(data) else: form[prop_name].data = db_prop_value else: # Default population of multiple file form list (only if # we are getting the form) if request.method == 'POST': continue if prop_name == 'attachments': if not db_prop_value: attachment_form = ProceduralFileSelectForm() attachment_form.file = 'file' attachment_form.slug = '' attachment_form.size = '' form[prop_name].append_entry(attachment_form) api = system_util.pillar_api() node = Node.find(node_id, api=api) project = Project.find(node.project, api=api) node_type = project.get_node_type(node.node_type) form = get_node_form(node_type) user_id = current_user.objectid dyn_schema = node_type['dyn_schema'].to_dict() form_schema = node_type['form_schema'].to_dict() error = "" node_properties = node.properties.to_dict() ensure_lists_exist_as_empty(node.to_dict(), node_type) set_properties(dyn_schema, form_schema, node_properties, form, set_data=False) if form.validate_on_submit(): if process_node_form(form, node_id=node_id, node_type=node_type, user=user_id): # Handle the specific case of a blog post if node_type.name == 'post': project_update_nodes_list(node, list_name='blog') else: project_update_nodes_list(node) # Emergency hardcore cache flush # cache.clear() return redirect( url_for('nodes.view', node_id=node_id, embed=1, _external=True, _scheme=app.config['SCHEME'])) else: log.debug('Error sending data to Pillar, see Pillar logs.') error = 'Server error' else: if form.errors: log.debug('Form errors: %s', form.errors) # Populate Form form.name.data = node.name form.description.data = node.description if 'picture' in form: form.picture.data = node.picture if node.parent: form.parent.data = node.parent set_properties(dyn_schema, form_schema, node_properties, form) # Get previews node.picture = get_file(node.picture, api=api) if node.picture else None # Get Parent try: parent = Node.find(node['parent'], api=api) except KeyError: parent = None except ResourceNotFound: parent = None embed_string = '' # Check if we want to embed the content via an AJAX call if request.args.get('embed'): if request.args.get('embed') == '1': # Define the prefix for the embedded template embed_string = '_embed' template = '{0}/edit{1}.html'.format(node_type['name'], embed_string) # We should more simply check if the template file actually exsists on # the filesystem level try: return render_template(template, node=node, parent=parent, form=form, errors=form.errors, error=error, api=api) except TemplateNotFound: template = 'nodes/edit{1}.html'.format(node_type['name'], embed_string) return render_template(template, node=node, parent=parent, form=form, errors=form.errors, error=error, api=api)
def edit(node_id): """Generic node editing form """ def set_properties( dyn_schema, form_schema, node_properties, form, prefix="", set_data=True): """Initialize custom properties for the form. We run this function once before validating the function with set_data=False, so that we can set any multiselect field that was originally specified empty and fill it with the current choices. """ for prop in dyn_schema: if not prop in node_properties: continue schema_prop = dyn_schema[prop] form_prop = form_schema[prop] prop_name = "{0}{1}".format(prefix, prop) if schema_prop['type'] == 'dict': set_properties( schema_prop['schema'], form_prop['schema'], node_properties[prop_name], form, "{0}__".format(prop_name)) else: try: data = node_properties[prop] except KeyError: print ("{0} not found in form".format(prop_name)) if schema_prop['type'] == 'datetime': data = datetime.strptime(data, RFC1123_DATE_FORMAT) if prop_name in form: # Other field types if isinstance(form[prop_name], SelectMultipleField): # If we are dealing with a multiselect field, check if # it's empty (usually because we can't query the whole # database to pick all the choices). If it's empty we # populate the choices with the actual data. if not form[prop_name].choices: form[prop_name].choices = [(d,d) for d in data] # Choices should be a tuple with value and name # Assign data to the field if set_data: form[prop_name].data = data api = SystemUtility.attract_api() node = Node.find(node_id, api=api) # TODO: simply embed node_type node_type = NodeType.find(node.node_type, api=api) form = get_node_form(node_type) user_id = current_user.objectid dyn_schema = node_type['dyn_schema'].to_dict() form_schema = node_type['form_schema'].to_dict() error = "" node_type_name = node_type.name node_properties = node.properties.to_dict() set_properties(dyn_schema, form_schema, node_properties, form, set_data=False) if form.validate_on_submit(): if process_node_form( form, node_id=node_id, node_type=node_type, user=user_id): project_update_nodes_list(node_id) return redirect(url_for('nodes.view', node_id=node_id, embed=1)) else: error = "Server error" print ("Error sending data") else: print form.errors # Populate Form form.name.data = node.name form.description.data = node.description if 'picture' in form: form.picture.data = node.picture if node.parent: form.parent.data = node.parent set_properties(dyn_schema, form_schema, node_properties, form) # Get Parent try: parent = Node.find(node['parent'], api=api) except KeyError: parent = None except ResourceNotFound: parent = None embed_string = '' # Check if we want to embed the content via an AJAX call if request.args.get('embed'): if request.args.get('embed') == '1': # Define the prefix for the embedded template embed_string = '_embed' template = '{0}/edit{1}.html'.format(node_type['name'], embed_string) # We should more simply check if the template file actually exsists on # the filesystem level try: return render_template( template, node=node, parent=parent, form=form, errors=form.errors, error=error) except TemplateNotFound: template = 'nodes/edit{1}.html'.format(node_type['name'], embed_string) return render_template( template, node=node, parent=parent, form=form, errors=form.errors, error=error)
def edit(node_id): """Generic node editing form """ def set_properties(dyn_schema, form_schema, node_properties, form, prefix="", set_data=True): """Initialize custom properties for the form. We run this function once before validating the function with set_data=False, so that we can set any multiselect field that was originally specified empty and fill it with the current choices. """ for prop in dyn_schema: schema_prop = dyn_schema[prop] form_prop = form_schema[prop] prop_name = "{0}{1}".format(prefix, prop) if schema_prop['type'] == 'dict': set_properties( schema_prop['schema'], form_prop['schema'], node_properties[prop_name], form, "{0}__".format(prop_name)) continue if prop_name not in form: continue try: db_prop_value = node_properties[prop] except KeyError: log.debug('%s not found in form for node %s', prop_name, node_id) continue if schema_prop['type'] == 'datetime': db_prop_value = datetime.strptime(db_prop_value, app.config['RFC1123_DATE_FORMAT']) if isinstance(form[prop_name], SelectMultipleField): # If we are dealing with a multiselect field, check if # it's empty (usually because we can't query the whole # database to pick all the choices). If it's empty we # populate the choices with the actual data. if not form[prop_name].choices: form[prop_name].choices = [(d, d) for d in db_prop_value] # Choices should be a tuple with value and name # Assign data to the field if set_data: if prop_name == 'attachments': for attachment_collection in db_prop_value: for a in attachment_collection['files']: attachment_form = ProceduralFileSelectForm() attachment_form.file = a['file'] attachment_form.slug = a['slug'] attachment_form.size = 'm' form[prop_name].append_entry(attachment_form) elif prop_name == 'files': schema = schema_prop['schema']['schema'] # Extra entries are caused by min_entries=1 in the form # creation. field_list = form[prop_name] if len(db_prop_value) > 0: while len(field_list): field_list.pop_entry() for file_data in db_prop_value: file_form_class = build_file_select_form(schema) subform = file_form_class() for key, value in file_data.iteritems(): setattr(subform, key, value) field_list.append_entry(subform) # elif prop_name == 'tags': # form[prop_name].data = ', '.join(data) else: form[prop_name].data = db_prop_value else: # Default population of multiple file form list (only if # we are getting the form) if request.method == 'POST': continue if prop_name == 'attachments': if not db_prop_value: attachment_form = ProceduralFileSelectForm() attachment_form.file = 'file' attachment_form.slug = '' attachment_form.size = '' form[prop_name].append_entry(attachment_form) api = system_util.pillar_api() node = Node.find(node_id, api=api) project = Project.find(node.project, api=api) node_type = project.get_node_type(node.node_type) form = get_node_form(node_type) user_id = current_user.objectid dyn_schema = node_type['dyn_schema'].to_dict() form_schema = node_type['form_schema'].to_dict() error = "" node_properties = node.properties.to_dict() ensure_lists_exist_as_empty(node.to_dict(), node_type) set_properties(dyn_schema, form_schema, node_properties, form, set_data=False) if form.validate_on_submit(): if process_node_form(form, node_id=node_id, node_type=node_type, user=user_id): # Handle the specific case of a blog post if node_type.name == 'post': project_update_nodes_list(node, list_name='blog') else: project_update_nodes_list(node) # Emergency hardcore cache flush # cache.clear() return redirect(url_for('nodes.view', node_id=node_id, embed=1, _external=True, _scheme=app.config['SCHEME'])) else: log.debug('Error sending data to Pillar, see Pillar logs.') error = 'Server error' else: if form.errors: log.debug('Form errors: %s', form.errors) # Populate Form form.name.data = node.name form.description.data = node.description if 'picture' in form: form.picture.data = node.picture if node.parent: form.parent.data = node.parent set_properties(dyn_schema, form_schema, node_properties, form) # Get previews node.picture = get_file(node.picture, api=api) if node.picture else None # Get Parent try: parent = Node.find(node['parent'], api=api) except KeyError: parent = None except ResourceNotFound: parent = None embed_string = '' # Check if we want to embed the content via an AJAX call if request.args.get('embed'): if request.args.get('embed') == '1': # Define the prefix for the embedded template embed_string = '_embed' template = '{0}/edit{1}.html'.format(node_type['name'], embed_string) # We should more simply check if the template file actually exsists on # the filesystem level try: return render_template( template, node=node, parent=parent, form=form, errors=form.errors, error=error, api=api) except TemplateNotFound: template = 'nodes/edit{1}.html'.format(node_type['name'], embed_string) return render_template( template, node=node, parent=parent, form=form, errors=form.errors, error=error, api=api)
def edit(node_id): """Generic node editing form """ def set_properties(dyn_schema, form_schema, node_properties, form, prefix="", set_data=True): """Initialize custom properties for the form. We run this function once before validating the function with set_data=False, so that we can set any multiselect field that was originally specified empty and fill it with the current choices. """ for prop in dyn_schema: if not prop in node_properties: continue schema_prop = dyn_schema[prop] form_prop = form_schema[prop] prop_name = "{0}{1}".format(prefix, prop) if schema_prop['type'] == 'dict': set_properties(schema_prop['schema'], form_prop['schema'], node_properties[prop_name], form, "{0}__".format(prop_name)) else: try: data = node_properties[prop] except KeyError: print("{0} not found in form".format(prop_name)) if schema_prop['type'] == 'datetime': data = datetime.strptime(data, RFC1123_DATE_FORMAT) if prop_name in form: # Other field types if isinstance(form[prop_name], SelectMultipleField): # If we are dealing with a multiselect field, check if # it's empty (usually because we can't query the whole # database to pick all the choices). If it's empty we # populate the choices with the actual data. if not form[prop_name].choices: form[prop_name].choices = [(d, d) for d in data] # Choices should be a tuple with value and name # Assign data to the field if set_data: form[prop_name].data = data api = SystemUtility.attract_api() node = Node.find(node_id, api=api) # TODO: simply embed node_type node_type = NodeType.find(node.node_type, api=api) form = get_node_form(node_type) user_id = current_user.objectid dyn_schema = node_type['dyn_schema'].to_dict() form_schema = node_type['form_schema'].to_dict() error = "" node_type_name = node_type.name node_properties = node.properties.to_dict() set_properties(dyn_schema, form_schema, node_properties, form, set_data=False) if form.validate_on_submit(): if process_node_form(form, node_id=node_id, node_type=node_type, user=user_id): project_update_nodes_list(node_id) return redirect(url_for('nodes.view', node_id=node_id, embed=1)) else: error = "Server error" print("Error sending data") else: print form.errors # Populate Form form.name.data = node.name form.description.data = node.description if 'picture' in form: form.picture.data = node.picture if node.parent: form.parent.data = node.parent set_properties(dyn_schema, form_schema, node_properties, form) # Get Parent try: parent = Node.find(node['parent'], api=api) except KeyError: parent = None except ResourceNotFound: parent = None embed_string = '' # Check if we want to embed the content via an AJAX call if request.args.get('embed'): if request.args.get('embed') == '1': # Define the prefix for the embedded template embed_string = '_embed' template = '{0}/edit{1}.html'.format(node_type['name'], embed_string) # We should more simply check if the template file actually exsists on # the filesystem level try: return render_template(template, node=node, parent=parent, form=form, errors=form.errors, error=error) except TemplateNotFound: template = 'nodes/edit{1}.html'.format(node_type['name'], embed_string) return render_template(template, node=node, parent=parent, form=form, errors=form.errors, error=error)