def followers(self, level=-1): """Find all the followers of this guy """ self_depth = self.get('boss_path').count('/') - 1 ww = Wiseguy() ww.set('boss_path', "%s%s/" % (self.get('boss_path'), '[0-9]+')) followers = Mafia.find(ww, find_all=True) if level > 0: followers = filter( lambda item: (item.get('boss_path').count('/') - 1) - self_depth <= level, followers ) return followers
def heir(self): """Find the heir of this guy. Calculate according to this formula: - Firstly, oldest wiseguy at the same level. - Second candidate is immediate and oldest subordinate. - Finally, return None if none match (only if no subordinates or if just one). """ path = self.get('boss_path') depth = path.count('/') - 1 ww = Wiseguy() # Simple regex to find the boss paths of same size. Which also implies # they are all at the same level in the tree. ww.set('boss_path', "^\/([0-9]+\/){%d}$" % depth) # Run the search and viola heir_candidates = Mafia.find(ww, find_all=True) if len(heir_candidates) == 0: heir_candidates = self.followers() if len(heir_candidates) == 0: return None date_sorted_wiseguys = [] for wiseguy in heir_candidates: if wiseguy.get('id') != self.get('id'): if wiseguy.get('active') == "1": date_sorted_wiseguys.append((wiseguy, wiseguy.get('date_of_initiation'))) date_sorted_wiseguys = sorted( date_sorted_wiseguys, key=operator.itemgetter(1) ) return date_sorted_wiseguys[0][0]
def find_one(cls, *args, **kwargs): """Find a wiseguy """ # Since rows are dicts anyway, I will just use this shortcut return Mafia.find(Wiseguy.from_dict(kwargs), find_all=False)
def save(self): Mafia.update(self)