Beispiel #1
0
def validate(request, *args, **kwargs):
    form_class = kwargs.pop('form_class')
    extra_args_func = kwargs.pop('callback',
                                 lambda request, *args, **kwargs: {})
    kwargs = extra_args_func(request, *args, **kwargs)
    kwargs['data'] = request.POST
    form = form_class(**kwargs)
    if form.is_valid():
        data = {
            'valid': True,
        }
    else:
        if request.POST.getlist('fields'):
            fields = request.POST.getlist('fields') + ['__all__']
            errors = dict([(key, val) for key, val in form.errors.iteritems()
                           if key in fields])
        else:
            errors = form.errors
        final_errors = {}
        for key, val in errors.iteritems():
            if key == '__all__':
                final_errors['__all__'] = val
            if not isinstance(form.fields[key], forms.FileField):
                html_id = form.fields[key].widget.attrs.get(
                    'id') or form[key].auto_id
                html_id = form.fields[key].widget.id_for_label(html_id)
                final_errors[html_id] = val
        data = {
            'valid': False,
            'errors': final_errors,
        }
    json_serializer = LazyEncoder()
    return HttpResponse(json_serializer.encode(data),
                        mimetype='application/json')
Beispiel #2
0
def validate(request, *args, **kwargs):
    form_class = kwargs.pop('form_class')
    extra_args_func = kwargs.pop('callback', lambda request, *args, **kwargs: {})
    kwargs = extra_args_func(request, *args, **kwargs)
    kwargs['data'] = request.POST
    form = form_class(**kwargs)
    if form.is_valid():
        data = {
            'valid': True,
        }
    else:
        if request.POST.getlist('fields'):
            fields = request.POST.getlist('fields') + ['__all__']
            errors = dict([(key, val) for key, val in form.errors.iteritems() if key in fields])
        else:
            errors = form.errors
        final_errors = {}
        for key, val in errors.iteritems():
            if key == '__all__':
                final_errors['__all__'] = val
            if not isinstance(form.fields[key], forms.FileField):
                html_id = form.fields[key].widget.attrs.get('id') or form[key].auto_id
                html_id = form.fields[key].widget.id_for_label(html_id)
                final_errors[html_id] = val
        data = {
            'valid': False,
            'errors': final_errors,
        }
    json_serializer = LazyEncoder()
    return HttpResponse(json_serializer.encode(data), mimetype='application/json')
Beispiel #3
0
    def validate(self, request, *args, **kwargs):
        if request.method != 'POST':
            return HttpResponseNotAllowed('Sorry, validation views required to use POST method')

        form_class = kwargs.pop('form_class', self.get_form_class())
        if not form_class:
            raise ImproperlyConfigured('form_class param have to be provided.')

        save_if_valid = kwargs.pop('save_if_valid', self.save_if_valid)
        save_fun = self.save if save_if_valid else lambda r, f: {}

        form = form_class(**self.get_form_kwargs(request, *args, **kwargs))
        data = {}

        if form.is_valid():
            data.update(valid=True, **save_fun(request, form))
        else:
            errors, formfields = self.get_form_error_and_formfields(form)
            # if fields have been specified then restrict the error list
            if request.POST.getlist('fields[]'):
                fields = request.POST.getlist('fields[]') + ['__all__']
                errors = dict((key, val) for key, val in errors.iteritems() if key in fields)

            final_errors = {}
            for key, val in errors.iteritems():
                key_id = self.get_formfield_id(key, formfields)
                if key_id:
                    final_errors[key_id] = val

            data.update(valid=not final_errors, errors=final_errors)

        json_serializer = LazyEncoder()
        return HttpResponse(json_serializer.encode(data), mimetype='application/json')
