def get_comments(id, page): res = mongo.comment.find(id, page - 1) if res is None or not res['comments']: return None profiles = current_user.accounts.profiles( [x['uid'] for x in res['comments']]) if profiles is None: return None profiles = dict((int(x), y) for x, y in profiles['profiles'].iteritems()) comments = [] num = (page - 1) * 10 for comment in res['comments']: num += 1 comments.append({ 'uid': comment['uid'], 'nickname': profiles[comment['uid']]['nickname'], 'avatar': profiles[comment['uid']]['avatar'], 'content': comment['content'], 'pubdate': time2best(comment['pubtime']), 'num': num, }) return list(reversed(comments))
def post(self, id): current_user.access_user() args = self.req.parse_args() args = strip(args) if not args['content']: abort(INVALID_ARGS) row = dict(content=args['content'], uid=current_user.uid, pubtime=time.time()) total = mongo.comment.add(id, row) cache.delete_memoized(get_comments, id, (total + 9) / 10) profiles = current_user.accounts.profiles(current_user.uid) if profiles is None: return abort(INVALID_ARGS) profiles = dict( (int(x), y) for x, y in profiles['profiles'].iteritems()) comment = { 'nickname': profiles[row['uid']]['nickname'], 'avatar': profiles[row['uid']]['avatar'], 'content': row['content'], 'pubdate': time2best(row['pubtime']), 'num': total + 1, } return dict(comment=comment)
def common(self, id): article = find_article({'id': id}) if article is None: abort(API_NOT_FOUND) # key = random.choice(article['icons'].keys()) # article['icon'] = img2url(article['icons'][key]['path']) article['text'] = mongo.text_file.get(article['content']) article['pubdate'] = time2best(article['pubtime']) tags = tags2icon(article['tags']) return dict(article=article, tags=tags)
def common(self, word, page, limit=20): articles = [] if limit > 20 or limit < 1: limit = 20 page = max(1, min(page, 100)) count, topic = find_topic_page(word, page) if topic is None: return dict(word=word, articles=articles, last='') fields = { 'id': 1, 'long': 1, 'title': 1, 'src_name': 1, 'icons': 1, 'pubtime': 1 } longs = [x['article'] for x in topic] arts = find_articles('long', longs, fields=fields) arts = dict((x['long'], x) for x in arts) for id in topic: if id['article'] not in arts: remove_index({'article': id['article']}) current_app.logger.error('article not found: %d' % id['article']) continue art = arts[id['article']] val = random.choice( art['icons'].values()) if art['icons'] else None icon = icon2url(val, 220, 120) if val is not None else '' articles.append({ 'id': art['id'], 'title': art['title'], 'src_name': art['src_name'], 'icon': icon, 'icons': [icon2url(x, 220, 120) for x in art['icons'].itervalues()], 'icon_cnt': len(art['icons']), 'pubdate': time2best(art['pubtime']), }) return dict(word=word, articles=articles, count=count)
def common(self, last, limit=20): if limit > 20 or limit < 1: limit = 20 ids = self.latest() articles = [] offset = 0 if last is not None: for index, id in enumerate(ids): if id == last: offset = index + 1 break last = 0 ids = ids[offset:offset + limit] fields = {'id': 1, 'title': 1, 'src_name': 1, 'icons': 1, 'pubtime': 1} arts = find_articles('id', ids, fields=fields) arts = dict((x['id'], x) for x in arts) for id in ids: if id not in arts: continue art = arts[id] val = random.choice( art['icons'].values()) if art['icons'] else None icon = icon2url(val, 220, 120) if val is not None else '' articles.append({ 'id': art['id'], 'title': art['title'], 'src_name': art['src_name'], 'icon': icon, 'icons': [icon2url(x, 220, 120) for x in art['icons'].itervalues()], 'icon_cnt': len(art['icons']), 'pubdate': time2best(art['pubtime']), }) last = id if len(ids) < limit: last = 0 return dict(articles=articles, last=last)
def common(self, last, limit=20): if limit > 20 or limit < 1: limit = 20 ids = self.latest() articles = [] offset = 0 if last is not None: for index, id in enumerate(ids): if id == last: offset = index + 1 break last = 0 ids = ids[offset:offset + limit] fields = {'id':1, 'title':1, 'src_name':1, 'icons':1, 'pubtime':1} arts = find_articles('id', ids, fields=fields) arts = dict((x['id'], x) for x in arts) for id in ids: if id not in arts: continue art = arts[id] val = random.choice(art['icons'].values()) if art['icons'] else None icon = icon2url(val, 220, 120) if val is not None else '' articles.append({ 'id': art['id'], 'title': art['title'], 'src_name': art['src_name'], 'icon': icon, 'icons': [icon2url(x, 220, 120) for x in art['icons'].itervalues()], 'icon_cnt': len(art['icons']), 'pubdate': time2best(art['pubtime']), }) last = id if len(ids) < limit: last = 0 return dict(articles=articles, last=last)
def common(self, word, page, limit=20): articles = [] if limit > 20 or limit < 1: limit = 20 page = max(1, min(page, 100)) count, topic = find_topic_page(word, page) if topic is None: return dict(word=word, articles=articles, last='') fields = {'id':1, 'long':1, 'title':1, 'src_name':1, 'icons':1, 'pubtime':1} longs = [x['article'] for x in topic] arts = find_articles('long', longs, fields=fields) arts = dict((x['long'], x) for x in arts) for id in topic: if id['article'] not in arts: remove_index({'article':id['article']}) current_app.logger.error('article not found: %d' % id['article']) continue art = arts[id['article']] val = random.choice(art['icons'].values()) if art['icons'] else None icon = icon2url(val, 220, 120) if val is not None else '' articles.append({ 'id': art['id'], 'title': art['title'], 'src_name': art['src_name'], 'icon': icon, 'icons': [icon2url(x, 220, 120) for x in art['icons'].itervalues()], 'icon_cnt': len(art['icons']), 'pubdate': time2best(art['pubtime']), }) return dict(word=word, articles=articles, count=count)
def format_time(view, context, model, name): return auto_markup(time2best(model['last']))
def format_time(view, context, model, name): return auto_markup(time2best(model['pubtime']))
def common(self): ids, tids = [], [] for cate in cates: _, topic = find_topic(cate, limit=6) if topic is None: continue ids.extend(topic[:2]) tids.append({'cate': cate, 'ids': topic[2:6]}) articles, index = [], 0 fields = {'id': 1, 'long': 1, 'title': 1, 'icons': 1} longs = [x['article'] for x in ids] arts = find_articles('long', longs, fields=fields) arts = dict((x['long'], x) for x in arts) for id in ids: if id['article'] not in arts: continue art = arts[id['article']] width, height = 230, 130 if index == 0: width, height = 500, 300 elif index == 11: width, height = 285, 248 index += 1 val = random.choice( art['icons'].values()) if art['icons'] else None icon = icon2url(val, width, height) if val is not None else '' article = { 'id': art['id'], 'title': art['title'], 'icon': icon, } articles.append(article) topics = [] fields = { 'id': 1, 'long': 1, 'title': 1, 'src_name': 1, 'icons': 1, 'pubtime': 1 } longs = set() for topic in tids: for id in topic['ids']: longs.add(id['article']) longs = list(longs) xarts = find_articles('long', longs, fields=fields) xarts = dict((x['long'], x) for x in xarts) for topic in tids: arts, last = [], 0 for id in topic['ids']: if id['article'] not in xarts: continue art = xarts[id['article']] val = random.choice( art['icons'].values()) if art['icons'] else None icon = icon2url(val, 220, 120) if val is not None else '' article = { 'id': art['id'], 'title': art['title'], 'src_name': art['src_name'], 'icon': icon, 'pubdate': time2best(art['pubtime']), } arts.append(article) last = id['article'] topics.append({ 'cate': topic['cate'], 'articles': arts, 'last': last }) return dict(articles=articles, topics=topics)
def common(self): ids, tids = [], [] for cate in cates: _, topic = find_topic(cate, limit=6) if topic is None: continue ids.extend(topic[:2]) tids.append({'cate':cate, 'ids':topic[2:6]}) articles, index = [], 0 fields = {'id':1, 'long':1, 'title':1, 'icons':1} longs = [x['article'] for x in ids] arts = find_articles('long', longs, fields=fields) arts = dict((x['long'], x) for x in arts) for id in ids: if id['article'] not in arts: continue art = arts[id['article']] width, height = 230, 130 if index == 0: width, height = 500, 300 elif index == 11: width, height = 285, 248 index += 1 val = random.choice(art['icons'].values()) if art['icons'] else None icon = icon2url(val, width, height) if val is not None else '' article = { 'id': art['id'], 'title': art['title'], 'icon': icon, } articles.append(article) topics = [] fields = {'id':1, 'long':1, 'title':1, 'src_name':1, 'icons':1, 'pubtime':1} longs = set() for topic in tids: for id in topic['ids']: longs.add(id['article']) longs = list(longs) xarts = find_articles('long', longs, fields=fields) xarts = dict((x['long'], x) for x in xarts) for topic in tids: arts, last = [], 0 for id in topic['ids']: if id['article'] not in xarts: continue art = xarts[id['article']] val = random.choice(art['icons'].values()) if art['icons'] else None icon = icon2url(val, 220, 120) if val is not None else '' article = { 'id': art['id'], 'title': art['title'], 'src_name': art['src_name'], 'icon': icon, 'pubdate': time2best(art['pubtime']), } arts.append(article) last = id['article'] topics.append({'cate':topic['cate'], 'articles':arts, 'last':last}) return dict(articles=articles, topics=topics)