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)
def getVoteHistory(request): try: device_id = request.GET['device_id'] location_id = request.GET['location_id'] except KeyError: error = utils.internalServerErrorResponse("Invalid request: Device Id and password required for sign up.") logger.warning("Invalid request: Device Id and password required for sign up.") return HttpResponse(simplejson.dumps(error), mimetype='application/json') logger.info("Incoming request- get vote history with parameters device_id " + str(device_id) + ", location_id " + str(location_id)) voting_service = VotingService() try: votes = voting_service.getVoteHistory(device_id) except InvalidDeviceError as ide: error = utils.internalServerErrorResponse(ide.value) logger.error(ide.value) return HttpResponse(simplejson.dumps(error), mimetype='application/json') playlist_item_list = [] for vote in votes: playlist_item_list.append(vote.playlist_item) return HttpResponse(serializers.serialize("json", playlist_item_list, relations={'music_track': {'relations': ('album', 'category', 'artist')}}), mimetype='application/json')