Beispiel #4
0
def validate(request, *args, **kwargs):
    form_class = kwargs.pop('form_class')
    defaults = {'data': request.POST}

    extra_args_func = kwargs.pop('callback',
                                 lambda request, *args, **kwargs: {})
    defaults.update(kwargs)

    kwargs = extra_args_func(request, *args, **kwargs)
    defaults.update(kwargs)
    form = form_class(**defaults)

    if form.is_valid():
        data = {
            'valid': True,
        }
    else:
        # if we're dealing with a FormSet then walk over .forms to populate errors and formfields
        if isinstance(form, BaseFormSet):
            errors = {}
            formfields = {}
            for f in form.forms:
                for field in f.fields.keys():
                    formfields[f.add_prefix(field)] = f[field]
                for field, error in f.errors.iteritems():
                    errors[f.add_prefix(field)] = error
            if form.non_form_errors():
                errors['__all__'] = form.non_form_errors()
        else:
            errors = form.errors
            formfields = dict([(fieldname, form[fieldname])
                               for fieldname in form.fields.keys()])

        # if fields have been specified then restrict the error list
        if request.POST.getlist('fields'):
            fields = request.POST.getlist('fields') + ['__all__']
            errors = dict([(key, val) for key, val in errors.iteritems()
                           if key in fields])

        final_errors = {}
        for key, val in errors.iteritems():
            if '__all__' in key:
                final_errors[key] = val
            elif not isinstance(formfields[key].field, forms.FileField):
                html_id = formfields[key].field.widget.attrs.get(
                    'id') or formfields[key].auto_id
                html_id = formfields[key].field.widget.id_for_label(html_id)
                final_errors[html_id] = val
        data = {
            'valid': False or not final_errors,
            'errors': final_errors,
        }
    json_serializer = LazyEncoder()
    return HttpResponse(json_serializer.encode(data),
                        mimetype='application/json')
def validate(request, *args, **kwargs):
    form_class = kwargs.pop('form_class')

    #this lets us pass in form factories if we're using dynamic forms
    if callable(form_class) and not issubclass(type(form_class), (forms.Form, forms.ModelForm)):
        extra_class_args_func = kwargs.pop('class_callback', lambda request, *args, **kwargs: {}) 
        class_kwargs = extra_class_args_func(request, *args, **kwargs)
        form_class = form_class(**class_kwargs)

    defaults = {
        'data': request.POST
    }
    extra_args_func = kwargs.pop('callback', lambda request, *args, **kwargs: {})
    kwargs = extra_args_func(request, *args, **kwargs)
    defaults.update(kwargs)
    form = form_class(**defaults)
    if form.is_valid():
        data = {
            'valid': True,
        }
    else:
        # if we're dealing with a FormSet then walk over .forms to populate errors and formfields
        if isinstance(form, BaseFormSet):
            errors = {}
            formfields = {}
            for f in form.forms:
                for field in f.fields.keys():
                    formfields[f.add_prefix(field)] = f[field]
                for field, error in f.errors.iteritems():
                    errors[f.add_prefix(field)] = error
            if form.non_form_errors():
                errors['__all__'] = form.non_form_errors()
        else:
            errors = form.errors
            formfields = dict([(fieldname, form[fieldname]) for fieldname in form.fields.keys()])

        # if fields have been specified then restrict the error list
        if request.POST.getlist('fields'):
            fields = request.POST.getlist('fields') + ['__all__']
            errors = dict([(key, val) for key, val in errors.iteritems() if key in fields])

        final_errors = {}
        for key, val in errors.iteritems():
            if '__all__' in key:
                final_errors[key] = val
            elif not isinstance(formfields[key].field, forms.FileField):
                html_id = formfields[key].field.widget.attrs.get('id') or formfields[key].auto_id
                html_id = formfields[key].field.widget.id_for_label(html_id)
                final_errors[html_id] = val
        data = {
            'valid': False or not final_errors,
            'errors': final_errors,
        }
    json_serializer = LazyEncoder()
    return HttpResponse(json_serializer.encode(data), mimetype='application/json')
