We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Can't delete any last TP on languages page in admin UI
If project is not pootle_fs and doesn't have templates then it's impossible to delete the last TP (checkbox is missing).
![image](https://cloud.githubusercontent.com/assets/682845/22365776/690a48e2-e48a-11e6-8069-7aec862a4e99.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_app/views/admin/util.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 import forms
10 from django.forms.models import modelformset_factory
11 from django.forms.utils import ErrorList
12 from django.shortcuts import render
13 from django.utils.safestring import mark_safe
14 
15 from pootle.core.paginator import paginate
16 from pootle.i18n.gettext import ugettext as _
17 
18 
19 def form_set_as_table(formset, link=None, linkfield='code'):
20     """Create an HTML table from the formset. The first form in the
21     formset is used to obtain a list of the fields that need to be
22     displayed.
23 
24     Errors, if there are any, appear in the row above the form which
25     triggered any errors.
26 
27     If the forms are based on database models, the order of the
28     columns is determined by the order of the fields in the model
29     specification.
30     """
31 
32     def add_header(result, fields, form):
33         result.append('<tr>\n')
34         for field in fields:
35             widget = form.fields[field].widget
36             widget_name = widget.__class__.__name__
37 
38             if widget.is_hidden or \
39                widget_name in ('CheckboxInput', 'SelectMultiple'):
40                 result.append('<th class="sorttable_nosort">')
41             else:
42                 result.append('<th>')
43 
44             if widget_name in ('CheckboxInput',):
45                 result.append(form[field].as_widget())
46                 result.append(form[field].label_tag())
47             elif form.fields[field].label is not None and not widget.is_hidden:
48                 result.append(unicode(form.fields[field].label))
49 
50             result.append('</th>\n')
51         result.append('</tr>\n')
52 
53     def add_footer(result, fields, form):
54         result.append('<tr>\n')
55         for field in fields:
56             field_obj = form.fields[field]
57             result.append('<td>')
58 
59             if field_obj.label is not None and not field_obj.widget.is_hidden:
60                 result.append(unicode(field_obj.label))
61 
62             result.append('</td>\n')
63         result.append('</tr>\n')
64 
65     def add_errors(result, fields, form):
66         # If the form has errors, then we'll add a table row with the
67         # errors.
68         if len(form.errors) > 0:
69             result.append('<tr>\n')
70             for field in fields:
71                 result.append('<td>')
72                 result.append(form.errors.get(field, ErrorList()).as_ul())
73                 result.append('</td>\n')
74 
75             result.append('</tr>\n')
76 
77     def add_widgets(result, fields, form, link):
78         result.append('<tr class="item">\n')
79         for i, field in enumerate(fields):
80             result.append('<td class="%s">' % field)
81             # Include a hidden element containing the form's id to the
82             # first column.
83             if i == 0:
84                 result.append(form['id'].as_hidden())
85 
86             # `link` indicates whether we put the first field as a link or as
87             # widget
88             if field == linkfield and linkfield in form.initial and link:
89                 if callable(link):
90                     result.append(link(form.instance))
91                 result.append(form[field].as_hidden())
92             else:
93                 result.append(form[field].as_widget())
94 
95             result.append('</td>\n')
96         result.append('</tr>\n')
97 
98     result = []
99     try:
100         first_form = formset.forms[0]
101         # Get the fields of the form, but filter our the 'id' field,
102         # since we don't want to print a table column for it.
103         fields = [field for field in first_form.fields if field != 'id']
104 
105         result.append('<thead>\n')
106         add_header(result, fields, first_form)
107         result.append('</thead>\n')
108         result.append('<tfoot>\n')
109         add_footer(result, fields, first_form)
110         result.append('</tfoot>\n')
111 
112         result.append('<tbody>\n')
113 
114         # Do not display the delete checkbox for the 'add a new entry' form.
115         formset.forms[-1].fields['DELETE'].widget = forms.HiddenInput()
116 
117         for form in formset.forms:
118             add_errors(result, fields, form)
119             add_widgets(result, fields, form, link)
120 
121         result.append('</tbody>\n')
122     except IndexError:
123         result.append('<tr>\n')
124         result.append('<td>\n')
125         result.append(_('No files in this project.'))
126         result.append('</td>\n')
127         result.append('</tr>\n')
128 
129     return u''.join(result)
130 
131 
132 def process_modelformset(request, model_class, queryset, **kwargs):
133     """With the Django model class `model_class` and the given `queryset`,
134     construct a formset process its submission.
135     """
136 
137     # Create a formset class for the model `model_class` (i.e. it will contain
138     # forms whose contents are based on the fields of `model_class`);
139     # parameters for the construction of the forms used in the formset should
140     # be in kwargs.
141     formset_class = modelformset_factory(model_class, **kwargs)
142 
143     if queryset is None:
144         queryset = model_class.objects.all()
145 
146     # If the request is a POST, we want to possibly update our data
147     if request.method == 'POST' and request.POST:
148         # Create a formset from all the 'model_class' instances whose values
149         # will be updated using the contents of request.POST
150         objects = paginate(request, queryset)
151         formset = formset_class(request.POST, queryset=objects.object_list)
152 
153         # Validate all the forms in the formset
154         if formset.is_valid():
155             # If all is well, Django can save all our data for us
156             formset.save()
157         else:
158             # Otherwise, complain to the user that something went wrong
159             return formset, _("There are errors in the form. Please review "
160                               "the problems below."), objects
161 
162         # Hack to force reevaluation of same query
163         queryset = queryset.filter()
164 
165     objects = paginate(request, queryset)
166 
167     return formset_class(queryset=objects.object_list), None, objects
168 
169 
170 def edit(request, template, model_class, ctx=None,
171          link=None, linkfield='code', queryset=None, **kwargs):
172     formset, msg, objects = process_modelformset(request, model_class,
173                                                  queryset=queryset, **kwargs)
174     if ctx is None:
175         ctx = {}
176 
177     ctx.update({
178         'formset_text': mark_safe(form_set_as_table(formset, link, linkfield)),
179         'formset': formset,
180         'objects': objects,
181         'error_msg': msg,
182         'can_add': kwargs.get('extra', 1) != 0,
183     })
184 
185     return render(request, template, ctx)
186
```
--- 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_app/views/admin/util.py b/pootle/apps/pootle_app/views/admin/util.py
--- a/pootle/apps/pootle_app/views/admin/util.py
+++ b/pootle/apps/pootle_app/views/admin/util.py
@@ -112,7 +112,8 @@
         result.append('<tbody>\n')
 
         # Do not display the delete checkbox for the 'add a new entry' form.
-        formset.forms[-1].fields['DELETE'].widget = forms.HiddenInput()
+        if formset.extra_forms:
+            formset.forms[-1].fields['DELETE'].widget = forms.HiddenInput()
 
         for form in formset.forms:
             add_errors(result, fields, form)

{"golden_diff": "diff --git a/pootle/apps/pootle_app/views/admin/util.py b/pootle/apps/pootle_app/views/admin/util.py\n--- a/pootle/apps/pootle_app/views/admin/util.py\n+++ b/pootle/apps/pootle_app/views/admin/util.py\n@@ -112,7 +112,8 @@\n         result.append('<tbody>\\n')\n \n         # Do not display the delete checkbox for the 'add a new entry' form.\n-        formset.forms[-1].fields['DELETE'].widget = forms.HiddenInput()\n+        if formset.extra_forms:\n+            formset.forms[-1].fields['DELETE'].widget = forms.HiddenInput()\n \n         for form in formset.forms:\n             add_errors(result, fields, form)\n", "issue": "Can't delete any last TP on languages page in admin UI\nIf project is not pootle_fs and doesn't have templates then it's impossible to delete the last TP (checkbox is missing).\r\n![image](https://cloud.githubusercontent.com/assets/682845/22365776/690a48e2-e48a-11e6-8069-7aec862a4e99.png)\r\n\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 import forms\nfrom django.forms.models import modelformset_factory\nfrom django.forms.utils import ErrorList\nfrom django.shortcuts import render\nfrom django.utils.safestring import mark_safe\n\nfrom pootle.core.paginator import paginate\nfrom pootle.i18n.gettext import ugettext as _\n\n\ndef form_set_as_table(formset, link=None, linkfield='code'):\n    \"\"\"Create an HTML table from the formset. The first form in the\n    formset is used to obtain a list of the fields that need to be\n    displayed.\n\n    Errors, if there are any, appear in the row above the form which\n    triggered any errors.\n\n    If the forms are based on database models, the order of the\n    columns is determined by the order of the fields in the model\n    specification.\n    \"\"\"\n\n    def add_header(result, fields, form):\n        result.append('<tr>\\n')\n        for field in fields:\n            widget = form.fields[field].widget\n            widget_name = widget.__class__.__name__\n\n            if widget.is_hidden or \\\n               widget_name in ('CheckboxInput', 'SelectMultiple'):\n                result.append('<th class=\"sorttable_nosort\">')\n            else:\n                result.append('<th>')\n\n            if widget_name in ('CheckboxInput',):\n                result.append(form[field].as_widget())\n                result.append(form[field].label_tag())\n            elif form.fields[field].label is not None and not widget.is_hidden:\n                result.append(unicode(form.fields[field].label))\n\n            result.append('</th>\\n')\n        result.append('</tr>\\n')\n\n    def add_footer(result, fields, form):\n        result.append('<tr>\\n')\n        for field in fields:\n            field_obj = form.fields[field]\n            result.append('<td>')\n\n            if field_obj.label is not None and not field_obj.widget.is_hidden:\n                result.append(unicode(field_obj.label))\n\n            result.append('</td>\\n')\n        result.append('</tr>\\n')\n\n    def add_errors(result, fields, form):\n        # If the form has errors, then we'll add a table row with the\n        # errors.\n        if len(form.errors) > 0:\n            result.append('<tr>\\n')\n            for field in fields:\n                result.append('<td>')\n                result.append(form.errors.get(field, ErrorList()).as_ul())\n                result.append('</td>\\n')\n\n            result.append('</tr>\\n')\n\n    def add_widgets(result, fields, form, link):\n        result.append('<tr class=\"item\">\\n')\n        for i, field in enumerate(fields):\n            result.append('<td class=\"%s\">' % field)\n            # Include a hidden element containing the form's id to the\n            # first column.\n            if i == 0:\n                result.append(form['id'].as_hidden())\n\n            # `link` indicates whether we put the first field as a link or as\n            # widget\n            if field == linkfield and linkfield in form.initial and link:\n                if callable(link):\n                    result.append(link(form.instance))\n                result.append(form[field].as_hidden())\n            else:\n                result.append(form[field].as_widget())\n\n            result.append('</td>\\n')\n        result.append('</tr>\\n')\n\n    result = []\n    try:\n        first_form = formset.forms[0]\n        # Get the fields of the form, but filter our the 'id' field,\n        # since we don't want to print a table column for it.\n        fields = [field for field in first_form.fields if field != 'id']\n\n        result.append('<thead>\\n')\n        add_header(result, fields, first_form)\n        result.append('</thead>\\n')\n        result.append('<tfoot>\\n')\n        add_footer(result, fields, first_form)\n        result.append('</tfoot>\\n')\n\n        result.append('<tbody>\\n')\n\n        # Do not display the delete checkbox for the 'add a new entry' form.\n        formset.forms[-1].fields['DELETE'].widget = forms.HiddenInput()\n\n        for form in formset.forms:\n            add_errors(result, fields, form)\n            add_widgets(result, fields, form, link)\n\n        result.append('</tbody>\\n')\n    except IndexError:\n        result.append('<tr>\\n')\n        result.append('<td>\\n')\n        result.append(_('No files in this project.'))\n        result.append('</td>\\n')\n        result.append('</tr>\\n')\n\n    return u''.join(result)\n\n\ndef process_modelformset(request, model_class, queryset, **kwargs):\n    \"\"\"With the Django model class `model_class` and the given `queryset`,\n    construct a formset process its submission.\n    \"\"\"\n\n    # Create a formset class for the model `model_class` (i.e. it will contain\n    # forms whose contents are based on the fields of `model_class`);\n    # parameters for the construction of the forms used in the formset should\n    # be in kwargs.\n    formset_class = modelformset_factory(model_class, **kwargs)\n\n    if queryset is None:\n        queryset = model_class.objects.all()\n\n    # If the request is a POST, we want to possibly update our data\n    if request.method == 'POST' and request.POST:\n        # Create a formset from all the 'model_class' instances whose values\n        # will be updated using the contents of request.POST\n        objects = paginate(request, queryset)\n        formset = formset_class(request.POST, queryset=objects.object_list)\n\n        # Validate all the forms in the formset\n        if formset.is_valid():\n            # If all is well, Django can save all our data for us\n            formset.save()\n        else:\n            # Otherwise, complain to the user that something went wrong\n            return formset, _(\"There are errors in the form. Please review \"\n                              \"the problems below.\"), objects\n\n        # Hack to force reevaluation of same query\n        queryset = queryset.filter()\n\n    objects = paginate(request, queryset)\n\n    return formset_class(queryset=objects.object_list), None, objects\n\n\ndef edit(request, template, model_class, ctx=None,\n         link=None, linkfield='code', queryset=None, **kwargs):\n    formset, msg, objects = process_modelformset(request, model_class,\n                                                 queryset=queryset, **kwargs)\n    if ctx is None:\n        ctx = {}\n\n    ctx.update({\n        'formset_text': mark_safe(form_set_as_table(formset, link, linkfield)),\n        'formset': formset,\n        'objects': objects,\n        'error_msg': msg,\n        'can_add': kwargs.get('extra', 1) != 0,\n    })\n\n    return render(request, template, ctx)\n", "path": "pootle/apps/pootle_app/views/admin/util.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 import forms\nfrom django.forms.models import modelformset_factory\nfrom django.forms.utils import ErrorList\nfrom django.shortcuts import render\nfrom django.utils.safestring import mark_safe\n\nfrom pootle.core.paginator import paginate\nfrom pootle.i18n.gettext import ugettext as _\n\n\ndef form_set_as_table(formset, link=None, linkfield='code'):\n    \"\"\"Create an HTML table from the formset. The first form in the\n    formset is used to obtain a list of the fields that need to be\n    displayed.\n\n    Errors, if there are any, appear in the row above the form which\n    triggered any errors.\n\n    If the forms are based on database models, the order of the\n    columns is determined by the order of the fields in the model\n    specification.\n    \"\"\"\n\n    def add_header(result, fields, form):\n        result.append('<tr>\\n')\n        for field in fields:\n            widget = form.fields[field].widget\n            widget_name = widget.__class__.__name__\n\n            if widget.is_hidden or \\\n               widget_name in ('CheckboxInput', 'SelectMultiple'):\n                result.append('<th class=\"sorttable_nosort\">')\n            else:\n                result.append('<th>')\n\n            if widget_name in ('CheckboxInput',):\n                result.append(form[field].as_widget())\n                result.append(form[field].label_tag())\n            elif form.fields[field].label is not None and not widget.is_hidden:\n                result.append(unicode(form.fields[field].label))\n\n            result.append('</th>\\n')\n        result.append('</tr>\\n')\n\n    def add_footer(result, fields, form):\n        result.append('<tr>\\n')\n        for field in fields:\n            field_obj = form.fields[field]\n            result.append('<td>')\n\n            if field_obj.label is not None and not field_obj.widget.is_hidden:\n                result.append(unicode(field_obj.label))\n\n            result.append('</td>\\n')\n        result.append('</tr>\\n')\n\n    def add_errors(result, fields, form):\n        # If the form has errors, then we'll add a table row with the\n        # errors.\n        if len(form.errors) > 0:\n            result.append('<tr>\\n')\n            for field in fields:\n                result.append('<td>')\n                result.append(form.errors.get(field, ErrorList()).as_ul())\n                result.append('</td>\\n')\n\n            result.append('</tr>\\n')\n\n    def add_widgets(result, fields, form, link):\n        result.append('<tr class=\"item\">\\n')\n        for i, field in enumerate(fields):\n            result.append('<td class=\"%s\">' % field)\n            # Include a hidden element containing the form's id to the\n            # first column.\n            if i == 0:\n                result.append(form['id'].as_hidden())\n\n            # `link` indicates whether we put the first field as a link or as\n            # widget\n            if field == linkfield and linkfield in form.initial and link:\n                if callable(link):\n                    result.append(link(form.instance))\n                result.append(form[field].as_hidden())\n            else:\n                result.append(form[field].as_widget())\n\n            result.append('</td>\\n')\n        result.append('</tr>\\n')\n\n    result = []\n    try:\n        first_form = formset.forms[0]\n        # Get the fields of the form, but filter our the 'id' field,\n        # since we don't want to print a table column for it.\n        fields = [field for field in first_form.fields if field != 'id']\n\n        result.append('<thead>\\n')\n        add_header(result, fields, first_form)\n        result.append('</thead>\\n')\n        result.append('<tfoot>\\n')\n        add_footer(result, fields, first_form)\n        result.append('</tfoot>\\n')\n\n        result.append('<tbody>\\n')\n\n        # Do not display the delete checkbox for the 'add a new entry' form.\n        if formset.extra_forms:\n            formset.forms[-1].fields['DELETE'].widget = forms.HiddenInput()\n\n        for form in formset.forms:\n            add_errors(result, fields, form)\n            add_widgets(result, fields, form, link)\n\n        result.append('</tbody>\\n')\n    except IndexError:\n        result.append('<tr>\\n')\n        result.append('<td>\\n')\n        result.append(_('No files in this project.'))\n        result.append('</td>\\n')\n        result.append('</tr>\\n')\n\n    return u''.join(result)\n\n\ndef process_modelformset(request, model_class, queryset, **kwargs):\n    \"\"\"With the Django model class `model_class` and the given `queryset`,\n    construct a formset process its submission.\n    \"\"\"\n\n    # Create a formset class for the model `model_class` (i.e. it will contain\n    # forms whose contents are based on the fields of `model_class`);\n    # parameters for the construction of the forms used in the formset should\n    # be in kwargs.\n    formset_class = modelformset_factory(model_class, **kwargs)\n\n    if queryset is None:\n        queryset = model_class.objects.all()\n\n    # If the request is a POST, we want to possibly update our data\n    if request.method == 'POST' and request.POST:\n        # Create a formset from all the 'model_class' instances whose values\n        # will be updated using the contents of request.POST\n        objects = paginate(request, queryset)\n        formset = formset_class(request.POST, queryset=objects.object_list)\n\n        # Validate all the forms in the formset\n        if formset.is_valid():\n            # If all is well, Django can save all our data for us\n            formset.save()\n        else:\n            # Otherwise, complain to the user that something went wrong\n            return formset, _(\"There are errors in the form. Please review \"\n                              \"the problems below.\"), objects\n\n        # Hack to force reevaluation of same query\n        queryset = queryset.filter()\n\n    objects = paginate(request, queryset)\n\n    return formset_class(queryset=objects.object_list), None, objects\n\n\ndef edit(request, template, model_class, ctx=None,\n         link=None, linkfield='code', queryset=None, **kwargs):\n    formset, msg, objects = process_modelformset(request, model_class,\n                                                 queryset=queryset, **kwargs)\n    if ctx is None:\n        ctx = {}\n\n    ctx.update({\n        'formset_text': mark_safe(form_set_as_table(formset, link, linkfield)),\n        'formset': formset,\n        'objects': objects,\n        'error_msg': msg,\n        'can_add': kwargs.get('extra', 1) != 0,\n    })\n\n    return render(request, template, ctx)\n", "path": "pootle/apps/pootle_app/views/admin/util.py"}]}