def discussions_create(): form = DiscussionForm(request.form) if not form.validate(): return render_template( "discussions/new.html", form=DiscussionForm(), error= "Title length must be between 5 and 100 characters. Content must be between 5 and 2000 characters." ) discussion = Discussion(form.title.data) discussion.set_account_id(current_user.id) #Tags are separated by whitespace, split them into array and append them to discussion separately. discussion_tags = form.tags.data.split(' ') for tagname in discussion_tags: # Check for empty strings if tagname and tagname.strip(): tag = Tag(tagname) discussion.tags.append(tag) db.session().add(discussion) db.session().commit() message = Message(form.content.data) message.set_account_id(current_user.id) message.set_discussion_id(discussion.id) db.session().add(message) db.session().commit() return redirect(url_for("discussions_show", discussion_id=discussion.id))
def discussions_comment(discussion_id): form = CommentForm(request.form) if not form.validate(): return redirect( url_for("discussions_show", discussion_id=discussion_id)) message = Message(form.comment.data) message.set_account_id(current_user.id) message.set_discussion_id(discussion_id) db.session().add(message) db.session().commit() return redirect(url_for("discussions_show", discussion_id=discussion_id))