@auth.requires_login()
def mark_friend():
    """
        Send a friend request
    """

    if len(request.args) < 1:
        return "Invalid URL"

    frtable = db.friend_requests
    ftable = db.friends
    friend_id = long(request.args[0])

    if friend_id != session.user_id:
        query = (ftable.user_id == session.user_id) & \
                (ftable.friend_id == friend_id)
        value = db(query).count()
        if value == 0:
            query = ((frtable.from_h == session.user_id) & \
                     (frtable.to_h == friend_id)) | \
                    ((frtable.from_h == friend_id) & \
                     (frtable.to_h == session.user_id))

            value = db(query).count()
            if value != 0:
                session.flash = "Friend request pending..."
                redirect(URL("default", "search"))
        else:
            return "Already friends !!"
    else:
        return "Invalid user argument!"

    # Insert a tuple of users' id into the friend_requests table
    db.friend_requests.insert(from_h=session.user_id, to_h=friend_id)

    # Send the user an email notifying about the request
    atable = db.auth_user
    query = (atable.id == friend_id)
    row = db(query).select(atable.email,
                           atable.stopstalk_handle).first()
    current.send_mail(to=row.email,
                      subject=session.handle + \
                              " wants to be a friend on StopStalk",
                      message="""
%s (%s) wants to connect on StopStalk
To view all friend requests go here - %s

To stop receiving mails - %s
                              """ % (session.handle,
                                     URL("user",
                                         "profile",
                                         args=[session.handle],
                                         scheme=True,
                                         host=True,
                                         extension=False),
                                     URL("user",
                                         "friend_requests",
                                         scheme=True,
                                         host=True,
                                         extension=False),
                                     URL("default", "unsubscribe",
                                         scheme=True,
                                         host=True,
                                         extension=False)))

    return "Friend Request sent"

# ----------------------------------------------------------------------------
dataset/ETHPy150Open stopstalk/stopstalk-deployment/controllers/default.py/mark_friend/original