Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notifications when a badge is awarded #3442

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default [
jQuery: 'readonly',
Clipboard: 'readonly',
Chart: 'readonly',
confetti: 'readonly',
NProgress: 'readonly',
diff_match_patch: 'readonly',
Highcharts: 'readonly',
Expand Down
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 59 additions & 31 deletions pontoon/base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

import bleach

from notifications.signals import notify

from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.urls import reverse
from django.utils import timezone

from pontoon.base import utils
Expand Down Expand Up @@ -91,6 +94,8 @@ class UserPermissionLogFormMixin:
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user")
super().__init__(*args, **kwargs)
# Track if user reached new level for Community Builder Badge
self.community_builder_level = 0

def assign_users_to_groups(self, group_name, users):
"""
Expand All @@ -102,42 +107,11 @@ def assign_users_to_groups(self, group_name, users):

group.user_set.clear()

before_count = self.user.badges_promotion_count
now = timezone.now()

if users:
group.user_set.add(*users)

log_group_members(self.user, group, (add_users, remove_users))

after_count = self.user.badges_promotion_count

# TODO:
# This code is the only consumer of the PermissionChangelog
# model, so we should refactor in the future to simplify
# how promotions are retrieved. (see #2195)

# Check if user was demoted from Manager to Translator
# In this case, it doesn't count as a promotion
if group_name == "managers":
removal = PermissionChangelog.objects.filter(
performed_by=self.user,
action_type=PermissionChangelog.ActionType.REMOVED,
created_at__gte=now,
)
if removal:
for item in removal:
if "managers" in item.group.name:
after_count -= 1

# Award Community Builder badge
if (
after_count > before_count
and after_count in settings.BADGES_PROMOTION_THRESHOLDS
):
# TODO: Send a notification to the user
pass


class LocalePermsForm(UserPermissionLogFormMixin, forms.ModelForm):
translators = forms.ModelMultipleChoiceField(
Expand All @@ -158,9 +132,63 @@ def save(self, *args, **kwargs):
translators = self.cleaned_data.get("translators", User.objects.none())
managers = self.cleaned_data.get("managers", User.objects.none())

before_count = self.user.badges_promotion_count

now = timezone.now()
self.assign_users_to_groups("translators", translators)
removal = PermissionChangelog.objects.filter(
performed_by=self.user,
action_type=PermissionChangelog.ActionType.REMOVED,
created_at__gte=now,
)
self.assign_users_to_groups("managers", managers)

after_count = self.user.badges_promotion_count

# Check if user was demoted from Manager to Translator
# In this case, it doesn't count as a promotion
#
# TODO:
# This code is the only consumer of the PermissionChangelog
# model, so we should refactor in the future to simplify
# how promotions are retrieved. (see #2195)

for item in removal:
if "managers" in item.group.name:
after_count -= 1

# Award Community Builder badge
if (
after_count > before_count
and after_count in settings.BADGES_PROMOTION_THRESHOLDS
):
self.community_builder_level = (
settings.BADGES_PROMOTION_THRESHOLDS.index(after_count) + 1
)
desc = """
You have gained a new badge level!
<br>
Community Builder Badge: Level {level}
<br>
You can view this badge on your <a href={profile_href}> profile page </a>.
""".format(
level=self.community_builder_level,
profile_href=reverse(
"pontoon.contributors.contributor.username",
kwargs={
"username": self.user.username,
},
),
)
notify.send(
sender=self.user,
recipient=self.user,
verb="", # Triggers render of description only
description=desc,
)

return self.community_builder_level


class ProjectLocalePermsForm(UserPermissionLogFormMixin, forms.ModelForm):
translators = forms.ModelMultipleChoiceField(
Expand Down
20 changes: 18 additions & 2 deletions pontoon/base/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,26 @@ def badges_review_count(self):
@property
def badges_promotion_count(self):
"""Role promotions performed by user that count towards their badges"""
return self.changed_permissions_log.filter(
added_logs = self.changed_permissions_log.filter(
action_type="added",
created_at__gte=settings.BADGES_START_DATE,
).count()
).order_by("created_at")

unmatched_added = []

# Don't count any 'added' logs that have a corresponding
# 'removed' log within the same group
for added in added_logs:
removed_exists = self.changed_permissions_log.filter(
action_type="removed",
created_at__gte=settings.BADGES_START_DATE,
group=added.group,
).exists()

if not removed_exists:
unmatched_added.append(added)

return len(unmatched_added)


@property
Expand Down
Loading