diff --git a/credentials/apps/badges/admin.py b/credentials/apps/badges/admin.py index 2c88a64e5..7bb84a8ce 100644 --- a/credentials/apps/badges/admin.py +++ b/credentials/apps/badges/admin.py @@ -36,7 +36,12 @@ class BadgeRequirementInline(admin.TabularInline): model = BadgeRequirement show_change_link = True extra = 0 - fields = ("event_type", "rules", "description") + fields = ( + "event_type", + "rules", + "description", + "group", + ) readonly_fields = ("rules",) def rules(self, obj): diff --git a/credentials/apps/badges/migrations/0018_alter_badgerequirement_group.py b/credentials/apps/badges/migrations/0018_alter_badgerequirement_group.py new file mode 100644 index 000000000..9a23b3d38 --- /dev/null +++ b/credentials/apps/badges/migrations/0018_alter_badgerequirement_group.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.20 on 2024-04-24 09:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('badges', '0017_remove_badgeprogress_user_credential'), + ] + + operations = [ + migrations.AlterField( + model_name='badgerequirement', + name='group', + field=models.CharField(blank=True, help_text='Optional. Put requirements into the same arbitrary Group ID to make them interchangeable (OR processing logic applies).', max_length=255, null=True), + ), + ] diff --git a/credentials/apps/badges/models.py b/credentials/apps/badges/models.py index 54c02eeb0..4f4da940d 100644 --- a/credentials/apps/badges/models.py +++ b/credentials/apps/badges/models.py @@ -6,9 +6,7 @@ import operator import uuid -from attrs import asdict from django.conf import settings -from django.core.exceptions import ValidationError from django.db import models from django.utils.translation import gettext_lazy as _ from django_extensions.db.models import TimeStampedModel @@ -168,7 +166,7 @@ class BadgeRequirement(models.Model): null=True, blank=True, help_text=_( - "Optional. Requirements with identical Group ID become interchangeable (OR processing logic applies)." + "Optional. Put requirements into the same arbitrary Group ID to make them interchangeable (OR processing logic applies)." ), ) diff --git a/credentials/apps/badges/processing/progression.py b/credentials/apps/badges/processing/progression.py index 6d0be37e0..9cd3e8912 100644 --- a/credentials/apps/badges/processing/progression.py +++ b/credentials/apps/badges/processing/progression.py @@ -20,7 +20,6 @@ def discover_requirements(event_type: str) -> List[BadgeRequirement]: return BadgeRequirement.objects.filter(event_type=event_type, template__is_active=True) - def process_requirements(event_type, username, payload): """ Finds all relevant requirements, tests them one by one, marks as completed if needed. diff --git a/credentials/apps/badges/processing/regression.py b/credentials/apps/badges/processing/regression.py index a3005f56a..121fba028 100644 --- a/credentials/apps/badges/processing/regression.py +++ b/credentials/apps/badges/processing/regression.py @@ -16,7 +16,7 @@ def discover_penalties(event_type: str) -> List[BadgePenalty]: """ Picks all relevant penalties based on the event type. """ - + return BadgePenalty.objects.filter(event_type=event_type, template__is_active=True) diff --git a/credentials/apps/badges/tests/test_services.py b/credentials/apps/badges/tests/test_services.py index 5c51ac00d..3a05edc53 100644 --- a/credentials/apps/badges/tests/test_services.py +++ b/credentials/apps/badges/tests/test_services.py @@ -255,12 +255,8 @@ def setUp(self): site=self.site, is_active=True, ) - self.COURSE_PASSING_EVENT = ( - "org.openedx.learning.course.passing.status.updated.v1" - ) - self.CCX_COURSE_PASSING_EVENT = ( - "org.openedx.learning.ccx.course.passing.status.updated.v1" - ) + self.COURSE_PASSING_EVENT = "org.openedx.learning.course.passing.status.updated.v1" + self.CCX_COURSE_PASSING_EVENT = "org.openedx.learning.ccx.course.passing.status.updated.v1" # test cases # A course completion - course A w/o a group; @@ -317,15 +313,9 @@ def test_course_a_or_b_completion(self): "course": {"display_name": "A"}, } process_requirements(self.COURSE_PASSING_EVENT, "test_username", kwargs) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_a).count(), 1 - ) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_b).count(), 0 - ) - self.assertTrue( - BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed - ) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_a).count(), 1) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_b).count(), 0) + self.assertTrue(BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed) def test_course_a_or_b_or_c_completion(self): requirement_a = BadgeRequirement.objects.create( @@ -368,18 +358,10 @@ def test_course_a_or_b_or_c_completion(self): "course": {"display_name": "A"}, } process_requirements(self.COURSE_PASSING_EVENT, "test_username", kwargs) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_a).count(), 1 - ) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_b).count(), 0 - ) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_c).count(), 0 - ) - self.assertTrue( - BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed - ) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_a).count(), 1) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_b).count(), 0) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_c).count(), 0) + self.assertTrue(BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed) def test_course_a_or_completion(self): requirement = BadgeRequirement.objects.create( @@ -399,9 +381,7 @@ def test_course_a_or_completion(self): } process_requirements(self.COURSE_PASSING_EVENT, "test_username", kwargs) self.assertEqual(Fulfillment.objects.filter(requirement=requirement).count(), 1) - self.assertTrue( - BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed - ) + self.assertTrue(BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed) def test_course_a_or_b_and_c_completion(self): requirement_a = BadgeRequirement.objects.create( @@ -443,18 +423,10 @@ def test_course_a_or_b_and_c_completion(self): "course": {"display_name": "A"}, } process_requirements(self.COURSE_PASSING_EVENT, "test_username", kwargs) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_a).count(), 1 - ) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_b).count(), 0 - ) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_c).count(), 1 - ) - self.assertTrue( - BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed - ) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_a).count(), 1) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_b).count(), 0) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_c).count(), 1) + self.assertTrue(BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed) def test_course_a_or_b_and_c_or_d_completion(self): requirement_a = BadgeRequirement.objects.create( @@ -509,18 +481,8 @@ def test_course_a_or_b_and_c_or_d_completion(self): "course": {"display_name": "A"}, } process_requirements(self.COURSE_PASSING_EVENT, "test_username", kwargs) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_a).count(), 1 - ) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_b).count(), 0 - ) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_c).count(), 1 - ) - self.assertEqual( - Fulfillment.objects.filter(requirement=requirement_d).count(), 0 - ) - self.assertTrue( - BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed - ) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_a).count(), 1) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_b).count(), 0) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_c).count(), 1) + self.assertEqual(Fulfillment.objects.filter(requirement=requirement_d).count(), 0) + self.assertTrue(BadgeProgress.for_user(username="test_username", template_id=self.badge_template.id).completed)