Ejemplo n.º 1
0
    def __getattr__(self, item):
        if item in ('category', 'status') and item in self._attributes:
            manager = ResourceManager(self.manager.redmine,
                                      'Deal{0}'.format(item.capitalize()))
            return manager.to_resource(self._attributes[item])

        return super(Deal, self).__getattr__(item)
Ejemplo n.º 2
0
    def __getattr__(self, item):
        if item == 'source' and item in self._attributes and self._attributes[
                item].get('type') in ('Deal', 'Contact'):
            manager = ResourceManager(self.manager.redmine,
                                      self._attributes[item]['type'])
            return manager.to_resource(self._attributes[item])

        return super(Note, self).__getattr__(item)
Ejemplo n.º 3
0
    def __getattr__(self, item):
        """Returns the requested attribute and makes a conversion if needed"""
        if item.startswith('_'):
            raise AttributeError

        if item in self._attributes:
            # If item shouldn't be converted let's return it as it is
            if item in self._unconvertible:
                return self._attributes[item]

            # If item should be a Resource object, let's convert it
            elif item in _RESOURCE_MAP:
                manager = ResourceManager(self.manager.redmine,
                                          _RESOURCE_MAP[item])
                return manager.to_resource(self._attributes[item])

            # If item should be a ResourceSet object, let's convert it
            elif item in _RESOURCE_SET_MAP and self._attributes[
                    item] is not None:
                manager = ResourceManager(self.manager.redmine,
                                          _RESOURCE_SET_MAP[item])
                return manager.to_resource_set(self._attributes[item])

            # If item is a relation and should be requested from Redmine, let's do it
            elif item in self._relations and self._attributes[item] is None:
                filters = {
                    '{0}_id'.format(self._relations_name): self.internal_id
                }
                manager = ResourceManager(self.manager.redmine,
                                          _RESOURCE_RELATIONS_MAP[item])
                self._attributes[item] = manager.filter(**filters)
                return self._attributes[item]

            # If item is an include and should be requested from Redmine, let's do it
            elif item in self._includes and self._attributes[item] is None:
                self._attributes[item] = self.refresh(
                    include=item)._attributes[item] or []
                return getattr(self, item)

        try:
            # If the requested item is a date/datetime string
            # we need to convert it to the appropriate object
            possible_dt = str(self._attributes[item])

            try:
                return datetime.strptime(possible_dt,
                                         self.manager.redmine.datetime_format)
            except ValueError:
                return datetime.strptime(
                    possible_dt, self.manager.redmine.date_format).date()
        except ValueError:
            return self._attributes[item]
        except KeyError:
            if self.is_new():
                if item in ('id', 'version'):
                    return 0
                return ''

            return self._action_if_attribute_absent()
Ejemplo n.º 4
0
    def __getattr__(self, item):
        if item == 'project':
            return Contact.Project(self)
        elif item == 'phones':
            return [p.get('number') if isinstance(p, dict) else p for p in self._attributes.get('phones', [])]
        elif item == 'emails':
            return [e.get('address') if isinstance(e, dict) else e for e in self._attributes.get('emails', [])]
        elif item == 'avatar' and item in self._attributes:
            manager = ResourceManager(self.manager.redmine, 'Attachment')
            return manager.to_resource({'id': self._attributes[item].get('attachment_id', 0)})

        return super(Contact, self).__getattr__(item)
Ejemplo n.º 5
0
    def __getattr__(self, item):
        if item == 'parent' and item in self._attributes:
            manager = ResourceManager(self.manager.redmine, 'WikiPage')
            manager.params['project_id'] = self.manager.params.get('project_id', 0)
            return manager.to_resource(self._attributes[item])

        # If a text attribute of a resource is missing, we should
        # refresh a resource automatically for user's convenience
        try:
            return super(WikiPage, self).__getattr__(item)
        except ResourceAttrError:
            if 'text' not in self._attributes:
                self._attributes = self.refresh()._attributes

            return super(WikiPage, self).__getattr__(item)
Ejemplo n.º 6
0
    def __getattr__(self, item):
        if item == 'parent' and item in self._attributes:
            manager = ResourceManager(self.manager.redmine, 'WikiPage')
            manager.params['project_id'] = self.manager.params.get(
                'project_id', 0)
            return manager.to_resource(self._attributes[item])

        # If a text attribute of a resource is missing, we should
        # refresh a resource automatically for user's convenience
        try:
            return super(WikiPage, self).__getattr__(item)
        except ResourceAttrError:
            if 'text' not in self._attributes:
                self._attributes = self.refresh()._attributes

            return super(WikiPage, self).__getattr__(item)
