def add_topic(): node_types = Node.get_types() for nt in node_types: nt['nodes'] = Node.get_nodes_by_type(nt['TypeID']) if request.method == 'GET': # choose a node to be default, here is node_id = 10001 node_id = int(request.args['node_id']) if "node_id" in request.args else 10001 form = TopicForm(node_id=node_id) node = Node.get_node_by_id(node_id) return render_template('topic/add_topic.html', node=node, node_types=node_types, form=form) else: form = TopicForm(request.form) if form.validate(): # node_id = int(form.node_id.data) node_id = 10001 title = cgi.escape(form.title.data) content = cgi.escape(form.content.data) user_id = session['user_id'] new_topic_id = Topic.add(node_id, title, content, user_id) return redirect(url_for('single_topic', topic_id=new_topic_id)) else: # choose a node to be default, here is node_id = 10001 # node_id = int(form.node_id.data) node_id = 10001 node = Node.get_node_by_id(node_id) return render_template('topic/add_topic.html', node=node, node_types=node_types, form=form)
def edit_topic(topic_id): # auth check topic = Topic.get_topic(topic_id) check_private(topic['UserID']) node_types = Node.get_types() for nt in node_types: nt['nodes'] = Node.get_nodes_by_type(nt['TypeID']) if request.method == 'GET': form = TopicForm(node_id=topic['NodeID'], title=topic['Title'], content=topic['Content']) return render_template('edit_topic.html', topic=topic, node_types=node_types, form=form) elif request.method == 'POST': form = TopicForm(request.form) if form.validate(): node_id = int(form.node_id.data) title = cgi.escape(form.title.data) content = cgi.escape(form.content.data) new_topic_id = Topic.edit(topic_id, node_id, title, content) return redirect(url_for('single_topic', topic_id=topic_id)) else: return render_template('edit_topic.html', topic=topic, node_types=node_types, form=form)
def add_topic(): if request.method == 'GET': form = TopicForm() return render_template('topic/add_topic.html', form=form) else: form = TopicForm(request.form) if form.validate(): topic = Topic(title=cgi.escape(form.title.data), content=cgi.escape(form.content.data), user_id=session['user_id']) db.session.add(topic) db.session.commit() return redirect(url_for('topic', topic_id=topic.id)) else: return render_template('topic/add_topic.html', form=form)
def edit_topic(topic_id): topic = Topic.query.get_or_404(topic_id) if topic.user_id != session['user_id']: abort(404) if request.method == 'GET': form = TopicForm(title=topic.title, content=topic.content) return render_template('topic/edit_topic.html', topic=topic, form=form) else: form = TopicForm(request.form) if form.validate(): topic.title = cgi.escape(form.title.data) topic.content = cgi.escape(form.content.data) db.session.add(topic) db.session.commit() return redirect(url_for('topic', topic_id=topic_id)) else: return render_template('topic/edit_topic.html', topic=topic, form=form)
def edit_topic(topic_id): # auth check topic = Topic.get_topic(topic_id) if topic['UserID'] != session['user_id']: abort(404) node_types = Node.get_types() for nt in node_types: nt['nodes'] = Node.get_nodes_by_type(nt['TypeID']) if request.method == 'GET': form = TopicForm(node_id=topic['NodeID'], title=topic['Title'], content=topic['Content']) return render_template('topic/edit_topic.html', topic=topic, node_types=node_types, form=form) else: form = TopicForm(request.form) if form.validate(): # node_id = int(form.node_id.data) node_id = 10001 title = cgi.escape(form.title.data) content = cgi.escape(form.content.data) new_topic_id = Topic.edit(topic_id, node_id, title, content) return redirect(url_for('single_topic', topic_id=topic_id)) else: return render_template('topic/edit_topic.html', topic=topic, node_types=node_types, form=form)