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):
"""
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:
return cls.objects.filter(username=username, template_id=template_id).first()

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