Ejemplo n.º 7
0
    def __getattr__(self, item):
        if item == 'parent' and item in self._attributes:
            return ResourceManager(self.manager.redmine,
                                   'Project').to_resource(
                                       self._attributes[item])

        return super(Project, self).__getattr__(item)
Ejemplo n.º 8
0
    def __getattr__(self, item):
        if item == 'version':
            return super(Issue, self).__getattr__('fixed_version')
        elif item == 'watcher':
            return Issue.Watcher(self)
        elif item == 'parent' and item in self._attributes:
            return ResourceManager(self.manager.redmine,
                                   'Issue').to_resource(self._attributes[item])

        return super(Issue, self).__getattr__(item)
Ejemplo n.º 9
0
    def __getattr__(self, item):
        if item == 'project':
            return Contact.Project(self)
        elif item == 'phones':
            return [
                p.get('number') if isinstance(p, dict) else p
                for p in self._attributes.get('phones', [])
            ]
        elif item == 'emails':
            return [
                e.get('address') if isinstance(e, dict) else e
                for e in self._attributes.get('emails', [])
            ]
        elif item == 'avatar' and item in self._attributes:
            manager = ResourceManager(self.manager.redmine, 'Attachment')
            return manager.to_resource(
                {'id': self._attributes[item].get('attachment_id', 0)})

        return super(Contact, self).__getattr__(item)
Ejemplo n.º 10
0
    def __getattr__(self, item):
        """Returns the requested attribute and makes a conversion if needed"""
        if item in self.attributes:
            # If item should be a Resource object, let's convert it
            if item in _RESOURCE_MAP:
                manager = ResourceManager(self.manager.redmine, _RESOURCE_MAP[item])
                return manager.to_resource(self.attributes[item])

            # If item should be a ResourceSet object, let's convert it
            elif item in _RESOURCE_SET_MAP and self.attributes[item] is not None:
                manager = ResourceManager(self.manager.redmine, _RESOURCE_SET_MAP[item])
                return manager.to_resource_set(self.attributes[item])

            # If item should be requested from Redmine, let's do it
            elif item in _RESOURCE_RELATIONS_MAP and self.attributes[item] is None:
                filters = {'{0}_id'.format(self.__class__.__name__.lower()): self.id}
                manager = ResourceManager(self.manager.redmine, _RESOURCE_RELATIONS_MAP[item])
                return manager.filter(**filters)

        try:
            # If the requested item is a date/datetime string
            # we need to convert it to the appropriate object
            possible_dt = str(self.attributes[item])

            try:
                return datetime.strptime(possible_dt, self.manager.redmine.datetime_format)
            except ValueError:
                return datetime.strptime(possible_dt, self.manager.redmine.date_format).date()
        except ValueError:
            return self.attributes[item]
        except KeyError:
            raise ResourceAttrError()
Ejemplo n.º 11
0
    def __getattr__(self, item):
        if item == 'parent' and item in self._attributes:
            return ResourceManager(self.manager.redmine,
                                   'Project').to_resource(
                                       self._attributes[item])

        value = super(Project, self).__getattr__(item)

        if item == 'enabled_modules':
            value = [
                module.get('name') if isinstance(module, dict) else module
                for module in value
            ]

        return value
