def default_queries(): from r2.models import Link, Subdigg from r2.lib.db.operators import desc from copy import deepcopy queries = [] q = Link._query(Link.c.sr_id == Subdigg.user_subreddits(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 link_from_url(path, filter_spam = False, multiple = True): from pylons import c from r2.models import IDBuilder, Link, Subdigg, NotFound if not path: return try: links = Link._by_url(path, c.site) except NotFound: return [] if multiple else None links = tup(links) # run the list through a builder to remove any that the user # isn't allowed to see 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(Subdigg.user_subreddits(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]