def get_news(self, user, start_date=None, end_date=None): try: profile = user.get_profile() subscriptions = profile.subscriptions removed_events = profile.removed_events removed_anns = profile.removed_anns except AttributeError: print 'Error: User is either None or Anonymous' return None # add to tuples to pq of the form (score, post) # pq is sorted by score from lowest to highest pq = Queue.PriorityQueue(0) added_to_q = {} # keeps track of posts in pq for group in subscriptions.all(): posts = self.get_posts(group, start_date, end_date) if posts == None: continue for post in posts: # skip this post if it's been deleted if post.post_type == PostType.EVENT and \ removed_events.filter(id=post.id): continue # skip this post if it's been deleted elif post.post_type == PostType.ANNOUNCEMENT and \ removed_anns.filter(id=post.id): continue elif (post, post.post_type) in added_to_q: continue else: pass score = calc_hot_score(post) try: pq.put_nowait((score, post)) added_to_q[(post, post.post_type)] = True except Queue.Full: (low_score, low_post) = pq.get_nowait() # keep the post with the higher score # if there's a tie, most recent posts should be put on pq, # but I don't think this is happening right now if low_score < score: pq.put_nowait((score, post)) else: pq.put_nowait((low_score, low_post)) top_posts = [] while True: try: (low_score, low_post) = pq.get_nowait() top_posts.append(low_post) except Queue.Empty: break ''' We could just reverse top posts here. However, then the ordering of posts will change on filter click if posts have equal scores (of 0). So sort primarily on hot score, then on date to keep a stable ordering. ''' if top_posts != None: self.sort_posts(top_posts) return top_posts
def hotscore(self): return calc_hot_score(self)
def hotscore(self): return calc_hot_score(self)