Example #1
0
def reply(request):
    """
    Reply to a message. If this message had be deleted by the receiver, it \
    will be recovered for the receiver together with all the reply messages. And the unread \
    messages count will be recalculated.
    """
    receiver_id = request.POST["to"]
    reply_to_id = request.POST["reply_to"]
    content = request.POST["content"]
    sender_id = request.user.pk
    msg = do_reply(receiver_id, reply_to_id, sender_id, content)
    if msg != "success":
        messages.error(request, msg)
        request.session["reply_msg"] = content
        return redirect("main.views.message.msg_content", message_id=reply_to_id)

    incr_user_afinity(sender_id, receiver_id, 1)
    return redirect("main.views.message.inbox")
Example #2
0
def send_message_action(request):
    """
    Send a message to one or many followers. User can send a message to more than \
    one person as long as the receiver had followed the sender. First, the message content will be \
    check, empty message is *forbidden*. Then check whether the receiver exists and had followed \
    the sender.   
    """
    receiver_ids = request.POST.getlist("uid")
    content = request.POST["content"]
    sender_id = request.user.pk
    msg = do_send_message(receiver_ids, sender_id, content)
    if msg != "success":
        messages.error(request, msg)
        return render_to_response(
            "main/message/msg_new.html", {"content": content}, context_instance=RequestContext(request)
        )

    for followed_id in receiver_ids:
        incr_user_afinity(sender_id, followed_id, 1)
    return redirect("main.views.message.inbox")
Example #3
0
def profile(request, user_id, type=0):
    """
    Profile page;
    
    Args:
      type =
        0: All items for own page
        1: Shared items for own page and other's profile
        2: Privately collected items for own page
        3: Unread items for own page
      `page_type` is used to distinguish between index and profile page when filtering in Redis   
    
    TODO: Right now allow user to see all items he shared; Will this slow the system by putting ids in top_n_list?
    """
    type = int(type)
    profile = Profile.objects.get(pk=user_id)
    #if not profile.is_active: 
    #    raise Http404
    if int(user_id) != request.user.pk:
        type = 1
        incr_user_afinity(request.user.pk, user_id, 1)
    
    complement_statement = ' order by ui.id desc'
    base_query = 'select i.* from main_item as i, main_user_item as ui \
                  where ui.user_id=%s and ui.item_id=i.id and i.deleted=0 ' % user_id

    if type == 0: #All items
        query = base_query + complement_statement
        items = Item.objects.raw(query)
    
    elif type == 1: #Shared
        query = base_query + ' and (ui.status=0 or ui.status=2)' + complement_statement
        items = Item.objects.raw(query)
        #Not used for now; Use memcached; RawQuerySet must be converted to list before stored in memcached
#        cache_key = 'p_' + user_id + '_shared'
#        cache_time = 300 # 5 min to live
#        if int(user_id) == request.user.pk: #Get real time update for owner
#            query =  base_query +  ' and ui.status=0 ' + complement_statement
#            items = Item.objects.raw(query)
#            cache.set(cache_key, list(items), cache_time)
#        else: #Get cached copy for other users
#            items = cache.get(cache_key)
#            if not items:
#                query =  base_query +  ' and ui.status=0 ' + complement_statement
#                items = Item.objects.raw(query)
#                cache.set(cache_key, list(items), cache_time)
    elif type == 2: #Private
        query = base_query + ' and (ui.status=1 or ui.status=2)' + complement_statement
        items = Item.objects.raw(query)
    elif type == 3: #Unread
        query = base_query + ' and ui.unread=1 ' + complement_statement
        items = Item.objects.raw(query)
    
    items = list(items)
    #To render the related topic and friends who recommend this
    items_id_10 = ','.join([str(i.id) for i in items[:10]])
    topics_of_item_dict = get_topics_of_item(items_id_10, request.user.pk)
    friends_of_item_dict = get_friends_of_item(items_id_10, request.user.pk)
    user_item_status_dict = get_user_items(items_id_10, request.user.pk)
    
    #To render more items
    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))   
                         
    cursor = connection.cursor() #@UndefinedVariable
    cursor.execute('select t.id, t.name, t.image, ut.topic_bio, ut.item_count \
                 from main_topic as t inner join \
                 (select * from main_user_topic where user_id= %s and followed=1) as ut \
                  on t.id=ut.topic_id \
                  order by ut.item_count desc' % user_id)
    followed_topics = cursor.fetchall()
    num_followed_topics = len(list(followed_topics))
    pronoun = get_pronoun(request, user_id, profile)
    
    
    own_itemlists = ItemList.objects.filter(creator__pk=profile.pk)
    num_own_itemlists = len(own_itemlists)
    followed_itemlists = ItemList.objects.raw('select i.* from main_user_itemlist as ui, main_itemlist as i \
                         where ui.user_id=%s and ui.itemlist_id=i.id' % profile.pk)
    num_followed_itemlists = len(list(followed_itemlists))
    
    if int(user_id) == request.user.pk:
        page_type = 'profile_own'
    else:
        page_type = 'profile'
    
    
    
    return render_to_response('main/profile/profile.html', {'type':type, 'page_type':page_type, 'profile':profile, 'pronoun':pronoun, 'items':items[:10],
                                                            'top_70_item_list':top_70_item_list, 'user_item_status_dict':user_item_status_dict,
                                                            'topics_of_item_dict':topics_of_item_dict, 'friends_of_item_dict':friends_of_item_dict,
                                                            'followed_topics':followed_topics[:10], 'num_followed_topics':num_followed_topics, 
                                                            'own_itemlists':own_itemlists, 'num_own_itemlists':num_own_itemlists, 
                                                            'followed_itemlists':followed_itemlists, 'num_followed_itemlists':num_followed_itemlists,},
                               context_instance=RequestContext(request))