Exemplo n.º 1
0
def _jinja2_url_for_other_page(page, remove=None, **kwargs):
    args = MultiDict()
    if request.view_args is not None and request.view_args != [] and request.view_args != {}:
        args.update(convert_type(request.view_args.copy()))
    if request.args is not None and request.args != [] and request.args != {}:
        args.update(convert_type(request.args.to_dict(flat=False)))

    # Process removes
    if remove is not None:
        if isinstance(remove, (basestring, int)):
            args.poplist(remove)
        elif isinstance(remove, list):
            for item in remove:
                if isinstance(item, (basestring, int)):
                    args.poplist(item)
                elif isinstance(item, tuple):
                    key, value = item
                    arg_values = args.poplist(key)
                    if arg_values is not None and arg_values != []:
                        new_values = [i for i in arg_values if not equal_args(i, value)]
                        args.setlist(key, new_values)
                else:
                    raise ValueError
        elif isinstance(remove, dict):
            for key, value in remove.items():
                if isinstance(value, (basestring, int)):
                    arg_values = args.poplist(key)
                    new_values = [i for i in arg_values if not equal_args(i, value)]
                    args.setlist(key, new_values)
                elif isinstance(value, (list, tuple)):
                    arg_values = args.poplist(key)
                    if arg_values is not None and arg_values != []:
                        new_values = [i for i in arg_values if not equal_args(i, value)]
                        args.setlist(key, new_values)
                else:
                    if current_app.debug:
                        print("Could not parse value : {}".format(value))
                    raise ValueError
        else:
            raise ValueError
   
    if kwargs is not None:
        args.update(convert_type(kwargs.copy()))

    # Remove duplicates
    for key in args.keys():
        items = args.poplist(key)
        items = [convert_type(item) for item in items]
        items = list(set(items))
        args.setlist(key, items)

    return url_for(page, **args)
Exemplo n.º 2
0
	def validate(self, values):
		"""
		Validate the given dict of values against the validators and post validators of this schema. This dict should be a
		dict with list values or an instance of a werkzeug `MultiDict`.

		Any parameter that is not explicitely validated will be returned as extra values.

		If a parameter is found it is passed to the validator's :meth:`~framework.validators.schema.Validator.validate` method that will either
		return a valid output or trigger a validation error.

		After individual validators are processed, the resulting valid parameters are passed to each `post_validators`.
		Each post validator can change the resulting values or trigger a validation error.

		When a validation error is triggered, an entry is added to the error dict mapping the field name that produced the error
		to an instance of :exc:`~framework.validators.errors.Invalid` (containing the error message and error value).

		After those steps, the validated values, the validation errors and the extra values dicts are returned.

		:param values: The dict of values to validate against this schema
		:type values: dict

		:return: The dict of validated values, The dict of error messages, The dict of extra values
		:rtype: a tuple (dict, dict)
		"""

		# Ensure we have a **new** dict with lists as values if it's a werkzeug MultiDict
		#  we are going to pop values from it...
		values = MultiDict(values)

		new_values = {}
		error_values = {}

		for name, validator in self.validators.iteritems():
			# Determine the key to fetch from the received values
			field = validator.field or name
			# Get the list of values, which may be empty if the field was not present or contain some values
			if validator.startswith:
				value = [(x, values[x]) for x in values.iterkeys() if x.startswith(validator.startswith)]
			else:
				value = values.poplist(field)

			try:
				value = validator.validate(value)
				if value is not NO_VALUE:
					new_values[name] = value
			except Invalid, e:
				error_values[e.field or field] = e