def createNewToken(self, user): """ Creates new token for a user. Contraint is that user can only have a single wigi token. If token already exists for user, delete previous tokens before creation of new token. """ from wigi import DbMan try: session = DbMan.getSession() print "01" #check if user already has a token user = session.merge(user) print "02" token = WigiTokens.getTokenForUser(user) print "03" if token: #token exists for user, delete token if WigiTokens.clearTokensForUser(user): print "tokens successfully cleared." else: print "tokens not cleared. " #create new token for user token = WigiTokens(user) print "1" assert(token.user_id) assert(token.wigi_token) session.add(token) session.commit() session.close() print "token created" return token except Exception as e: print "error creating token: " print e return None
def getUserForFbId(self, fb_id): """ returns the user corresponding to the supplied facebook id. """ from wigi import DbMan try: session = DbMan.getSession() user = session.query(User).filter_by(facebook_id=fb_id).first() session.close() return user if user else None except Exception as e: print e return None
def doesUserExist(self, fb_id): """Checks to see if users exists based on facebook_id""" from wigi import DbMan try: session = DbMan.getSession() user = session.query(User).filter_by(facebook_id=fb_id).first() session.close() return True if user else False except Exception as e: print e return False
def getItemsForUser(self, user): """ Retrieves a users items based off the user.id column. """ from wigi import DbMan try: session = DbMan.getSession() session.merge(user) #get items for user items = session.query(WigiItems).filter_by(user_id=user.id).all() session.close() return items if items else None except Exception as e: logging.error("Error in WigiItems.getItemsForUser: " + e.__str__()) return None
def addTagForItem(self,item,tag): from wigi import DbMan try: session = DbMan.getSession() session.merge(item) #create tag for item newTag = ItemTags(item, tag) session.add(newTag) session.commit() session.close() return newTag if newTag else None except Exception as e: logging.error("Error in ItemTags.addTagForItem: " + e.__str__()) return None
def createNewUser(self, fb_id, access_token): """ creates a new user. """ from wigi import DbMan try: session = DbMan.getSession() user = User(fb_id, access_token) assert(user.facebook_id) assert(user.access_token) session.add(user) session.commit() session.close() return user except Exception as e: print e return None
def clearTokensForUser(self, user): """ clears exisiting tokens for user. """ from wigi import DbMan try: session = DbMan.getSesssion() user = session.merge(user) exisitngTokens = session.query(WigiTokens).filter_by(user_id=user.id) for token in existingTokens: session.delete(token) session.commit() session.close() return True except Exception as e: logging.error("Error in WigiTokens.clearTokensForUser: " + e) return False
def getTokenForUser(self, user): """ Retrieves the current token for a user. If no token is found, return None. """ from wigi import DbMan try: session = DbMan.getSession() print "04" user = session.merge(user) print "05" token = session.query(WigiTokens).filter_by(user_id=user.id).first() print "06" session.close() return token if token else None except Exception as e: logging.error("Error in WigiTokens.getTokenForUser: " + e.__str__()) return None
def addNewItemForUser(self, user, item_loc, status, initial_comment): """ adds a new item for the specified user. """ from wigi import DbMan try: session = DbMan.getSession() session.merge(user) #create new item newItem = WigiItems(user, item_loc, status, initial_comment) session.add(newItem) session.commit() session.close() return newItem if newItem else None except Exception as e: logging.error("Error in WigiItems.addNewItemForUser: " + e.__str__()) return None