def post_details_form_delete(post_id): """ Treats the POST request to delete a post. """ post = Post.get(Post, post_id) post.delete() return redirect(f'{g.USERS.PATH}/{post.user_id}')
def create_form_view(user_id): """ Renders the Add Post form. Sends some special variables to the front-end: - method: the method to be sent by the form on click on the 'Save' button. - crud: the operation to configure the form. """ post = Post({}) post.user_id = user_id user = User.get(User, user_id) tags = Tag.all(Tag) return render_template( 'post_form.html', method='POST', crud='create', page_title=f'{g.POSTS.ADD_PG} for {user.full_name()}', post=post, all_tags=tags)
def post_details_form_view(post_id): """ Renders the form that shows the post details. From' Contexts --------------- User Details -> <Post title on the list> """ # print(post.__repr__) # This is cool post = Post.get(Post, post_id) return render_template('post_view.html', page_title=g.POSTS.VIEW_PG, post=post)
def edit_view(post_id): """Renders Edit Post form. Sends some special variables to the front-end: - method: the method to be sent by the form on click on the 'Save' button. - crud: the operation to configure the form. """ post = Post.get(Post, post_id) all_tags = Tag.all(Tag) post_tag_ids = post.get_tag_ids() return render_template('post_form.html', method='POST', crud='update', page_title=g.POSTS.EDIT_PG, post=post, all_tags=all_tags, post_tag_ids=post_tag_ids)
def post_add(user_id): """ Treats the POST request to add the a new post. """ dict_form = dict(request.form) dict_form['user_id'] = user_id post_id = Post.add(Post, dict_form) tag_keys = request.form.getlist("tag_keys") tag_post_list = [ TagPost({ 'post_id': post_id, 'tag_id': tk }) for tk in tag_keys ] TagPost.add_all(tag_post_list) return redirect(f'{g.USERS.PATH}/{dict_form["user_id"]}')
def edit_save(post_id): """ Treats the POST request to update a post. """ dict_form = dict(request.form) dict_form['id'] = post_id post = Post.get(Post, post_id) post.update(dict_form) tag_keys = request.form.getlist("tag_keys") # tag_post_list = [TagPost({'post_id': post_id, 'tag_id': tk}) for tk in tag_keys] TagPost.remove_from_post(post_id) post_tags = [ TagPost({ 'tag_id': tag_id, 'post_id': post_id }) for tag_id in tag_keys ] Tag.add_all(post_tags) return redirect(f'{g.USERS.PATH}/{post.user_id}')
def insert_data(): from blogly.users.user_model import User budda = User({ 'first_name': 'Siddhārtha', 'last_name': 'Gautama', 'image_url': None }) jesus = User({'first_name': 'Jesus', 'last_name': 'Christ'}) krishna = User({ 'first_name': 'Krishna', 'last_name': 'Vasudeva', 'image_url': None }) lahiru = User({ 'first_name': 'Lahiru', 'last_name': 'Gamathige', 'image_url': 'https://res-4.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_256,w_256,f_auto,q_auto:eco/v1446050463/wle8mms7cc0leozu27fy.jpg' }) db.session.add_all([budda, jesus, krishna, lahiru]) db.session.commit() from blogly.posts.post_model import Post post_2_1 = Post({ 'title': 'הדיבר העשירי', 'content': 'עכשיו אני אומר לך לאהוב אחד את השני, כמו שאהבתי ', 'user_id': 2 }) post_3_1 = Post({ 'title': 'विश्वासों', 'content': 'एक आदमी अपनी मान्यताओं से बनता है। जैसा वह मानता है। तो वह बन जाता है।', 'user_id': 3 }) post_4_1 = Post({ 'title': 'මැක්සිමා ප්ර is ාව', 'content': 'මම යමක් කියන්නම් නමුත් ඔබට එය තේරෙන්නේ නැත.', 'user_id': 4 }) post_4_2 = Post({ 'title': 'For My Students', 'content': 'I wish my hair was growing as much as your knowledge.', 'user_id': 4 }) db.session.add_all([post_2_1, post_3_1, post_4_1, post_4_2]) db.session.commit() from blogly.tags.tag_model import Tag tag1 = Tag({'name': 'Religion'}) tag2 = Tag({'name': 'Art'}) tag3 = Tag({'name': 'Kitties'}) db.session.add_all([tag1, tag2, tag3]) db.session.commit() from blogly.tags.tag_post_model import TagPost tag_post_1_1 = TagPost({'post_id': 1, 'tag_id': 1}) # tag_post_1_2 = TagPost({'post_id': 1, 'tag_id': 2}) tag_post_1_3 = TagPost({'post_id': 1, 'tag_id': 3}) db.session.add_all([tag_post_1_1, tag_post_1_3]) db.session.commit()