Пример #1
0
def update_buddies(request, group_id):

    selected_buddies_list = eval(request.POST.get('selected_buddies_list', '[]'))
    # print "=== selected_buddies_list : ", selected_buddies_list
    selected_buddies_userids_list = Author.get_user_id_list_from_author_oid_list(selected_buddies_list)
    selected_buddies_userids_set = set(selected_buddies_userids_list)

    updated_buddies_authid_name_dict = {}
    already_active_userid_name_dict = {}

    buddies_authid_list = request.session.get('buddies_authid_list', [])
    existing_buddies_userid_list = Author.get_user_id_list_from_author_oid_list(buddies_authid_list)
    # print "=== buddies_authid_list : ", buddies_authid_list
    # print "=== existing_buddies_userid_list : ", existing_buddies_userid_list
    intersection_buddy_userids = selected_buddies_userids_set.intersection(set(existing_buddies_userid_list))
    # print "=======", intersection_buddy_userids

    if selected_buddies_list:

        # sitewide_active_userids_list = DjangoActiveUsersGroup.get_all_user_set_ids_list()
        sitewide_active_userids_list = Buddy.get_active_buddies_user_ids_list()
        sitewide_active_userids_set  = set(sitewide_active_userids_list)

        already_active_user_ids = list(selected_buddies_userids_set.intersection(sitewide_active_userids_set) - set(intersection_buddy_userids))

        if already_active_user_ids:
            auth_cur = node_collection.find({'_type': u'Author', 'created_by': {'$in': already_active_user_ids} }, {'_id': 0, 'name': 1, 'created_by': 1} )
                        # { b['_id'].__str__(): b['name'] for b in updated_buddies_cur}
            if auth_cur:
                already_active_userid_name_dict = {a['created_by']: a['name'] for a in auth_cur}
            # print "==== already_active_userid_name_dict : ", already_active_userid_name_dict

        selected_buddies_list = list(selected_buddies_userids_set - sitewide_active_userids_set) + list(intersection_buddy_userids)
        # print "== sitewide_active_userids_set: ", sitewide_active_userids_set
        # print "selected_buddies_list : ", selected_buddies_list
        # print "== selected_buddies_userids_set: ", selected_buddies_userids_set

        selected_buddies_list = Author.get_author_oid_list_from_user_id_list(user_ids_list=selected_buddies_list, list_of_str_oids=True)

    if selected_buddies_list or buddies_authid_list:

        # update_buddies method signature:
        # def update_buddies(self, loggedin_userid, session_key, buddy_auth_ids_list=[]):
        active_buddy_auth_list = Buddy.update_buddies(request.user.id, request.session.session_key, selected_buddies_list)
        # print "\n\nactive_buddy_auth_list : ", active_buddy_auth_list

        # ab: active buddy
        updated_buddies_cur = node_collection.find({
                                                    '_id': {
                                                        '$in': [ObjectId(ab) for ab in active_buddy_auth_list]
                                                    }
                                                },
                                                {'name': 1, 'created_by': 1}).sort('name', 1)

        updated_buddies_authid_name_dict = { str(b['_id']): b['name'] for b in updated_buddies_cur}
        updated_buddies_cur.rewind()
        buddies_username_id_dict = { str(b['name']): b['created_by'] for b in updated_buddies_cur}
        updated_buddies_cur.rewind()

        request.session['buddies_userid_list']      = [ b['created_by'] for b in updated_buddies_cur]
        request.session['buddies_authid_list']      = active_buddy_auth_list
        request.session['buddies_authid_name_dict'] = json.dumps(updated_buddies_authid_name_dict)
        request.session['buddies_username_id_dict'] = json.dumps(buddies_username_id_dict)
        # print "\n\nrequest.session['buddies_authid_name_dict'] : ", request.session['buddies_authid_name_dict']

    result_dict = {
                'buddies': updated_buddies_authid_name_dict,
                'already_active': already_active_userid_name_dict
            }
    # print "=== result_dict : ", result_dict

    return HttpResponse(json.dumps(result_dict))
