Exemple #1
0
def index(request):
    #Set cookie
    if 'voter_id' not in request.session:
        cookie = False
        voter = User()
        voter.save()
        request.session['voter_id'] = voter.id
        request.session.set_expiry(24*60*60)
    else:
        try:
            voter = User.objects.get(pk=request.session['voter_id'])
        except User.DoesNotExist:
            #In case there is a cookie with a voter_id but not an corresponding entry in the database
            voter = User()
            voter.save()
            request.session['voter_id'] = voter.id
        request.session.set_expiry(24*60*60)
        cookie = True
        
    #Get current track
    sock = mysocket.Mysocket()
    sock.connect(mysocket.ADDR, mysocket.PORT)
    sock.mysend(format(4, '07'))
    answer = sock.myreceive()
    sock.close()
    
    current_track = Track.objects.get(pk=int(answer))

    #Get current playing status
    sock = mysocket.Mysocket()
    sock.connect(mysocket.ADDR, mysocket.PORT)
    sock.mysend(format(5, '07'))
    answer = sock.myreceive()
    sock.close()

    if answer == format(1, '07'):
        playing = True
    elif answer == format(0, '07'):
        playing = False
    else:
        playing = 'Error'
        
    #Get tracks ranked by votes
    track_ranking = Track.objects.filter(votes__gt=0).order_by('-votes')

    #Check if admin
    admin = request.user.is_superuser

    context = {
        'current_track': current_track,
        'track_ranking': track_ranking,
        'cookie': cookie,
        'voter': voter,
        'playing': playing,
        'admin': admin,
        }
    response = render(request, 'musicvoting/index.html', context)
    if cookie == False:
        response.set_cookie('test_cookie', 'true')
    return response
Exemple #2
0
def unpause(request):
    permission = request.user.is_superuser
    if permission == True:
        sock = mysocket.Mysocket()
        sock.connect(mysocket.ADDR, mysocket.PORT)
        sock.mysend(format(2, '07'))
        answer = sock.myreceive()
        sock.close()
        if answer == format(1, '07'):
            return HttpResponse("Music is unpaused. " + answer)
        else:
            return HttpResponse("ERROR")
    else:
        return HttpResponse('Unauthorized', status=401)
Exemple #3
0
def next_track(request):
    permission = request.user.is_superuser
    if permission == True:
        sock = mysocket.Mysocket()
        sock.connect(mysocket.ADDR, mysocket.PORT)
        sock.mysend(format(3, '07'))
        answer = sock.myreceive()
        sock.close()
        current_track = get_object_or_404(Track, pk=int(answer))
        context = {
            'current_track': current_track,
            }
        return render(request, 'musicvoting/player.html', context)
        
    else:
        return HttpResponse('Unautorized', status=401)
Exemple #4
0
def handle_clientsocket(clientsocket):
    mysock = mysocket.Mysocket(clientsocket)
    msg = mysock.myreceive()
    #pause
    if msg == format(1, '07'):
        print "pausing music"
        pygame.mixer.music.pause()
        global playing
        playing = False
        mysock.mysend(format(1, '07'))
        mysock.close()
    #unpause
    elif msg == format(2, '07'):
        print "unpausing music"
        pygame.mixer.music.unpause()
        global playing
        playing = True
        mysock.mysend(format(1, '07'))
        mysock.close()
    #nexttrack
    elif msg == format(3, '07'):
        print "playing next track"
        next_track()
        global current_track
        mysock.mysend(format(current_track.id, '07'))
        mysock.close()
    #get track
    elif msg == format(4, '07'):
        global current_track
        #send back id of new track
        ret = format(current_track.id, '07')
        mysock.mysend(ret)
        mysock.close()
    #is playing?
    elif msg == format(5, '07'):
        global playing
        if playing:
            mysock.mysend(format(1, '07'))
        else:
            mysock.mysend(format(0, '07'))
        mysock.close()