Recalculate topic community count if topic changed #338

This commit is contained in:
freamon 2024-10-07 02:21:24 +00:00
parent 5f42de3893
commit f73b428cc8
2 changed files with 21 additions and 8 deletions

View file

@ -662,6 +662,7 @@ def admin_communities_no_topic():
def admin_community_edit(community_id): def admin_community_edit(community_id):
form = EditCommunityForm() form = EditCommunityForm()
community = Community.query.get_or_404(community_id) community = Community.query.get_or_404(community_id)
old_topic_id = community.topic_id if community.topic_id else None
form.topic.choices = topics_for_form(0) form.topic.choices = topics_for_form(0)
form.languages.choices = languages_for_form() form.languages.choices = languages_for_form()
if form.validate_on_submit(): if form.validate_on_submit():
@ -708,11 +709,17 @@ def admin_community_edit(community_id):
community.languages.append(Language.query.get(language_choice)) community.languages.append(Language.query.get(language_choice))
# Always include the undetermined language, so posts with no language will be accepted # Always include the undetermined language, so posts with no language will be accepted
community.languages.append(Language.query.filter(Language.code == 'und').first()) community.languages.append(Language.query.filter(Language.code == 'und').first())
db.session.commit()
if community.topic_id != old_topic_id:
if community.topic_id:
community.topic.num_communities = community.topic.communities.count()
if old_topic_id:
topic = Topic.query.get(old_topic_id)
if topic:
topic.num_communities = topic.communities.count()
db.session.commit()
db.session.commit()
if community.topic_id:
community.topic.num_communities = community.topic.communities.count()
db.session.commit()
flash(_('Saved')) flash(_('Saved'))
return redirect(url_for('admin.admin_communities')) return redirect(url_for('admin.admin_communities'))
else: else:

View file

@ -929,6 +929,7 @@ def community_edit(community_id: int):
if current_user.banned: if current_user.banned:
return show_ban_message() return show_ban_message()
community = Community.query.get_or_404(community_id) community = Community.query.get_or_404(community_id)
old_topic_id = community.topic_id if community.topic_id else None
if community.is_owner() or current_user.is_admin(): if community.is_owner() or current_user.is_admin():
form = EditCommunityForm() form = EditCommunityForm()
form.topic.choices = topics_for_form(0) form.topic.choices = topics_for_form(0)
@ -970,11 +971,16 @@ def community_edit(community_id: int):
community.languages.append(Language.query.get(language_choice)) community.languages.append(Language.query.get(language_choice))
# Always include the undetermined language, so posts with no language will be accepted # Always include the undetermined language, so posts with no language will be accepted
community.languages.append(Language.query.filter(Language.code == 'und').first()) community.languages.append(Language.query.filter(Language.code == 'und').first())
db.session.commit()
db.session.commit() if community.topic_id != old_topic_id:
if community.topic: if community.topic_id:
community.topic.num_communities = community.topic.communities.count() community.topic.num_communities = community.topic.communities.count()
db.session.commit() if old_topic_id:
topic = Topic.query.get(old_topic_id)
if topic:
topic.num_communities = topic.communities.count()
db.session.commit()
flash(_('Saved')) flash(_('Saved'))
return redirect(url_for('activitypub.community_profile', actor=community.ap_id if community.ap_id is not None else community.name)) return redirect(url_for('activitypub.community_profile', actor=community.ap_id if community.ap_id is not None else community.name))
else: else: