def handle_asks(post, list_of_wants, page=None): post = Post.query.filter(Post.id==post).one() if isinstance(post, int) else post ret = {"current_post": post.id, "rels": {}, "posts": {}} if "children" in list_of_wants: rels = child_rel_query(post.id) child_ids = [rel.child_id for rel in rels] children = [] if child_ids: children = Post.query.filter(Post.id.in_(child_ids)).all() child_posts = [p.writeable for p in children] ret["link_ids"] = [r.id for r in rels] rels = [r.writeable_with_vote_info(current_user) for r in rels] ret["posts"].update(dict_by_id(child_posts + [post.writeable])) ret["rels"].update(dict_by_id(rels)) if "actions" in list_of_wants: action_count = total_actions(post.id) if page is None: # set page to the last page if not given page = math.ceil(float(action_count) / float(ACTIONS_PER_PAGE)) action_info = actions_with_data(post.id, page) ret["actions"] = action_info["actions"] ret["rels"].update(dict_by_id(action_info["rels"])) ret["posts"].update(dict_by_id(action_info["posts"])) ret["action_count"] = action_count ret["page"] = int(page) ret["comments"] = dict_by_id(action_info["comments"]) return ret
def handle_asks(post, list_of_wants, page=None): post = Post.query.filter( Post.id == post).one() if isinstance(post, int) else post ret = {"current_post": post.id, "rels": {}, "posts": {}} if "children" in list_of_wants: rels = child_rel_query(post.id) child_ids = [rel.child_id for rel in rels] children = [] if child_ids: children = Post.query.filter(Post.id.in_(child_ids)).all() child_posts = [p.writeable for p in children] ret["link_ids"] = [r.id for r in rels] rels = [r.writeable_with_vote_info(current_user) for r in rels] ret["posts"].update(dict_by_id(child_posts + [post.writeable])) ret["rels"].update(dict_by_id(rels)) if "actions" in list_of_wants: action_count = total_actions(post.id) if page is None: # set page to the last page if not given page = math.ceil(float(action_count) / float(ACTIONS_PER_PAGE)) action_info = actions_with_data(post.id, page) ret["actions"] = action_info["actions"] ret["rels"].update(dict_by_id(action_info["rels"])) ret["posts"].update(dict_by_id(action_info["posts"])) ret["action_count"] = action_count ret["page"] = int(page) ret["comments"] = dict_by_id(action_info["comments"]) return ret
def actions_endpoint(post_id): action_count = total_actions(post_id) page = request.args.get( 'page', math.ceil(float(action_count) / float(ACTIONS_PER_PAGE))) print("page is %s" % page) action_info = actions_with_data(post_id, page) return transitify({ "actions": action_info["actions"], "action_count": action_count, "posts": dict_by_id(action_info["posts"]), "rels": dict_by_id(action_info["rels"]), "comments": dict_by_id(action_info["comments"]), "page": int(page) })
def actions_endpoint(post_id): action_count = total_actions(post_id) page = request.args.get('page', math.ceil(float(action_count) / float(ACTIONS_PER_PAGE))) print "page is %s" % page action_info = actions_with_data(post_id, page) return transitify({ "actions": action_info["actions"], "action_count": action_count, "posts": dict_by_id(action_info["posts"]), "rels": dict_by_id(action_info["rels"]), "comments": dict_by_id(action_info["comments"]), "page": int(page) })
def render_post(post_id): from localsettings import SETTINGS print "post is %s" % post_id req_data = get_post_data_from_req(request) page = req_data.get("page", math.ceil(float(total_actions(post_id)) / float(ACTIONS_PER_PAGE))) data_only = request.args.get('data-only') print "page is %s" % page app_state = handle_asks(post_id, ["children", "actions"], page=page) app_state["user"] = writable_current_user() print app_state if not data_only: return render_template('base.html', debug=SETTINGS["DEBUG"], app_state=transitify(app_state)) return transitify(app_state)
def render_post(post_id): print("post is %s" % post_id) req_data = get_post_data_from_req(request) page = req_data.get( "page", math.ceil(float(total_actions(post_id)) / float(ACTIONS_PER_PAGE))) data_only = request.args.get('data-only') print("page is %s" % page) app_state = handle_asks(post_id, ["children", "actions"], page=page) app_state["user"] = writable_current_user() print(app_state) if not data_only: return render_template('base.html', debug=SETTINGS["DEBUG"], app_state=transitify(app_state)) return transitify(app_state)
def link_post(): req_data = get_post_data_from_req(request) parent_id = req_data.get('parent') relation = "Post not found" child_id = get_post_id_from_text(req_data.get('child-text')) if child_id: print "linking post %s" % child_id relation = Relation.link_posts(parent_id, child_id, current_user) if isinstance(relation, basestring): return transitify({"error": relation}) app_state = {"success": "linked successfully"} if req_data.get('current_post') and req_data.get('ask_for'): page = req_data.get("page", math.ceil(float(total_actions(parent_id)) / float(ACTIONS_PER_PAGE))) app_state.update(handle_asks(req_data.get('current_post'), req_data.get('ask_for'), page)) return transitify(app_state)
def link_post(): req_data = get_post_data_from_req(request) parent_id = req_data.get('parent') relation = "Post not found" child_id = get_post_id_from_text(req_data.get('child-text')) if child_id: print("linking post %s" % child_id) relation = Relation.link_posts(parent_id, child_id, current_user) if isinstance(relation, str): return transitify({"error": relation}) app_state = {"success": "linked successfully"} if req_data.get('current_post') and req_data.get('ask_for'): page = req_data.get( "page", math.ceil( float(total_actions(parent_id)) / float(ACTIONS_PER_PAGE))) app_state.update( handle_asks(req_data.get('current_post'), req_data.get('ask_for'), page)) return transitify(app_state)
def submit_post(): req_data = get_post_data_from_req(request) post = Post.submit_post(current_user, req_data.get("text"), req_data.get("title")) if isinstance(post, str): return transitify({"error": post, "error_type": "create-post"}) post_id = req_data.get('parent', Post.root_post_id()) relation = Relation.link_posts(post_id, post, current_user) if isinstance(relation, str): return transitify({"error": relation, "error_type": "link-posts"}) app_state = {"success": "posted successfully"} if req_data.get('current_post') and req_data.get('ask_for'): page = req_data.get( "page", math.ceil(float(total_actions(post_id)) / float(ACTIONS_PER_PAGE))) app_state.update( handle_asks(req_data.get('current_post'), req_data.get('ask_for'), page)) return transitify(app_state)
def submit_post(): req_data = get_post_data_from_req(request) post = Post.submit_post(current_user, req_data.get("text"), req_data.get("title")) if isinstance(post, basestring): return transitify({"error": post, "error_type": "create-post"}) post_id = req_data.get('parent', Post.root_post_id()) relation = Relation.link_posts(post_id, post, current_user) if isinstance(relation, basestring): return transitify({"error": relation, "error_type": "link-posts"}) app_state = {"success": "posted successfully"} if req_data.get('current_post') and req_data.get('ask_for'): page = req_data.get("page", math.ceil(float(total_actions(post_id)) / float(ACTIONS_PER_PAGE))) app_state.update(handle_asks(req_data.get('current_post'), req_data.get('ask_for'), page)) return transitify(app_state)