Exemplo n.º 1
0
    def upvote_song(room_number, url, cookie):
        user_id = SecurityUtils.get_userId(cookie)
        url = SecurityUtils.encrypt_url(url)
        pending_songs = DBUtils.get_pending_songs(room_number)

        # Check if a song is in the queue/pending songs
        if url not in pending_songs:
            return False, ErrorMsg.NO_SONG.value

        songs = DBUtils.get_votes_per_user(room_number, user_id)
        if url not in songs:
            result = DBUtils.upvote(room_number, url, user_id)
            msg = 'Something went wrong, please vote again!'
            pending_songs = DBUtils.get_pending_songs(room_number)
            sorted_queue = QueueModerator.sort_pending_songs(pending_songs)

            return (True, sorted_queue,
                    None) if result else (False, sorted_queue, msg)
        elif songs[url]:
            sorted_queue = QueueModerator.sort_pending_songs(pending_songs)
            msg = 'User has already voted for this song'
            return False, sorted_queue, msg

        sorted_queue = QueueModerator.sort_pending_songs(pending_songs)
        return False, sorted_queue, None
Exemplo n.º 2
0
    def unvote_song(room_number, url, cookie):
        user_id = SecurityUtils.get_userId(cookie)
        url = SecurityUtils.encrypt_url(url)
        pending_songs = DBUtils.get_pending_songs(room_number)

        # Check if a song is in the queue/pending songs
        if url not in pending_songs:
            sorted_queue = QueueModerator.sort_pending_songs(pending_songs)
            return False, sorted_queue, ErrorMsg.NO_SONG.value

        songs = DBUtils.get_votes_per_user(room_number, user_id)

        if url not in songs or songs[url] == 0:
            sorted_queue = QueueModerator.sort_pending_songs(pending_songs)
            return False, sorted_queue, ErrorMsg.NO_VOTE.value

        result, err = DBUtils.unvote(room_number, url, user_id)
        pending_songs = DBUtils.get_pending_songs(room_number)
        sorted_queue = QueueModerator.sort_pending_songs(pending_songs)

        if result:
            return True, sorted_queue, None
        else:
            return False, sorted_queue, err.value