-
Notifications
You must be signed in to change notification settings - Fork 31
feat: propagate categories from parent/child questions to conditional posts #4525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SylvainChevalier
wants to merge
3
commits into
main
Choose a base branch
from
claude/issue-4524-conditionals-should-have-all-the
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
28707be
feat: propagate categories from parent/child questions to conditional…
github-actions[bot] ba57d01
fix: resolve ruff formatting/linting and test collection errors
github-actions[bot] 9bd63cd
fix: re-sync conditional categories on any update_post call
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| from django.db import migrations | ||
|
|
||
|
|
||
| def backfill_conditional_categories(apps, schema_editor): | ||
| """ | ||
| Populate categories on conditional posts from their condition | ||
| and condition_child posts. | ||
| """ | ||
| Post = apps.get_model("posts", "Post") | ||
|
|
||
| category_type = "category" | ||
|
|
||
| conditional_posts = Post.objects.filter( | ||
| conditional__isnull=False, | ||
| ).select_related( | ||
| "conditional__condition", | ||
| "conditional__condition_child", | ||
| ) | ||
|
|
||
| for post in conditional_posts: | ||
| conditional = post.conditional | ||
| categories_to_add = set() | ||
|
|
||
| # Get categories from condition's post | ||
| condition_post_id = conditional.condition.post_id | ||
| if condition_post_id: | ||
| condition_post = Post.objects.get(pk=condition_post_id) | ||
| categories_to_add.update( | ||
| condition_post.projects.filter(type=category_type) | ||
| ) | ||
|
|
||
| # Get categories from condition_child's post | ||
| child_post_id = conditional.condition_child.post_id | ||
| if child_post_id: | ||
| child_post = Post.objects.get(pk=child_post_id) | ||
| categories_to_add.update( | ||
| child_post.projects.filter(type=category_type) | ||
| ) | ||
|
|
||
| if categories_to_add: | ||
| existing = set(post.projects.filter(type=category_type)) | ||
| new_categories = categories_to_add - existing | ||
| if new_categories: | ||
| post.projects.add(*new_categories) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("posts", "0029_remove_notebook_markdown_summary_and_more"), | ||
| ("projects", "0021_projectindex_project_index_projectindexpost"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| backfill_conditional_categories, | ||
| migrations.RunPython.noop, | ||
| ), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
tests/unit/test_posts/test_services/test_conditional_categories.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| from posts.services.common import ( | ||
| get_conditional_categories, | ||
| sync_conditional_categories, | ||
| update_post, | ||
| ) | ||
| from projects.models import Project | ||
| from questions.models import Question | ||
| from tests.unit.test_posts.factories import factory_post | ||
| from tests.unit.test_projects.factories import factory_project | ||
| from tests.unit.test_questions.factories import create_conditional, create_question | ||
|
|
||
|
|
||
| class TestConditionalCategoryPropagation: | ||
| def _make_conditional_setup(self): | ||
| """Create condition and child questions with their own posts and categories.""" | ||
| cat_a = factory_project(type=Project.ProjectTypes.CATEGORY, name="Category A") | ||
| cat_b = factory_project(type=Project.ProjectTypes.CATEGORY, name="Category B") | ||
| cat_c = factory_project(type=Project.ProjectTypes.CATEGORY, name="Category C") | ||
|
|
||
| condition = create_question( | ||
| question_type=Question.QuestionType.BINARY, | ||
| ) | ||
| condition_child = create_question( | ||
| question_type=Question.QuestionType.BINARY, | ||
| ) | ||
|
|
||
| factory_post( | ||
| question=condition, | ||
| projects=[cat_a, cat_b], | ||
| ) | ||
| factory_post( | ||
| question=condition_child, | ||
| projects=[cat_b, cat_c], | ||
| ) | ||
|
|
||
| conditional = create_conditional( | ||
| condition=condition, | ||
| condition_child=condition_child, | ||
| question_yes=create_question( | ||
| question_type=Question.QuestionType.BINARY, | ||
| title="If Yes", | ||
| ), | ||
| question_no=create_question( | ||
| question_type=Question.QuestionType.BINARY, | ||
| title="If No", | ||
| ), | ||
| ) | ||
|
|
||
| return conditional, cat_a, cat_b, cat_c | ||
|
|
||
| def test_get_conditional_categories(self): | ||
| conditional, cat_a, cat_b, cat_c = self._make_conditional_setup() | ||
|
|
||
| categories = get_conditional_categories(conditional) | ||
| category_ids = {c.id for c in categories} | ||
|
|
||
| assert cat_a.id in category_ids | ||
| assert cat_b.id in category_ids | ||
| assert cat_c.id in category_ids | ||
|
|
||
| def test_sync_conditional_categories(self): | ||
| conditional, cat_a, cat_b, cat_c = self._make_conditional_setup() | ||
|
|
||
| post = factory_post(conditional=conditional) | ||
| sync_conditional_categories(post) | ||
|
|
||
| post_categories = set(post.projects.filter(type=Project.ProjectTypes.CATEGORY)) | ||
| assert cat_a in post_categories | ||
| assert cat_b in post_categories | ||
| assert cat_c in post_categories | ||
|
|
||
| def test_sync_conditional_categories_no_duplicates(self): | ||
| conditional, cat_a, cat_b, cat_c = self._make_conditional_setup() | ||
|
|
||
| post = factory_post(conditional=conditional, projects=[cat_a]) | ||
| sync_conditional_categories(post) | ||
|
|
||
| cat_a_count = post.projects.filter( | ||
| type=Project.ProjectTypes.CATEGORY, id=cat_a.id | ||
| ).count() | ||
| assert cat_a_count == 1 | ||
|
|
||
| def test_sync_noop_for_non_conditional(self): | ||
| question = create_question( | ||
| question_type=Question.QuestionType.BINARY, | ||
| ) | ||
| post = factory_post(question=question) | ||
|
|
||
| # Should not raise | ||
| sync_conditional_categories(post) | ||
|
|
||
| def test_update_post_categories_preserves_inherited(self, mocker): | ||
| """ | ||
| Regression: calling update_post(..., categories=[...]) on a conditional | ||
| post must not drop categories inherited from parent/child questions, | ||
| even when no `conditional` payload is passed. | ||
| """ | ||
| # Avoid downstream side effects that aren't relevant to this test | ||
| mocker.patch("posts.tasks.run_post_indexing.send") | ||
|
|
||
| conditional, cat_a, cat_b, cat_c = self._make_conditional_setup() | ||
| post = factory_post(conditional=conditional) | ||
| sync_conditional_categories(post) | ||
|
|
||
| # Sanity check: inherited categories are present before the update | ||
| initial_categories = set( | ||
| post.projects.filter(type=Project.ProjectTypes.CATEGORY) | ||
| ) | ||
| assert {cat_a, cat_b, cat_c} <= initial_categories | ||
|
|
||
| # User edits the post with only cat_a in the categories payload | ||
| update_post(post, categories=[cat_a]) | ||
|
|
||
| post_categories = set( | ||
| post.projects.filter(type=Project.ProjectTypes.CATEGORY) | ||
| ) | ||
| # Inherited categories must still be present | ||
| assert cat_a in post_categories | ||
| assert cat_b in post_categories | ||
| assert cat_c in post_categories | ||
|
|
||
| # And cat_a should not have been duplicated | ||
| cat_a_count = post.projects.filter( | ||
| type=Project.ProjectTypes.CATEGORY, id=cat_a.id | ||
| ).count() | ||
| assert cat_a_count == 1 | ||
|
|
||
| def test_get_conditional_categories_missing_posts(self): | ||
| """Categories should be collected even if one parent has no post.""" | ||
| condition = create_question( | ||
| question_type=Question.QuestionType.BINARY, | ||
| ) | ||
| condition_child = create_question( | ||
| question_type=Question.QuestionType.BINARY, | ||
| ) | ||
|
|
||
| cat = factory_project(type=Project.ProjectTypes.CATEGORY, name="Only Cat") | ||
| factory_post(question=condition, projects=[cat]) | ||
| # condition_child has no post (post_id is None) | ||
|
|
||
| conditional = create_conditional( | ||
| condition=condition, | ||
| condition_child=condition_child, | ||
| question_yes=create_question( | ||
| question_type=Question.QuestionType.BINARY, | ||
| title="If Yes", | ||
| ), | ||
| question_no=create_question( | ||
| question_type=Question.QuestionType.BINARY, | ||
| title="If No", | ||
| ), | ||
| ) | ||
|
|
||
| categories = get_conditional_categories(conditional) | ||
| assert len(categories) == 1 | ||
| assert categories[0].id == cat.id |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: Metaculus/metaculus
Length of output: 2931
🏁 Script executed:
Repository: Metaculus/metaculus
Length of output: 2289
🏁 Script executed:
Repository: Metaculus/metaculus
Length of output: 45
🏁 Script executed:
Repository: Metaculus/metaculus
Length of output: 381
🏁 Script executed:
Repository: Metaculus/metaculus
Length of output: 2778
Don't rely on
.post_idin this migration.conditional.conditionandconditional.condition_childareQuestionobjects (via ForeignKey). ThePostmodel has a OneToOneField toQuestionwithrelated_name="+", which disables the reverse accessor.Questionobjects have no.post_idattribute, so this migration will raiseAttributeErrorat runtime. Query the historicalPostmodel using the Question ID instead.Safer migration pattern
🤖 Prompt for AI Agents