def test_remove_from_cache(self): cache_key = 1 zenpy_object = self.cache_item(id=cache_key) self.assertIs(query_cache(get_object_type(zenpy_object), cache_key), zenpy_object) delete_from_cache(zenpy_object) self.assertIs(query_cache(get_object_type(zenpy_object), cache_key), None)
def add_to_cache(zenpy_object): """ Add a Zenpy object to the relevant cache. If no cache exists for this object nothing is done. """ object_type = get_object_type(zenpy_object) if object_type not in cache_mapping: return attr_name = _cache_key_attribute(object_type) cache_key = getattr(zenpy_object, attr_name) log.debug("Caching: [{}({}={})]".format(zenpy_object.__class__.__name__, attr_name, cache_key)) cache_mapping[object_type][cache_key] = zenpy_object
def add_to_cache(zenpy_object): """ Add a Zenpy object to the relevant cache. If no cache exists for this object nothing is done. """ object_type = get_object_type(zenpy_object) if object_type not in cache_mapping: return attr_name = _cache_key_attribute(object_type) cache_key = getattr(zenpy_object, attr_name) log.debug("Caching: [{}({}={})]".format(zenpy_object.__class__.__name__, attr_name, cache_key)) cache_mapping[object_type][cache_key] = zenpy_object
def delete_from_cache(to_delete): """ Purge one or more items from the relevant caches """ if not isinstance(to_delete, list): to_delete = [to_delete] for zenpy_object in to_delete: object_type = get_object_type(zenpy_object) object_cache = cache_mapping.get(object_type, None) if object_cache: removed_object = object_cache.pop(zenpy_object.id, None) if removed_object: log.debug("Cache RM: [%s %s]" % (object_type.capitalize(), zenpy_object.id))
def delete_from_cache(to_delete): """ Purge one or more items from the relevant caches """ if not isinstance(to_delete, list): to_delete = [to_delete] for zenpy_object in to_delete: object_type = get_object_type(zenpy_object) object_cache = cache_mapping.get(object_type, None) if object_cache: removed_object = object_cache.pop(zenpy_object.id, None) if removed_object: log.debug("Cache RM: [%s %s]" % (object_type.capitalize(), zenpy_object.id))
def format_key(self, key, parent): if key == 'result': key = "{}_result".format(get_object_type(parent)) elif key in ('metadata', 'from', 'system', 'photo', 'thumbnails'): key = '{}'.format(key) return key
def format_key(self, key, parent): if key == 'result': key = "{}_result".format(get_object_type(parent)) elif key in ('from', ): key = '{}_'.format(key) return key
def format_key(self, key, parent): if key == 'result': key = "{}_result".format(get_object_type(parent)) elif key in ('metadata', 'from', 'system', 'photo', 'thumbnails'): key = '{}'.format(key) return key
def test_cache_object_with_non_id(self): cache_key = 'somekey' user_field = self.cache_item(UserField, key=cache_key) self.assertIs(query_cache(get_object_type(user_field), cache_key), user_field)
def query_cache_by_object(zenpy_object): """ Convenience method for testing. Given an object, return the cached version """ object_type = get_object_type(zenpy_object) cache_key = _cache_key_attribute(object_type) return query_cache(object_type, getattr(zenpy_object, cache_key))
def should_cache(zenpy_object): """ Determine whether or not this object should be cached (ie, a cache exists for it's object_type) """ return get_object_type(zenpy_object) in cache_mapping
def test_cache_object(self): cache_key = 1 ticket = self.cache_item(id=cache_key) self.assertIs(query_cache(get_object_type(ticket), cache_key), ticket)
def build_payload(self, account): return {get_object_type(account): self.api._serialize(account)}
def in_cache(zenpy_object): """ Determine whether or not this object is in the cache """ object_type = get_object_type(zenpy_object) cache_key_attr = _cache_key_attribute(object_type) return query_cache(object_type, getattr(zenpy_object, cache_key_attr)) is not None
def post(self, rating, *args, **kwargs): url = self.api._build_url(self.api.endpoint.satisfaction_ratings(*args)) payload = {get_object_type(rating): self.api._serialize(rating)} return self.api._post(url, payload=payload)
def build_payload(self, help_centre_object): return {get_object_type(help_centre_object): self.api._serialize(help_centre_object)}
def test_remove_from_cache(self): cache_key = 1 zenpy_object = self.cache_item(id=cache_key) self.assertIs(query_cache(get_object_type(zenpy_object), cache_key), zenpy_object) delete_from_cache(zenpy_object) self.assertIs(query_cache(get_object_type(zenpy_object), cache_key), None)
def test_cache_object_with_non_id(self): cache_key = 'somekey' user_field = self.cache_item(UserField, key=cache_key) self.assertIs(query_cache(get_object_type(user_field), cache_key), user_field)
def test_cache_object(self): cache_key = 1 ticket = self.cache_item(id=cache_key) self.assertIs(query_cache(get_object_type(ticket), cache_key), ticket)
def should_cache(zenpy_object): """ Determine whether or not this object should be cached (ie, a cache exists for it's object_type) """ return get_object_type(zenpy_object) in cache_mapping
def in_cache(zenpy_object): """ Determine whether or not this object is in the cache """ object_type = get_object_type(zenpy_object) cache_key_attr = _cache_key_attribute(object_type) return query_cache(object_type, getattr(zenpy_object, cache_key_attr)) is not None
def build_payload(self, account): return {get_object_type(account): self.api._serialize(account)}
def query_cache_by_object(zenpy_object): """ Convenience method for testing. Given an object, return the cached version """ object_type = get_object_type(zenpy_object) cache_key = _cache_key_attribute(object_type) return query_cache(object_type, getattr(zenpy_object, cache_key))
def build_payload(self, translation): return {get_object_type(translation): self.api._serialize(translation)}
def post(self, rating, *args, **kwargs): url = self.api._build_url(self.api.endpoint.satisfaction_ratings(*args)) payload = {get_object_type(rating): self.api._serialize(rating)} return self.api._post(url, payload=payload)