show posts from child topics

This commit is contained in:
rimu 2024-11-08 15:09:24 +13:00
parent 70bb9ab68f
commit 0b5f515de6
5 changed files with 52 additions and 2 deletions

View file

@ -119,6 +119,7 @@ class EditTopicForm(FlaskForm):
name = StringField(_l('Name'), validators=[DataRequired()], render_kw={'title': _l('Human readable name for the topic.')}) name = StringField(_l('Name'), validators=[DataRequired()], render_kw={'title': _l('Human readable name for the topic.')})
machine_name = StringField(_l('Slug'), validators=[DataRequired()], render_kw={'title': _l('A short and unique identifier that becomes part of the URL.')}) machine_name = StringField(_l('Slug'), validators=[DataRequired()], render_kw={'title': _l('A short and unique identifier that becomes part of the URL.')})
parent_id = SelectField(_l('Parent topic'), coerce=int, validators=[Optional()], render_kw={'class': 'form-select'}) parent_id = SelectField(_l('Parent topic'), coerce=int, validators=[Optional()], render_kw={'class': 'form-select'})
show_posts_in_children = BooleanField(_l('Show posts from child topics'), validators=[Optional()])
submit = SubmitField(_l('Save')) submit = SubmitField(_l('Save'))

View file

@ -825,7 +825,8 @@ def admin_topic_add():
form = EditTopicForm() form = EditTopicForm()
form.parent_id.choices = topics_for_form(0) form.parent_id.choices = topics_for_form(0)
if form.validate_on_submit(): if form.validate_on_submit():
topic = Topic(name=form.name.data, machine_name=slugify(form.machine_name.data.strip()), num_communities=0) topic = Topic(name=form.name.data, machine_name=slugify(form.machine_name.data.strip()), num_communities=0,
show_posts_in_children=form.show_posts_in_children.data)
if form.parent_id.data: if form.parent_id.data:
topic.parent_id = form.parent_id.data topic.parent_id = form.parent_id.data
else: else:
@ -855,6 +856,7 @@ def admin_topic_edit(topic_id):
topic.name = form.name.data topic.name = form.name.data
topic.num_communities = topic.communities.count() topic.num_communities = topic.communities.count()
topic.machine_name = form.machine_name.data topic.machine_name = form.machine_name.data
topic.show_posts_in_children = form.show_posts_in_children.data
if form.parent_id.data: if form.parent_id.data:
topic.parent_id = form.parent_id.data topic.parent_id = form.parent_id.data
else: else:
@ -867,6 +869,7 @@ def admin_topic_edit(topic_id):
form.name.data = topic.name form.name.data = topic.name
form.machine_name.data = topic.machine_name form.machine_name.data = topic.machine_name
form.parent_id.data = topic.parent_id form.parent_id.data = topic.parent_id
form.show_posts_in_children.data = topic.show_posts_in_children
return render_template('admin/edit_topic.html', title=_('Edit topic'), form=form, topic=topic, return render_template('admin/edit_topic.html', title=_('Edit topic'), form=form, topic=topic,
moderating_communities=moderating_communities(current_user.get_id()), moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()),

View file

@ -371,6 +371,7 @@ class Topic(db.Model):
name = db.Column(db.String(50)) name = db.Column(db.String(50))
num_communities = db.Column(db.Integer, default=0) num_communities = db.Column(db.Integer, default=0)
parent_id = db.Column(db.Integer) parent_id = db.Column(db.Integer)
show_posts_in_children = db.Column(db.Boolean, default=False)
communities = db.relationship('Community', lazy='dynamic', backref='topic', cascade="all, delete-orphan") communities = db.relationship('Community', lazy='dynamic', backref='topic', cascade="all, delete-orphan")
def path(self): def path(self):

View file

@ -1,6 +1,7 @@
from collections import namedtuple from collections import namedtuple
from datetime import timedelta, timezone from datetime import timedelta, timezone
from random import randint from random import randint
from typing import List
from feedgen.feed import FeedGenerator from feedgen.feed import FeedGenerator
from flask import request, flash, json, url_for, current_app, redirect, abort, make_response, g from flask import request, flash, json, url_for, current_app, redirect, abort, make_response, g
@ -52,7 +53,12 @@ def show_topic(topic_path):
if current_topic: if current_topic:
# get posts from communities in that topic # get posts from communities in that topic
posts = Post.query.join(Community, Post.community_id == Community.id).filter(Community.topic_id == current_topic.id, Community.banned == False) if current_topic.show_posts_in_children: # include posts from child topics
topic_ids = get_all_child_topic_ids(current_topic)
else:
topic_ids = [current_topic.id]
posts = Post.query.join(Community, Post.community_id == Community.id).filter(Community.topic_id.in_(topic_ids),
Community.banned == False)
# filter out nsfw and nsfl if desired # filter out nsfw and nsfl if desired
if current_user.is_anonymous: if current_user.is_anonymous:
@ -273,6 +279,13 @@ def suggestion_denied():
return render_template('topic/suggestion_denied.html') return render_template('topic/suggestion_denied.html')
def get_all_child_topic_ids(topic: Topic) -> List[int]:
# recurse down the topic tree, gathering all the topic IDs found
topic_ids = [topic.id]
for child_topic in Topic.query.filter(Topic.parent_id == topic.id):
topic_ids.extend(get_all_child_topic_ids(child_topic))
return topic_ids
def topics_for_form(): def topics_for_form():
topics = Topic.query.filter_by(parent_id=None).order_by(Topic.name).all() topics = Topic.query.filter_by(parent_id=None).order_by(Topic.name).all()
result = [] result = []

View file

@ -0,0 +1,32 @@
"""topic: show_posts_in_children
Revision ID: a4debcf5ac6f
Revises: e23ce3d53def
Create Date: 2024-11-08 14:46:56.009352
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'a4debcf5ac6f'
down_revision = 'e23ce3d53def'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('topic', schema=None) as batch_op:
batch_op.add_column(sa.Column('show_posts_in_children', sa.Boolean(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('topic', schema=None) as batch_op:
batch_op.drop_column('show_posts_in_children')
# ### end Alembic commands ###