Beispiel #1
0
def remove_user_from_event(request_id, user_id, event_id):
    """
    Delete user from a event.
    :param request_id: user requesting operation (must be event owner)
    :param user_id: user to be deleted from event
    :param event_id: event to delete user from
    """
    to_delete = userevent_store.get_userevent(user_id, event_id)
    if not to_delete:
        raise exceptions.NotFoundError("Invalid user id or event id.")
    if request_id != event_store.get_owner_id(event_id):
        raise exceptions.NotFoundError("Invalid user id or event id.")
    if user_id == event_store.get_owner_id(event_id):
        raise exceptions.InvalidRequestError("Cannot remove event owner.")
    userevent_store.delete_userevent(to_delete)
    return None
Beispiel #2
0
def delete_user(user_id):
    """
    Delete user.
    """
    user = user_store.get_user_by_id(user_id)
    if not user:
        raise exceptions.NotFoundError("Invalid user id.")
    user_store.delete_user(user)
    return None
Beispiel #3
0
def get_info(user_id):
    """
    Get full info about currently logged in user
    :return full info for current user
    """
    user = user_store.get_user_by_id(user_id)
    if not user:
        raise exceptions.NotFoundError("Invalid user id.")
    return user.full_info()
Beispiel #4
0
def delete_event(request_id, event_id):
    """
    Delete a event.
    :param request_id: user deleting the event (must be event owner)
    :param event_id: event to be deleted
    """
    to_delete = event_store.get_event_by_id(event_id)
    if not to_delete or not to_delete.event_owner_id == request_id:
        raise exceptions.NotFoundError("Invalid event id.")
    event_store.delete_event(to_delete)
    return None
Beispiel #5
0
def leave_event(request_id, event_id):
    """
    Leave an event.
    :param request_id: user requesting operation (must be event member)
    :param event_id: event to leave
    """
    to_delete = userevent_store.get_userevent(request_id, event_id)
    if not to_delete:
        raise exceptions.NotFoundError("Invalid user id or event id.")
    if request_id == event_store.get_owner_id(event_id):
        raise exceptions.InvalidRequestError("Cannot leave owned event.")
    userevent_store.delete_userevent(to_delete)
    return None
Beispiel #6
0
def add_user_to_event(user_id, event_id):
    """
    Add user to a event. Make sure event ownership checks are done BEFORE this
    function is called.
    :param user_id: user to be added to event
    :param event_id: event to add user to
    """
    event = event_store.get_event_by_id(event_id)
    if not event or not user_store.get_user_by_id(user_id):
        raise exceptions.NotFoundError("Invalid user id or event id.")
    userevent = UserEvent(user_id=user_id, event_id=event_id)
    userevent_store.add_userevent(userevent)
    return userevent.as_dict()
Beispiel #7
0
def get_members(request_id, event_id):
    """
    Get members from event
    :param request_id: user requesting operation (must be event member)
    :param event_id: event of which members are requested
    :return: basic user info for all users in group
    """
    if not userevent_store.get_userevent(request_id, event_id):
        raise exceptions.NotFoundError("Invalid user id or event id.")
    users = userevent_store.get_users_by_event(event_id)
    if not users:
        return []
    return [user.basic_info() for user in users]
Beispiel #8
0
def invite_user_to_event(request_id, user_id, event_id):
    """
    Invite user to an event.
    :param request_id: user requesting operation (must be event owner)
    :param user_id: user to be invited to event
    :param event_id: event to add user to
    """
    if request_id == user_id:
        raise exceptions.InvalidRequestError("Cannot invite yourself.")
    event = event_store.get_event_by_id(event_id)
    if not event or not event.event_owner_id == request_id:
        raise exceptions.NotFoundError("Invalid event id.")
    if not user_store.get_user_by_id(user_id):
        raise exceptions.NotFoundError("Invalid user id.")
    if userevent_store.get_userevent(user_id, event_id):
        raise exceptions.InvalidRequestError("User already a member of event.")
    if userinvite_store.get_invite_by_details(user_id, event_id):
        raise exceptions.InvalidRequestError("Invite already pending.")
    userinvite = UserInvite(inviter_id=request_id,
                            invitee_id=user_id,
                            event_id=event_id)
    userinvite_store.add_userinvite(userinvite)
    return userinvite.as_dict()
