prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Enable sorting by contribution in contributor command
  Currently contributors are sorted in alphabetical order.  This is great for crediting.  But it would be more helpful to allow sorting by contribution in cases where you want to use to list to make other decisions around the amount of contribution.
  
  Thus add `--sort-by-contribution` and `--sort-by-name` options.
  
  
  --- 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_app/management/commands/contributors.py`
  Content:
  ```
  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 #
  4 # Copyright (C) Pootle contributors.
  5 #
  6 # This file is a part of the Pootle project. It is distributed under the GPL3
  7 # or later license. See the LICENSE file for a copy of the license and the
  8 # AUTHORS file for copyright and authorship information.
  9 
  10 import os
  11 from collections import Counter
  12 from optparse import make_option
  13 
  14 os.environ["DJANGO_SETTINGS_MODULE"] = "pootle.settings"
  15 
  16 from django.contrib.auth import get_user_model
  17 
  18 from pootle_store.models import Unit
  19 
  20 from . import PootleCommand
  21 
  22 
  23 User = get_user_model()
  24 
  25 
  26 class Command(PootleCommand):
  27     option_list = PootleCommand.option_list + (
  28         make_option(
  29             "--from-revision",
  30             type=int,
  31             default=0,
  32             dest="revision",
  33             help="Only count contributions newer than this revision",
  34         ),
  35     )
  36 
  37     help = "Print a list of contributors."
  38 
  39     def handle_all(self, **options):
  40         system_user = User.objects.get_system_user()
  41         units = Unit.objects.exclude(submitted_by=system_user) \
  42                             .exclude(submitted_by=None)
  43 
  44         if options["revision"]:
  45             units = units.filter(revision__gte=options["revision"])
  46 
  47         if self.projects:
  48             units = units.filter(
  49                 store__translation_project__project__code__in=self.projects,
  50             )
  51 
  52         if self.languages:
  53             units = units.filter(
  54                 store__translation_project__language__code__in=self.languages,
  55             )
  56 
  57         contribs = Counter()
  58         for v in units.values("submitted_by"):
  59             contribs.update((v["submitted_by"], ))
  60 
  61         self.list_contributions(contribs)
  62 
  63     def list_contributions(self, contribs):
  64         out = []
  65         for id, count in contribs.items():
  66             user = User.objects.get(id=id)
  67             name = user.display_name
  68             if user.email:
  69                 name += " <%s>" % (user.email)
  70             out.append("%s (%i contributions)" % (name, count))
  71 
  72         # Sort users alphabetically
  73         for line in sorted(out):
  74             self.stdout.write(line)
  75
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/pootle/apps/pootle_app/management/commands/contributors.py b/pootle/apps/pootle_app/management/commands/contributors.py
  --- a/pootle/apps/pootle_app/management/commands/contributors.py
  +++ b/pootle/apps/pootle_app/management/commands/contributors.py
  @@ -32,6 +32,15 @@
               dest="revision",
               help="Only count contributions newer than this revision",
           ),
  +        make_option(
  +            "--sort-by",
  +            type="choice",
  +            default="name",
  +            choices=["name", "contributions"],
  +            dest="sort_by",
  +            help="Sort by specified item. Accepts name and contributions. "
  +                 "Default: %default",
  +        ),
       )
   
       help = "Print a list of contributors."
  @@ -58,17 +67,25 @@
           for v in units.values("submitted_by"):
               contribs.update((v["submitted_by"], ))
   
  -        self.list_contributions(contribs)
  +        self.list_contributions(contribs, options["sort_by"])
  +
  +    def list_contributions(self, contribs, sort_by):
  +        if sort_by == "name":
  +            contributions = contribs.items()
  +        else:
  +            contributions = contribs.most_common()
   
  -    def list_contributions(self, contribs):
           out = []
  -        for id, count in contribs.items():
  +        for id, count in contributions:
               user = User.objects.get(id=id)
               name = user.display_name
               if user.email:
                   name += " <%s>" % (user.email)
               out.append("%s (%i contributions)" % (name, count))
   
  -        # Sort users alphabetically
  -        for line in sorted(out):
  +        if sort_by == "name":
  +            # Sort users alphabetically
  +            out = sorted(out)
  +
  +        for line in out:
               self.stdout.write(line)
  
