We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Top scorers list includes zero score users
The top scorer list in e.g. `/af/?details` includes a number of users with zero score.

I'm doubtful that these contributed in last 30 days.  So they shouldn't be on the list at all.

![screen shot 2017-05-17 at 10 37 08](https://cloud.githubusercontent.com/assets/647438/26147888/d6e7c214-3aec-11e7-8db8-e847a4e8335a.png)

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pootle/apps/pootle_score/utils.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) Pootle contributors.
4 #
5 # This file is a part of the Pootle project. It is distributed under the GPL3
6 # or later license. See the LICENSE file for a copy of the license and the
7 # AUTHORS file for copyright and authorship information.
8 
9 from datetime import date, datetime, timedelta
10 
11 import pytz
12 
13 from django.contrib.auth import get_user_model
14 from django.db.models import Sum
15 from django.utils.functional import cached_property
16 
17 from pootle.core.decorators import persistent_property
18 from pootle.core.delegate import display, revision, scores
19 from pootle.core.utils.timezone import localdate, make_aware
20 from pootle_app.models import Directory
21 from pootle_language.models import Language
22 
23 from .apps import PootleScoreConfig
24 from .models import UserTPScore
25 
26 
27 User = get_user_model()
28 
29 
30 def to_datetime(possible_dt):
31     if possible_dt is None:
32         return
33     if isinstance(possible_dt, datetime):
34         return possible_dt
35     if isinstance(possible_dt, date):
36         return make_aware(
37             datetime.combine(
38                 possible_dt,
39                 datetime.min.time())).astimezone(
40                     pytz.timezone("UTC"))
41 
42 
43 class Scores(object):
44     ns = "pootle.score"
45     sw_version = PootleScoreConfig.version
46 
47     def __init__(self, context):
48         self.context = context
49 
50     @property
51     def revision(self):
52         return revision.get(Directory)(
53             self.context.directory).get(key="stats")
54 
55     @property
56     def score_model(self):
57         return UserTPScore.objects.exclude(
58             user__username__in=User.objects.META_USERS)
59 
60     def get_daterange(self, days):
61         now = localdate()
62         return now - timedelta(days), now
63 
64     def scores_within_days(self, days):
65         return self.score_model.filter(
66             date__range=self.get_daterange(days))
67 
68     def get_scores(self, days):
69         return self.filter_scores(self.scores_within_days(days))
70 
71     def get_top_scorers(self, days=30):
72         """Returns users with the top scores.
73 
74         :param days: period of days to account for scores.
75         """
76         return self.get_scores(days).order_by("user__username").values(
77             "user__username", "user__email", "user__full_name").annotate(
78                 Sum("score"),
79                 Sum("suggested"),
80                 Sum("reviewed"),
81                 Sum("translated")).order_by("-score__sum")
82 
83     def filter_scores(self, qs):
84         return qs
85 
86     @persistent_property
87     def top_scorers(self):
88         return tuple(self.get_top_scorers())
89 
90     def display(self, offset=0, limit=5, language=None, formatter=None):
91         scorers = self.top_scorers
92         if offset or limit:
93             scorers = list(scorers)
94         if offset:
95             scorers = scorers[offset:]
96         if limit:
97             scorers = scorers[:limit]
98         return display.get(Scores)(
99             top_scores=scorers,
100             formatter=formatter,
101             language=language)
102 
103 
104 class LanguageScores(Scores):
105     ns = "pootle.score.language"
106 
107     @cached_property
108     def cache_key(self):
109         return (
110             "%s.%s.%s"
111             % (self.context.code,
112                localdate(),
113                self.revision))
114 
115     def filter_scores(self, qs):
116         return qs.filter(tp__language_id=self.context.id)
117 
118 
119 class ProjectScores(Scores):
120     ns = "pootle.score.project"
121 
122     @cached_property
123     def cache_key(self):
124         return (
125             "%s.%s.%s"
126             % (self.context.code,
127                localdate(),
128                self.revision))
129 
130     def filter_scores(self, qs):
131         return qs.filter(tp__project_id=self.context.id)
132 
133 
134 class ProjectSetScores(Scores):
135     ns = "pootle.score.projects"
136 
137     @cached_property
138     def cache_key(self):
139         return (
140             "%s.%s"
141             % (localdate(),
142                self.revision))
143 
144 
145 class TPScores(Scores):
146     ns = "pootle.score.tp"
147 
148     @cached_property
149     def cache_key(self):
150         return (
151             "%s/%s.%s.%s"
152             % (self.context.language.code,
153                self.context.project.code,
154                localdate(),
155                self.revision))
156 
157     def filter_scores(self, qs):
158         return qs.filter(tp_id=self.context.id)
159 
160 
161 class UserScores(Scores):
162     ns = "pootle.score.user"
163 
164     @cached_property
165     def cache_key(self):
166         return (
167             "%s.%s.%s"
168             % (self.context.id,
169                localdate(),
170                self.revision))
171 
172     @property
173     def revision(self):
174         return revision.get(Directory)(
175             Directory.objects.projects).get(key="stats")
176 
177     @property
178     def score_model(self):
179         return self.context.scores
180 
181     @property
182     def public_score(self):
183         return self.context.public_score
184 
185     @persistent_property
186     def top_language(self):
187         return self.get_top_language()
188 
189     def get_top_language_within(self, days):
190         top_lang = self.get_scores_by_language(
191             days).order_by("score__sum").first()
192         if top_lang:
193             return Language.objects.get(id=top_lang["tp__language"])
194 
195     def get_scores_by_language(self, days):
196         """Languages that the user has contributed to in the last `days`,
197         and the summary score
198         """
199         return self.get_scores(days).order_by(
200             "tp__language").values("tp__language").annotate(Sum("score"))
201 
202     def get_language_top_scores(self, language):
203         return scores.get(language.__class__)(language).top_scorers
204 
205     def get_top_language(self, days=30):
206         """Returns the top language the user has contributed to and its
207         position.
208 
209         "Top language" is defined as the language with the highest
210         aggregate score delta within the last `days` days.
211 
212         :param days: period of days to account for scores.
213         :return: Tuple of `(position, Language)`. If there's no delta in
214             the score for the given period for any of the languages,
215             `(-1, None)` is returned.
216         """
217         language = self.get_top_language_within(days)
218         if language:
219             # this only gets scores for the last 30 days as that is cached
220             language_scores = self.get_language_top_scores(language)
221             for index, user_score in enumerate(language_scores):
222                 if user_score['user__username'] == self.context.username:
223                     return index + 1, language
224         return -1, language
225
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pootle/apps/pootle_score/utils.py b/pootle/apps/pootle_score/utils.py
--- a/pootle/apps/pootle_score/utils.py
+++ b/pootle/apps/pootle_score/utils.py
@@ -78,7 +78,8 @@
                 Sum("score"),
                 Sum("suggested"),
                 Sum("reviewed"),
