예제 #1
0
def _create_from_existing_users(user, fb_friends):
    """ Creates Friendships using existing Users """
    found_uids = set()
    for fb_friend in fb_friends:
        uid = str(fb_friend["uid"])
        results = User.objects.filter(fb_uid=uid)[:1]
        if len(results) > 0:
            found_uids.add(uid)
            try:
                Friendship.create_from(user, results[0]).save()
            except IntegrityError:
                pass
    return found_uids
예제 #2
0
def _create_from_existing_users(user, fb_friends):
    """ Creates Friendships using existing Users """
    found_uids = set()
    for fb_friend in fb_friends:
        uid = str(fb_friend["uid"])
        results = User.objects.filter(fb_uid=uid)[:1]
        if len(results) > 0:
            found_uids.add(uid)
            try:
                Friendship.create_from(user, results[0]).save()
            except IntegrityError:
                pass
    return found_uids
예제 #3
0
def _make_main_batches(user_id, access_token, fb_friends, found_uids):
    user = User.objects.get(id=user_id)
    user.update_friends_fetch()
    user.save()
    facebook_profiles = []
    for fb_friend in fb_friends:
        uid = str(fb_friend["uid"])
        if uid in found_uids:
            continue
        facebook_profiles.append(FacebookProfile(fb_friend))
        if Friendship.objects.filter(user=user, fb_uid=uid).count() > 0:
            continue
        profile = FacebookProfile(fb_friend)
        if not Friendship.objects.filter(user=user, fb_uid=uid).exists():
            f = Friendship.create_from_fb_profile(user, profile)
            f.save()
    fetch_voters_from_fb_profiles(facebook_profiles)
예제 #4
0
def _make_main_batches(user_id, access_token, fb_friends, found_uids):
    user = User.objects.get(id=user_id)
    user.update_friends_fetch()
    user.save()
    facebook_profiles = []
    for fb_friend in fb_friends:
        uid = str(fb_friend["uid"])
        if uid in found_uids:
            continue
        facebook_profiles.append(FacebookProfile(fb_friend))
        if Friendship.objects.filter(user=user, fb_uid=uid).count() > 0:
            continue
        profile = FacebookProfile(fb_friend)
        if not Friendship.objects.filter(user=user, fb_uid=uid).exists():
            f = Friendship.create_from_fb_profile(user, profile)
            f.save()
    fetch_voters_from_fb_profiles(facebook_profiles)
예제 #5
0
def _make_initial_batches(user, fb_friends, found_uids):
    newly_found_uids = set()
    for fb_friend in fb_friends:
        uid = str(fb_friend["uid"])
        if uid in found_uids:
            continue
        profile = FacebookProfile(fb_friend)
        batch_type = _initial_batch_type(user, profile)
        if not batch_type:
            continue
        newly_found_uids.add(uid)
        f = Friendship.create_from_fb_profile(user, profile)
        try:
            f.save()
        except IntegrityError:
            pass
    user.friendshipbatch_set.filter(type=BATCH_NEARBY).update(completely_fetched=True)
    user.friendshipbatch_set.filter(type=BATCH_FAR_FROM_HOME).update(completely_fetched=True)
    user.friendshipbatch_set.filter(type=BATCH_BARELY_LEGAL).update(completely_fetched=True)
    return newly_found_uids
예제 #6
0
def _make_initial_batches(user, fb_friends, found_uids):
    newly_found_uids = set()
    for fb_friend in fb_friends:
        uid = str(fb_friend["uid"])
        if uid in found_uids:
            continue
        profile = FacebookProfile(fb_friend)
        batch_type = _initial_batch_type(user, profile)
        if not batch_type:
            continue
        newly_found_uids.add(uid)
        f = Friendship.create_from_fb_profile(user, profile)
        try:
            f.save()
        except IntegrityError:
            pass
    user.friendshipbatch_set.filter(type=BATCH_NEARBY).update(
        completely_fetched=True)
    user.friendshipbatch_set.filter(type=BATCH_FAR_FROM_HOME).update(
        completely_fetched=True)
    user.friendshipbatch_set.filter(type=BATCH_BARELY_LEGAL).update(
        completely_fetched=True)
    return newly_found_uids
