Ejemplo n.º 1
0
def notifications(request):
    parameters = {}

    if request.user.is_authenticated():
        user_id = request.user.id
        user_profile_obj = UserProfile()
        user_profile = user_profile_obj.get_profile_by_id(str(user_id))
        user_info = UserInfo(user_id)
        parameters['userinfo'] = user_info
        parameters['user_id'] = request.user.id

        user_profile_obj = UserProfile()
        userprofile = user_profile_obj.get_profile_by_id(request.user.id)
        parameters['userprofile'] = userprofile
    else:
        return HttpResponseRedirect('/')

    user_name = request.user.username
    user_email = request.user.email

    notices = Notification()
    my_notifications = notices.get_notification(user_name,
                                                page_number=1,
                                                n_type='all')
    parameters['archived_notification_count'] = my_notifications[
        'archived_notification_count']
    parameters['all_notification_count'] = my_notifications[
        'all_notification_count']
    parameters['unread_notification_count'] = my_notifications[
        'unread_notification_count']
    parameters['email'] = user_email
    parameters['screen_name'] = SocialAccount.objects.get(
        user__id=user_id).extra_data['screen_name']

    myNotice = []
    for eachNotification in my_notifications['notifications']:
        if eachNotification['notification_archived_status'] == 'true':
            continue
        processed_notice = {}
        user_profile_obj = UserProfile()
        notifying_user_profile = user_profile_obj.get_profile_by_username(
            eachNotification['notifying_user'])
        try:
            if (notifying_user_profile['email'] != ''):
                processed_notice[
                    'notifying_user_email'] = notifying_user_profile['email']
        except:
            user = User.objects.get(
                username=eachNotification['notifying_user'])
            # print eachNotification
            processed_notice['notifying_user_email'] = user.email
        try:
            if (notifying_user_profile['screen_name'] != ''):
                processed_notice[
                    'notifying_user_screenname'] = notifying_user_profile[
                        'screen_name']
        except:
            processed_notice[
                'notifying_user_screenname'] = account = SocialAccount.objects.get(
                    user__id=request.user.id).extra_data['screen_name']
        processed_notice['notification_id'] = eachNotification['uid']['id']
        processed_notice['notifying_user'] = eachNotification['notifying_user']
        processed_notice['notification_message'] = eachNotification[
            'notification_message'][0:50] + '....'
        processed_notice['notification_message_full'] = eachNotification[
            'notification_message']
        processed_notice['time_elapsed'] = calculate_time_ago(
            eachNotification['notification_time'])
        processed_notice['notifying_user_profile'] = notifying_user_profile
        processed_notice['notification_view_status'] = eachNotification[
            'notification_view_status']
        myNotice.append(processed_notice)
    parameters['notifications'] = myNotice
    return render_to_response('notice.html',
                              parameters,
                              context_instance=RequestContext(request))
