Created
September 22, 2021 11:46
-
-
Save mathjazz/a4f0a2023cbb8e8ddebf665cd2325aa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/pontoon/base/models.py b/pontoon/base/models.py | |
index 128acf66f..04b1180ef 100644 | |
--- a/pontoon/base/models.py | |
+++ b/pontoon/base/models.py | |
@@ -439,6 +439,18 @@ class PermissionChangelog(models.Model): | |
) | |
+class AggregatedStatsQuerySet(models.QuerySet): | |
+ def aggregated_stats(self): | |
+ return self.aggregate( | |
+ total_strings=Sum("total_strings"), | |
+ approved_strings=Sum("approved_strings"), | |
+ fuzzy_strings=Sum("fuzzy_strings"), | |
+ strings_with_errors=Sum("strings_with_errors"), | |
+ strings_with_warnings=Sum("strings_with_warnings"), | |
+ unreviewed_strings=Sum("unreviewed_strings"), | |
+ ) | |
+ | |
+ | |
class AggregatedStats(models.Model): | |
total_strings = models.PositiveIntegerField(default=0) | |
approved_strings = models.PositiveIntegerField(default=0) | |
@@ -447,6 +459,8 @@ class AggregatedStats(models.Model): | |
strings_with_warnings = models.PositiveIntegerField(default=0) | |
unreviewed_strings = models.PositiveIntegerField(default=0) | |
+ objects = AggregatedStatsQuerySet.as_manager() | |
+ | |
class Meta: | |
abstract = True | |
@@ -1616,17 +1630,7 @@ class ExternalResource(models.Model): | |
return self.name | |
-class ProjectLocaleQuerySet(models.QuerySet): | |
- def aggregated_stats(self): | |
- return self.aggregate( | |
- total_strings=Sum("total_strings"), | |
- approved_strings=Sum("approved_strings"), | |
- fuzzy_strings=Sum("fuzzy_strings"), | |
- strings_with_errors=Sum("strings_with_errors"), | |
- strings_with_warnings=Sum("strings_with_warnings"), | |
- unreviewed_strings=Sum("unreviewed_strings"), | |
- ) | |
- | |
+class ProjectLocaleQuerySet(AggregatedStatsQuerySet): | |
def visible_for(self, user): | |
""" | |
Filter project locales by the visibility of their projects. | |
@@ -3506,26 +3510,16 @@ class TranslationMemoryEntry(models.Model): | |
objects = TranslationMemoryEntryQuerySet.as_manager() | |
-class TranslatedResourceQuerySet(models.QuerySet): | |
- def aggregated_stats(self): | |
- return self.aggregate( | |
- total=Sum("resource__total_strings"), | |
- approved=Sum("approved_strings"), | |
- fuzzy=Sum("fuzzy_strings"), | |
- errors=Sum("strings_with_errors"), | |
- warnings=Sum("strings_with_warnings"), | |
- unreviewed=Sum("unreviewed_strings"), | |
- ) | |
- | |
+class TranslatedResourceQuerySet(AggregatedStatsQuerySet): | |
def aggregate_stats(self, instance): | |
aggregated_stats = self.aggregated_stats() | |
- instance.total_strings = aggregated_stats["total"] or 0 | |
- instance.approved_strings = aggregated_stats["approved"] or 0 | |
- instance.fuzzy_strings = aggregated_stats["fuzzy"] or 0 | |
- instance.strings_with_errors = aggregated_stats["errors"] or 0 | |
- instance.strings_with_warnings = aggregated_stats["warnings"] or 0 | |
- instance.unreviewed_strings = aggregated_stats["unreviewed"] or 0 | |
+ instance.total_strings = aggregated_stats["total_strings"] or 0 | |
+ instance.approved_strings = aggregated_stats["approved_strings"] or 0 | |
+ instance.fuzzy_strings = aggregated_stats["fuzzy_strings"] or 0 | |
+ instance.strings_with_errors = aggregated_stats["strings_with_errors"] or 0 | |
+ instance.strings_with_warnings = aggregated_stats["strings_with_warnings"] or 0 | |
+ instance.unreviewed_strings = aggregated_stats["unreviewed_strings"] or 0 | |
instance.save( | |
update_fields=[ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment