Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions credentials/apps/badges/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,15 @@ def user_progress(self, username: str) -> float:
"""
Determines a completion progress for user.
"""
progress = BadgeProgress.for_user(username=username, template_id=self.id)
progress = BadgeProgress.for_user(
username=username,
template_id=self.id,
create=False
)

if not progress:
return 0.00

return progress.ratio

def is_completed(self, username: str) -> bool:
Expand Down Expand Up @@ -274,7 +282,15 @@ def is_group_fulfilled(cls, *, group: str, template: BadgeTemplate, username: st
Checks if the group is fulfilled.
"""

progress = BadgeProgress.for_user(username=username, template_id=template.id)
progress = BadgeProgress.for_user(
username=username,
template_id=template.id,
create=False
)

if not progress:
return False

requirements = cls.objects.filter(template=template, blend=group)
fulfilled_requirements = requirements.filter(fulfillments__progress=progress).count()

Expand Down Expand Up @@ -509,13 +525,16 @@ def __str__(self):
return f"BadgeProgress:{self.username}"

@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.

"""
Service shortcut.
"""

progress, __ = cls.objects.get_or_create(username=username, template_id=template_id)
return progress
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

return cls.objects.filter(username=username, template_id=template_id).first()

@property
def ratio(self) -> float:
Expand Down
Loading