def _build_list(cls, attributes):
     resources = []
     elements = Util.remove_root(attributes)
     if isinstance(elements, dict):
         elements = [elements]
     for element in elements:
         resources.append(cls(Util.remove_root(element)))
     return resources
 def __new__(mcs, name, bases, new_attrs):
     if '_singular' not in new_attrs or not new_attrs['_singular']:
         new_attrs['_singular'] = Util.underscore(name)
     if '_plural' not in new_attrs or not new_attrs['_plural']:
         new_attrs['_plural'] = Util.pluralize(new_attrs['_singular'])
     klass = type.__new__(mcs, name, bases, new_attrs)
     klass._fields = {}
     for attr, val in new_attrs.items():
         if isinstance(val, BaseField):
             klass._fields[attr] = val
     return klass
 def count(cls, opts=None, **kwargs):
     """Use this method to view the total number of resources
     in your account.
     """
     if opts is None:
         opts = kwargs
     return int(Util.remove_root(cls.get(custom_path="/count", **opts)))
Exemple #4
0
    def attachments_count(self, opts=None, **kwargs):
        """Use this method to view the total number of media attachments for
        a message in your account.
        """
        if opts is None:
            opts = kwargs

        url = self.klass._custom_path(id_=self.id, custom_path="/attachments/count", options=None) + \
            self.klass._query_string(opts)

        return Util.remove_root(Attachment.request.get(url))
    def _find_class_for(cls,
                        element_name=None,
                        class_name=None,
                        create_missing=True):
        if not element_name and not class_name:
            raise ResourceError('element_name or class_name must be specified')
        elif not element_name:
            element_name = Util.underscore(class_name)
        elif not class_name:
            class_name = Util.camelize(element_name)

        module_path = cls.__module__.split('.')
        for depth in range(len(module_path), 0, -1):
            try:
                __import__('.'.join(module_path[:depth]))
                module = sys.modules['.'.join(module_path[:depth])]
            except ImportError:
                continue
            try:
                klass = getattr(module, class_name)
                return klass
            except AttributeError:
                try:
                    __import__('.'.join([module.__name__, element_name]))
                    submodule = sys.modules['.'.join(
                        [module.__name__, element_name])]
                except ImportError:
                    continue
                try:
                    klass = getattr(submodule, class_name)
                    return klass
                except AttributeError:
                    continue

        # Woow, we made it this far, and no class was found
        if create_missing:
            return type(str(class_name), (cls, ),
                        {'__module__': cls.__module__})
    def save(self):
        """Saves :class:`Resource` object to the server.

        :returns: ``True`` on success, or throws an error.
        :raises: :class:`RequestorError`: On any communications errors.
            :class:`ResourceError`: On any other errors.
        """
        attributes = self._wrap_attributes(root=self._singular)
        if self.id:
            response = self.klass.request.put(
                self._element_path(self.id,
                                   path=None,
                                   options=self._prefix_options), attributes)
        else:
            response = self.klass.request.post(self._collection_path(
                path=None, options=self._prefix_options),
                                               params=attributes)
        self._update(Util.remove_root(response))
        return True
 def encode_body(cls, params=None):
     return cls._utf8(Util.to_json(params, root=None))
 def _query_string(cls, query_options=None):
     if query_options:
         return '?' + Util.to_query(query_options)
     else:
         return ''
 def _find_class_for_collection(cls, collection_name):
     return cls._find_class_for(Util.singularize(collection_name))
 def _build_object(cls, attributes):
     return cls(Util.remove_root(attributes))