Beispiel #1
0
def like_kweek(request, authorized_username):
    """
            To like a kweek.


            *Parameters:*
                - *request*: Request which contains the id of th kweek to be liked.
                - *authorized_username(string)*: The user currently logged in.

            *Returns:*
                   -*Tuple*: {
                                | *bool*: To indicate whether kweek credentials creation was successful or not.,
                                | *message (str)*: To specify the reason of failure if detected.
                                | *code*: The code to be returned in the request.

                                | }

        """
    kweek_id = request['id']
    if kweek_id is not None:
        if len(kweek_id) == 0 or (kweek_id.isspace()):
            return False, 'No kweek id found.', 400
        if not kweek_id.isdigit():
            return False, 'Invalid kweek ID.', 400
        check = validate_id(kweek_id)
        if len(check) == 0:
            return False, 'Kweek does not exist.', 404
    else:
        return False, 'No kweek id found.', 400
    add_like(kweek_id, authorized_username)
    notified_user = retrieve_user(kweek_id, 1)[0]['username']
    create_notifications(authorized_username, notified_user, 'LIKE', kweek_id)
    return True, 'success.', 200
Beispiel #2
0
def create_rekweek(request, authorized_username):
    """
            Insert a rekweek in the data base.


            *Parameters:*
                - *request*: the request which contains the id of th kweek to be rekweeked.

            *Returns:*
                   -*Tuple*: {
                                | *bool*: To indicate whether kweek credentials creation was successful or not.,
                                | *message (str)*: To specify the reason of failure if detected.
                                | *code*: The code to be returned in the request.

                                | }

    """
    rekweek_id = request["id"]
    if rekweek_id is not None:
        if len(rekweek_id) == 0 or (rekweek_id.isspace()):
            return False, 'No kweek id found.', 400
        if not rekweek_id.isdigit():
            return False, 'Invalid kweek ID.', 400
        check = validate_id(rekweek_id)
        if len(check) == 0:
            return False, 'Kweek does not exist.', 404
    else:
        return False, 'No kweek id found.', 400
    add_rekweek(rekweek_id, authorized_username)
    notified_user = retrieve_user(rekweek_id, 1)[0]['username']
    create_notifications(authorized_username, notified_user, 'REKWEEK',
                         rekweek_id)
    return True, 'success.', 200
Beispiel #3
0
def get_likers(kweek_id, authorized_username):
    check, message = validate_request(kweek_id)
    if not check:
        print('here0')
        return check, message, None
    likers = retrieve_user(kweek_id, 2)
    if likers:
        print('here1')
        retrieve_users(authorized_username, likers)
    else:
        print('here2')
        return False, 'The kweek has no likers', None
Beispiel #4
0
def insert_kweek(kweek: Kweek):
    """
            Insert the kweek with its associated data in the data base.


            *Parameters:*
                - *kweek object*: The kweek object to be inserted.

            *Returns:*
                   -*Tuple*: {
                                | *bool*: To indicate whether kweek credentials creation was successful or not.,
                                | *message (str)*: To specify the reason of failure if detected.
                                | *code*: The code to be returned in the request.

                                | }

    """
    add_kweek(kweek)
    kid = get_kweek_id()[0]['id']
    if kweek.reply_to:
        notified_user = retrieve_user(kweek.reply_to, 1)[0]['username']
        create_notifications(kweek.user.username, notified_user, 'REPLY', kid)
    for hash_obj in kweek.hashtags:
        test = check_existing_hashtag(hash_obj)
        if not test:  # then it is a new hashtag
            create_hashtag(hash_obj)  # create a new hashtag
            hid = check_existing_hashtag(hash_obj)[0][
                'id']  # then insert it into kweek-hashtag table
        else:
            hid = test[0]['id']
        add_kweek_hashtag(hid, kid, hash_obj)

    for ment in kweek.mentions:
        existed = check_kweek_mention(kid, ment)[0]['count']
        if existed != 0:
            return True, 'success.', 200
        response = create_mention(kid, ment)
        if response is not None:
            return True, 'success.', 200
        notified_user = ment.username
        create_notifications(kweek.user.username, notified_user, 'MENTION',
                             kid)

    return True, 'success.', 200
