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 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 @@

续!

+