Ejemplo n.º 2
0
def invite(request):
    if request.GET.get('page') == None:
        page_num = 1
    else:
        page_num = int(request.GET.get('page'))

    parameters = {}
    parameters['current_page_number'] = page_num

    tweetfeed_obj = TweetFeed()
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/')

    if request.user.is_authenticated():
        user_id = request.user.id
        user_profile_obj = UserProfile()
        user_profile = user_profile_obj.get_profile_by_id(str(user_id))
        user_info = UserInfo(user_id)
        parameters['userinfo'] = user_info
        parameters['user_id'] = request.user.id

    if request.method == 'GET':
        friends_obj = Friends()
        friends_count = friends_obj.get_friends(request.user.username)
        if len(friends_count) > 0:
            twitter_err_obj = TwitterError()
            twitter_error = twitter_err_obj.get_error(request.user.username)
            twitter_err_obj.change_error_status(request.user.username)
            # print twitter_error
            if len(twitter_error) > 0:
                # print "Inside use case 1"
                try:
                    next_cursor = twitter_error[0]['next_cursor_str']
                    # print next_cursor
                    friends = tweetfeed_obj.get_friends(
                        request.user.id, next_cursor)
                    count = 0
                    while (next_cursor != '0'):
                        # print "Inside use case 1 while loop"
                        next_cursor = friends['next_cursor_str']
                        friends_obj = Friends()
                        count = count + 1
                        for eachFriend in friends['users']:
                            friends_obj.save_friends({
                                'username': request.user.username,
                                'friends': eachFriend
                            })
                        if next_cursor != 0:
                            friends = tweetfeed_obj.get_friends(
                                request.user.id, next_cursor)
                except:
                    # print "Inside use case 1 exception"
                    twitter_err_obj.save_error({
                        'username': request.user.username,
                        'next_cursor_str': next_cursor,
                        'error_solve_stat': 'false'
                    })
                # print "Inside use case 1 get values"
                friends = friends_obj.get_paginated_friends(
                    request.user.username, page_num)
                friend_count = friends_obj.get_friends_count(
                    request.user.username)
            else:
                # print "Inside use case 1 else"
                friends = friends_obj.get_paginated_friends(
                    request.user.username, page_num)
                friend_count = friends_obj.get_friends_count(
                    request.user.username)
        else:
            # print "Inside use case 2"
            next_cursor = -1
            friends = tweetfeed_obj.get_friends(request.user.id, next_cursor)
            count = 0
            try:
                while (next_cursor != 0):
                    # print "Inside use case 1 while loop"
                    next_cursor = friends['next_cursor_str']
                    friends_obj = Friends()
                    count = count + 1
                    for eachFriend in friends['users']:
                        friends_obj.save_friends({
                            'username': request.user.username,
                            'friends': eachFriend
                        })
                    if next_cursor != 0:
                        friends = tweetfeed_obj.get_friends(
                            request.user.id, next_cursor)
            except:
                # print "Inside use case 2 exception"
                twitter_err_obj = TwitterError()
                twitter_err_obj.save_error({
                    'username': request.user.username,
                    'next_cursor_str': next_cursor,
                    'error_solve_stat': 'false'
                })
            #print "Inside use case 2 get friends"
            friends = friends_obj.get_paginated_friends(
                request.user.username, page_num)
            friend_count = friends_obj.get_friends_count(request.user.username)
    friend_list = []

    for eachFriend in friends:
        invites_obj = Invites()
        my_name = request.user.username
        if invites_obj.check_invited(eachFriend['friends']['screen_name'],
                                     my_name) == False:
            friend_list.append({
                'friends': {
                    'screen_name': eachFriend['friends']['screen_name'],
                    'name': eachFriend['friends']['name'],
                    'profile_image_url':
                    eachFriend['friends']['profile_image_url']
                }
            })

    page_count = int(friend_count / 15) + 1
    while (len(friend_list) == 0 and page_num <= page_count):
        page_num = page_num + 1
        friends = friends_obj.get_paginated_friends(request.user.username,
                                                    page_num)
        for eachFriend in friends:
            invites_obj = Invites()
            my_name = request.user.username
            if invites_obj.check_invited(eachFriend['friends']['screen_name'],
                                         my_name) == False:
                friend_list.append({
                    'friends': {
                        'screen_name':
                        eachFriend['friends']['screen_name'],
                        'name':
                        eachFriend['friends']['name'],
                        'profile_image_url':
                        eachFriend['friends']['profile_image_url']
                    }
                })

    parameters['friend'] = friend_list
    parameters['page_count'] = page_count
    parameters['friend_count'] = len(friend_list)

    user_profile_obj = UserProfile()
    userprofile = user_profile_obj.get_profile_by_id(request.user.id)
    parameters['userprofile'] = userprofile

    invites_obj = InviteId()
    invite_id = invites_obj.get_unused_id(request.user.id)

    invite_tweet = construct_invite_tweet(request, invite_id)
    parameters['invite_id'] = invite_id['uid']['id']
    parameters['invite_tweet'] = invite_tweet
    return render_to_response('invites.html',
                              parameters,
                              context_instance=RequestContext(request))