Example #1
0
 def has_access(self, instance_name, api_name):
     key = '{0}_{1}_{2}'.format(self.id, instance_name, api_name)
     res = get_cache().get(self.cache_prefix, key)
     if res is None:
         res = self._has_access(instance_name, api_name)
         get_cache().set(self.cache_prefix, key, res)
     return res
Example #2
0
 def get_by_name(cls, name):
     cache_res = get_cache().get(cls.cache_prefix, name)
     if cache_res is None:  # we store a tuple to be able to distinguish
         #  if we have already look for this element
         res = cls.query.filter_by(name=name).first()
         get_cache().set(cls.cache_prefix, name, (res, ))
         return res
     else:
         if cache_res[0]:
             return db.session.merge(cache_res[0], load=False)
         else:
             return None
Example #3
0
 def get_from_external_code(cls, external_code):
     prefix = cls.prefix_ext_code
     cache_res = get_cache().get(prefix, external_code)
     if cache_res is None:  # we store a tuple to be able to distinguish
         #  if we have already look for this element
         res = cls.query.filter(cls.external_code == external_code).first()
         get_cache().set(prefix, external_code, (res, ))
         return res
     else:
         if cache_res[0]:
             return db.session.merge(cache_res[0], load=False)
         else:
             return None
Example #4
0
 def get_from_uri(cls, uri):
     prefix = "uri"
     cache_res = get_cache().get(prefix, uri)
     if cache_res is None:  # we store a tuple to be able to distinguish
         #  if we have already look for this element
         res = cls.query.filter(cls.uri == uri).first()
         get_cache().set(prefix, uri, (res, ))
         return res
     else:
         if cache_res[0]:
             return db.session.merge(cache_res[0], load=False)
         else:
             return None
Example #5
0
 def get_from_token(cls, token, valid_until):
     cache_res = get_cache().get(cls.cache_prefix, token)
     if cache_res is None:  # we store a tuple to be able to distinguish
         #  if we have already look for this element
         query = cls.query.join(Key).filter(Key.token == token,
                                            (Key.valid_until > valid_until)
                                            | (Key.valid_until == None))
         res = query.first()
         get_cache().set(cls.cache_prefix, token, (res, ))
         return res
     else:
         if cache_res[0]:
             return db.session.merge(cache_res[0], load=False)
         else:
             return None
Example #6
0
 def is_accessible_by(self, user):
     """
     Check if an instance is accessible by a user
     We don't check the api used here!
     """
     if user:
         user_id = user.id
     else:
         user_id = None
     key = '{0}_{1}'.format(self.name, user_id)
     res = get_cache().get(self.cache_prefix + '_access', key)
     if res is None:
         res = self._is_accessible_by(user)
         get_cache().set(self.cache_prefix + '_access', key, res)
     return res