Beispiel #6
0
def validate(request, *args, **kwargs):
    form_class = kwargs.pop('form_class')
    defaults = {
        'data': request.POST
    }
    extra_args_func = kwargs.pop('callback', lambda request, *args, **kwargs: {})
    kwargs = extra_args_func(request, *args, **kwargs)
    defaults.update(kwargs)
    form = form_class(**defaults)
    fields = request.POST.getlist('fields')
    if not fields and not isinstance(form, BaseFormSet):
        fields = [form[f].field.widget.id_for_label(form[f].field.widget.attrs.get('id') or form[f].auto_id) for f in form.fields.keys()] + ['__all__']
    if form.is_valid():
        data = {
            'errors': {},
            'fields': fields,
            'valid': True,
        }
    else:
        # if we're dealing with a FormSet then walk over .forms to populate errors and formfields
        if isinstance(form, BaseFormSet):
            errors = {}
            formfields = {}
            for f in form.forms:
                for field in f.fields.keys():
                    formfields[f.add_prefix(field)] = f[field]
                for field, error in f.errors.iteritems():
                    errors[f.add_prefix(field)] = error
            if form.non_form_errors():
                errors['__all__'] = form.non_form_errors()
        else:
            errors = form.errors
            formfields = dict([(fieldname, form[fieldname]) for fieldname in form.fields.keys()])

        final_errors = {}
        for key, val in errors.iteritems():
            if '__all__' in key:
                field_id = key
            elif not isinstance(formfields[key].field, forms.FileField):
                field_id = formfields[key].field.widget.attrs.get('id') or formfields[key].auto_id
                field_id = formfields[key].field.widget.id_for_label(field_id)
            if not fields or field_id in fields:
                final_errors[field_id] = val
        data = {
            'fields': fields,
            'valid': False or not final_errors,
            'errors': final_errors,
        }
    json_serializer = LazyEncoder()
    return HttpResponse(json_serializer.encode(data), mimetype='application/json')
def validate(request, *args, **kwargs):
    form_class = kwargs.pop('form_class')
    defaults = {
        'data': request.POST
    }
    extra_args_func = kwargs.pop('callback', lambda request, *args, **kwargs: {})
    kwargs = extra_args_func(request, *args, **kwargs)
    defaults.update(kwargs)
    form = form_class(**defaults)
    if form.is_valid():
        data = {
            'errors': {},
            'fields': request.POST.getlist('fields'),
            'valid': True,
        }
    else:
        # if we're dealing with a FormSet then walk over .forms to populate errors and formfields
        if isinstance(form, BaseFormSet):
            errors = {}
            formfields = {}
            for f in form.forms:
                for field in f.fields.keys():
                    formfields[f.add_prefix(field)] = f[field]
                for field, error in f.errors.iteritems():
                    errors[f.add_prefix(field)] = error
            if form.non_form_errors():
                errors['__all__'] = form.non_form_errors()
        else:
            errors = form.errors
            formfields = dict([(fieldname, form[fieldname]) for fieldname in form.fields.keys()])

        # if fields have been specified then restrict the error list
        if request.POST.getlist('fields'):
            fields = request.POST.getlist('fields') + ['__all__']
            errors = dict([(key, val) for key, val in errors.iteritems() if key in fields])
        else:
            fields = []

        data = {
            'fields': fields,
            'valid': not errors,
            'errors': errors,
        }
    json_serializer = LazyEncoder()
    return HttpResponse(json_serializer.encode(data), mimetype='application/json')