예제 #7
0
def seed():
    """Seeds the database with dummy data"""
    print("Seeding the database")

    # ------------------- #
    # --- RANDOM DATA --- #
    # ------------------- #

    random_data = {

        # Descriptions
        'descriptions': [
            'Nullam sit amet ex volutpat, accumsan ex eu, ullamcorper libero. Nunc libero sapien, volutpat at ex a, vulputate viverra sem. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aenean quis efficitur arcu',
            'Nullam molestie dapibus libero volutpat viverra. Etiam sit amet nulla leo.',
            'Etiam velit tortor, venenatis a leo commodo, feugiat scelerisque erat. Curabitur sed dui ac urna congue vestibulum vitae ut enim. Quisque at tellus finibus orci pretium laoreet.',
            'Quisque vulputate eros nisi, ut fermentum tellus lobortis eget',
            'Etiam vel mi vitae magna congue malesuada.'
        ],

        # Users
        'users': [],

        # Users
        'games':
        ['Tic-Tac-Toe', 'Guess the word', 'Whack-A-Mole', 'Pong', 'Othello'],
        'game_ids': [],
    }

    # ------------------ #
    # --- SEED USERS --- #
    # ------------------ #

    # Loop 25 times
    for i in range(125, 150):

        # Generate random description
        random_description = random_data['descriptions'][random.randint(
            0,
            len(random_data['descriptions']) - 1)]

        # Create a random User
        user = User(username='******'.format(i),
                    picture_url='via.placeholder.com/100x100',
                    description=random_description)
        # Use the hash_password() method on the User object (Model)
        user.hash_password('password{0}'.format(i))

        # Add the user to the database
        db.session.add(user)
        db.session.commit()

        # Add the user to the users list
        random_data['users'].append(user)

    # ------------------ #
    # --- SEED GAMES --- #
    # ------------------ #

    # Loop random_data['games'] size times
    for i in range(0, len(random_data['games'])):

        # Generate random description
        random_description = random_data['descriptions'][random.randint(
            0,
            len(random_data['descriptions']) - 1)]

        # Create a random Game
        game = Game(name=random_data['games'][i],
                    description=random_description)

        # Add the game to the database
        db.session.add(game)
        db.session.commit()

        # Add the user to the users list
        random_data['game_ids'].append(game.id)

    # ------------------------ #
    # --- SEED FRIENDSHIPS --- #
    # ------------------------ #

    # Loop 50 times
    for i in range(0, 50):

        # The same user can never befriend itself so if id1 and id2 are the same, keep looping
        same_id = True
        while same_id:
            # Generate random user id's
            random_user_id1 = random_data['users'][random.randint(
                0,
                len(random_data['users']) - 1)].id
            random_user_id2 = random_data['users'][random.randint(
                0,
                len(random_data['users']) - 1)].id

            # If the id's are different OR if the random_user_id1 is empty
            if random_user_id1 != random_user_id2 or random_user_id1 == None:
                same_id = False

        # Create a random Friendship
        friendship = Friendship(user_id_1=random_user_id1,
                                user_id_2=random_user_id2)

        # Add the friendship to the database
        db.session.add(friendship)
        db.session.commit()

    # ------------------------- #
    # --- SEED ACHIEVEMENTS --- #
    # ------------------------- #

    for i in range(0, 50):

        # Generate a random game id & description
        random_description = random_data['descriptions'][random.randint(
            0,
            len(random_data['descriptions']) - 1)]
        random_game_id = random_data['game_ids'][random.randint(
            0,
            len(random_data['game_ids']) - 1)]

        # Create a random Achievement
        achievement = Achievement(name='Achievement {0}'.format(i),
                                  description=random_description,
                                  game_id=random_game_id)

        # Add the friendship to the database
        db.session.add(achievement)
        db.session.commit()

    print("Done seeding the database")