diff --git a/app.py b/app.py index 9c0c34c..f54a6ed 100644 --- a/app.py +++ b/app.py @@ -98,7 +98,7 @@ class Reaction(db.Model): def choose_new_next(min_like_num=MIN_LIKE_NUM): for story in Story.query.filter_by(is_tree=False).all(): last_paragraph_id = story.tail - next_one = Paragraph.query.filter_by(parent_id=last_paragraph_id, is_hidden=False)\ + next_one = Paragraph.query.filter_by(story_id=story.id, parent_id=last_paragraph_id, is_hidden=False)\ .order_by(Paragraph.like_num.desc()).first() if next_one and next_one.like_num >= min_like_num: story.text += next_one.text @@ -106,7 +106,35 @@ def choose_new_next(min_like_num=MIN_LIKE_NUM): story.tail = next_one.id next_one.is_chosen = True - db.session.commit() + for story in Story.query.filter_by(is_tree=True).all(): + _max = story.total_like_num + _tail = story.tail + _sum_dict = {} + + def _get_like_sum(p): + if p.id in _sum_dict: + return _sum_dict[p.id] + s = p.like_num + (p.parent_id and _get_like_sum(Paragraph.query.get(p.parent_id))) + _sum_dict[p.id] = s + return s + + for p in Paragraph.query.filter(Paragraph.like_num >= min_like_num)\ + .filter_by(story_id=story.id, is_hidden=False).all(): + if _get_like_sum(p) > _max: + _max = _get_like_sum(p) + _tail = p.id + + p = Paragraph.query.get(_tail) + _text = p.text + while p.parent_id: + p = Paragraph.query.get(p.parent_id) + _text = p.text + _text + + story.total_like_num = _max + story.tail = _tail + story.text = _text + + db.session.commit() def sample_question(qs, n=3):