Exemplo n.º 1
0
    def update(self, instance, auto_update=True):
        """Update a model instance.

        An InvalidRequestException will be raised if the instance has not been
        marked as existing.

        :param instance: The model instance being updated.
        :param auto_update: Auto-update the instance with the values from
                            Shopify. When set to False, the raw JSON object
                            will be returned.

        example usage::

            from shopify_trois.engines.http import Json as Shopify
            from shopify_trois import Credentials
            from shopify_trois.models import Blog

            credentials = Credentials(...)
            shopify = Shopify(store_name="test", credentials=credentials)

            blog = shopify.fetch(Blog, 4)
            blog.commentable = False
            shopify.update(blog)

        """

        self._can_request("update", instance)

        if not instance._meta__.exists:
            msg = "The supplied instance does not yet exists."
            raise InvalidRequestException(msg)

        enclosure = instance.enclosure or instance.to_underscore_name()

        if self.ignore_model_properties:
            data = instance.to_dict()
        else:
            data = dict([
                (k, v) for k, v in instance.to_dict().items()
                if k in instance.properties
            ])

        req = Request(instance)
        req.data = json.dumps({enclosure: data})

        res = self._put(req)

        if res.status_code == requests.codes.ok:
            if auto_update:
                instance.update(
                    res.json(),
                    ignore_properties=self.ignore_model_properties
                )
                return
            else:
                return res.json()

        raise ShopifyException(res)
Exemplo n.º 2
0
    def update(self, instance, auto_update=True):
        """Update a model instance.

        An InvalidRequestException will be raised if the instance has not been
        marked as existing.

        :param instance: The model instance being updated.
        :param auto_update: Auto-update the instance with the values from
                            Shopify. When set to False, the raw JSON object
                            will be returned.

        example usage::

            from shopify_trois.engines.http import Json as Shopify
            from shopify_trois import Credentials
            from shopify_trois.models import Blog

            credentials = Credentials(...)
            shopify = Shopify(store_name="test", credentials=credentials)

            blog = shopify.fetch(Blog, 4)
            blog.commentable = False
            shopify.update(blog)

        """

        self._can_request("update", instance)

        if not instance._meta__.exists:
            msg = "The supplied instance does not yet exists."
            raise InvalidRequestException(msg)

        enclosure = instance.enclosure or instance.to_underscore_name()

        if self.ignore_model_properties:
            data = instance.to_dict()
        else:
            data = dict([(k, v) for k, v in instance.to_dict().items()
                         if k in instance.properties])

        req = Request(instance)
        req.data = json.dumps({enclosure: data})

        res = self._put(req)

        if res.status_code == requests.codes.ok:
            if auto_update:
                instance.update(res.json(),
                                ignore_properties=self.ignore_model_properties)
                return
            else:
                return res.json()

        raise ShopifyException(res)
Exemplo n.º 3
0
    def add(self, instance, auto_update=True):
        """Add the model instance to the store.

        :param instance: The model instance being added.
        :param auto_update: Auto-update the instance with the values from
                            Shopify. When set to false, the raw JSON object
                            will be returned.

        example usage::

            from shopify_trois.engines.http import Json as Shopify
            from shopify_trois import Credentials
            from shopify_trois.models import Blog

            credentials = Credentials(...)
            shopify = Shopify(store_name="test", credentials=credentials)

            blog = Blog(title="Awesome")
            shopify.add(blog)

        """

        self._can_request("create", instance)

        req = Request(instance)

        enclosure = instance.enclosure or instance.to_underscore_name()

        if self.ignore_model_properties:
            data = instance.to_dict()
        else:
            data = dict([
                (k, v) for k, v in instance.to_dict().items()
                if k in instance.properties
            ])

        req.data = json.dumps({enclosure: data})

        res = self._post(req)

        if res.status_code == requests.codes.created:
            if auto_update:
                instance.update(
                    res.json(),
                    ignore_properties=self.ignore_model_properties
                )
                return
            else:
                return res.json()

        raise ShopifyException(res)
Exemplo n.º 4
0
    def add(self, instance, auto_update=True):
        """Add the model instance to the store.

        :param instance: The model instance being added.
        :param auto_update: Auto-update the instance with the values from
                            Shopify. When set to false, the raw JSON object
                            will be returned.

        example usage::

            from shopify_trois.engines.http import Json as Shopify
            from shopify_trois import Credentials
            from shopify_trois.models import Blog

            credentials = Credentials(...)
            shopify = Shopify(store_name="test", credentials=credentials)

            blog = Blog(title="Awesome")
            shopify.add(blog)

        """

        self._can_request("create", instance)

        req = Request(instance)

        enclosure = instance.enclosure or instance.to_underscore_name()

        if self.ignore_model_properties:
            data = instance.to_dict()
        else:
            data = dict([(k, v) for k, v in instance.to_dict().items()
                         if k in instance.properties])

        req.data = json.dumps({enclosure: data})

        res = self._post(req)

        if res.status_code == requests.codes.created:
            if auto_update:
                instance.update(res.json(),
                                ignore_properties=self.ignore_model_properties)
                return
            else:
                return res.json()

        raise ShopifyException(res)
Exemplo n.º 5
0
    def custom_post(self, instance, action, data=None):
        """Executes a custom post method on an instance.

        :param instance: The model instance being modified.
        :param action: The action to be executed.
        """

        self._can_request(action, instance)

        req = Request(instance)
        req.resource += action
        req.data = data

        res = self._post(req)
        if res.status_code == requests.codes.ok:
            return res.json()

        raise ShopifyException(res)
Exemplo n.º 6
0
    def custom_post(self, instance, action, data=None):
        """Executes a custom post method on an instance.

        :param instance: The model instance being modified.
        :param action: The action to be executed.
        """

        self._can_request(action, instance)

        req = Request(instance)
        req.resource += action
        req.data = data

        res = self._post(req)
        if res.status_code == requests.codes.ok:
            return res.json()

        raise ShopifyException(res)