def itemlist(request,page=1): #itemlists = ItemList.objects.raw('select i.* from main_itemlist as i, main_user_user as uu\ # where uu.follower_id=%s and uu.followed_id=i.creator_id' % request.user.pk) itemlists = list(ItemList.objects.all()) itemlists.sort(key=lambda x: ((x.follower_count + x.item_count) / (abs(datetime.now() -x.create_date).days *24 + abs(datetime.now() -x.create_date).seconds/3600 + 2) ** 1.8), reverse=True ) itemlists_id = '' for i in itemlists: itemlists_id += str(i.id) + ',' topics_of_itemlist_dict = get_topics_of_itemlist(itemlists_id[:-1]) return render_to_response('main/index_itemlist.html', {'page_type':'index_itemlist', 'itemlists':itemlists, 'topics_of_itemlist_dict':topics_of_itemlist_dict}, context_instance=RequestContext(request))
def discover(request): """ Displays top 10 tags and top items """ topics = Topic.objects.all().order_by('-follower_count')[:17] items = Item.objects.all().order_by('-share_count')[:70] top_70_item_list = [] len_items = len(items) for i in range(10, len_items): if i < len_items: top_70_item_list.append(int(items[i].pk)) feeds_id_10 = '' for f in items[:10]: feeds_id_10 += str(f.id) + ',' feeds_id_10 = feeds_id_10[:-1] topics_of_item_dict = get_topics_of_item(feeds_id_10, request.user.pk) friends_of_item_dict = get_friends_of_item(feeds_id_10, request.user.pk) user_item_status_dict = get_user_items(feeds_id_10, request.user.pk) itemlists = ItemList.objects.all().order_by('-follower_count') itemlists_id = '' for i in itemlists: itemlists_id += str(i.id) + ',' topics_of_itemlist_dict = get_topics_of_itemlist(itemlists_id[:-1]) return render_to_response('main/discover/discover.html', {'topics':topics,'items':items[:10], 'top_70_item_list':top_70_item_list, 'topics_of_item_dict':topics_of_item_dict, 'friends_of_item_dict':friends_of_item_dict, 'user_item_status_dict':user_item_status_dict, 'itemlists':itemlists, 'topics_of_itemlist_dict':topics_of_itemlist_dict }, context_instance=RequestContext(request))
def topic(request, topic_id, type=0, redirect_from_id=0): """ Renders topic page Args: type = 0: Display all recent items 1: Display hot items under this topic """ type = int(type) if type != 0 and type != 1 and type != 2: raise Http404 topic = Topic.objects.get(pk=topic_id) if topic.merged_to_id: return redirect("main.views.topic.topic", topic.merged_to_id, 0, topic.pk) if redirect_from_id: redirect_from_topic = Topic.objects.get(pk=redirect_from_id) else: redirect_from_topic = None topic_childs = Topic.objects.raw('select t.* from main_topic as t, main_topic_parent as tp where\ tp.parent_id= %s and tp.topic_id=t.id' % topic_id) topic_parents = Topic_Parent.objects.filter(topic__id=topic_id, parent_deleted=False) #topic_experts = Profile.objects.raw('select p.* from main_profile as p, main_user_topic_fame as utf \ # where score>0 and utf.topic_id=%s and p.user_id=utf.user_id \ # order by score desc limit 0,5' % topic.pk) user_topic = User_Topic.objects.filter(user__pk=request.user.pk, topic__pk=topic_id) if(user_topic): topic_bio = user_topic[0].topic_bio already_followed = True else: topic_bio = '' already_followed = False deleted = topic.deleted merged_to_id = topic.merged_to_id image_form = ImageForm() if type == 0: items = Item.objects.raw('select i.* \ from main_item as i inner join \ (select * from main_item_topic where topic_id= %s ) as it \ on i.id=it.item_id and i.deleted=0 \ order by create_date desc limit 0,20' % topic_id) #Not used for now: Use memcached; RawQuerySet must be converted to list before stored in memcached # cache_key = 't_' + topic_id + '_recent_items' # cache_time = 1800 # 30 min to live # items = cache.get(cache_key) # if not items: # items = Item.objects.raw('select i.* \ # from main_item as i inner join \ # (select * from main_item_topic where topic_id= %s ) as it \ # on i.id=it.item_id and i.deleted=0 \ # order by create_date desc limit 0,20' % topic_id ) # cache.set(cache_key, list(items), cache_time) elif type == 1: items = Item.objects.raw('select i.* \ from main_item as i inner join \ (select * from main_topic_hot_item where topic_id= %s ) as thi \ on i.id=thi.item_id and i.deleted=0 \ order by share_count desc' % topic_id) else: #type=2, display itemlists under this topic itemlists = ItemList.objects.raw('select i.* \ from main_itemlist as i inner join \ (select * from main_itemlist_topic where topic_id= %s ) as it \ on i.id=it.itemlist_id \ order by i.follower_count desc limit 0,20' % topic_id) context_dict = {'topic':topic, 'type':type, 'redirect_from_id':redirect_from_id, 'redirect_from_topic':redirect_from_topic, 'topic_parents':topic_parents, #'topic_experts':topic_experts, 'topic_childs':topic_childs, 'image_form':image_form, 'already_followed':already_followed, 'deleted':deleted, 'merged_to_id':merged_to_id, 'topic_bio':topic_bio, } if type == 0 or type == 1: items = list(items) feeds_id_10 = ','.join([str(i.id) for i in items[:10]]) topics_of_item_dict = get_topics_of_item(feeds_id_10, request.user.pk) friends_of_item_dict = get_friends_of_item(feeds_id_10, request.user.pk) user_item_status_dict = get_user_items(feeds_id_10, request.user.pk) top_70_item_list = [] for i in range(10, 69): if i < len(items): top_70_item_list.append(int(items[i].pk)) # cache_key = 't_' + topic_id + '_hot_items' # cache_time = 1800 # 30 min to live # items = cache.get(cache_key) # if not items: # items = Item.objects.raw('select i.* \ # from main_item as i inner join \ # (select * from main_topic_hot_item where topic_id= %s ) as thi \ # on i.id=thi.item_id and i.deleted=0 \ # order by share_count desc limit 0,20' % topic_id ) # cache.set(cache_key, list(items), cache_time) context_dict['items'] = items[:10] context_dict['topics_of_item_dict'] = topics_of_item_dict context_dict['friends_of_item_dict'] = friends_of_item_dict context_dict['user_item_status_dict'] = user_item_status_dict context_dict['top_70_item_list'] = top_70_item_list else: #for itemlist context_dict['itemlists'] = itemlists itemlists_id = '' for i in itemlists: itemlists_id += str(i.id) + ',' topics_of_itemlist_dict = get_topics_of_itemlist(itemlists_id[:-1]) context_dict['topics_of_itemlist_dict'] = topics_of_itemlist_dict return render_to_response('main/topic/topic.html', context_dict, context_instance=RequestContext(request))