Skip to content

refactor(badges): prevent orphaned BadgeProgress creation#2989

Open
luisfelipec95 wants to merge 4 commits into
openedx:masterfrom
Pearson-Advance:lfc/prevent-orphan-badge-progress
Open

refactor(badges): prevent orphaned BadgeProgress creation#2989
luisfelipec95 wants to merge 4 commits into
openedx:masterfrom
Pearson-Advance:lfc/prevent-orphan-badge-progress

Conversation

@luisfelipec95

Copy link
Copy Markdown
Contributor

Description

The badge processing system is creating an excessive number of orphaned BadgeProgress records (records without associated Fulfillment data) due to a critical logic flaw. With 140+ active badge templates, this issue is significantly amplified, creating thousands of unnecessary database records daily.

Changes

An optional create parameter (default True) was added to the for_user() method. This parameter controls whether the method should create a new BadgeProgress record when it doesn't exist, or simply retrieve it if it already exists.

Previously, the method always created a record using get_or_create(), with no option to just query. This meant any code that called this method, even just to check if a record existed, would end up automatically creating one.

How to test

  1. Configure Badge Template:

    • Navigate to the badge template admin panel
    • Enable "Is active" checkbox
    • In Badge requirements section, add an EVENT_TYPE:
      • org.openedx.learning.course.passing.status.updated.v1
      • org.openedx.learning.ccx.course.passing.status.updated.v1
  2. Create Badge Requirements:

    • Go to Badge requirement admin panel
    • Add Data rules for the requirement (e.g., is_passing = true)
  3. Create a test course in Studio:

    • Create a new course with grading enabled
    • Add a subsection with grading enabled
    • Add a problem worth points that leads to passing grade
    • Enroll and complete course:
  4. Enroll test student in course

    • Answer problems to meet passing grade
    • Verify "Passing" status is displayed

Before the changes

At the time of testing, there are 8 active badge templates, of which only 4 meet the rules for a completed course, yet 8 badgeProgress records are generated.

image

After the changes

It only creates in badgeProgress the necessary records—those that meet the badge template rules.

image

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Jun 1, 2026
@openedx-webhooks

openedx-webhooks commented Jun 1, 2026

Copy link
Copy Markdown

Thanks for the pull request, @luisfelipec95!

This repository is currently maintained by @jajjibhai008.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@GlugovGrGlib GlugovGrGlib left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @luisfelipec95, the solution is clear and straightforward.
I left a couple of suggestion for the method.

Before it can be merged this method also should be covered with test cases for new params/return combinations.
Also, please check CI lint failures. I think there are several whitespace issues.

Comment thread credentials/apps/badges/models.py Outdated

@classmethod
def for_user(cls, *, username, template_id):
def for_user(cls, *, username, template_id, create=True):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I agree with this approach. But I suggest set create=False by default and pass True explicitly in signals handlers https://github.com/openedx/credentials/blob/master/credentials/apps/badges/signals/handlers.py#L53.

I also optionally propose renaming this parameter to create_if_absent, so it's a little more specific.

Also, please update a docstring and type annotations for this method with new implementation and contract, as the current docstring not really useful at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I agree

Comment thread credentials/apps/badges/models.py Outdated
if create:
progress, __ = cls.objects.get_or_create(username=username, template_id=template_id)
return progress
else:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This else block can be removed as per linter recommendation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@luisfelipec95 luisfelipec95 marked this pull request as ready for review June 19, 2026 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: No status
Status: Waiting on Author

Development

Successfully merging this pull request may close these issues.

4 participants