def list_cafes(): data, cursor = cursor_query(Cafe) data = list(iter_items_with_users(data)) if not current_user or request.args.get('cursor'): return jsonify(data=data, cursor=cursor) cafe_ids = CafeMember.get_user_following_cafe_ids(current_user.id) following = Cafe.cache.get_many(cafe_ids) following = list(iter_items_with_users(following)) return jsonify(following=following, data=data, cursor=cursor)
def view_user_topics(username): cursor = int_or_raise('cursor', 0) count = int_or_raise('count', 20, 100) user = User.cache.first_or_404(username=username) q = db.session.query(Topic.id) q = q.filter(Topic.status != Topic.STATUS_DRAFT) q = q.filter_by(user_id=user.id) if cursor: q = q.filter(Topic.id < cursor) q = q.order_by(Topic.id.desc()).limit(count).all() topic_ids = [i for i, in q] if not topic_ids: return jsonify(data=[], cursor=0) topics = Topic.cache.get_many(topic_ids) data = list(iter_items_with_users(topics, {str(user.id): user})) data = list(iter_topics_with_statuses(data, current_user.id)) if len(topic_ids) < count: cursor = 0 else: cursor = topics[-1].id return jsonify(data=data, cursor=cursor)
def list_cafe_topics(slug): cafe = Cafe.cache.first_or_404(slug=slug) cts, p = pagination_query(CafeTopic, 'updated_at', cafe_id=cafe.id) data = Topic.cache.get_many([c.topic_id for c in cts]) data = list(iter_items_with_users(data)) data = list(iter_topics_with_statuses(data, current_user.id)) return jsonify(data=data, pagination=dict(p))
def timeline(): cursor = int_or_raise('cursor', 0) if request.args.get('show') == 'all': topics, cursor = get_all_topics(cursor) else: topics, cursor = get_timeline_topics(cursor, current_user.id) topics_cafes = CafeTopic.get_topics_cafes([t.id for t in topics]) data = [] for d in iter_items_with_users(topics): d['cafes'] = topics_cafes.get(d['id']) data.append(d) data = list(iter_topics_with_statuses(data, current_user.id)) return jsonify(data=data, cursor=cursor)
def view_topic_comments(tid): topic = Topic.cache.get_or_404(tid) comments, cursor = cursor_query(Comment, lambda q: q.filter_by(topic_id=topic.id)) data = [] if current_user: statuses = Comment.get_multi_statuses([c['id'] for c in comments], current_user.id) else: statuses = {} for d in iter_items_with_users(comments): d['content'] = markup(d['content']) # update status d.update(statuses.get(str(d['id']), {})) data.append(d) return jsonify(data=data, cursor=cursor)
def view_topic_comments(tid): topic = Topic.cache.get_or_404(tid) comments, cursor = cursor_query( Comment, lambda q: q.filter_by(topic_id=topic.id) ) data = [] if current_user: statuses = Comment.get_multi_statuses( [c['id'] for c in comments], current_user.id ) else: statuses = {} for d in iter_items_with_users(comments): d['content'] = markup(d['content']) # update status d.update(statuses.get(str(d['id']), {})) data.append(d) return jsonify(data=data, cursor=cursor)
def view_user_cafes(username): user = User.cache.first_or_404(username=username) cafe_ids = CafeMember.get_user_following_cafe_ids(user.id) cafes = Cafe.cache.get_many(cafe_ids) data = list(iter_items_with_users(cafes)) return jsonify(data=data)