def test_formset_errors(self):
        """Formset errors get propagated properly."""
        data = {
            'formset-emails-INITIAL_FORMS': 0,
            'formset-emails-TOTAL_FORMS': 1,
            'formset-emails-MAX_NUM_FORMS': 3,
            'formset-emails-0-email': 'foobar',
            'username': '******',
            'form-nested_form-name': 'Some Name',
        }
        form = AccountForm(data)

        if django.VERSION >= (1, 5):
            expected_errors = ErrorList([
                ErrorDict({
                    u'email': ErrorList([u'Enter a valid email address.'])
                })
            ])
        else:
            # Fix for Django 1.4
            expected_errors = ErrorList([
                ErrorDict({
                    u'email': ErrorList([u'Enter a valid e-mail address.'])
                })
            ])

        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors['emails'], expected_errors)
 def full_clean(self):
     """
     Clean the form, including all formsets and add formset errors to the
     errors dict. Errors of nested forms and formsets are only included if
     they actually contain errors.
     """
     super(SuperFormMixin, self).full_clean()
     for field_name, composite in self.forms.items():
         composite.full_clean()
         if not composite.is_valid() and composite._errors:
             self._errors[field_name] = ErrorDict(composite._errors)
     for field_name, composite in self.formsets.items():
         composite.full_clean()
         if not composite.is_valid() and composite._errors:
             self._errors[field_name] = ErrorList(composite._errors)
Exemplo n.º 3
0
 def full_clean(self):
     """
     Cleans all of self.data and populates self._errors and
     self.cleaned_data.
     """
     self._errors = ErrorDict()
     if not self.is_bound: # Stop further processing.
         return
     self.cleaned_data = {}
     # If the form is permitted to be empty, and none of the form data has
     # changed from the initial data, short circuit any validation.
     if self.empty_permitted and not self.has_changed() and not self.dependency_has_changed():
         return
     self._clean_fields()
     self._clean_form()
     self._post_clean()
Exemplo n.º 4
0
def edit_oauth_app(request, app_id=None):
    """Create or edit an OAuth2 application.

    Args:
        request (django.http.HttpRequest):
            The current HTTP request.

        app_id (int, optional):
            The ID of the application to edit.

            If this argument is ``None`` a new application will be edited.

    Returns:
        django.http.HttpResponse:
        The rendered view.
    """
    # If we import this at global scope, it will cause issues with admin sites
    # being automatically registered.
    from reviewboard.oauth.admin import ApplicationAdmin

    if app_id:
        app = get_object_or_404(
            Application,
            pk=app_id,
            user=request.user,
        )
        form_cls = UserApplicationChangeForm
        fieldsets = ApplicationAdmin.fieldsets
    else:
        app = None
        form_cls = UserApplicationCreationForm
        fieldsets = ApplicationAdmin.add_fieldsets

    if request.method == 'POST':
        form_data = request.POST.copy()

        form = form_cls(user=request.user,
                        data=form_data,
                        initial=None,
                        instance=app)

        if form.is_valid():
            app = form.save()

            if app_id is not None:
                next_url = OAuth2Page.get_absolute_url()
            else:
                next_url = reverse('edit-oauth-app', args=(app.pk, ))

            return HttpResponseRedirect(next_url)
    else:
        form = form_cls(user=request.user,
                        data=None,
                        initial=None,
                        instance=app)

        # Show a warning at the top of the form when the form is disabled for
        # security.
        #
        # We don't need to worry about full_clean not being called (which would
        # be if we went through form.errors) because this form will never be
        # saved.
        if app and app.is_disabled_for_security:
            form._errors = ErrorDict({
                '__all__':
                form.error_class([form.DISABLED_FOR_SECURITY_ERROR], ),
            })

    return render(request=request,
                  template_name='accounts/edit_oauth_app.html',
                  context={
                      'app':
                      app,
                      'form':
                      form,
                      'fieldsets':
                      filter_fieldsets(form=form_cls, fieldsets=fieldsets),
                      'oauth2_page_url':
                      OAuth2Page.get_absolute_url(),
                      'request':
                      request,
                  })