We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Team page is very slow to load
not sure of the cause, but new team page is loading v slowly

--- 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_language/views.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 django.contrib import messages
10 from django.http import Http404
11 from django.shortcuts import redirect, render
12 from django.urls import reverse
13 from django.utils.functional import cached_property
14 from django.utils.safestring import mark_safe
15 
16 from pootle.core.browser import make_project_item
17 from pootle.core.decorators import (
18     get_object_or_404, get_path_obj, permission_required)
19 from pootle.core.exceptions import Http400
20 from pootle.core.views import PootleBrowseView, PootleTranslateView
21 from pootle.core.views.admin import PootleFormView
22 from pootle.core.views.decorators import requires_permission, set_permissions
23 from pootle.core.views.formtable import Formtable
24 from pootle.core.views.mixins import PootleJSONMixin
25 from pootle.i18n import formatter
26 from pootle.i18n.gettext import ugettext_lazy as _, ungettext_lazy
27 from pootle_misc.util import cmp_by_last_activity
28 from pootle_store.constants import STATES_MAP
29 
30 from .apps import PootleLanguageConfig
31 from .forms import (
32     LanguageSpecialCharsForm, LanguageSuggestionAdminForm,
33     LanguageTeamAdminForm, LanguageTeamNewMemberSearchForm)
34 from .models import Language
35 
36 
37 class LanguageMixin(object):
38     ns = "pootle.language"
39     sw_version = PootleLanguageConfig.version
40     model = Language
41     browse_url_path = "pootle-language-browse"
42     translate_url_path = "pootle-language-translate"
43     template_extends = 'languages/base.html'
44 
45     @property
46     def language(self):
47         return self.object
48 
49     @property
50     def permission_context(self):
51         return self.get_object().directory
52 
53     @property
54     def url_kwargs(self):
55         return {"language_code": self.object.code}
56 
57     def get_object(self):
58         return self.object
59 
60     @cached_property
61     def object(self):
62         lang = Language.get_canonical(self.kwargs["language_code"])
63         if lang is None:
64             raise Http404
65         return lang
66 
67     def get(self, *args, **kwargs):
68         self.object = self.get_object()
69         if self.object.code != kwargs["language_code"]:
70             return redirect(
71                 self.url_pattern_name,
72                 self.object.code,
73                 permanent=True)
74         return super(LanguageMixin, self).get(*args, **kwargs)
75 
76 
77 class LanguageBrowseView(LanguageMixin, PootleBrowseView):
78     url_pattern_name = "pootle-language-browse"
79     view_name = "language"
80 
81     @cached_property
82     def object_children(self):
83         items = [make_project_item(tp)
84                  for tp in self.object.get_children_for_user(self.request.user)]
85         items = self.add_child_stats(items)
86         items.sort(cmp_by_last_activity)
87         return items
88 
89     @property
90     def language(self):
91         return {
92             'code': self.object.code,
93             'name': self.object.name}
94 
95     def get(self, *args, **kwargs):
96         response = super(LanguageBrowseView, self).get(*args, **kwargs)
97         response.set_cookie('pootle-language', self.object.code)
98         return response
99 
100 
101 class LanguageTranslateView(LanguageMixin, PootleTranslateView):
102     url_pattern_name = "pootle-language-translate"
103 
104 
105 @get_path_obj
106 @permission_required('administrate')
107 def language_characters_admin(request, language):
108     form = LanguageSpecialCharsForm(request.POST
109                                     if request.method == 'POST'
110                                     else None,
111                                     instance=language)
112     if form.is_valid():
113         form.save()
114         return redirect('pootle-language-browse', language.code)
115 
116     ctx = {
117         'page': 'admin-characters',
118 
119         'browse_url': reverse('pootle-language-browse', kwargs={
120             'language_code': language.code,
121         }),
122         'translate_url': reverse('pootle-language-translate', kwargs={
123             'language_code': language.code,
124         }),
125 
126         'language': language,
127         'directory': language.directory,
128         'form': form,
129     }
130 
131     return render(request, 'languages/admin/characters.html', ctx)
132 
133 
134 class SuggestionFormtable(Formtable):
135     row_field = "suggestions"
136     filters_template = "languages/admin/includes/suggestions_header.html"
137 
138     @property
139     def messages(self):
140         return self.kwargs.get("messages", [])
141 
142 
143 class SuggestionDisplay(object):
144 
145     def __init__(self, suggestion):
146         self.__suggestion__ = suggestion
147 
148     @property
149     def unit(self):
150         return self.__suggestion__.unit.source_f
151 
152     @property
153     def project(self):
154         tp = self.__suggestion__.unit.store.translation_project
155         return mark_safe(
156             "<a href='%s'>%s</a>"
157             % (tp.get_absolute_url(),
158                tp.project.code))
159 
160     @property
161     def unit_state(self):
162         return STATES_MAP[self.__suggestion__.unit.state]
163 
164     @property
165     def unit_link(self):
166         return mark_safe(
167             "<a href='%s'>#%s</a>"
168             % (self.__suggestion__.unit.get_translate_url(),
169                self.__suggestion__.unit.id))
170 
171     def __getattr__(self, k):
172         try:
173             return getattr(self.__suggestion__, k)
174         except AttributeError:
175             return self.__getattribute__(k)
176 
177 
178 class PootleLanguageAdminFormView(PootleFormView):
179 
180     @property
181     def permission_context(self):
182         return self.language.directory
183 
184     @set_permissions
185     @requires_permission("administrate")
186     def dispatch(self, request, *args, **kwargs):
187         # get funky with the request 8/
188         return super(
189             PootleLanguageAdminFormView, self).dispatch(request, *args, **kwargs)
190 
191     @cached_property
192     def language(self):
193         return get_object_or_404(
194             Language.objects.select_related("directory"),
195             code=self.kwargs["language_code"])
196 
197     def get_form_kwargs(self):
198         kwargs = super(PootleLanguageAdminFormView, self).get_form_kwargs()
199         kwargs["language"] = self.language
200         return kwargs
201 
202     @property
203     def success_kwargs(self):
204         return dict(language_code=self.language.code)
205 
206 
207 class LanguageSuggestionAdminView(PootleLanguageAdminFormView):
208     template_name = 'languages/admin/language_team_suggestions.html'
209     form_class = LanguageSuggestionAdminForm
210     success_url_pattern = "pootle-language-admin-suggestions"
211     formtable_columns = (
212         _("Unit"),
213         _("State"),
214         _("Source"),
215         _("Suggestion"),
216         _("Suggested by"),
217         _("Suggested at"),
218         _("Project"))
219 
220     @property
221     def default_form_kwargs(self):
222         return dict(
223             page_no=1,
224             results_per_page=10)
225 
226     def add_success_message(self, form):
227         count = (
228             form.fields["suggestions"].queryset.count()
229             if form.cleaned_data["select_all"]
230             else len(form.cleaned_data["suggestions"]))
231         reject_and_notify = (
232             form.cleaned_data["actions"] == "reject"
233             and form.cleaned_data["comment"])
234         accept_and_notify = (
235             form.cleaned_data["actions"] == "accept"
236             and form.cleaned_data["comment"])
237         if reject_and_notify:
238             message = ungettext_lazy(
239                 "Rejected %s suggestion with comment. User will be notified",
240                 "Rejected %s suggestions with comment. Users will be notified",
241                 count, count)
242         elif accept_and_notify:
243             message = ungettext_lazy(
244                 "Accepted %s suggestion with comment. User will be notified",
245                 "Accepted %s suggestions with comment. Users will be notified",
246                 count, count)
247         elif form.cleaned_data["actions"] == "reject":
248             message = ungettext_lazy(
249                 "Rejected %s suggestion",
250                 "Rejected %s suggestions",
251                 count, count)
252         else:
253             message = ungettext_lazy(
254                 "Accepted %s suggestion",
255                 "Accepted %s suggestions",
256                 count, count)
257         messages.success(self.request, message)
258 
259     def get_context_data(self, **kwargs):
260         context = super(
261             LanguageSuggestionAdminView, self).get_context_data(**kwargs)
262         context["page"] = "admin-suggestions"
263         context["language"] = self.language
264         form = context["form"]
265         form.is_valid()
266         batch = form.batch()
267         form.fields["suggestions"].choices = [
268             (item.id, SuggestionDisplay(item))
269             for item in
270             batch.object_list]
271         context["formtable"] = SuggestionFormtable(
272             form,
273             columns=self.formtable_columns,
274             page=batch,
275             messages=messages.get_messages(self.request))
276         return context
277 
278     def get_form_kwargs(self):
279         kwargs = super(LanguageSuggestionAdminView, self).get_form_kwargs()
280         if not self.request.POST:
281             kwargs["data"] = self.default_form_kwargs
282         kwargs["user"] = self.request.user
283         return kwargs
284 
285 
286 class LanguageTeamAdminFormView(PootleLanguageAdminFormView):
287     form_class = LanguageTeamAdminForm
288     template_name = "languages/admin/language_team.html"
289     success_url_pattern = "pootle-language-admin-team"
290 
291     def get_context_data(self, **kwargs):
292         context = super(LanguageTeamAdminFormView, self).get_context_data(**kwargs)
293         form = context["form"]
294         context["tps"] = self.language.translationproject_set.exclude(
295             project__disabled=True)
296         stats = self.language.data_tool.get_stats(
297             include_children=False,
298             user=self.request.user)
299         keys = ("total", "critical", "incomplete", "translated", "fuzzy",
300                 "untranslated")
301         for k in keys:
302             if k in stats:
303                 stats[k + "_display"] = formatter.number(stats[k])
304         context["stats"] = stats
305         context["suggestions"] = form.language_team.suggestions
306         suggestions_count = 0
307         if context["suggestions"]:
308             suggestions_count = context["suggestions"].count()
309         context["suggestions_display"] = formatter.number(suggestions_count)
310         context["language"] = self.language
311         context["page"] = "admin-team"
312         context["browse_url"] = reverse(
313             "pootle-language-browse",
314             kwargs=dict(language_code=self.language.code))
315         context["translate_url"] = reverse(
316             "pootle-language-translate",
317             kwargs=dict(language_code=self.language.code))
318         return context
319 
320 
321 class LanguageTeamAdminNewMembersJSON(PootleJSONMixin, PootleLanguageAdminFormView):
322     form_class = LanguageTeamNewMemberSearchForm
323 
324     def get_context_data(self, **kwargs):
325         context = super(
326             LanguageTeamAdminNewMembersJSON, self).get_context_data(**kwargs)
327         form = context["form"]
328         return (
329             dict(items=form.search())
330             if form.is_valid()
331             else dict(items=[]))
332 
333     def get_form_kwargs(self):
334         kwargs = super(LanguageTeamAdminNewMembersJSON, self).get_form_kwargs()
335         kwargs["data"] = self.request.POST
336         return kwargs
337 
338     def form_valid(self, form):
339         return self.render_to_response(
340             self.get_context_data(form=form))
341 
342     def form_invalid(self, form):
343         raise Http400(form.errors)
344
```
--- 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_language/views.py b/pootle/apps/pootle_language/views.py
--- a/pootle/apps/pootle_language/views.py
+++ b/pootle/apps/pootle_language/views.py
@@ -302,11 +302,9 @@
             if k in stats:
                 stats[k + "_display"] = formatter.number(stats[k])
         context["stats"] = stats
-        context["suggestions"] = form.language_team.suggestions
-        suggestions_count = 0
-        if context["suggestions"]:
-            suggestions_count = context["suggestions"].count()
-        context["suggestions_display"] = formatter.number(suggestions_count)
+        context["suggestions"] = form.language_team.suggestions.count()
+        context["suggestions_display"] = formatter.number(
+            context["suggestions"])
         context["language"] = self.language
         context["page"] = "admin-team"
         context["browse_url"] = reverse(

{"golden_diff": "diff --git a/pootle/apps/pootle_language/views.py b/pootle/apps/pootle_language/views.py\n--- a/pootle/apps/pootle_language/views.py\n+++ b/pootle/apps/pootle_language/views.py\n@@ -302,11 +302,9 @@\n             if k in stats:\n                 stats[k + \"_display\"] = formatter.number(stats[k])\n         context[\"stats\"] = stats\n-        context[\"suggestions\"] = form.language_team.suggestions\n-        suggestions_count = 0\n-        if context[\"suggestions\"]:\n-            suggestions_count = context[\"suggestions\"].count()\n-        context[\"suggestions_display\"] = formatter.number(suggestions_count)\n+        context[\"suggestions\"] = form.language_team.suggestions.count()\n+        context[\"suggestions_display\"] = formatter.number(\n+            context[\"suggestions\"])\n         context[\"language\"] = self.language\n         context[\"page\"] = \"admin-team\"\n         context[\"browse_url\"] = reverse(\n", "issue": "Team page is very slow to load\nnot sure of the cause, but new team page is loading v slowly\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 django.contrib import messages\nfrom django.http import Http404\nfrom django.shortcuts import redirect, render\nfrom django.urls import reverse\nfrom django.utils.functional import cached_property\nfrom django.utils.safestring import mark_safe\n\nfrom pootle.core.browser import make_project_item\nfrom pootle.core.decorators import (\n    get_object_or_404, get_path_obj, permission_required)\nfrom pootle.core.exceptions import Http400\nfrom pootle.core.views import PootleBrowseView, PootleTranslateView\nfrom pootle.core.views.admin import PootleFormView\nfrom pootle.core.views.decorators import requires_permission, set_permissions\nfrom pootle.core.views.formtable import Formtable\nfrom pootle.core.views.mixins import PootleJSONMixin\nfrom pootle.i18n import formatter\nfrom pootle.i18n.gettext import ugettext_lazy as _, ungettext_lazy\nfrom pootle_misc.util import cmp_by_last_activity\nfrom pootle_store.constants import STATES_MAP\n\nfrom .apps import PootleLanguageConfig\nfrom .forms import (\n    LanguageSpecialCharsForm, LanguageSuggestionAdminForm,\n    LanguageTeamAdminForm, LanguageTeamNewMemberSearchForm)\nfrom .models import Language\n\n\nclass LanguageMixin(object):\n    ns = \"pootle.language\"\n    sw_version = PootleLanguageConfig.version\n    model = Language\n    browse_url_path = \"pootle-language-browse\"\n    translate_url_path = \"pootle-language-translate\"\n    template_extends = 'languages/base.html'\n\n    @property\n    def language(self):\n        return self.object\n\n    @property\n    def permission_context(self):\n        return self.get_object().directory\n\n    @property\n    def url_kwargs(self):\n        return {\"language_code\": self.object.code}\n\n    def get_object(self):\n        return self.object\n\n    @cached_property\n    def object(self):\n        lang = Language.get_canonical(self.kwargs[\"language_code\"])\n        if lang is None:\n            raise Http404\n        return lang\n\n    def get(self, *args, **kwargs):\n        self.object = self.get_object()\n        if self.object.code != kwargs[\"language_code\"]:\n            return redirect(\n                self.url_pattern_name,\n                self.object.code,\n                permanent=True)\n        return super(LanguageMixin, self).get(*args, **kwargs)\n\n\nclass LanguageBrowseView(LanguageMixin, PootleBrowseView):\n    url_pattern_name = \"pootle-language-browse\"\n    view_name = \"language\"\n\n    @cached_property\n    def object_children(self):\n        items = [make_project_item(tp)\n                 for tp in self.object.get_children_for_user(self.request.user)]\n        items = self.add_child_stats(items)\n        items.sort(cmp_by_last_activity)\n        return items\n\n    @property\n    def language(self):\n        return {\n            'code': self.object.code,\n            'name': self.object.name}\n\n    def get(self, *args, **kwargs):\n        response = super(LanguageBrowseView, self).get(*args, **kwargs)\n        response.set_cookie('pootle-language', self.object.code)\n        return response\n\n\nclass LanguageTranslateView(LanguageMixin, PootleTranslateView):\n    url_pattern_name = \"pootle-language-translate\"\n\n\n@get_path_obj\n@permission_required('administrate')\ndef language_characters_admin(request, language):\n    form = LanguageSpecialCharsForm(request.POST\n                                    if request.method == 'POST'\n                                    else None,\n                                    instance=language)\n    if form.is_valid():\n        form.save()\n        return redirect('pootle-language-browse', language.code)\n\n    ctx = {\n        'page': 'admin-characters',\n\n        'browse_url': reverse('pootle-language-browse', kwargs={\n            'language_code': language.code,\n        }),\n        'translate_url': reverse('pootle-language-translate', kwargs={\n            'language_code': language.code,\n        }),\n\n        'language': language,\n        'directory': language.directory,\n        'form': form,\n    }\n\n    return render(request, 'languages/admin/characters.html', ctx)\n\n\nclass SuggestionFormtable(Formtable):\n    row_field = \"suggestions\"\n    filters_template = \"languages/admin/includes/suggestions_header.html\"\n\n    @property\n    def messages(self):\n        return self.kwargs.get(\"messages\", [])\n\n\nclass SuggestionDisplay(object):\n\n    def __init__(self, suggestion):\n        self.__suggestion__ = suggestion\n\n    @property\n    def unit(self):\n        return self.__suggestion__.unit.source_f\n\n    @property\n    def project(self):\n        tp = self.__suggestion__.unit.store.translation_project\n        return mark_safe(\n            \"<a href='%s'>%s</a>\"\n            % (tp.get_absolute_url(),\n               tp.project.code))\n\n    @property\n    def unit_state(self):\n        return STATES_MAP[self.__suggestion__.unit.state]\n\n    @property\n    def unit_link(self):\n        return mark_safe(\n            \"<a href='%s'>#%s</a>\"\n            % (self.__suggestion__.unit.get_translate_url(),\n               self.__suggestion__.unit.id))\n\n    def __getattr__(self, k):\n        try:\n            return getattr(self.__suggestion__, k)\n        except AttributeError:\n            return self.__getattribute__(k)\n\n\nclass PootleLanguageAdminFormView(PootleFormView):\n\n    @property\n    def permission_context(self):\n        return self.language.directory\n\n    @set_permissions\n    @requires_permission(\"administrate\")\n    def dispatch(self, request, *args, **kwargs):\n        # get funky with the request 8/\n        return super(\n            PootleLanguageAdminFormView, self).dispatch(request, *args, **kwargs)\n\n    @cached_property\n    def language(self):\n        return get_object_or_404(\n            Language.objects.select_related(\"directory\"),\n            code=self.kwargs[\"language_code\"])\n\n    def get_form_kwargs(self):\n        kwargs = super(PootleLanguageAdminFormView, self).get_form_kwargs()\n        kwargs[\"language\"] = self.language\n        return kwargs\n\n    @property\n    def success_kwargs(self):\n        return dict(language_code=self.language.code)\n\n\nclass LanguageSuggestionAdminView(PootleLanguageAdminFormView):\n    template_name = 'languages/admin/language_team_suggestions.html'\n    form_class = LanguageSuggestionAdminForm\n    success_url_pattern = \"pootle-language-admin-suggestions\"\n    formtable_columns = (\n        _(\"Unit\"),\n        _(\"State\"),\n        _(\"Source\"),\n        _(\"Suggestion\"),\n        _(\"Suggested by\"),\n        _(\"Suggested at\"),\n        _(\"Project\"))\n\n    @property\n    def default_form_kwargs(self):\n        return dict(\n            page_no=1,\n            results_per_page=10)\n\n    def add_success_message(self, form):\n        count = (\n            form.fields[\"suggestions\"].queryset.count()\n            if form.cleaned_data[\"select_all\"]\n            else len(form.cleaned_data[\"suggestions\"]))\n        reject_and_notify = (\n            form.cleaned_data[\"actions\"] == \"reject\"\n            and form.cleaned_data[\"comment\"])\n        accept_and_notify = (\n            form.cleaned_data[\"actions\"] == \"accept\"\n            and form.cleaned_data[\"comment\"])\n        if reject_and_notify:\n            message = ungettext_lazy(\n                \"Rejected %s suggestion with comment. User will be notified\",\n                \"Rejected %s suggestions with comment. Users will be notified\",\n                count, count)\n        elif accept_and_notify:\n            message = ungettext_lazy(\n                \"Accepted %s suggestion with comment. User will be notified\",\n                \"Accepted %s suggestions with comment. Users will be notified\",\n                count, count)\n        elif form.cleaned_data[\"actions\"] == \"reject\":\n            message = ungettext_lazy(\n                \"Rejected %s suggestion\",\n                \"Rejected %s suggestions\",\n                count, count)\n        else:\n            message = ungettext_lazy(\n                \"Accepted %s suggestion\",\n                \"Accepted %s suggestions\",\n                count, count)\n        messages.success(self.request, message)\n\n    def get_context_data(self, **kwargs):\n        context = super(\n            LanguageSuggestionAdminView, self).get_context_data(**kwargs)\n        context[\"page\"] = \"admin-suggestions\"\n        context[\"language\"] = self.language\n        form = context[\"form\"]\n        form.is_valid()\n        batch = form.batch()\n        form.fields[\"suggestions\"].choices = [\n            (item.id, SuggestionDisplay(item))\n            for item in\n            batch.object_list]\n        context[\"formtable\"] = SuggestionFormtable(\n            form,\n            columns=self.formtable_columns,\n            page=batch,\n            messages=messages.get_messages(self.request))\n        return context\n\n    def get_form_kwargs(self):\n        kwargs = super(LanguageSuggestionAdminView, self).get_form_kwargs()\n        if not self.request.POST:\n            kwargs[\"data\"] = self.default_form_kwargs\n        kwargs[\"user\"] = self.request.user\n        return kwargs\n\n\nclass LanguageTeamAdminFormView(PootleLanguageAdminFormView):\n    form_class = LanguageTeamAdminForm\n    template_name = \"languages/admin/language_team.html\"\n    success_url_pattern = \"pootle-language-admin-team\"\n\n    def get_context_data(self, **kwargs):\n        context = super(LanguageTeamAdminFormView, self).get_context_data(**kwargs)\n        form = context[\"form\"]\n        context[\"tps\"] = self.language.translationproject_set.exclude(\n            project__disabled=True)\n        stats = self.language.data_tool.get_stats(\n            include_children=False,\n            user=self.request.user)\n        keys = (\"total\", \"critical\", \"incomplete\", \"translated\", \"fuzzy\",\n                \"untranslated\")\n        for k in keys:\n            if k in stats:\n                stats[k + \"_display\"] = formatter.number(stats[k])\n        context[\"stats\"] = stats\n        context[\"suggestions\"] = form.language_team.suggestions\n        suggestions_count = 0\n        if context[\"suggestions\"]:\n            suggestions_count = context[\"suggestions\"].count()\n        context[\"suggestions_display\"] = formatter.number(suggestions_count)\n        context[\"language\"] = self.language\n        context[\"page\"] = \"admin-team\"\n        context[\"browse_url\"] = reverse(\n            \"pootle-language-browse\",\n            kwargs=dict(language_code=self.language.code))\n        context[\"translate_url\"] = reverse(\n            \"pootle-language-translate\",\n            kwargs=dict(language_code=self.language.code))\n        return context\n\n\nclass LanguageTeamAdminNewMembersJSON(PootleJSONMixin, PootleLanguageAdminFormView):\n    form_class = LanguageTeamNewMemberSearchForm\n\n    def get_context_data(self, **kwargs):\n        context = super(\n            LanguageTeamAdminNewMembersJSON, self).get_context_data(**kwargs)\n        form = context[\"form\"]\n        return (\n            dict(items=form.search())\n            if form.is_valid()\n            else dict(items=[]))\n\n    def get_form_kwargs(self):\n        kwargs = super(LanguageTeamAdminNewMembersJSON, self).get_form_kwargs()\n        kwargs[\"data\"] = self.request.POST\n        return kwargs\n\n    def form_valid(self, form):\n        return self.render_to_response(\n            self.get_context_data(form=form))\n\n    def form_invalid(self, form):\n        raise Http400(form.errors)\n", "path": "pootle/apps/pootle_language/views.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 django.contrib import messages\nfrom django.http import Http404\nfrom django.shortcuts import redirect, render\nfrom django.urls import reverse\nfrom django.utils.functional import cached_property\nfrom django.utils.safestring import mark_safe\n\nfrom pootle.core.browser import make_project_item\nfrom pootle.core.decorators import (\n    get_object_or_404, get_path_obj, permission_required)\nfrom pootle.core.exceptions import Http400\nfrom pootle.core.views import PootleBrowseView, PootleTranslateView\nfrom pootle.core.views.admin import PootleFormView\nfrom pootle.core.views.decorators import requires_permission, set_permissions\nfrom pootle.core.views.formtable import Formtable\nfrom pootle.core.views.mixins import PootleJSONMixin\nfrom pootle.i18n import formatter\nfrom pootle.i18n.gettext import ugettext_lazy as _, ungettext_lazy\nfrom pootle_misc.util import cmp_by_last_activity\nfrom pootle_store.constants import STATES_MAP\n\nfrom .apps import PootleLanguageConfig\nfrom .forms import (\n    LanguageSpecialCharsForm, LanguageSuggestionAdminForm,\n    LanguageTeamAdminForm, LanguageTeamNewMemberSearchForm)\nfrom .models import Language\n\n\nclass LanguageMixin(object):\n    ns = \"pootle.language\"\n    sw_version = PootleLanguageConfig.version\n    model = Language\n    browse_url_path = \"pootle-language-browse\"\n    translate_url_path = \"pootle-language-translate\"\n    template_extends = 'languages/base.html'\n\n    @property\n    def language(self):\n        return self.object\n\n    @property\n    def permission_context(self):\n        return self.get_object().directory\n\n    @property\n    def url_kwargs(self):\n        return {\"language_code\": self.object.code}\n\n    def get_object(self):\n        return self.object\n\n    @cached_property\n    def object(self):\n        lang = Language.get_canonical(self.kwargs[\"language_code\"])\n        if lang is None:\n            raise Http404\n        return lang\n\n    def get(self, *args, **kwargs):\n        self.object = self.get_object()\n        if self.object.code != kwargs[\"language_code\"]:\n            return redirect(\n                self.url_pattern_name,\n                self.object.code,\n                permanent=True)\n        return super(LanguageMixin, self).get(*args, **kwargs)\n\n\nclass LanguageBrowseView(LanguageMixin, PootleBrowseView):\n    url_pattern_name = \"pootle-language-browse\"\n    view_name = \"language\"\n\n    @cached_property\n    def object_children(self):\n        items = [make_project_item(tp)\n                 for tp in self.object.get_children_for_user(self.request.user)]\n        items = self.add_child_stats(items)\n        items.sort(cmp_by_last_activity)\n        return items\n\n    @property\n    def language(self):\n        return {\n            'code': self.object.code,\n            'name': self.object.name}\n\n    def get(self, *args, **kwargs):\n        response = super(LanguageBrowseView, self).get(*args, **kwargs)\n        response.set_cookie('pootle-language', self.object.code)\n        return response\n\n\nclass LanguageTranslateView(LanguageMixin, PootleTranslateView):\n    url_pattern_name = \"pootle-language-translate\"\n\n\n@get_path_obj\n@permission_required('administrate')\ndef language_characters_admin(request, language):\n    form = LanguageSpecialCharsForm(request.POST\n                                    if request.method == 'POST'\n                                    else None,\n                                    instance=language)\n    if form.is_valid():\n        form.save()\n        return redirect('pootle-language-browse', language.code)\n\n    ctx = {\n        'page': 'admin-characters',\n\n        'browse_url': reverse('pootle-language-browse', kwargs={\n            'language_code': language.code,\n        }),\n        'translate_url': reverse('pootle-language-translate', kwargs={\n            'language_code': language.code,\n        }),\n\n        'language': language,\n        'directory': language.directory,\n        'form': form,\n    }\n\n    return render(request, 'languages/admin/characters.html', ctx)\n\n\nclass SuggestionFormtable(Formtable):\n    row_field = \"suggestions\"\n    filters_template = \"languages/admin/includes/suggestions_header.html\"\n\n    @property\n    def messages(self):\n        return self.kwargs.get(\"messages\", [])\n\n\nclass SuggestionDisplay(object):\n\n    def __init__(self, suggestion):\n        self.__suggestion__ = suggestion\n\n    @property\n    def unit(self):\n        return self.__suggestion__.unit.source_f\n\n    @property\n    def project(self):\n        tp = self.__suggestion__.unit.store.translation_project\n        return mark_safe(\n            \"<a href='%s'>%s</a>\"\n            % (tp.get_absolute_url(),\n               tp.project.code))\n\n    @property\n    def unit_state(self):\n        return STATES_MAP[self.__suggestion__.unit.state]\n\n    @property\n    def unit_link(self):\n        return mark_safe(\n            \"<a href='%s'>#%s</a>\"\n            % (self.__suggestion__.unit.get_translate_url(),\n               self.__suggestion__.unit.id))\n\n    def __getattr__(self, k):\n        try:\n            return getattr(self.__suggestion__, k)\n        except AttributeError:\n            return self.__getattribute__(k)\n\n\nclass PootleLanguageAdminFormView(PootleFormView):\n\n    @property\n    def permission_context(self):\n        return self.language.directory\n\n    @set_permissions\n    @requires_permission(\"administrate\")\n    def dispatch(self, request, *args, **kwargs):\n        # get funky with the request 8/\n        return super(\n            PootleLanguageAdminFormView, self).dispatch(request, *args, **kwargs)\n\n    @cached_property\n    def language(self):\n        return get_object_or_404(\n            Language.objects.select_related(\"directory\"),\n            code=self.kwargs[\"language_code\"])\n\n    def get_form_kwargs(self):\n        kwargs = super(PootleLanguageAdminFormView, self).get_form_kwargs()\n        kwargs[\"language\"] = self.language\n        return kwargs\n\n    @property\n    def success_kwargs(self):\n        return dict(language_code=self.language.code)\n\n\nclass LanguageSuggestionAdminView(PootleLanguageAdminFormView):\n    template_name = 'languages/admin/language_team_suggestions.html'\n    form_class = LanguageSuggestionAdminForm\n    success_url_pattern = \"pootle-language-admin-suggestions\"\n    formtable_columns = (\n        _(\"Unit\"),\n        _(\"State\"),\n        _(\"Source\"),\n        _(\"Suggestion\"),\n        _(\"Suggested by\"),\n        _(\"Suggested at\"),\n        _(\"Project\"))\n\n    @property\n    def default_form_kwargs(self):\n        return dict(\n            page_no=1,\n            results_per_page=10)\n\n    def add_success_message(self, form):\n        count = (\n            form.fields[\"suggestions\"].queryset.count()\n            if form.cleaned_data[\"select_all\"]\n            else len(form.cleaned_data[\"suggestions\"]))\n        reject_and_notify = (\n            form.cleaned_data[\"actions\"] == \"reject\"\n            and form.cleaned_data[\"comment\"])\n        accept_and_notify = (\n            form.cleaned_data[\"actions\"] == \"accept\"\n            and form.cleaned_data[\"comment\"])\n        if reject_and_notify:\n            message = ungettext_lazy(\n                \"Rejected %s suggestion with comment. User will be notified\",\n                \"Rejected %s suggestions with comment. Users will be notified\",\n                count, count)\n        elif accept_and_notify:\n            message = ungettext_lazy(\n                \"Accepted %s suggestion with comment. User will be notified\",\n                \"Accepted %s suggestions with comment. Users will be notified\",\n                count, count)\n        elif form.cleaned_data[\"actions\"] == \"reject\":\n            message = ungettext_lazy(\n                \"Rejected %s suggestion\",\n                \"Rejected %s suggestions\",\n                count, count)\n        else:\n            message = ungettext_lazy(\n                \"Accepted %s suggestion\",\n                \"Accepted %s suggestions\",\n                count, count)\n        messages.success(self.request, message)\n\n    def get_context_data(self, **kwargs):\n        context = super(\n            LanguageSuggestionAdminView, self).get_context_data(**kwargs)\n        context[\"page\"] = \"admin-suggestions\"\n        context[\"language\"] = self.language\n        form = context[\"form\"]\n        form.is_valid()\n        batch = form.batch()\n        form.fields[\"suggestions\"].choices = [\n            (item.id, SuggestionDisplay(item))\n            for item in\n            batch.object_list]\n        context[\"formtable\"] = SuggestionFormtable(\n            form,\n            columns=self.formtable_columns,\n            page=batch,\n            messages=messages.get_messages(self.request))\n        return context\n\n    def get_form_kwargs(self):\n        kwargs = super(LanguageSuggestionAdminView, self).get_form_kwargs()\n        if not self.request.POST:\n            kwargs[\"data\"] = self.default_form_kwargs\n        kwargs[\"user\"] = self.request.user\n        return kwargs\n\n\nclass LanguageTeamAdminFormView(PootleLanguageAdminFormView):\n    form_class = LanguageTeamAdminForm\n    template_name = \"languages/admin/language_team.html\"\n    success_url_pattern = \"pootle-language-admin-team\"\n\n    def get_context_data(self, **kwargs):\n        context = super(LanguageTeamAdminFormView, self).get_context_data(**kwargs)\n        form = context[\"form\"]\n        context[\"tps\"] = self.language.translationproject_set.exclude(\n            project__disabled=True)\n        stats = self.language.data_tool.get_stats(\n            include_children=False,\n            user=self.request.user)\n        keys = (\"total\", \"critical\", \"incomplete\", \"translated\", \"fuzzy\",\n                \"untranslated\")\n        for k in keys:\n            if k in stats:\n                stats[k + \"_display\"] = formatter.number(stats[k])\n        context[\"stats\"] = stats\n        context[\"suggestions\"] = form.language_team.suggestions.count()\n        context[\"suggestions_display\"] = formatter.number(\n            context[\"suggestions\"])\n        context[\"language\"] = self.language\n        context[\"page\"] = \"admin-team\"\n        context[\"browse_url\"] = reverse(\n            \"pootle-language-browse\",\n            kwargs=dict(language_code=self.language.code))\n        context[\"translate_url\"] = reverse(\n            \"pootle-language-translate\",\n            kwargs=dict(language_code=self.language.code))\n        return context\n\n\nclass LanguageTeamAdminNewMembersJSON(PootleJSONMixin, PootleLanguageAdminFormView):\n    form_class = LanguageTeamNewMemberSearchForm\n\n    def get_context_data(self, **kwargs):\n        context = super(\n            LanguageTeamAdminNewMembersJSON, self).get_context_data(**kwargs)\n        form = context[\"form\"]\n        return (\n            dict(items=form.search())\n            if form.is_valid()\n            else dict(items=[]))\n\n    def get_form_kwargs(self):\n        kwargs = super(LanguageTeamAdminNewMembersJSON, self).get_form_kwargs()\n        kwargs[\"data\"] = self.request.POST\n        return kwargs\n\n    def form_valid(self, form):\n        return self.render_to_response(\n            self.get_context_data(form=form))\n\n    def form_invalid(self, form):\n        raise Http400(form.errors)\n", "path": "pootle/apps/pootle_language/views.py"}]}