Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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))
Exemplo n.º 5
0
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))
Exemplo n.º 6
0
 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
Exemplo n.º 7
0
 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
Exemplo n.º 8
0
 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
Exemplo n.º 9
0
 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)
Exemplo n.º 10
0
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))
Exemplo n.º 11
0
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
Exemplo n.º 12
0
 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)
Exemplo n.º 13
0
 def build_payload(self, account):
     return {get_object_type(account): self.api._serialize(account)}
Exemplo n.º 14
0
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
Exemplo n.º 15
0
 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)
Exemplo n.º 16
0
 def build_payload(self, help_centre_object):
     return {get_object_type(help_centre_object): self.api._serialize(help_centre_object)}
Exemplo n.º 17
0
 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)
Exemplo n.º 18
0
 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)
Exemplo n.º 19
0
 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)
Exemplo n.º 20
0
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
Exemplo n.º 21
0
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
Exemplo n.º 22
0
 def build_payload(self, account):
     return {get_object_type(account): self.api._serialize(account)}
Exemplo n.º 23
0
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))
Exemplo n.º 24
0
 def build_payload(self, translation):
     return {get_object_type(translation): self.api._serialize(translation)}
Exemplo n.º 25
0
 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)