Ejemplo n.º 12
0
    def __getattr__(self, item):
        """Returns the requested attribute and makes a conversion if needed"""
        if item.startswith('_'):
            raise AttributeError

        if item in self._attributes:
            # If item shouldn't be converted let's return it as it is
            if item in self._unconvertible:
                return self._attributes[item]

            # If item should be a Resource object, let's convert it
            elif item in _RESOURCE_MAP:
                manager = ResourceManager(self.manager.redmine, _RESOURCE_MAP[item])
                return manager.to_resource(self._attributes[item])

            # If item should be a ResourceSet object, let's convert it
            elif item in _RESOURCE_SET_MAP and self._attributes[item] is not None:
                manager = ResourceManager(self.manager.redmine, _RESOURCE_SET_MAP[item])
                return manager.to_resource_set(self._attributes[item])

            # If item is a relation and should be requested from Redmine, let's do it
            elif item in self._relations and self._attributes[item] is None:
                filters = {'{0}_id'.format(self._relations_name): self.internal_id}
                manager = ResourceManager(self.manager.redmine, _RESOURCE_RELATIONS_MAP[item])
                self._attributes[item] = manager.filter(**filters)
                return self._attributes[item]

            # If item is an include and should be requested from Redmine, let's do it
            elif item in self._includes and self._attributes[item] is None:
                self._attributes[item] = self.refresh(include=item)._attributes[item] or []
                return getattr(self, item)

        try:
            # If the requested item is a date/datetime string
            # we need to convert it to the appropriate object
            possible_dt = str(self._attributes[item])

            try:
                return datetime.strptime(possible_dt, self.manager.redmine.datetime_format)
            except ValueError:
                return datetime.strptime(possible_dt, self.manager.redmine.date_format).date()
        except ValueError:
            return self._attributes[item]
        except KeyError:
            if self.is_new():
                if item in ('id', 'version'):
                    return 0
                return ''

            return self._action_if_attribute_absent()
Ejemplo n.º 13
0
    def __getattr__(self, item):
        """Returns the requested attribute and makes a conversion if needed"""
        if item in self.attributes:
            # If item should be a Resource object, let's convert it
            if item in _RESOURCE_MAP:
                manager = ResourceManager(self.manager.redmine, _RESOURCE_MAP[item])
                return manager.to_resource(self.attributes[item])

            # If item should be a ResourceSet object, let's convert it
            elif item in _RESOURCE_SET_MAP and self.attributes[item] is not None:
                manager = ResourceManager(self.manager.redmine, _RESOURCE_SET_MAP[item])
                return manager.to_resource_set(self.attributes[item])

            # If item is a relation and should be requested from Redmine, let's do it
            elif item in self._relations and self.attributes[item] is None:
                filters = {'{0}_id'.format(self.__class__.__name__.lower()): self.internal_id}
                manager = ResourceManager(self.manager.redmine, _RESOURCE_RELATIONS_MAP[item])
                self.attributes[item] = manager.filter(**filters)
                return self.attributes[item]

            # If item is an include and should be requested from Redmine, let's do it
            elif item in self._includes and self.attributes[item] is None:
                self.attributes[item] = self.refresh(include=item).attributes[item]
                return getattr(self, item)

        try:
            # If the requested item is a date/datetime string
            # we need to convert it to the appropriate object
            possible_dt = str(self.attributes[item])

            try:
                return datetime.strptime(possible_dt, self.manager.redmine.datetime_format)
            except ValueError:
                return datetime.strptime(possible_dt, self.manager.redmine.date_format).date()
        except ValueError:
            return self.attributes[item]
        except KeyError:
            raise_attr_exception = self.manager.redmine.raise_attr_exception

            if isinstance(raise_attr_exception, bool) and raise_attr_exception:
                raise ResourceAttrError()
            elif isinstance(raise_attr_exception, (list, tuple)) and self.__class__.__name__ in raise_attr_exception:
                raise ResourceAttrError()

            return None
Ejemplo n.º 14
0
    def __getattr__(self, item):
        if item == 'source' and item in self._attributes and self._attributes[item].get('type') in ('Deal', 'Contact'):
            manager = ResourceManager(self.manager.redmine, self._attributes[item]['type'])
            return manager.to_resource(self._attributes[item])

        return super(Note, self).__getattr__(item)
Ejemplo n.º 15
0
    def __getattr__(self, item):
        if item in ('category', 'status') and item in self._attributes:
            manager = ResourceManager(self.manager.redmine, 'Deal{0}'.format(item.capitalize()))
            return manager.to_resource(self._attributes[item])

        return super(Deal, self).__getattr__(item)
Ejemplo n.º 16
0
    def __getattr__(self, resource):
        """Returns either ResourceSet or Resource object depending on the method used on the ResourceManager"""
        if resource.startswith('_'):
            raise AttributeError

        return ResourceManager(self, resource)
Ejemplo n.º 17
0
 def __getattr__(self, resource):
     """Returns either ResourceSet or Resource object depending on the method used on the ResourceManager"""
     return ResourceManager(self, resource)