def get_member(member): if not member: return None if isinstance(member, Member): return member assert type(member) in [str, unicode], debug_type(member) member = make_username(member) def get_member_nocache(member_id): try: return Session.query(Member).with_polymorphic('*').get(member_id) except NoResultFound: return None cache = _cache.get('member') cache_func = lambda: get_member_nocache(member) if cache: result = cache.get(key='member:%s' % member, createfunc=cache_func) #try: #Session.add(result) #return result #except InvalidRequestError: return Session.merge(result, load=False) return cache_func()
def get_content(content): if not content: return None if isinstance(content,Content): return content try: content = int(content) except: return None def get_content_nocache(content_id): #http://www.sqlalchemy.org/docs/mappers.html#controlling-which-tables-are-queried # could use .with_polymorphic([DraftContent, ArticleContent, AssignmentContent]), will see if this is needed #assert str(content_id).isdigit(), debug_type(content_id) try: result = Session.query(Content).with_polymorphic('*').get(content_id) #Session.query(Content).with_polymorphic('*').filter_by(id=int(content_id)).one() #Session.expunge(result) return result except NoResultFound: return None # used to have NoResultFound but didnt want a 500 error raised, the caller code can detect NONE and just say "not found" neatly cache = _cache.get('content') cache_func = lambda: get_content_nocache(content) if cache: result = cache.get(key='content:%s' % content, createfunc=cache_func) #try: #Session.add(result) #return result #except InvalidRequestError: #log.warn('Content object already in session, but how do we get a reference to the exisitng object in the session?') return Session.merge(result, load=False) return cache_func()