Пример #1
0
    def get_value(self):
        obj = self.field.proxied_args['ObjectArg']
        val = utils.resolve_dotted_attribute(obj, self.field.model_field_name)

        # Prevent throwing a MethodException if the value is None
        if val is None:
            return None
        return self.get_method(val)(*self.args, **self.kwargs)
Пример #2
0
    def get_value(self):
        obj = self.field.proxied_args['ObjectArg']
        val = utils.resolve_dotted_attribute(obj, self.field.model_field_name)

        # Prevent throwing a MethodException if the value is None
        if val is None:
            return None
        return self.get_method(val)(*self.args, **self.kwargs)
Пример #3
0
    def get_method(self, obj=None):
        """ Resolves the given method into a callable object.

            If 'obj' is provided, the method will be looked for as an
            attribute of the 'obj'

            Supports dotted names.

            Usage:
                >>> method = Method('obj.obj.method', RequestArg)
                >>> method()
                'Result of method called with: obj.obj.method(request)'
                >>>

                Dotted attributes are most useful when using something like an
                an ObjectMethod:

                (where 'user' is an instance of Django's 'User' model,
                    the Object in this example is the 'user' instance)

                >>> method = ObjectMethod('date_joined.strftime', '%Y-%m-%d %H:%M:%S')
                >>> method()
                2009-10-02 09:58:39
                >>>

                The actual method call looks like:
                >>> user.date_joined.strftime('%Y-%m-%d %H:%M:%S')
                2009-10-02 09:58:39
                >>>

                It also supports attributes which are not actually methods:

                >>> method = ObjectMethod('first_name', 'ignored arguments', ...) # Arguments to a non-callable are ignored.
                >>> method()
                u'Bilbo'
                >>> method = ValueMethod('first_name', 'upper') # Called on the returned value
                >>> method()
                u'BILBO'
                >>>

                The method call for the last one looks like:
                >>> user.first_name.upper()
                u'BILBO'
                >>>

        """

        if callable(self.method_or_methodname):
            return self.method_or_methodname

        if not isinstance(self.method_or_methodname, (str, unicode)):
            raise MethodException('Method must a string or callable')

        if obj is not None:

            try:
                method = utils.resolve_dotted_attribute(
                    obj, self.method_or_methodname)
            except AttributeError:
                raise MethodException(
                    'Cannot resolve method "%s" in object "%s"' %
                    (self.method_or_methodname, type(obj)))

            if not callable(method):

                # Turn this into a callable
                m = method

                def _m(*args, **kwargs):
                    return m

                method = _m

            return method

        try:
            return eval(self.method_or_methodname
                        )  # Just try to get it in current scope
        except NameError:
            raise MethodException('Cannot resolve method "%s"' %
                                  self.method_or_methodname)
Пример #4
0
    def get_method(self, obj=None):
        """ Resolves the given method into a callable object.

            If 'obj' is provided, the method will be looked for as an
            attribute of the 'obj'

            Supports dotted names.

            Usage:
                >>> method = Method('obj.obj.method', RequestArg)
                >>> method()
                'Result of method called with: obj.obj.method(request)'
                >>>

                Dotted attributes are most useful when using something like an
                an ObjectMethod:

                (where 'user' is an instance of Django's 'User' model,
                    the Object in this example is the 'user' instance)

                >>> method = ObjectMethod('date_joined.strftime', '%Y-%m-%d %H:%M:%S')
                >>> method()
                2009-10-02 09:58:39
                >>>

                The actual method call looks like:
                >>> user.date_joined.strftime('%Y-%m-%d %H:%M:%S')
                2009-10-02 09:58:39
                >>>

                It also supports attributes which are not actually methods:

                >>> method = ObjectMethod('first_name', 'ignored arguments', ...) # Arguments to a non-callable are ignored.
                >>> method()
                u'Bilbo'
                >>> method = ValueMethod('first_name', 'upper') # Called on the returned value
                >>> method()
                u'BILBO'
                >>>

                The method call for the last one looks like:
                >>> user.first_name.upper()
                u'BILBO'
                >>>

        """

        if callable(self.method_or_methodname):
            return self.method_or_methodname

        if not isinstance(self.method_or_methodname, (str, unicode)):
            raise MethodException("Method must a string or callable")

        if obj is not None:

            try:
                method = utils.resolve_dotted_attribute(obj, self.method_or_methodname)
            except AttributeError:
                raise MethodException(
                    'Cannot resolve method "%s" in object "%s"' % (self.method_or_methodname, type(obj))
                )

            if not callable(method):

                # Turn this into a callable
                m = method

                def _m(*args, **kwargs):
                    return m

                method = _m

            return method

        try:
            return eval(self.method_or_methodname)  # Just try to get it in current scope
        except NameError:
            raise MethodException('Cannot resolve method "%s"' % self.method_or_methodname)