def process_modelformset(request, model_class, queryset, **kwargs): """With the Django model class `model_class` and the given `queryset`, construct a formset process its submission.""" # Create a formset class for the model `model_class` (i.e. it will contain # forms whose contents are based on the fields of `model_class`); # parameters for the construction of the forms used in the formset should # be in kwargs. formset_class = modelformset_factory(model_class, **kwargs) if queryset is None: queryset = model_class.objects.all() # If the request is a POST, we want to possibly update our data if request.method == 'POST' and request.POST: # Create a formset from all the 'model_class' instances whose values # will be updated using the contents of request.POST objects = paginate(request, queryset) formset = formset_class(request.POST, queryset=objects.object_list) # Validate all the forms in the formset if formset.is_valid(): # If all is well, Django can save all our data for us formset.save() else: # Otherwise, complain to the user that something went wrong return formset, _("There are errors in the form. Please review " "the problems below."), objects objects = paginate(request, queryset) return formset_class(queryset=objects.object_list), None, objects
def process_modelformset(request, model_class, queryset, **kwargs): """With the Django model class `model_class` and the given `queryset`, construct a formset process its submission.""" # Create a formset class for the model `model_class` (i.e. it will contain # forms whose contents are based on the fields of `model_class`); # parameters for the construction of the forms used in the formset should # be in kwargs. formset_class = modelformset_factory(model_class, **kwargs) if queryset is None: queryset = model_class.objects.all() # If the request is a POST, we want to possibly update our data if request.method == 'POST' and request.POST: # Create a formset from all the 'model_class' instances whose values # will be updated using the contents of request.POST objects = paginate(request, queryset) formset = formset_class(request.POST, queryset=objects.object_list) # Validate all the forms in the formset if formset.is_valid(): # If all is well, Django can save all our data for us formset.save() else: # Otherwise, complain to the user that something went wrong return formset, _("There are errors in the form. Please review " "the problems below."), objects # Hack to force reevaluation of same query queryset = queryset.filter() objects = paginate(request, queryset) return formset_class(queryset=objects.object_list), None, objects
def _test_admin_view(response, project): request = response.wsgi_request qs = TranslationProject.objects.filter( project=project).order_by('pootle_path') page = paginate(request, qs) url_kwargs = {'project_code': project.code, 'dir_path': '', 'filename': ''} assert page.number == response.context["objects"].number assert page.start_index() == response.context["objects"].start_index() assert page.end_index() == response.context["objects"].end_index() assert (list(response.context["objects"].object_list.values_list( "pk", flat=True)) == list(qs.values_list("pk", flat=True))) assert (response.context["formset"].__class__.__name__ == "TranslationProjectFormFormSet") assert response.context["page"] == "admin-languages" assert response.context["browse_url"] == reverse('pootle-project-browse', kwargs=url_kwargs) assert response.context["translate_url"] == reverse( 'pootle-project-translate', kwargs=url_kwargs) assert (response.context['project'] == { 'code': project.code, 'name': project.fullname, 'treestyle': project.treestyle }) assert (response.context["formset_text"] == mark_safe( form_set_as_table( response.context["formset"], lambda tp: (u'<a href="%s">%s</a>' % (reverse('pootle-tp-admin-permissions', args=split_pootle_path(tp.pootle_path)[:2]), tp.language )), "language")))
def _test_admin_view(response, project): request = response.wsgi_request qs = TranslationProject.objects.filter( project=project).order_by('pootle_path') page = paginate(request, qs) url_kwargs = { 'project_code': project.code, 'dir_path': '', 'filename': ''} assert page.number == response.context["objects"].number assert page.start_index() == response.context["objects"].start_index() assert page.end_index() == response.context["objects"].end_index() assert ( list(response.context["objects"].object_list.values_list("pk", flat=True)) == list(qs.values_list("pk", flat=True))) assert ( response.context["formset"].__class__.__name__ == "TranslationProjectFormFormSet") assert response.context["page"] == "admin-languages" assert response.context["browse_url"] == reverse( 'pootle-project-browse', kwargs=url_kwargs) assert response.context["translate_url"] == reverse( 'pootle-project-translate', kwargs=url_kwargs) assert ( response.context['project'] == {'code': project.code, 'name': project.fullname}) assert ( response.context["formset_text"] == mark_safe( form_set_as_table( response.context["formset"], lambda tp: ( u'<a href="%s">%s</a>' % (reverse('pootle-tp-admin-permissions', args=split_pootle_path(tp.pootle_path)[:2]), tp.language)), "language")))
def page(self): return paginate(self.request, self.qs)