Beispiel #5
0
def get_likers(kweek_id, authorized_username):
    """
                Get the credentials of the likers of a specific kweek.

                 *Parameters:*
                     - *kweek_id*: ID of the kweek to retreive its likers.
                     - *authorized_username(string)*: The user currently logged in.

                 *Returns:*
                     -*Tuple*: {
                              | *check (bool)*: To indicate whether users credentials retrival was
                              | successful or not.,
                              | *message (str)*: To specify the reason of failure if detected.
                              | *users_list (list of user objects )*: credentials of the users sent to the functions.
                              | *code*: The code to be returned in the request.
                              | }

    """
    check, message, code = validate_request(kweek_id)
    if not check:
        return check, message, None, code
    likers = retrieve_user(kweek_id, 2)
    return retrieve_users(authorized_username, likers)
Beispiel #6
0
def get_kweek(kid, authorized_username, replies_only):
    """
           Get the requested kweek with its credentials.


           *Parameters:*
               - *kid*: The id of the kweek to be retrieved.
               - *authorized_username(string)*: The user currently logged in.
               - *replies_only (bool)*: To indicate whether the kweek with its replies
                  is to be retrieved or the replies only

           *Returns:*
               -*Tuple*: {
                            | *check (bool)*: To indicate whether kweek credentials creation
                            | was successful or not.,
                            | *message (str)*: To specify the reason of failure if detected.
                            | *kweekobj (kweek object )*: the kweek to be retrieved,
                            | *replies (list of int )*: Ids of  the replies to the retrieved kweek .
                            | *code*: The code to be returned in the request.

                            | }

    """
    check, message, code = validate_request(kid)
    if not check:
        return check, message, None, None, code
    replies = retrieve_replies(
        kid
    )  # rows of kweek table who is set as a reply to the retrieved kweek (ids)
    if replies_only:
        return True, message, None, replies, code
    hashtags = retrieve_hashtags(kid)  # rows of hahstag-kweek table (*)
    mentions = retrieve_mentions(kid)  # rows of mention table (*)
    rekweeks = retrieve_user(kid, 3)
    likers = retrieve_user(
        kid,
        2)  # rows of likers table for those who liked the kweek (usernames)
    user = retrieve_user(kid, 1)
    hashtags_list = []  # list of hashtag objects
    mentions_list = []  # list of mention objects
    rekweeked_by_user = False
    liked_by_user = False
    if hashtags:
        for hash_obj in hashtags:
            hid = hash_obj['hashtag_id']
            s_index = hash_obj['starting_index']
            e_index = hash_obj['ending_index']
            indices = [s_index, e_index]
            text = hash_obj['text']
            hash_dic = {'id': hid, 'indices': indices, 'text': text}
            hashtag = Hashtag(hash_dic)
            hashtags_list.append(hashtag)

    if mentions:
        for ment in mentions:
            s_index = ment['starting_index']
            e_index = ment['ending_index']
            indices = [s_index, e_index]
            username = ment['username']
            ment_dic = {'indices': indices, 'username': username}
            mention = Mention(ment_dic)
            mentions_list.append(mention)

    user = user[0]
    extrauser = {}
    me = authorized_username  # should be replaced by the function getting the current user
    check = check_following(me, user['username'])
    if check:
        extrauser['following'] = True
    else:
        extrauser['following'] = False

    check = check_following(user['username'], me)
    if check:
        extrauser['follows_you'] = True
    else:
        extrauser['follows_you'] = False

    check = check_blocked(user['username'], me)
    if check:
        extrauser['blocked'] = True
    else:
        extrauser['blocked'] = False
    check = check_muted(user['username'], me)
    if check:
        extrauser['muted'] = True
    else:
        extrauser['muted'] = False
    extrauser.update(user)

    userobj = User(extrauser)

    if replies:
        num_of_replies = len(replies)
    else:
        num_of_replies = 0

    if likers:
        num_of_likes = len(likers)
        for user in likers:
            if user['username'] == me:
                liked_by_user = True

    else:
        num_of_likes = 0

    if rekweeks:
        num_of_rekweeks = len(rekweeks)
        for user in rekweeks:
            if user['username'] == me:
                rekweeked_by_user = True
    else:
        num_of_rekweeks = 0

    kweekdic = {
        'hashtags': hashtags_list,
        'mentions': mentions_list,
        'number_of_likes': num_of_likes,
        'number_of_rekweeks': num_of_rekweeks,
        'number_of_replies': num_of_replies,
        'rekweek_info': None,
        'liked_by_user': liked_by_user,
        'rekweeked_by_user': rekweeked_by_user,
        'user': userobj
    }
    kweek = retrieve_kweek(kid)  # a row of kweek table
    kweek = kweek[0]
    kweekdic.update(kweek)
    kweekdic['reply_info'] = get_reply_to_info(kid)
    kweekobj = Kweek(kweekdic)
    return True, 'success.', kweekobj, replies, 200