Skip to content

Commit

Permalink
Addressed backend review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmit Goswami committed Dec 19, 2024
1 parent 34e0a5e commit 1179abe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
20 changes: 20 additions & 0 deletions pontoon/base/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,24 @@ def badges_promotion_count(self):
)


@property
def badges_review_level(self):
thresholds = settings.BADGES_REVIEW_THRESHOLDS
for level in range(len(thresholds) - 1):
if thresholds[level] <= self.badges_review_count < thresholds[level + 1]:
return level + 1
return 0


@property
def badges_translation_level(self):
thresholds = settings.BADGES_TRANSLATION_THRESHOLDS
for level in range(len(thresholds) - 1):
if thresholds[level] <= self.badges_translation_count < thresholds[level + 1]:
return level + 1
return 0


@property
def top_contributed_locale(self):
"""Locale the user has made the most contributions to."""
Expand Down Expand Up @@ -523,6 +541,8 @@ def latest_action(self):
User.add_to_class("badges_translation_count", badges_translation_count)
User.add_to_class("badges_review_count", badges_review_count)
User.add_to_class("badges_promotion_count", badges_promotion_count)
User.add_to_class("badges_review_level", badges_review_level)
User.add_to_class("badges_translation_level", badges_translation_level)
User.add_to_class("has_approved_translations", has_approved_translations)
User.add_to_class("top_contributed_locale", top_contributed_locale)
User.add_to_class("can_translate", can_translate)
Expand Down
38 changes: 9 additions & 29 deletions pontoon/batch/actions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.conf import settings
from django.utils import timezone

from pontoon.actionlog.models import ActionLog
Expand All @@ -10,13 +9,6 @@
from pontoon.messaging.notifications import send_badge_notification


def _get_badge_level(thresholds, action_count):
for level in range(len(thresholds) - 1):
if thresholds[level] <= action_count < thresholds[level + 1]:
return level + 1
return 0


def batch_action_template(form, user, translations, locale):
"""Empty batch action, does nothing, only used for documentation.
Expand Down Expand Up @@ -78,9 +70,7 @@ def approve_translations(form, user, translations, locale):
locale,
)

before_level = _get_badge_level(
settings.BADGES_REVIEW_THRESHOLDS, user.badges_review_count
)
before_level = user.badges_review_level

# Log approving actions
actions_to_log = [
Expand All @@ -94,14 +84,12 @@ def approve_translations(form, user, translations, locale):
ActionLog.objects.bulk_create(actions_to_log)

# Send Review Master Badge notification information
after_level = _get_badge_level(
settings.BADGES_REVIEW_THRESHOLDS, user.badges_review_count
)
after_level = user.badges_review_level
badge_update = {}
if after_level > before_level:
badge_update["level"] = after_level
badge_update["name"] = "Review Master Badge"
send_badge_notification(user, badge_update["name"], badge_update["level"])
send_badge_notification(user, "Review Master Badge", after_level)

# Approve translations.
translations.update(
Expand Down Expand Up @@ -148,9 +136,7 @@ def reject_translations(form, user, translations, locale):
)
TranslationMemoryEntry.objects.filter(translation__in=suggestions).delete()

before_level = _get_badge_level(
settings.BADGES_REVIEW_THRESHOLDS, user.badges_review_count
)
before_level = user.badges_review_level

# Log rejecting actions
actions_to_log = [
Expand All @@ -164,14 +150,12 @@ def reject_translations(form, user, translations, locale):
ActionLog.objects.bulk_create(actions_to_log)

# Send Review Master Badge notification information
after_level = _get_badge_level(
settings.BADGES_REVIEW_THRESHOLDS, user.badges_review_count
)
after_level = user.badges_review_level
badge_update = {}
if after_level > before_level:
badge_update["level"] = after_level
badge_update["name"] = "Review Master Badge"
send_badge_notification(user, badge_update["name"], badge_update["level"])
send_badge_notification(user, "Review Master Badge", after_level)

# Reject translations.
suggestions.update(
Expand Down Expand Up @@ -255,9 +239,7 @@ def replace_translations(form, user, translations, locale):
translations_to_create,
)

before_level = _get_badge_level(
settings.BADGES_TRANSLATION_THRESHOLDS, user.badges_translation_count
)
before_level = user.badges_translation_level

# Log creating actions
actions_to_log = [
Expand All @@ -271,14 +253,12 @@ def replace_translations(form, user, translations, locale):
ActionLog.objects.bulk_create(actions_to_log)

# Send Translation Champion Badge notification information
after_level = _get_badge_level(
settings.BADGES_TRANSLATION_THRESHOLDS, user.badges_translation_count
)
after_level = user.badges_translation_level
badge_update = {}
if after_level > before_level:
badge_update["level"] = after_level
badge_update["name"] = "Translation Champion Badge"
send_badge_notification(user, badge_update["name"], badge_update["level"])
send_badge_notification(user, "Translation Champion Badge", after_level)

changed_translation_pks = [c.pk for c in changed_translations]

Expand Down

0 comments on commit 1179abe

Please sign in to comment.