Esempio n. 1
0
def add_user(name='Sukhiya',
             username='******',
             nationality='Indian',
             age=20,
             **kwargs):
    """
    Args:
    name: Full Name of the user,
    username: username (should not be already present),
    nationality: nationality,
    age: age

    Returns:
    user_id
    """
    if check_user(username):
        return None

    user = User()
    user.name = name
    user.username = username
    user.nationality = nationality
    user.age = age
    user.save()
    user_id = user.id

    return user_id
Esempio n. 2
0
def add_user_by_object(user):
    if user.id is not None:
        if check_user(user.username) == False:
            new_user = User()
            new_user.id = user.id
            new_user.name = user.name
            new_user.username = user.username
            new_user.nationality = user.nationality
            new_user.age = user.age
            new_user.save()
    else:
        return None
Esempio n. 3
0
def get_user_by_id(pk, **kwargs):
    """
    Args: user id (primary key)
    Returns : User Object
    """
    user = User.objects(id=pk).first()
    return user
Esempio n. 4
0
def check_if_post_liked_by_username(post_id, username, **kwargs):
    user = User.objects(username=username).first()
    liked_posts = user.liked_posts
    if post_id in liked_posts:
        return True
    else:
        return False
Esempio n. 5
0
def check_if_post_liked_by_userid(post_id, user_pk, **kwargs):
    user = User.objects(id=user_pk).first()
    liked_posts = user.liked_posts
    if post_id in liked_posts:
        return True
    else:
        return False
Esempio n. 6
0
def add_post(author='aush',
             creation_time=datetime.datetime.now,
             content='This is just the beginning',
             likes=0,
             **kwargs):
    """
    Args:
    author: username of author of the post,
    creation_time: date and time of creation of the post

    Returns:
    post_id / Error message
    """
    if (check_user(author)):
        post = Post()
        post.author = author
        post.creation_time = creation_time
        post.content = content
        post.likes = likes
        post.save()
        post_id = post.id
        user = User.objects(username=author).first()
        user.posts.append(post_id)
        user.save()
        return post_id
    return None
Esempio n. 7
0
def readUsers(**kwargs):
    """
    Returns:
        Returns All Users Objects in a list
    """
    users = User.objects()
    return users
Esempio n. 8
0
def get_user_by_username(username, **kwargs):
    """
    Args:
        username 
    Returns:
        User Object
    """
    user = User.objects(username=username).first()
    return user
Esempio n. 9
0
def add_user(name='Ayush Sharma',
             username='******',
             nationality='Indian',
             age=20,
             **kwargs):
    user = User()
    user.full_name = name
    user.username = username
    user.nationality = nationality
    user.age = age
    user.save()
Esempio n. 10
0
def get_following_by_userid(user_pk, **kwargs):
    """
    Returns list of following usernames of a user.
    Args:
        userid
    Returns:
        # List of Following users
    """
    user = User.objects(id=user_pk).first()
    return user.followers
Esempio n. 11
0
def get_following_by_username(username, **kwargs):
    """
    Returns list of following usernames of a user.
    Args:
        username
    Returns:
        # List of Following users
    """
    user = User.objects(username=username).first()
    return user.followers
Esempio n. 12
0
def get_liked_posts_by_username(username, **kwargs):
    """
    Returns list of follower usernames of a user.
    Args:
        username
    Returns:
        # List of Post_ids liked by the user
    """
    user = User.objects(username=username).first()
    return user.liked_posts
Esempio n. 13
0
def remove_follower(username, follower, **kwargs):
    """
    Args:
    username: username of the user being followed,
    follower: username of the follower

    Returns:
    Boolean - True/False
    """
    if (check_user(username) == False or check_user(follower) == False):
        return False
    user = User.objects(username=username).first()
    if (follower in user.followers):
        user.followers.remove(follower)
    user.save()
    user1 = User.objects(username=follower).first()
    if (username in user1.following):
        user1.following.remove(username)
    user1.save()
    return True
