From a4cc806e5d8b25b280caa091b6edc8e8834407b9 Mon Sep 17 00:00:00 2001 From: Tdxdxoz Date: Fri, 5 Nov 2021 22:30:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=A0=91=E5=BD=A2=E7=9A=84=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 28 +++++++++++++++++++++++----- templates/story.html | 18 +++++++++++++----- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 47f351b..9c0c34c 100644 --- a/app.py +++ b/app.py @@ -185,13 +185,26 @@ def story(story_id): story = Story.query.get_or_404(story_id) is_tree = story.is_tree - paragraph_part = Paragraph.query.filter_by( - story_id=story_id, is_chosen=True, is_hidden=False - ).all() + if is_tree: + tail = request.args.get('tail', story.tail, type=int) + p_tail = Paragraph.query.get_or_404(tail) + if p_tail.story_id != story_id: + abort(404) + paragraph_part = [p_tail] + p = p_tail + while p.parent_id: + p = Paragraph.query.get_or_404(p.parent_id) + paragraph_part.insert(0, p) + + else: + tail = story.tail + paragraph_part = Paragraph.query.filter_by( + story_id=story_id, is_chosen=True, is_hidden=False + ).all() sort_by = request.args.get('sort_by', 'time') - q = Paragraph.query.filter_by(parent_id=story.tail, is_hidden=False) + q = Paragraph.query.filter_by(story_id=story_id, parent_id=tail, is_hidden=False) q = q.order_by(Paragraph.like_num.desc() if sort_by == 'like' else Paragraph.id.desc()) pagination = q.paginate(max_per_page=100) @@ -217,9 +230,14 @@ def create(): if not text or len(text) > 140: abort(422) story = Story.query.get_or_404(story_id) + + if story.is_tree: + parent_id = request.form.get('tail', type=int) + else: + parent_id = story.tail p = Paragraph( - parent_id=story.tail, + parent_id=parent_id, story_id=story_id, text=text, author=session['username'] diff --git a/templates/story.html b/templates/story.html index 9a8beb4..a997e1c 100644 --- a/templates/story.html +++ b/templates/story.html @@ -38,18 +38,22 @@

{{story.title}}

-

{{paragraph_part[0].text}}


- {% for p in paragraph_part[1:] %} -

{{p.text}}

+ {% for p in paragraph_part %} +

+ {{p.text}} + {% if is_tree and p.id != tail %} + ← 回到这里 + {% endif %} +

{% endfor %}
  • 点击“续!”进行创作。
  • - {% if story.is_tree %} + {% if is_tree %}
  • 从第二天开始,在每天的10:00/16:00/22:00,对应故事线赞数总和最高且自身赞数不小于{{min_like_num}}的续写将入选,成为这个角色默认展示故事的一部分。
  • {% else %}
  • 在每天的10:00/16:00/22:00,赞数最高且不小于{{min_like_num}}的续写将入选,成为这个角色最终故事的一部分。如果没有,所有都会被保留进入下一时段。
  • @@ -61,7 +65,7 @@
-
+

备选后续

{% if sort_by == 'like' %} @@ -74,6 +78,9 @@ {% for p in pagination.items %}
No. {{p.id}} + {% if is_tree %} + ← 查看后续 + {% endif %}

{{p.text}}

{% for emoji, num, reacted, kind in p.reaction_status() %} @@ -109,6 +116,7 @@

续!

+
From ee59ed453fe5817b7cbef39b8ee7edd7dc9ad410 Mon Sep 17 00:00:00 2001 From: Tdxdxoz Date: Fri, 5 Nov 2021 23:07:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=A0=91=E5=BD=A2=E7=9A=84=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=9C=80=E9=AB=98=E8=B5=9E=E6=95=85=E4=BA=8B=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) 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):