def filter_links(links, filter_spam = False, multiple = True): # run the list through a builder to remove any that the user # isn't allowed to see from pylons import c from r2.models import IDBuilder, Link, Subsciteit, NotFound links = IDBuilder([link._fullname for link in links], skip = False).get_items()[0] if not links: return if filter_spam: # first, try to remove any spam links_nonspam = [ link for link in links if not link._spam ] if links_nonspam: links = links_nonspam # if it occurs in one or more of their subscriptions, show them # that one first subs = set(Subsciteit.user_subsciteits(c.user, limit = None)) def cmp_links(a, b): if a.sr_id in subs and b.sr_id not in subs: return -1 elif a.sr_id not in subs and b.sr_id in subs: return 1 else: return cmp(b._hot, a._hot) links = sorted(links, cmp = cmp_links) # among those, show them the hottest one return links if multiple else links[0]
def default_queries(): from r2.models import Link, Subsciteit from r2.lib.db.operators import desc from copy import deepcopy queries = [] q = Link._query(Link.c.sr_id == Subsciteit.user_subsciteits(None), sort = desc('_hot'), limit = 37) queries.append(q) #add a higher limit one too q = deepcopy(q) q._limit = 75 queries.append(q) return queries
def assign_trial(account, juries_already_on, ip, slash16): from r2.models import Jury, Subsciteit, Trial from r2.lib.db import queries defendants_assigned_to = [] for jury in juries_already_on: defendants_assigned_to.append(jury._thing2_id) subscribed_sr_ids = Subsciteit.user_subsciteits(account, ids=True, limit=None) # Pull defendants, except ones which already have lots of juryvotes defs = Trial.all_defendants(quench=True) # Filter out defendants outside this user's subscribed SRs defs = filter (lambda d: d.sr_id in subscribed_sr_ids, defs) # Dictionary of sr_id => SR for all defendants' SRs srs = Subsciteit._byID(set([ d.sr_id for d in defs ])) # Dictionary of sr_id => eligibility bool submit_srs = {} for sr_id, sr in srs.iteritems(): submit_srs[sr_id] = sr.can_submit(account) and not sr._spam # Filter out defendants with ineligible SRs defs = filter (lambda d: submit_srs.get(d.sr_id), defs) likes = queries.get_likes(account, defs) if not g.debug: # Filter out things that the user has upvoted or downvoted defs = filter (lambda d: likes.get((account, d)) is None, defs) # Prefer oldest trials defs.sort(key=lambda x: x._date) for defendant in defs: sr = srs[defendant.sr_id] if voir_dire(account, ip, slash16, defendants_assigned_to, defendant, sr): j = Jury._new(account, defendant) return defendant return None