Esempio n. 14
0
def add_follower(username, follower, **kwargs):
    """
    Args:
    username: username of person being followed,
    follower: username of follower

    Returns:
    Boolean - True/False
    """
    if (check_user(username) and check_user(follower)):
        user = User.objects(username=username).first()
        if follower in user.followers:
            return False
        user.followers.append(follower)
        user.save()
        user1 = User.objects(username=follower).first()
        user1.following.append(username)
        user1.save()
        return True
    return False
Esempio n. 15
0
def get_posts_by_username(username, **kwargs):
    """
    Returns post ids of posts made by a user.
    Args:
        username
    Returns:
        List of Post_Ids
    """
    user = User.objects(username=username).first()
    posts = user.posts
    return posts
Esempio n. 16
0
def get_posts_by_userid(user_pk, **kwargs):
    """
    Args:
        user_pk 
    Returns:
        List of Post_Ids
    """
    user = User.objects(id=user_pk).first()
    posts = user.posts
    # print("Posts : ", posts)
    return posts
Esempio n. 17
0
def add_user_update_to_underlying():
    middleware_users = middlewareDatabaseRead.get_user_updates()
    if middleware_users==None:
        return


    middleware_users_Gset = GSet().loadFromDict(json_util.loads(middleware_users.users))
    
    users = [ User.from_json(element) for element in middleware_users_Gset.payload ]
    
    if len(users) > 0:
        for user in users:
            underlyingDatabaseWrite.add_user_by_object(user)
Esempio n. 18
0
def update_user_nationality(username, nationality, **kwargs):
    """
    Args:
    username: username,
    nationality: nationality,
    
    Returns:
    Boolean - True/False
    """
    if (check_user(username)):
        user = User.objects(username=username).first()
        user.nationality = nationality
        user.save()
        return True
    return False
Esempio n. 19
0
def update_user_name(username, name, **kwargs):
    """
    Args:
    username: username,
    name: Full Name of the user,
    
    Returns:
    Boolean - True/False
    """
    if (check_user(username)):
        user = User.objects(username=username).first()
        user.name = name
        user.save()
        return True
    return False
Esempio n. 20
0
def update_user_age(username, age=0, **kwargs):
    """
    Args:
    username: username,
    age: age

    Returns:
    Boolean - True/False
    """
    if (check_user(username)):
        user = User.objects(username=username).first()
        if (age != 0):
            user.age = age
        user.save()
        return True
    return False
Esempio n. 21
0
def reduce_post_likes(post_id, username, **kwargs):
    """
    Args:
    post_id: id of the post,
    username: username of the user unliking the post

    Returns:
    Boolean - True/False
    """
    if (check_user(username) and check_post(post_id)):
        post = Post.objects(id=post_id).first()
        user = User.objects(username=username).first()
        if (post_id in user.liked_posts):
            post.likes = post.likes - 1
            user.liked_posts.remove(post_id)
        post.save()
        user.save()
        return True
    return False
Esempio n. 22
0
def check_user(name):
    user = User.objects(username=name).first()
    if user is None:
        return False
    return True
Esempio n. 23
0
def readUsers(**kwargs):
    users = User.objects()
    return users
Esempio n. 24
0
                 nationality=user.nationality,
                 age=user.age)

    users = readUsers(dbName=dbName)
    # print_users(users)

    users_update = middlewareDatabaseRead.get_user_updates(
        dbName=dbName_middleware)
    users_Gset = GSet()
    users_Gset.__dict__ = json_util.loads(users_update.users)
    users_from_Gset = json_util.loads(users_update.users)['payload']

    users_loaded = []
    last_username = ""
    for element in users_from_Gset:
        user = User.from_json(element, created=True)
        users_loaded.append(user)
        last_username = user.username

    update_user_name(last_username, "Rakesh")

    user_fullname_updates = middlewareDatabaseRead.get_fullname_updates_by_username(
        last_username)
    # print(last_username, user_fullname_updates.update_value)

    update_user_age(last_username, 101)

    user_age_updates = middlewareDatabaseRead.get_age_updates_by_username(
        last_username)
    # print(last_username, user_age_updates.update_value)