Ejemplo n.º 1
0
    def __str__(self):
        class_name = get_class_name(self)
        attrs_str = []
        for attr in self.attrs:
            attrs_str.append('%s: "%s"' % (attr, getattr(self, attr) if hasattr(self, attr) else None))

        return '%s(%s)' % (class_name, ', '.join(attrs_str))
Ejemplo n.º 2
0
    def delete(self):
        class_name = get_class_name(self)

        if not hasattr(self, 'id') or type(self.id) is uuid.UUID:
            raise SquadObjectException('Failed to delete %s: it must contain a valid "id"' % class_name)

        endpoint = '%s%s/' % (self.endpoint, self.id)

        try:
            response = SquadApi.delete(endpoint)
            if response.status_code in [400, 401, 405]:
                raise SquadObjectException('Failed to delete %s: %s' % (class_name, response.text))

        except ApiException as e:
            logger.error(e)
            raise SquadObjectException(str(e))
Ejemplo n.º 3
0
    def save(self):
        self.pre_save()

        data = {}
        class_name = get_class_name(self)

        for attr in self.attrs:
            if not hasattr(self, attr):
                continue

            value = getattr(self, attr, None)
            if value is None:
                continue

            if isinstance(value, SquadObject):
                # value is a relation object
                if not hasattr(value, 'id'):
                    raise SquadObjectException(
                        'Failed to save %s: %s has no id' % (class_name, attr))
                # TODO: some objects have url as reference to other objects, ex:
                # project.group is a group url, instead of id
                # we need to standardize it
                # For now, only projects can be created this way
                data[attr] = value.url
            else:
                data[attr] = value

        endpoint = self.endpoint
        request = SquadApi.post
        if hasattr(self, 'id') and type(self.id) is not uuid.UUID:
            endpoint = '%s%s/' % (endpoint, self.id)
            request = SquadApi.patch

        try:
            response = request(endpoint, data=data)
            if response.status_code in [400, 401, 405]:
                raise SquadObjectException('Failed to save %s: %s' %
                                           (class_name, response.text))

            self.__fill_object__(response.json())
            self.post_save()

        except ApiException as e:
            logger.error(e)
            raise SquadObjectException(str(e))
Ejemplo n.º 4
0
    def __fill_object__(self, result, obj=None):

        if obj is None:
            obj = self

        nested_endpoints = [
            x[1] for x in SquadApi.schema_nested_eps
            if x[0].lower() == get_class_name(obj).lower()
        ]
        if nested_endpoints:
            setattr(obj, 'nested_endpoints', nested_endpoints)

        attrs = obj.attrs if len(
            obj.attrs) else [attr for attr in result.keys()]
        for attr in attrs:
            if attr in result.keys():
                setattr(
                    obj,
                    attr.replace(' ', '_').replace('/', '_').replace('-', '_'),
                    result[attr])