Skip to content

Commit

Permalink
Changed query structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmit Goswami authored and Harmit Goswami committed Dec 6, 2024
1 parent 26db4e6 commit a39fc0f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
35 changes: 14 additions & 21 deletions pontoon/base/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.db.models import Count, Exists, OuterRef
from django.db.models import Count, Exists, OuterRef, Q
from django.urls import reverse
from django.utils import timezone

Expand Down Expand Up @@ -235,29 +235,22 @@ def badges_translation_count(self):
@property
def badges_review_count(self):
"""Translation reviews provided by user that count towards their badges."""
approved_reviews = self.actions.filter(
action_type="translation:approved",
created_at__gte=settings.BADGES_START_DATE,
)

rejected_reviews = self.actions.filter(
action_type="translation:rejected",
created_at__gte=settings.BADGES_START_DATE,
)

# Exclude auto-rejections caused by creating a new translation or approving an existing one
rejected_reviews = rejected_reviews.exclude(
Exists(
self.actions.filter(
performed_by=OuterRef("performed_by"),
action_type__in=["translation:created", "translation:approved"],
created_at__gt=OuterRef("created_at"),
created_at__lte=OuterRef("created_at") + timedelta(milliseconds=100),
)
)
closely_preceded_action = ActionLog.objects.filter(
performed_by=OuterRef("performed_by"),
action_type__in=["translation:created", "translation:approved"],
created_at__gt=OuterRef("created_at"),
created_at__lte=OuterRef("created_at") + timedelta(milliseconds=100),
)

return approved_reviews.count() + rejected_reviews.count()
return self.actions.filter(
Q(action_type="translation:approved")
| Q(
~Exists(closely_preceded_action),
action_type="translation:rejected",
),
created_at__gte=settings.BADGES_START_DATE,
).count()


@property
Expand Down
2 changes: 1 addition & 1 deletion pontoon/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ def _default_from_email():
SECURE_SSL_REDIRECT = not (DEBUG or os.environ.get("CI", False))

# Content-Security-Policy headers
# 'blob:' is needed for confetti.browser.js
CSP_DEFAULT_SRC = ("'none'",)
CSP_FRAME_SRC = ("https:",)
CSP_WORKER_SRC = ("https:",) + ("blob:",)
Expand Down Expand Up @@ -941,7 +942,6 @@ def _default_from_email():
)

# Needed if site not hosted on HTTPS domains (like local setup)
# 'blob:' is needed for confetti.browser.js
if not (HEROKU_DEMO or SITE_URL.startswith("https")):
CSP_IMG_SRC = CSP_IMG_SRC + ("http://www.gravatar.com/avatar/",)
CSP_WORKER_SRC = CSP_FRAME_SRC = CSP_FRAME_SRC + ("http:",)
Expand Down

0 comments on commit a39fc0f

Please sign in to comment.