-                Sum("translated")).order_by("-score__sum")
+                Sum("translated")).filter(
+                    score__sum__gt=0).order_by("-score__sum")
 
     def filter_scores(self, qs):
         return qs

{"golden_diff": "diff --git a/pootle/apps/pootle_score/utils.py b/pootle/apps/pootle_score/utils.py\n--- a/pootle/apps/pootle_score/utils.py\n+++ b/pootle/apps/pootle_score/utils.py\n@@ -78,7 +78,8 @@\n                 Sum(\"score\"),\n                 Sum(\"suggested\"),\n                 Sum(\"reviewed\"),\n-                Sum(\"translated\")).order_by(\"-score__sum\")\n+                Sum(\"translated\")).filter(\n+                    score__sum__gt=0).order_by(\"-score__sum\")\n \n     def filter_scores(self, qs):\n         return qs\n", "issue": "Top scorers list includes zero score users\nThe top scorer list in e.g. `/af/?details` includes a number of users with zero score.\r\n\r\nI'm doubtful that these contributed in last 30 days.  So they shouldn't be on the list at all.\r\n\r\n![screen shot 2017-05-17 at 10 37 08](https://cloud.githubusercontent.com/assets/647438/26147888/d6e7c214-3aec-11e7-8db8-e847a4e8335a.png)\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nfrom datetime import date, datetime, timedelta\n\nimport pytz\n\nfrom django.contrib.auth import get_user_model\nfrom django.db.models import Sum\nfrom django.utils.functional import cached_property\n\nfrom pootle.core.decorators import persistent_property\nfrom pootle.core.delegate import display, revision, scores\nfrom pootle.core.utils.timezone import localdate, make_aware\nfrom pootle_app.models import Directory\nfrom pootle_language.models import Language\n\nfrom .apps import PootleScoreConfig\nfrom .models import UserTPScore\n\n\nUser = get_user_model()\n\n\ndef to_datetime(possible_dt):\n    if possible_dt is None:\n        return\n    if isinstance(possible_dt, datetime):\n        return possible_dt\n    if isinstance(possible_dt, date):\n        return make_aware(\n            datetime.combine(\n                possible_dt,\n                datetime.min.time())).astimezone(\n                    pytz.timezone(\"UTC\"))\n\n\nclass Scores(object):\n    ns = \"pootle.score\"\n    sw_version = PootleScoreConfig.version\n\n    def __init__(self, context):\n        self.context = context\n\n    @property\n    def revision(self):\n        return revision.get(Directory)(\n            self.context.directory).get(key=\"stats\")\n\n    @property\n    def score_model(self):\n        return UserTPScore.objects.exclude(\n            user__username__in=User.objects.META_USERS)\n\n    def get_daterange(self, days):\n        now = localdate()\n        return now - timedelta(days), now\n\n    def scores_within_days(self, days):\n        return self.score_model.filter(\n            date__range=self.get_daterange(days))\n\n    def get_scores(self, days):\n        return self.filter_scores(self.scores_within_days(days))\n\n    def get_top_scorers(self, days=30):\n        \"\"\"Returns users with the top scores.\n\n        :param days: period of days to account for scores.\n        \"\"\"\n        return self.get_scores(days).order_by(\"user__username\").values(\n            \"user__username\", \"user__email\", \"user__full_name\").annotate(\n                Sum(\"score\"),\n                Sum(\"suggested\"),\n                Sum(\"reviewed\"),\n                Sum(\"translated\")).order_by(\"-score__sum\")\n\n    def filter_scores(self, qs):\n        return qs\n\n    @persistent_property\n    def top_scorers(self):\n        return tuple(self.get_top_scorers())\n\n    def display(self, offset=0, limit=5, language=None, formatter=None):\n        scorers = self.top_scorers\n        if offset or limit:\n            scorers = list(scorers)\n        if offset:\n            scorers = scorers[offset:]\n        if limit:\n            scorers = scorers[:limit]\n        return display.get(Scores)(\n            top_scores=scorers,\n            formatter=formatter,\n            language=language)\n\n\nclass LanguageScores(Scores):\n    ns = \"pootle.score.language\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s.%s.%s\"\n            % (self.context.code,\n               localdate(),\n               self.revision))\n\n    def filter_scores(self, qs):\n        return qs.filter(tp__language_id=self.context.id)\n\n\nclass ProjectScores(Scores):\n    ns = \"pootle.score.project\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s.%s.%s\"\n            % (self.context.code,\n               localdate(),\n               self.revision))\n\n    def filter_scores(self, qs):\n        return qs.filter(tp__project_id=self.context.id)\n\n\nclass ProjectSetScores(Scores):\n    ns = \"pootle.score.projects\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s.%s\"\n            % (localdate(),\n               self.revision))\n\n\nclass TPScores(Scores):\n    ns = \"pootle.score.tp\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s/%s.%s.%s\"\n            % (self.context.language.code,\n               self.context.project.code,\n               localdate(),\n               self.revision))\n\n    def filter_scores(self, qs):\n        return qs.filter(tp_id=self.context.id)\n\n\nclass UserScores(Scores):\n    ns = \"pootle.score.user\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s.%s.%s\"\n            % (self.context.id,\n               localdate(),\n               self.revision))\n\n    @property\n    def revision(self):\n        return revision.get(Directory)(\n            Directory.objects.projects).get(key=\"stats\")\n\n    @property\n    def score_model(self):\n        return self.context.scores\n\n    @property\n    def public_score(self):\n        return self.context.public_score\n\n    @persistent_property\n    def top_language(self):\n        return self.get_top_language()\n\n    def get_top_language_within(self, days):\n        top_lang = self.get_scores_by_language(\n            days).order_by(\"score__sum\").first()\n        if top_lang:\n            return Language.objects.get(id=top_lang[\"tp__language\"])\n\n    def get_scores_by_language(self, days):\n        \"\"\"Languages that the user has contributed to in the last `days`,\n        and the summary score\n        \"\"\"\n        return self.get_scores(days).order_by(\n            \"tp__language\").values(\"tp__language\").annotate(Sum(\"score\"))\n\n    def get_language_top_scores(self, language):\n        return scores.get(language.__class__)(language).top_scorers\n\n    def get_top_language(self, days=30):\n        \"\"\"Returns the top language the user has contributed to and its\n        position.\n\n        \"Top language\" is defined as the language with the highest\n        aggregate score delta within the last `days` days.\n\n        :param days: period of days to account for scores.\n        :return: Tuple of `(position, Language)`. If there's no delta in\n            the score for the given period for any of the languages,\n            `(-1, None)` is returned.\n        \"\"\"\n        language = self.get_top_language_within(days)\n        if language:\n            # this only gets scores for the last 30 days as that is cached\n            language_scores = self.get_language_top_scores(language)\n            for index, user_score in enumerate(language_scores):\n                if user_score['user__username'] == self.context.username:\n                    return index + 1, language\n        return -1, language\n", "path": "pootle/apps/pootle_score/utils.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nfrom datetime import date, datetime, timedelta\n\nimport pytz\n\nfrom django.contrib.auth import get_user_model\nfrom django.db.models import Sum\nfrom django.utils.functional import cached_property\n\nfrom pootle.core.decorators import persistent_property\nfrom pootle.core.delegate import display, revision, scores\nfrom pootle.core.utils.timezone import localdate, make_aware\nfrom pootle_app.models import Directory\nfrom pootle_language.models import Language\n\nfrom .apps import PootleScoreConfig\nfrom .models import UserTPScore\n\n\nUser = get_user_model()\n\n\ndef to_datetime(possible_dt):\n    if possible_dt is None:\n        return\n    if isinstance(possible_dt, datetime):\n        return possible_dt\n    if isinstance(possible_dt, date):\n        return make_aware(\n            datetime.combine(\n                possible_dt,\n                datetime.min.time())).astimezone(\n                    pytz.timezone(\"UTC\"))\n\n\nclass Scores(object):\n    ns = \"pootle.score\"\n    sw_version = PootleScoreConfig.version\n\n    def __init__(self, context):\n        self.context = context\n\n    @property\n    def revision(self):\n        return revision.get(Directory)(\n            self.context.directory).get(key=\"stats\")\n\n    @property\n    def score_model(self):\n        return UserTPScore.objects.exclude(\n            user__username__in=User.objects.META_USERS)\n\n    def get_daterange(self, days):\n        now = localdate()\n        return now - timedelta(days), now\n\n    def scores_within_days(self, days):\n        return self.score_model.filter(\n            date__range=self.get_daterange(days))\n\n    def get_scores(self, days):\n        return self.filter_scores(self.scores_within_days(days))\n\n    def get_top_scorers(self, days=30):\n        \"\"\"Returns users with the top scores.\n\n        :param days: period of days to account for scores.\n        \"\"\"\n        return self.get_scores(days).order_by(\"user__username\").values(\n            \"user__username\", \"user__email\", \"user__full_name\").annotate(\n                Sum(\"score\"),\n                Sum(\"suggested\"),\n                Sum(\"reviewed\"),\n                Sum(\"translated\")).filter(\n                    score__sum__gt=0).order_by(\"-score__sum\")\n\n    def filter_scores(self, qs):\n        return qs\n\n    @persistent_property\n    def top_scorers(self):\n        return tuple(self.get_top_scorers())\n\n    def display(self, offset=0, limit=5, language=None, formatter=None):\n        scorers = self.top_scorers\n        if offset or limit:\n            scorers = list(scorers)\n        if offset:\n            scorers = scorers[offset:]\n        if limit:\n            scorers = scorers[:limit]\n        return display.get(Scores)(\n            top_scores=scorers,\n            formatter=formatter,\n            language=language)\n\n\nclass LanguageScores(Scores):\n    ns = \"pootle.score.language\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s.%s.%s\"\n            % (self.context.code,\n               localdate(),\n               self.revision))\n\n    def filter_scores(self, qs):\n        return qs.filter(tp__language_id=self.context.id)\n\n\nclass ProjectScores(Scores):\n    ns = \"pootle.score.project\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s.%s.%s\"\n            % (self.context.code,\n               localdate(),\n               self.revision))\n\n    def filter_scores(self, qs):\n        return qs.filter(tp__project_id=self.context.id)\n\n\nclass ProjectSetScores(Scores):\n    ns = \"pootle.score.projects\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s.%s\"\n            % (localdate(),\n               self.revision))\n\n\nclass TPScores(Scores):\n    ns = \"pootle.score.tp\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s/%s.%s.%s\"\n            % (self.context.language.code,\n               self.context.project.code,\n               localdate(),\n               self.revision))\n\n    def filter_scores(self, qs):\n        return qs.filter(tp_id=self.context.id)\n\n\nclass UserScores(Scores):\n    ns = \"pootle.score.user\"\n\n    @cached_property\n    def cache_key(self):\n        return (\n            \"%s.%s.%s\"\n            % (self.context.id,\n               localdate(),\n               self.revision))\n\n    @property\n    def revision(self):\n        return revision.get(Directory)(\n            Directory.objects.projects).get(key=\"stats\")\n\n    @property\n    def score_model(self):\n        return self.context.scores\n\n    @property\n    def public_score(self):\n        return self.context.public_score\n\n    @persistent_property\n    def top_language(self):\n        return self.get_top_language()\n\n    def get_top_language_within(self, days):\n        top_lang = self.get_scores_by_language(\n            days).order_by(\"score__sum\").first()\n        if top_lang:\n            return Language.objects.get(id=top_lang[\"tp__language\"])\n\n    def get_scores_by_language(self, days):\n        \"\"\"Languages that the user has contributed to in the last `days`,\n        and the summary score\n        \"\"\"\n        return self.get_scores(days).order_by(\n            \"tp__language\").values(\"tp__language\").annotate(Sum(\"score\"))\n\n    def get_language_top_scores(self, language):\n        return scores.get(language.__class__)(language).top_scorers\n\n    def get_top_language(self, days=30):\n        \"\"\"Returns the top language the user has contributed to and its\n        position.\n\n        \"Top language\" is defined as the language with the highest\n        aggregate score delta within the last `days` days.\n\n        :param days: period of days to account for scores.\n        :return: Tuple of `(position, Language)`. If there's no delta in\n            the score for the given period for any of the languages,\n            `(-1, None)` is returned.\n        \"\"\"\n        language = self.get_top_language_within(days)\n        if language:\n            # this only gets scores for the last 30 days as that is cached\n            language_scores = self.get_language_top_scores(language)\n            for index, user_score in enumerate(language_scores):\n                if user_score['user__username'] == self.context.username:\n                    return index + 1, language\n        return -1, language\n", "path": "pootle/apps/pootle_score/utils.py"}]}