def delete_community(name): """ Route that handles deleting a community. """ community = community_service.get_community(name) if community: if community.user_id != current_user.id: return redirect(url_for("community.community", name=name)) community_service.delete_community(community) flash("Successfully deleted community.", "primary") return redirect(url_for("feed.feed")) else: abort(404)
def leave_community(name): """ Route that handles removing the current user as a community member. """ community = community_service.get_community(name) if community: community_member = community_service.get_community_member( community.id, current_user.id) if community_member: community_service.delete_community_member(community_member) flash("Successfully left community.", "primary") return redirect(url_for("community.community", name=community.name)) else: abort(404)
def join_community(name): """ Route that handles adding the current user as a community member. """ community = community_service.get_community(name) if community: community_member = community_service.get_community_member( community.id, current_user.id) if community_member == None: community_service.create_community_member(community, current_user) flash("Successfully joined community.", "primary") return redirect(url_for("community.community", name=community.name)) else: abort(404)
def create_post(name): """ Route for creating a post. On a GET request, it returns the post creation form. On a POST request, it handles creating a post. """ community = community_service.get_community(name) if community: form = PostForm() form.community_id.data = community.id if form.validate_on_submit(): post_service.create_post( form.title.data, form.post.data, community, current_user ) flash("Successfully created post.", "primary") return redirect(url_for("post.post", name=name, title=form.title.data)) return render_template("create_post.html", name=name, form=form) else: abort(404)
def update_community(name): """ Route for updating a community description. On a GET request, it returns the update community form. On a POST request, it processes the community update. """ community = community_service.get_community(name) if community: if community.user_id != current_user.id: return redirect(url_for("community.community", name=name)) form = UpdateCommunityForm() if form.validate_on_submit(): community_service.update_community(community, form.description.data) flash("Successfully updated community.", "primary") return redirect(url_for("community.community", name=name)) form.description.data = community.description return render_template("update_community.jinja2", name=name, form=form) else: abort(404)
def top_community(name): """ Route for page displaying a community and its posts sorted by number of upvotes. """ page = int(request.args.get("page", 1)) community = community_service.get_community(name) if community: posts = community_service.get_community_posts(community.id, page, True) community_member = None if current_user.is_authenticated: community_member = community_service.get_community_member( community.id, current_user.id) return render_template( "community.jinja2", tab="top", community=community, posts=posts, community_member=community_member, ) else: abort(404)
def community(name): """ Route for page displaying a community and its posts sorted by date created. """ page = int(request.args.get("page", 1)) community = community_service.get_community(name) if community: posts = community_service.get_community_posts(community.id, page, False) community_member = None if current_user.is_authenticated: community_member = community_service.get_community_member( community.id, current_user.id) return render_template( "community.html", tab="recent", community=community, posts=posts, community_member=community_member, ) else: abort(404)