Exemple #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')) 
Exemple #2
0
def checkInLocation(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)
    
    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)
    
    try: 
        location = Location.objects.get(pk=location_id)
    except (KeyError, Location.DoesNotExist):
        raise InvalidLocationError(location_id)
    
    if not location.is_active:
        raise InvalidLocationError(location_id)
    
    location_service = LocationService()   
    location_service.checkIn(consumer, location)
    
    return __refreshPlaylistHelper__(consumer, location_id)
Exemple #3
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)
Exemple #4
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)
Exemple #5
0
def getSongLibrary(request):
    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)
    
    if not consumer_service.isValidUser(user_id, api_token):
        logger.warn("Not using proper user_id and api_token")
        #raise InvalidUserError(user_id)
    
    popular = params.get('popular', False)
    search_string = params.get('search_string', None)
    category = params.get('category', None)
    
    logger.info("Incoming request- get library with parameters device_id " + str(user_id))

    library = None
    kwargs = {}
        
    if popular:
        kwargs['is_popular'] = True
        
    if search_string:
    	kwargs_name = {'name__icontains':search_string}
    	kwargs_artistname = {'artist__name__icontains':search_string}
        
    if category:
        kwargs['category_id'] = category

    if search_string is not None:
		library = MusicTrack.objects.filter(Q(**kwargs_name) | Q(**kwargs_artistname))
    elif len(kwargs.keys()) > 0:
        library = MusicTrack.objects.filter(**kwargs)
    else:
        library = MusicTrack.objects.all()
        
    library = sorted(library, key=lambda MusicTrack: MusicTrack.name)
      
    return HttpResponse(serializers.serialize("json", library, fields= ('name', 'track_URL', 'artist', 'category', 'album'),
        relations={'album' : {'fields':('name', 'image_URL')}, 'category'  : {'fields':('name')}, 
        'artist'  : {'fields':('name')},}), mimetype='application/json')
Exemple #6
0
def getLocations(request):
    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)
    
    if not consumer_service.isValidUser(user_id, api_token):
        logger.warn("Not using proper user_id and api_token")
        #raise InvalidUserError(user_id)
    
    return HttpResponse(HttpResponse(serializers.serialize("json", Location.objects.all().filter(is_active = True), fields=('pk', 'name', 'location', 'phone_number')), mimetype='application/json'))
Exemple #7
0
def getCategories(request):
    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)
    
    if not consumer_service.isValidUser(user_id, api_token):
        logger.warn("Not using proper user_id and api_token")
        #raise InvalidUserError(user_id)
        
    return HttpResponse(HttpResponse(serializers.serialize("json", MusicCategory.objects.all(), fields=('name')), mimetype='application/json'))
Exemple #8
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)