Пример #2
0
def update_buddies(request, group_id):

    selected_buddies_list = eval(
        request.POST.get('selected_buddies_list', '[]'))
    # print "=== selected_buddies_list : ", selected_buddies_list
    selected_buddies_userids_list = Author.get_user_id_list_from_author_oid_list(
        selected_buddies_list)
    selected_buddies_userids_set = set(selected_buddies_userids_list)

    updated_buddies_authid_name_dict = {}
    already_active_userid_name_dict = {}

    buddies_authid_list = request.session.get('buddies_authid_list', [])
    existing_buddies_userid_list = Author.get_user_id_list_from_author_oid_list(
        buddies_authid_list)
    # print "=== buddies_authid_list : ", buddies_authid_list
    # print "=== existing_buddies_userid_list : ", existing_buddies_userid_list
    aa = selected_buddies_userids_set.intersection(
        set(existing_buddies_userid_list))
    # print "999999999999900000000", aa

    if selected_buddies_list:

        # sitewide_active_userids_list = DjangoActiveUsersGroup.get_all_user_set_ids_list()
        sitewide_active_userids_list = Buddy.get_active_buddies_user_ids_list()
        sitewide_active_userids_set = set(sitewide_active_userids_list)

        already_active_user_ids = list(
            selected_buddies_userids_set.intersection(
                sitewide_active_userids_set) - set(aa))

        if already_active_user_ids:
            auth_cur = node_collection.find(
                {
                    '_type': u'Author',
                    'created_by': {
                        '$in': already_active_user_ids
                    }
                }, {
                    '_id': 0,
                    'name': 1,
                    'created_by': 1
                })
            # { b['_id'].__str__(): b['name'] for b in updated_buddies_cur}
            if auth_cur:
                already_active_userid_name_dict = {
                    a['created_by']: a['name']
                    for a in auth_cur
                }
            # print "==== already_active_userid_name_dict : ", already_active_userid_name_dict

        selected_buddies_list = list(selected_buddies_userids_set -
                                     sitewide_active_userids_set) + list(aa)
        # print "== sitewide_active_userids_set: ", sitewide_active_userids_set
        # print "selected_buddies_list : ", selected_buddies_list
        # print "== selected_buddies_userids_set: ", selected_buddies_userids_set

        selected_buddies_list = Author.get_author_oid_list_from_user_id_list(
            user_ids_list=selected_buddies_list, list_of_str_oids=True)

    if selected_buddies_list or buddies_authid_list:

        # update_buddies method signature:
        # def update_buddies(self, loggedin_userid, session_key, buddy_auth_ids_list=[]):
        active_buddy_auth_list = Buddy.update_buddies(
            request.user.id, request.session.session_key,
            selected_buddies_list)
        # print "\n\nactive_buddy_auth_list : ", active_buddy_auth_list

        # ab: active buddy
        updated_buddies_cur = node_collection.find(
            {'_id': {
                '$in': [ObjectId(ab) for ab in active_buddy_auth_list]
            }}, {'name': 1})

        updated_buddies_authid_name_dict = {
            b['_id'].__str__(): b['name']
            for b in updated_buddies_cur
        }
        # print "\n\nupdated_buddies : ", updated_buddies_authid_name_dict

        request.session['buddies_userid_list'] = [
            b['created_by'] for b in updated_buddies_cur
        ]
        request.session['buddies_authid_list'] = active_buddy_auth_list
        request.session[
            'buddies_authid_name_dict'] = updated_buddies_authid_name_dict

    result_dict = {
        'buddies': updated_buddies_authid_name_dict,
        'already_active': already_active_userid_name_dict
    }
    # print "=== result_dict : ", result_dict

    return HttpResponse(json.dumps(result_dict))
Пример #3
0
def update_buddies(request, group_id):

    # list of `User` ids. i.e: [int]
    selected_buddies_list = eval(
        request.POST.get('selected_buddies_list', '[]'))
    # Author ids: i.e: [ObjectId]
    selected_buddies_userids_list = Author.get_user_id_list_from_author_oid_list(
        selected_buddies_list)
    # ensure unique entries:
    selected_buddies_userids_set = set(selected_buddies_userids_list)

    updated_buddies_authid_name_dict = {}
    already_active_userid_name_dict = {}

    # [ObjectId]
    buddies_authid_list = request.session.get('buddies_authid_list', [])
    # existing buddies
    existing_buddies_userid_list = Author.get_user_id_list_from_author_oid_list(
        buddies_authid_list)
    intersection_buddy_userids = selected_buddies_userids_set.intersection(
        set(existing_buddies_userid_list))

    if selected_buddies_list:

        # sitewide_active_userids_list = DjangoActiveUsersGroup.get_all_user_set_ids_list()
        sitewide_active_userids_list = Buddy.get_active_buddies_user_ids_list()
        sitewide_active_userids_set = set(sitewide_active_userids_list)

        already_active_user_ids = list(
            selected_buddies_userids_set.intersection(
                sitewide_active_userids_set) - set(intersection_buddy_userids))

        if already_active_user_ids:
            auth_cur = node_collection.find(
                {
                    '_type': u'Author',
                    'created_by': {
                        '$in': already_active_user_ids
                    }
                }, {
                    '_id': 0,
                    'name': 1,
                    'created_by': 1
                })
            # { b['_id'].__str__(): b['name'] for b in updated_buddies_cur}
            if auth_cur:
                already_active_userid_name_dict = {
                    a['created_by']: a['name']
                    for a in auth_cur
                }

        selected_buddies_list = list(selected_buddies_userids_set -
                                     sitewide_active_userids_set) + list(
                                         intersection_buddy_userids)
        selected_buddies_list = Author.get_author_oid_list_from_user_id_list(
            user_ids_list=selected_buddies_list, list_of_str_oids=True)

    if selected_buddies_list or buddies_authid_list:

        # update_buddies method signature:
        # def update_buddies(self, loggedin_userid, session_key, buddy_auth_ids_list=[]):
        active_buddy_auth_list = Buddy.update_buddies(
            request.user.id, request.session.session_key,
            selected_buddies_list)

        # ab: active buddy
        updated_buddies_cur = node_collection.find(
            {
                '_id': {
                    '$in': [ObjectId(ab) for ab in active_buddy_auth_list]
                }
            }, {
                'name': 1,
                'created_by': 1
            }).sort('name', 1)

        updated_buddies_authid_name_dict = {
            str(b['_id']): b['name']
            for b in updated_buddies_cur
        }
        updated_buddies_cur.rewind()
        buddies_username_id_dict = {
            str(b['name']): b['created_by']
            for b in updated_buddies_cur
        }
        updated_buddies_cur.rewind()

        request.session['buddies_userid_list'] = [
            b['created_by'] for b in updated_buddies_cur
        ]
        request.session['buddies_authid_list'] = active_buddy_auth_list
        request.session['buddies_authid_name_dict'] = json.dumps(
            updated_buddies_authid_name_dict)
        request.session['buddies_username_id_dict'] = json.dumps(
            buddies_username_id_dict)

    result_dict = {
        'buddies': updated_buddies_authid_name_dict,
        'already_active': already_active_userid_name_dict
    }

    return HttpResponse(json.dumps(result_dict))