def _generate_subresource_for_model(self, model, resource, is_instance): parent = model.is_subresource_of parent_id = None if is_instance: parent_pk = "{name}_{pk}".format( name=parent.__name__.lower(), pk=parent.primary_key ) if not hasattr(model, parent_pk): raise ShopifyException( "Missing parent primary key `%s`." % parent_pk ) parent_id = getattr(model, parent_pk) else: parent_pk = "parent_id" if self.params: parent_id = self.params.pop("parent_id", None) if parent_id is None: raise ShopifyException( "Missing parent primary key `%s`." % (parent_pk,) ) resource = "{parent}/{parent_id}/{resource}".format( parent=parent.resource, parent_id=parent_id, resource=model.resource ) return resource
def count(self, model, **params): """Return the count of a model, filtered by parameters 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) blogs = shopify.count(Blog) """ self._can_request("count", model) req = Request(model, **params) req.resource += "/count" res = self._get(req) if res.status_code == requests.codes.ok: data = res.json() return data["count"] raise ShopifyException(res)
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)
def oauth_access_token(self): """Fetch the OAuth access token from shopify. :meth:`setup_access_token` should be used. """ url = self.oauth_access_token_url() headers = {"Content-Type": self.mime} r = self.session.post(url, headers=headers) if r.status_code == requests.codes.ok: data = r.json() return data['access_token'] raise ShopifyException(r)
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)
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)
def fetch(self, model, primary_key=None, auto_instance=True, **params): """Get a specific model instance by primary key. :param Model: The class being queried. :param primary_key: The primary key value of the instance. :param auto_instance: Automatically create an instance instead of returning a json object. :param params: Query parameters. 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, 2) """ self._can_request("view", model) req = Request(model, **params) # Shop is the sole resource not following the same naming # convention for its resource url. if not model.__name__.lower() == "shop": req.resource += "/{primary_key}".format(primary_key=primary_key) res = self._get(req) if res.status_code == requests.codes.ok: data = res.json() if auto_instance: enclosure = model.enclosure or model.to_underscore_name() return model(**data[enclosure]) else: return data raise ShopifyException(res)
def delete(self, instance, **params): """Delete a model instance. An InvalidRequestException will be raised if the instance does not yet exists. :param instance: The model instance to remove. :param params: Query parameters. 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(id=3) shopify.delete(blog) """ self._can_request("delete", instance) if not instance._meta__.exists: msg = "The supplied instance does not yet exists." raise InvalidRequestException(msg) req = Request(instance) req.params = params res = self._delete(req) if res.status_code == requests.codes.ok: return True raise ShopifyException(res)
def index(self, model, auto_instance=True, **params): """Fetch the index for a given model and supplied parameters. :param model: The model being queried. :param auto_instance: Automatically create a Collection instance instead of returning a json object. :param params: Query parameters (see shopify documentation) 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) blogs = shopify.index(Blog) for blog in blogs: print(blog.to_dict()) """ self._can_request("index", model) req = Request(model, **params) res = self._get(req) if res.status_code == requests.codes.ok: if auto_instance: return Collection(model, res.json()) else: return res.json() raise ShopifyException(res)