verification_info: {"golden_diff": "diff --git a/pootle/apps/pootle_app/management/commands/contributors.py b/pootle/apps/pootle_app/management/commands/contributors.py\n--- a/pootle/apps/pootle_app/management/commands/contributors.py\n+++ b/pootle/apps/pootle_app/management/commands/contributors.py\n@@ -32,6 +32,15 @@\n             dest=\"revision\",\n             help=\"Only count contributions newer than this revision\",\n         ),\n+        make_option(\n+            \"--sort-by\",\n+            type=\"choice\",\n+            default=\"name\",\n+            choices=[\"name\", \"contributions\"],\n+            dest=\"sort_by\",\n+            help=\"Sort by specified item. Accepts name and contributions. \"\n+                 \"Default: %default\",\n+        ),\n     )\n \n     help = \"Print a list of contributors.\"\n@@ -58,17 +67,25 @@\n         for v in units.values(\"submitted_by\"):\n             contribs.update((v[\"submitted_by\"], ))\n \n-        self.list_contributions(contribs)\n+        self.list_contributions(contribs, options[\"sort_by\"])\n+\n+    def list_contributions(self, contribs, sort_by):\n+        if sort_by == \"name\":\n+            contributions = contribs.items()\n+        else:\n+            contributions = contribs.most_common()\n \n-    def list_contributions(self, contribs):\n         out = []\n-        for id, count in contribs.items():\n+        for id, count in contributions:\n             user = User.objects.get(id=id)\n             name = user.display_name\n             if user.email:\n                 name += \" <%s>\" % (user.email)\n             out.append(\"%s (%i contributions)\" % (name, count))\n \n-        # Sort users alphabetically\n-        for line in sorted(out):\n+        if sort_by == \"name\":\n+            # Sort users alphabetically\n+            out = sorted(out)\n+\n+        for line in out:\n             self.stdout.write(line)\n", "issue": "Enable sorting by contribution in contributor command\nCurrently contributors are sorted in alphabetical order.  This is great for crediting.  But it would be more helpful to allow sorting by contribution in cases where you want to use to list to make other decisions around the amount of contribution.\n\nThus add `--sort-by-contribution` and `--sort-by-name` options.\n\n", "before_files": [{"content": "#!/usr/bin/env python\n# -*- 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\nimport os\nfrom collections import Counter\nfrom optparse import make_option\n\nos.environ[\"DJANGO_SETTINGS_MODULE\"] = \"pootle.settings\"\n\nfrom django.contrib.auth import get_user_model\n\nfrom pootle_store.models import Unit\n\nfrom . import PootleCommand\n\n\nUser = get_user_model()\n\n\nclass Command(PootleCommand):\n    option_list = PootleCommand.option_list + (\n        make_option(\n            \"--from-revision\",\n            type=int,\n            default=0,\n            dest=\"revision\",\n            help=\"Only count contributions newer than this revision\",\n        ),\n    )\n\n    help = \"Print a list of contributors.\"\n\n    def handle_all(self, **options):\n        system_user = User.objects.get_system_user()\n        units = Unit.objects.exclude(submitted_by=system_user) \\\n                            .exclude(submitted_by=None)\n\n        if options[\"revision\"]:\n            units = units.filter(revision__gte=options[\"revision\"])\n\n        if self.projects:\n            units = units.filter(\n                store__translation_project__project__code__in=self.projects,\n            )\n\n        if self.languages:\n            units = units.filter(\n                store__translation_project__language__code__in=self.languages,\n            )\n\n        contribs = Counter()\n        for v in units.values(\"submitted_by\"):\n            contribs.update((v[\"submitted_by\"], ))\n\n        self.list_contributions(contribs)\n\n    def list_contributions(self, contribs):\n        out = []\n        for id, count in contribs.items():\n            user = User.objects.get(id=id)\n            name = user.display_name\n            if user.email:\n                name += \" <%s>\" % (user.email)\n            out.append(\"%s (%i contributions)\" % (name, count))\n\n        # Sort users alphabetically\n        for line in sorted(out):\n            self.stdout.write(line)\n", "path": "pootle/apps/pootle_app/management/commands/contributors.py"}], "after_files": [{"content": "#!/usr/bin/env python\n# -*- 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\nimport os\nfrom collections import Counter\nfrom optparse import make_option\n\nos.environ[\"DJANGO_SETTINGS_MODULE\"] = \"pootle.settings\"\n\nfrom django.contrib.auth import get_user_model\n\nfrom pootle_store.models import Unit\n\nfrom . import PootleCommand\n\n\nUser = get_user_model()\n\n\nclass Command(PootleCommand):\n    option_list = PootleCommand.option_list + (\n        make_option(\n            \"--from-revision\",\n            type=int,\n            default=0,\n            dest=\"revision\",\n            help=\"Only count contributions newer than this revision\",\n        ),\n        make_option(\n            \"--sort-by\",\n            type=\"choice\",\n            default=\"name\",\n            choices=[\"name\", \"contributions\"],\n            dest=\"sort_by\",\n            help=\"Sort by specified item. Accepts name and contributions. \"\n                 \"Default: %default\",\n        ),\n    )\n\n    help = \"Print a list of contributors.\"\n\n    def handle_all(self, **options):\n        system_user = User.objects.get_system_user()\n        units = Unit.objects.exclude(submitted_by=system_user) \\\n                            .exclude(submitted_by=None)\n\n        if options[\"revision\"]:\n            units = units.filter(revision__gte=options[\"revision\"])\n\n        if self.projects:\n            units = units.filter(\n                store__translation_project__project__code__in=self.projects,\n            )\n\n        if self.languages:\n            units = units.filter(\n                store__translation_project__language__code__in=self.languages,\n            )\n\n        contribs = Counter()\n        for v in units.values(\"submitted_by\"):\n            contribs.update((v[\"submitted_by\"], ))\n\n        self.list_contributions(contribs, options[\"sort_by\"])\n\n    def list_contributions(self, contribs, sort_by):\n        if sort_by == \"name\":\n            contributions = contribs.items()\n        else:\n            contributions = contribs.most_common()\n\n        out = []\n        for id, count in contributions:\n            user = User.objects.get(id=id)\n            name = user.display_name\n            if user.email:\n                name += \" <%s>\" % (user.email)\n            out.append(\"%s (%i contributions)\" % (name, count))\n\n        if sort_by == \"name\":\n            # Sort users alphabetically\n            out = sorted(out)\n\n        for line in out:\n            self.stdout.write(line)\n", "path": "pootle/apps/pootle_app/management/commands/contributors.py"}]}