Beispiel #9
0
def propose_friendship(request_id, user_id):
    """
    Propose friendship to a user
    :param request_id: user requesting operation
    :param user_id: user to befriend
    :return FriendInvite
    """
    if request_id == user_id:
        raise exceptions.InvalidRequestError("Cannot befriend yourself.")
    to_befriend = user_store.get_user_by_id(user_id)
    if not to_befriend:
        raise exceptions.NotFoundError("Invalid user id.")
    if friend_store.get_invite_by_details(request_id, user_id):
        raise exceptions.InvalidRequestError("Invite already pending.")
    friendinvite = FriendInvite(requesting_id=request_id, requested_id=user_id)
    friend_store.add_friendinvite(friendinvite)
    return friendinvite.as_dict()
Beispiel #10
0
def respond_to_invite(request_id, invite_id, accept):
    """
    Respond to event invite
    :param request_id: id of user responding to invite
    :param invite_id: id of the UserInvite
    :param accept: bool, True = accept, False = Reject
    :return: None if accept is False, Event info if accept is True
    """
    userinvite = userinvite_store.get_invite_by_id(invite_id)
    if not userinvite or not userinvite.invitee_id == request_id:
        raise exceptions.NotFoundError("Invite not found.")
    if accept:
        userevent = events.add_user_to_event_from_invite(userinvite)
        return userevent
    else:
        userinvite_store.delete_userinvite(userinvite)
        return None
Beispiel #11
0
def update_location(user_id, lat, long):
    """
    Update user location in database
    :param user_id: requesting user
    :param lat: new latitude
    :param long: new longitude
    :return: location info
    """
    if not location.MIN_LAT <= lat <= location.MAX_LAT \
            or not location.MIN_LON <= long <= location.MAX_LON:
        raise exceptions.LocationError("Invalid coordinates.")
    user = user_store.get_user_by_id(user_id)
    if not user:
        raise exceptions.NotFoundError("Invalid user id.")
    user.latitude, user.longitude = lat, long
    user_store.commit_changes()
    return user.get_location()
Beispiel #12
0
def respond_to_invite(request_id, invite_id, accept):
    """
    Respond to friend invite
    :param request_id: id of user responding to invite
    :param invite_id: id of the FriendInvite
    :param accept: bool, True = accept, False = Reject
    :return: None if accept is False, Friend info if accept is True
    """
    friendships = []
    friendinvite = friend_store.get_invite_by_id(invite_id)
    if not friendinvite or not friendinvite.requested_id == request_id:
        raise exceptions.NotFoundError("Invite not found.")
    if accept:
        friendships = [
            friend.as_dict()
            for friend in friend_store.add_friendship_from_invite(friendinvite)
        ]
    else:
        friend_store.delete_friendinvite(friendinvite)
    return friendships
Beispiel #13
0
def invite_users_to_event(request_id, emails, event_id):
    """
    Invite multiple users to an event given their usernames.
    :param request_id: user requesting operation (must be event owner)
    :param emails: emails of users to be invited to the event
    :param event_id: event to add user to
    :return: usernames, which weren't invited successfully
    """
    if len(emails) > MAX_INVITES_PER_REQUEST:
        raise exceptions.InvalidRequestError("Too many invites per request.")

    event = event_store.get_event_by_id(event_id)
    if not event or not event.event_owner_id == request_id:
        raise exceptions.NotFoundError("Invalid event id.")

    userinvites = userinvite_store.get_users_by_event(event_id)
    already_pending = set(u.id for u in userinvites) if userinvites else {}
    userevents = userevent_store.get_users_by_event(event_id)
    already_accepted = set(u.id for u in userevents) if userevents else {}

    # TODO: figure out what to do with already rejected

    users_to_process = set(emails)

    # TODO: also allow to bulk invite by phone numbers and user IDs
    users = user_store.get_users_by_emails(emails)
    userinvites = []

    for user in users:
        if (user.id == request_id or user.id in already_pending
                or user.id in already_accepted):
            continue
        userinvites.append(
            UserInvite(inviter_id=request_id,
                       invitee_id=user.id,
                       event_id=event_id))
        users_to_process.remove(user.email)

    userinvite_store.add_userinvites(userinvites)

    return users_to_process