Exemple #1
0
def request_song(request):  #, song_id):
    """
	Create a new SongRequest object for the given song id.
	"""
    request_user = request.user
    song_id = long(request.POST['song_id'])
    if not request.user.is_authenticated():
        # Set this to None to avoid storing an AnonymousUser in the
        # SongRequest (this would raise an exception).
        request_user = None
        # You can allow anonymous user requests in settings.py.
        if not settings.ALLOW_ANON_REQUESTS:
            # Anonymous user requests not allowed, error out.
            message = JSMessage("You must be logged in to request songs.",
                                is_error=True)
            return HttpResponse(message)

    # Look the song up and create a request.
    song = Song.objects.get(id=song_id)
    if SongRequest.objects.get_active_requests().filter(song=song):
        # Don't allow requesting a song that is currently in the queue.
        return HttpResponse(
            JSMessage("Song has already been requested.", is_error=True))
    elif SongRequest.objects.get_active_requests().filter(
            requester=request_user).count(
            ) >= settings.MAX_OUTSTANDING_REQUESTS_PER_USER:
        return HttpResponse(
            JSMessage(
                "You've already got the maximum number of outstanding requests.",
                is_error=True))
    else:
        # Song isn't already in the SongRequest queue, add it.
        request = SongRequest(song=song, requester=request_user)
        request.save()
        return HttpResponse(JSMessage("Song Requested."))
Exemple #2
0
def enqueue_song(song):
    """
    Given a Song object, create a SongRequest.
    
    song: (Song) The Song object to enqueue.
    """
    new_request = SongRequest(song=song)
    new_request.save()