Skip to content

Commit

Permalink
Merge pull request #162 from dimagi/ce/user-learn-progress
Browse files Browse the repository at this point in the history
user learn progress
  • Loading branch information
pxwxnvermx authored Oct 12, 2023
2 parents 800af1b + ea9fb1e commit 4f704e4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion commcare_connect/opportunity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Meta:
def learn_progress(self):
learn_modules = LearnModule.objects.filter(app=self.opportunity.learn_app)
completed_modules = CompletedModule.objects.filter(
opportunity=self.opportunity, module__in=learn_modules
opportunity=self.opportunity, module__in=learn_modules, user=self.user
).count()
percentage = (completed_modules / learn_modules.count()) * 100
return round(percentage, 2)
Expand Down
11 changes: 11 additions & 0 deletions commcare_connect/opportunity/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,14 @@ class OpportunityAccessFactory(DjangoModelFactory):

class Meta:
model = "opportunity.OpportunityAccess"


class CompletedModuleFactory(DjangoModelFactory):
opportunity = SubFactory(OpportunityFactory)
user = SubFactory("commcare_connect.users.tests.factories.UserFactory")
date = Faker("date")
module = SubFactory(LearnModuleFactory, app=SelfAttribute("..opportunity.learn_app"))
duration = Faker("time_delta")

class Meta:
model = "opportunity.CompletedModule"
12 changes: 12 additions & 0 deletions commcare_connect/opportunity/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest

from commcare_connect.opportunity.tests.factories import CompletedModuleFactory, OpportunityAccessFactory


@pytest.mark.django_db
def test_learn_progress():
module = CompletedModuleFactory()
access_1 = OpportunityAccessFactory(opportunity=module.opportunity, user=module.user)
access_2 = OpportunityAccessFactory(opportunity=module.opportunity)
assert access_1.learn_progress == 100
assert access_2.learn_progress == 0

0 comments on commit 4f704e4

Please sign in to comment.