Ejemplo n.º 1
0
def checkOutLocation(request):
    location = None
    params = None
    if (request.method == 'GET'):
        params = request.GET
    elif request.method == 'POST':
        params = request.POST
    
    consumer_service = ConsumerService()
    
    user_id = params.get('user_id', None)
    api_token = params.get('api_key', None)
        
    consumer = consumer_service.isValidUser(user_id, api_token)
    
    if not consumer:
        raise InvalidUserError(user_id)
    
    location_service = LocationService()   
    location_service.checkOut(consumer)

    response_data = {}
    response_data['status'] = 200
    
    return HttpResponse(HttpResponse(json.dumps(response_data), mimetype='application/json')) 
Ejemplo n.º 2
0
def addToPlaylist(request):
    params = None
    if (request.method == 'GET'):
        params = request.GET
    elif request.method == 'POST':
        params = request.POST
    
    location_service = LocationService()
    consumer_service = ConsumerService()
    
    user_id = params.get('user_id', None)
    api_token = params.get('api_key', None)
    
    location_id = params.get('location_id', None)
    music_track_id = params.get('music_track_id', None)
    
    if not location_id:
        raise InvalidLocationError(location_id)
    
    if not music_track_id:
        raise MusicTrackNotFoundError(music_track_id)
        
    consumer = consumer_service.isValidUser(user_id, api_token)
    
    if not consumer:
        raise InvalidUserError(user_id)
    
    if not location_service.isActive(location_id):
        response_data = {}
        location_service.checkOut(consumer)
        response_data['message'] = 'Location is not active'
        response_data['checked_out'] = True
        response_data['status'] = 200
        
        return HttpResponse(HttpResponse(json.dumps(response_data), mimetype='application/json'))
    
    logger.info("Incoming request- add to playlist with parameters device_id " + str(user_id) + ", music_track_id " + str(music_track_id) + ", location_id " + str(location_id))

    playlist_service = PlaylistService()
    
    try: 
        playlist_service.addToPlaylist(consumer, location_id, music_track_id)
        logger.info("added music track " + str(music_track_id) + " to playlist for location " + str(location_id))
    # Improve exception handling.
    except (KeyError, PlaylistNotFoundError, UnableToAddMusicError) as exception:
        error = utils.internalServerErrorResponse(exception.value)
        logger.error(exception.value)
        return HttpResponse(simplejson.dumps(error), mimetype='application/json')
    
    return __refreshPlaylistHelper__(consumer, location_id)
Ejemplo n.º 3
0
def voteUp(request):
    params = None
    if (request.method == 'GET'):
        params = request.GET
    elif request.method == 'POST':
        params = request.POST
    
    location_service = LocationService()
    consumer_service = ConsumerService()
    
    user_id = params.get('user_id', None)
    api_token = params.get('api_key', None)
    
    location_id = params.get('location_id', None)
    music_track_id = params.get('music_track_id', None)
    
    if not location_id:
        raise InvalidLocationError(location_id)
    
    if not music_track_id:
        raise MusicTrackNotFoundError(music_track_id)
        
    consumer = consumer_service.isValidUser(user_id, api_token)
    
    if not consumer:
        raise InvalidUserError(user_id)
    
    if not location_service.isActive(location_id):
        response_data = {}
        location_service.checkOut(consumer)
        response_data['message'] = 'Location is not active'
        response_data['checked_out'] = True
        response_data['status'] = 200
        
        return HttpResponse(HttpResponse(json.dumps(response_data), mimetype='application/json'))
    
    logger.info("Incoming request- vote up with parameters user_id " + str(user_id) + ", location_id " + str(location_id) + ", music_track_id " + str(music_track_id))
    voting_service = VotingService()
    
    try: 
        voting_service.voteUp(consumer, location_id, music_track_id)
        logger.info("Updated playlist after vote up for music track " + str(music_track_id) + " at location " + str(location_id) + " from user " + str(user_id))
    except UnableToVoteError as utv:
        error = utils.internalServerErrorResponse(utv.value)
        logger.error(utv.value)
        return HttpResponse(simplejson.dumps(error), mimetype='application/json')
    
    return __refreshPlaylistHelper__(consumer, location_id)
Ejemplo n.º 4
0
def refreshPlaylist(request):
    params = None
    if (request.method == 'GET'):
        params = request.GET
    elif request.method == 'POST':
        params = request.POST
    
    location_service = LocationService()
    consumer_service = ConsumerService()
    
    user_id = params.get('user_id', None)
    api_token = params.get('api_key', None)
    
    location_id = params.get('location_id', None)
    
    if not location_id:
        raise InvalidLocationError(location_id)
        
    consumer = consumer_service.isValidUser(user_id, api_token)
    
    if not consumer:
        raise InvalidUserError(user_id)
    
    if not location_service.isActive(location_id):
        response_data = {}
        location_service.checkOut(consumer)
        response_data['message'] = 'Location is not active'
        response_data['checked_out'] = True
        response_data['status'] = 200
        
        return HttpResponse(HttpResponse(json.dumps(response_data), mimetype='application/json'))
    
    location_map = location_service.getLocationMap(consumer)
    if not location_map:
        logger.warn("user with user_id" + str(user_id) + " not checked in")
        
    if location_map and str(location_map.location.pk) != location_id:
        logger.warn("user with user_id" + str(user_id) + " not checked in to correct location: " + location_id)

    logger.info("Incoming request- refresh playlist with parameters device_id " + str(user_id) + ", location_id " + str(location_id))
    # Use location id to fetch current playlist
    return __refreshPlaylistHelper__(consumer, location_id)