def cache_lists(): def _chop(srs): srs.sort(key=lambda s: s._downs, reverse=True) return srs[:limit] # bylang =:= dict((lang, over18_state) -> [Subsciteit]) # lang =:= all | lang() # nsfwstate =:= no_over18 | allow_over18 | only_over18 bylang = {} for sr in fetch_things2(Subsciteit._query(sort=desc('_date'), data=True)): aid = getattr(sr, 'author_id', None) if aid is not None and aid < 0: # skip special system sciteits like promos continue type = getattr(sr, 'type', 'private') if type not in ('public', 'restricted'): # skips sciteits that can't appear in the default list # because of permissions continue for lang in 'all', sr.lang: over18s = ['allow_over18'] if sr.over_18: over18s.append('only_over18') else: over18s.append('no_over18') for over18 in over18s: k = (lang, over18) bylang.setdefault(k, []).append(sr) # keep the lists small while we work if len(bylang[k]) > limit*2: bylang[k] = _chop(bylang[k]) for (lang, over18), srs in bylang.iteritems(): srs = _chop(srs) sr_tuples = map(lambda sr: (sr._downs, sr.allow_top, sr._id), srs) print "For %s/%s setting %s" % (lang, over18, map(lambda sr: sr.name, srs[:50])) SubsciteitPopularityByLanguage._set_values(lang, {over18: sr_tuples})
def pop_sciteits(langs, over18, over18_only, filter_allow_top = False): if not over18: over18_state = 'no_over18' elif over18_only: over18_state = 'only_over18' else: over18_state = 'allow_over18' # we only care about base languages, not subtags here. so en-US -> en unique_langs = [] seen_langs = set() for lang in langs: if '-' in lang: lang = lang.split('-', 1)[0] if lang not in seen_langs: unique_langs.append(lang) seen_langs.add(lang) # dict(lang_key -> [(_downs, allow_top, sr_id)]) bylang = SubsciteitPopularityByLanguage._byID(unique_langs, properties=[over18_state]) tups = flatten([lang_lists[over18_state] for lang_lists in bylang.values()]) if filter_allow_top: # remove the folks that have opted out of being on the front # page as appropriate tups = filter(lambda tpl: tpl[1], tups) if len(tups) > 1: # if there was only one returned, it's already sorted tups.sort(key = lambda tpl: tpl[0], reverse=True) #Ignore the lot of them and just print the daughters of c.site #tups=[(2,True,sr._id) for sr in getattr(c.site,"children",[])] #print c.site #print tups return map(lambda tpl: tpl[2], tups)