Skip to content

feat: Reorder highlight items on favorite action#1133

Open
chethan2106 wants to merge 1 commit into
openedx:masterfrom
chethan2106:chethan/ENT-10610
Open

feat: Reorder highlight items on favorite action#1133
chethan2106 wants to merge 1 commit into
openedx:masterfrom
chethan2106:chethan/ENT-10610

Conversation

@chethan2106
Copy link
Copy Markdown

Description

This PR implements stable favorite ordering behavior for highlight set items so that favorites maintain deterministic insertion order and ranking updates correctly when items are unfavorited.

Related Jira: ENT-10610


Overview

This change updates highlight favorite toggling logic to enforce stable sort behavior for favorited items:

  • Favoriting an item places it just below the current lowest-ranked favorite (or at rank 0 if no favorites exist).
  • Unfavoriting an item compacts subsequent favorite sort orders so there are no gaps.
  • Retrieval now consistently returns favorites first, ordered by favorite rank, then by creation order for non-favorites.

Changes Made

  • Updated favorite toggle behavior in highlights.py
  • Added transactional and locking safeguards for concurrent favorite updates in highlights.py
  • Improved highlighted content query ordering and prefetch efficiency in highlights.py
  • Updated serializer ordering fallback to match new backend ordering in serializers.py
  • Added/expanded tests for boolean handling and favorite reordering behavior in test_curation_views.py

Implementation Details

Favorite Toggle Logic

File: highlights.py

  • Validates request inputs up front:

    • Missing content_uuid
    • Missing favorite parameter (supports explicit boolean false)
    • Invalid UUID values
  • Uses transaction.atomic with row-level locks via select_for_update for consistency.

On favorite = true:

  • Finds current max sort_order
  • Assigns next position (max + 1), or 0 if this is the first favorite

On favorite = false:

  • Resets unfavorited item:

    • sort_order = 0
    • is_favorite = false
  • Decrements sort_order for favorites ranked below the removed item

  • Ensures stable behavior under concurrent requests


Retrieval Ordering

Files:

  • highlights.py

  • serializers.py

  • Added shared ordering tuple:

    1. Favorites first
    2. Then sort_order
    3. Then created
  • Prefetches highlighted content using ordered queryset in the view layer

  • Serializer:

    • Uses prefetched data when available
    • Applies same ordering when not prefetched

Tests Added / Updated

File: test_curation_views.py

  • Verifies favorite accepts boolean false
  • Verifies favoriting multiple items assigns stable ascending sort_order
  • Verifies API response returns favorites first in correct order
  • Verifies unfavoriting compacts remaining favorite sort_order correctly

Additional CI Stability Updates Included

  • Date-robust test fixture updates in factories.py
  • Matching test expectation updates in test_tasks.py
  • Lint suppression cleanup and model/migration alignment in models.py

Testing

All mandatory checks passed locally:

make test
make quality
make validate
python manage.py makemigrations --check --dry-run
make check_keywords

Acceptance Criteria (ENT-10610)

  • Upon favoriting a highlight item, it moves just below the current lowest-ranked favorite
  • If no favorites exist, newly favorited item is placed at top favorite rank
  • Upon unfavoriting, subsequent favorite sort orders are adjusted to remove gaps
  • Favorite ordering is stable and consistently reflected in highlight set responses

Merge Checklist

  • No new requirements added
  • No new API endpoint introduced
  • No new data model introduced
  • No new migration required
  • Tests updated to cover favorite reorder behavior
  • Mandatory validation and quality checks passing
image image image

Sample Response:
Sort Favourites.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant