We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Check 'delete' checkbox for a new entry renders error
_Version: 2.5.1_

_Originally posted by Mike Massonnet:_

_Imported from attachment 976:_

``` pytb
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py", line 109, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.7/dist-packages/pootle/core/decorators.py", line 87, in wrapped
    return func(request, path_obj, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pootle/core/decorators.py", line 180, in _wrapped
    return func(request, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pootle/apps/pootle_project/views.py", line 427, in project_admin
    exclude=('description',))
  File "/usr/lib/python2.7/dist-packages/pootle/apps/pootle_app/views/admin/util.py", line 191, in edit
    queryset=queryset, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pootle/apps/pootle_app/views/admin/util.py", line 172, in process_modelformset
    if formset.is_valid():
  File "/usr/lib/python2.7/dist-packages/django/forms/formsets.py", line 272, in is_valid
    err = self.errors
  File "/usr/lib/python2.7/dist-packages/django/forms/formsets.py", line 250, in _get_errors
    self.full_clean()
  File "/usr/lib/python2.7/dist-packages/django/forms/formsets.py", line 293, in full_clean
    self._errors.append(form.errors)
  File "/usr/lib/python2.7/dist-packages/django/forms/forms.py", line 115, in _get_errors
    self.full_clean()
  File "/usr/lib/python2.7/dist-packages/django/forms/forms.py", line 272, in full_clean
    self._post_clean()
  File "/usr/lib/python2.7/dist-packages/django/forms/models.py", line 309, in _post_clean
    self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
  File "/usr/lib/python2.7/dist-packages/django/forms/models.py", line 51, in construct_instance
    f.save_form_data(instance, cleaned_data[f.name])
  File "/usr/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 454, in save_form_data
    setattr(instance, self.name, data)
  File "/usr/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 362, in __set__
    (instance._meta.object_name, self.field.name))
ValueError: Cannot assign None: "TranslationProject.language" does not allow null values.
```

_Created [attachment 976](http://bugs.locamotion.org/attachment.cgi?id=976)_

pootle-check-all-traceback.txt

Hello,

In the adminstration page of a project, checking the Delete checkbox for a new entry renders an error. This checkbox is also automatically selected when checking the Delete checkbox at the top of the form, to check all checkboxes.

The following error is rendered:
"Cannot assign None: "TranslationProject.language" does not allow null values."

See attached screenshots and traceback.

Regards,
Mike


--- 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 #!/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 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 from django.utils.translation import ugettext as _
15 
16 from pootle.core.paginator import paginate
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         for i, form in enumerate(formset.forms):
114             add_errors(result, fields, form)
115             add_widgets(result, fields, form, link)
116 
117         result.append('</tbody>\n')
118     except IndexError:
119         result.append('<tr>\n')
120         result.append('<td>\n')
121         result.append(_('No files in this project.'))
122         result.append('</td>\n')
123         result.append('</tr>\n')
124 
125     return u''.join(result)
126 
127 
128 def process_modelformset(request, model_class, queryset, **kwargs):
129     """With the Django model class `model_class` and the given `queryset`,
130     construct a formset process its submission."""
131 
132     # Create a formset class for the model `model_class` (i.e. it will contain
133     # forms whose contents are based on the fields of `model_class`);
134     # parameters for the construction of the forms used in the formset should
135     # be in kwargs.
136     formset_class = modelformset_factory(model_class, **kwargs)
137 
138     if queryset is None:
139         queryset = model_class.objects.all()
140 
141     # If the request is a POST, we want to possibly update our data
142     if request.method == 'POST' and request.POST:
143         # Create a formset from all the 'model_class' instances whose values
144         # will be updated using the contents of request.POST
145         objects = paginate(request, queryset)
146         formset = formset_class(request.POST, queryset=objects.object_list)
147 
148         # Validate all the forms in the formset
149         if formset.is_valid():
150             # If all is well, Django can save all our data for us
151             formset.save()
152         else:
153             # Otherwise, complain to the user that something went wrong
154             return formset, _("There are errors in the form. Please review "
155                               "the problems below."), objects
156 
157         # Hack to force reevaluation of same query
158         queryset = queryset.filter()
159 
160     objects = paginate(request, queryset)
161 
162     return formset_class(queryset=objects.object_list), None, objects
163 
164 
165 def edit(request, template, model_class, ctx=None,
166          link=None, linkfield='code', queryset=None, **kwargs):
167     formset, msg, objects = process_modelformset(request, model_class,
168                                                  queryset=queryset, **kwargs)
169     if ctx is None:
170         ctx = {}
171 
172     ctx.update({
173         'formset_text': mark_safe(form_set_as_table(formset, link, linkfield)),
174         'formset': formset,
175         'objects': objects,
176         'error_msg': msg,
177     })
178 
179     return render(request, template, ctx)
180
```
--- 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
@@ -7,6 +7,7 @@
 # or later license. See the LICENSE file for a copy of the license and the
 # AUTHORS file for copyright and authorship information.
 
+from django import forms
 from django.forms.models import modelformset_factory
 from django.forms.utils import ErrorList
 from django.shortcuts import render
@@ -110,6 +111,10 @@
         result.append('</tfoot>\n')
 
         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()
+
         for i, form in enumerate(formset.forms):
             add_errors(result, fields, form)
             add_widgets(result, fields, form, link)

{"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@@ -7,6 +7,7 @@\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 \n+from django import forms\n from django.forms.models import modelformset_factory\n from django.forms.utils import ErrorList\n from django.shortcuts import render\n@@ -110,6 +111,10 @@\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 i, form in enumerate(formset.forms):\n             add_errors(result, fields, form)\n             add_widgets(result, fields, form, link)\n", "issue": "Check 'delete' checkbox for a new entry renders error\n_Version: 2.5.1_\n\n_Originally posted by Mike Massonnet:_\n\n_Imported from attachment 976:_\n\n``` pytb\nTraceback (most recent call last):\n  File \"/usr/lib/python2.7/dist-packages/django/core/handlers/base.py\", line 109, in get_response\n    response = callback(request, *callback_args, **callback_kwargs)\n  File \"/usr/lib/python2.7/dist-packages/pootle/core/decorators.py\", line 87, in wrapped\n    return func(request, path_obj, *args, **kwargs)\n  File \"/usr/lib/python2.7/dist-packages/pootle/core/decorators.py\", line 180, in _wrapped\n    return func(request, *args, **kwargs)\n  File \"/usr/lib/python2.7/dist-packages/pootle/apps/pootle_project/views.py\", line 427, in project_admin\n    exclude=('description',))\n  File \"/usr/lib/python2.7/dist-packages/pootle/apps/pootle_app/views/admin/util.py\", line 191, in edit\n    queryset=queryset, **kwargs)\n  File \"/usr/lib/python2.7/dist-packages/pootle/apps/pootle_app/views/admin/util.py\", line 172, in process_modelformset\n    if formset.is_valid():\n  File \"/usr/lib/python2.7/dist-packages/django/forms/formsets.py\", line 272, in is_valid\n    err = self.errors\n  File \"/usr/lib/python2.7/dist-packages/django/forms/formsets.py\", line 250, in _get_errors\n    self.full_clean()\n  File \"/usr/lib/python2.7/dist-packages/django/forms/formsets.py\", line 293, in full_clean\n    self._errors.append(form.errors)\n  File \"/usr/lib/python2.7/dist-packages/django/forms/forms.py\", line 115, in _get_errors\n    self.full_clean()\n  File \"/usr/lib/python2.7/dist-packages/django/forms/forms.py\", line 272, in full_clean\n    self._post_clean()\n  File \"/usr/lib/python2.7/dist-packages/django/forms/models.py\", line 309, in _post_clean\n    self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)\n  File \"/usr/lib/python2.7/dist-packages/django/forms/models.py\", line 51, in construct_instance\n    f.save_form_data(instance, cleaned_data[f.name])\n  File \"/usr/lib/python2.7/dist-packages/django/db/models/fields/__init__.py\", line 454, in save_form_data\n    setattr(instance, self.name, data)\n  File \"/usr/lib/python2.7/dist-packages/django/db/models/fields/related.py\", line 362, in __set__\n    (instance._meta.object_name, self.field.name))\nValueError: Cannot assign None: \"TranslationProject.language\" does not allow null values.\n```\n\n_Created [attachment 976](http://bugs.locamotion.org/attachment.cgi?id=976)_\n\npootle-check-all-traceback.txt\n\nHello,\n\nIn the adminstration page of a project, checking the Delete checkbox for a new entry renders an error. This checkbox is also automatically selected when checking the Delete checkbox at the top of the form, to check all checkboxes.\n\nThe following error is rendered:\n\"Cannot assign None: \"TranslationProject.language\" does not allow null values.\"\n\nSee attached screenshots and traceback.\n\nRegards,\nMike\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\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\nfrom django.utils.translation import ugettext as _\n\nfrom pootle.core.paginator import paginate\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        for i, form in enumerate(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    # 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    })\n\n    return render(request, template, ctx)\n", "path": "pootle/apps/pootle_app/views/admin/util.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\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\nfrom django.utils.translation import ugettext as _\n\nfrom pootle.core.paginator import paginate\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 i, form in enumerate(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    # 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    })\n\n    return render(request, template, ctx)\n", "path": "pootle/apps/pootle_app/views/admin/util.py"}]}