def validate(request, *args, **kwargs):
    form_class = kwargs.pop("form_class")
    defaults = {"data": request.POST}
    extra_args_func = kwargs.pop("callback", lambda request, *args, **kwargs: {})
    kwargs = extra_args_func(request, *args, **kwargs)
    defaults.update(kwargs)
    form = form_class(**defaults)
    if form.is_valid():
        data = {"valid": True}
    else:
        # if we're dealing with a FormSet then walk over .forms to populate errors and formfields
        if isinstance(form, BaseFormSet):
            errors = {}
            formfields = {}
            for f in form.forms:
                for field in f.fields.keys():
                    formfields[f.add_prefix(field)] = f[field]
                for field, error in f.errors.iteritems():
                    errors[f.add_prefix(field)] = error
            if form.non_form_errors():
                errors["__all__"] = form.non_form_errors()
        else:
            errors = form.errors
            formfields = dict([(fieldname, form[fieldname]) for fieldname in form.fields.keys()])

        # if fields have been specified then restrict the error list
        fields = request.POST.getlist("fields")
        if fields:
            fields = fields + ["__all__"]
            errors = dict([(key, val) for key, val in errors.iteritems() if key in fields])

        final_errors = {}
        for key, val in errors.iteritems():
            if "__all__" in key:
                final_errors[key] = val
            elif not isinstance(formfields[key].field, forms.FileField):
                html_id = formfields[key].field.widget.attrs.get("id") or formfields[key].auto_id
                html_id = formfields[key].field.widget.id_for_label(html_id)
                final_errors[html_id] = val
        data = {"valid": False or not final_errors, "errors": final_errors}
    json_serializer = LazyEncoder()
    return HttpResponse(json_serializer.encode(data), mimetype="application/json")
def validate(request, *args, **kwargs):
    if 'save_session' in kwargs:
        save_session = kwargs['save_session']
    else:
        save_session = False
    
    if 'return_form_data' in kwargs and kwargs['return_form_data'] == True:
        return_form_data = True
    else:
        return_form_data = False
    
    if 'prefix' in kwargs:
        prefix = kwargs['prefix']
    else:
        prefix = None
    
    form_class = kwargs.pop('form_class')
    defaults = {
        'data': request.POST,
        'prefix': prefix,
    }
    extra_args_func = kwargs.pop('callback', lambda request, *args, **kwargs: {})
    kwargs = extra_args_func(request, *args, **kwargs)
    defaults.update(kwargs)
    form = form_class(**defaults)
    if form.is_valid():
        # Conditionally save the post data for later use
        if save_session:
            # Save in simple dict if no session key given
            if type(save_session) == type(bool()):
                if return_form_data:
                    request.session[form.__class__.__name__] = form.cleaned_data
                else:
                    request.session[form.__class__.__name__] = request.POST
            # Save in specified session key
            else:
                if not save_session in request.session:
                    request.session[save_session] = dict()
                if return_form_data:
                    request.session[save_session][form.__class__.__name__] = form.cleaned_data
                else:
                    request.session[save_session][form.__class__.__name__] = request.POST
            # Make sure the updated session gets saved
            request.session.modified = True
        
        data = {
            'valid': True,
        }
        
        if return_form_data:
            data['form'] = form.cleaned_data
    else:
        # if we're dealing with a FormSet then walk over .forms to populate errors and formfields
        if isinstance(form, BaseFormSet):
            errors = {}
            formfields = {}
            for f in form.forms:
                for field in f.fields.keys():
                    formfields[f.add_prefix(field)] = f[field]
                for field, error in f.errors.iteritems():
                    errors[f.add_prefix(field)] = error
            if form.non_form_errors():
                errors['__all__'] = form.non_form_errors()
        else:
            errors = form.errors
            formfields = dict([(fieldname, form[fieldname]) for fieldname in form.fields.keys()])

        # if fields have been specified then restrict the error list
        if request.POST.getlist('fields'):
            fields = request.POST.getlist('fields') + ['__all__']
            errors = dict([(key, val) for key, val in errors.iteritems() if key in fields])

        final_errors = {}
        for key, val in errors.iteritems():
            if '__all__' in key:
                final_errors[key] = val
            elif not isinstance(formfields[key].field, forms.FileField):
                html_id = formfields[key].field.widget.attrs.get('id') or formfields[key].auto_id
                html_id = formfields[key].field.widget.id_for_label(html_id)
                final_errors[html_id] = val
        data = {
            'valid': False or not final_errors,
            'errors': final_errors,
        }
    json_serializer = LazyEncoder()
    return HttpResponse(json_serializer.encode(data), mimetype='application/json')