def createPlayer(request): user = getUserForTicket(request) try: newPlayerJSON = json.loads(request.raw_post_data) except ValueError: return HttpResponseBadRequest('Bad JSON') #Ensure the name attribute was provided with the JSON try: newPlayerName = newPlayerJSON['name'] except KeyError: return HttpResponseBadRequest('No name given') #Determine which sorting algorithm to use if 'sorting_algorithm_id' in newPlayerJSON: try: sortingAlgo = SortingAlgorithm.objects.get(pk=newPlayerJSON['sorting_algorithm_id']) except ObjectDoesNotExist: toReturn = HttpResponseNotFound() toReturn[MISSING_RESOURCE_HEADER] = 'sorting-algorithm' return toReturn else: try: sortingAlgo = SortingAlgorithm.objects.get(function_name=default_sorting_algo) except ObjectDoesNotExist: raise ImproperlyConfigured('Default sorting algorithm is not in database') #Ensure that the suers doesn't already have a player with the given name conflictingPlayer = Player.objects.filter(owning_user=user, name=newPlayerName) if conflictingPlayer.exists(): return HttpResponse('A player with that name already exists', status=409) #Create and save new player newPlayer = Player( owning_user=user, name=newPlayerName, sorting_algo=sortingAlgo) newPlayer.save() #If password provided, create and save password if 'password' in newPlayerJSON: PlayerPassword(player=newPlayer, password_hash=hashPlayerPassword(newPlayerJSON['password'])).save() #If locaiton provided, geocode it and save it if 'location' in newPlayerJSON: location = newPlayerJSON['location'] if isValidLocation(location): try: setPlayerLocation(location, newPlayer) except LocationNotFoundError as e: return HttpResponseBadRequest('Location not found. Geocoder error: ' + str(e)) else: return HttpResponseBadRequest('Bad location') return HttpJSONResponse(json.dumps(newPlayer, cls=UDJEncoder), status=201)
def createPlayer(request, user_id): user = User.objects.get(pk=user_id) try: newPlayerJSON = json.loads(request.raw_post_data) except ValueError: return HttpResponseBadRequest('Bad JSON') #Ensure the name attribute was provided with the JSON newPlayerName = "" try: newPlayerName = newPlayerJSON['name'] except KeyError: return HttpResponseBadRequest('No name given') #Ensure that the suers doesn't already have a player with the given name conflictingPlayer = Player.objects.filter(owning_user=user, name=newPlayerName) if conflictingPlayer.exists(): return HttpResponse('A player with that name already exists', status=409) #Create and save new player newPlayer = Player(owning_user=user, name=newPlayerName) newPlayer.save() #If password provided, create and save password if 'password' in newPlayerJSON: PlayerPassword(player=newPlayer, password_hash=hashPlayerPassword( newPlayerJSON['password'])).save() #If locaiton provided, geocode it and save it if 'location' in newPlayerJSON: location = newPlayerJSON['location'] if isValidLocation(location): try: doLocationSet(location['address'], location['city'], location['state'], location['zipcode'], newPlayer) except LocationNotFoundError: return HttpResponseBadRequest('Location not found') else: return HttpResponseBadRequest('Bad location') return HttpResponse(json.dumps({'player_id': newPlayer.id}), status=201, content_type="text/json")
def createPlayer(request, user_id): user = User.objects.get(pk=user_id) try: newPlayerJSON = json.loads(request.raw_post_data) except ValueError: return HttpResponseBadRequest('Bad JSON') #Ensure the name attribute was provided with the JSON newPlayerName = "" try: newPlayerName = newPlayerJSON['name'] except KeyError: return HttpResponseBadRequest('No name given') #Ensure that the suers doesn't already have a player with the given name conflictingPlayer = Player.objects.filter(owning_user=user, name=newPlayerName) if conflictingPlayer.exists(): return HttpResponse('A player with that name already exists', status=409) #Create and save new player algo = SortingAlgorithm.objects.get(name='Total Votes') newPlayer = Player(owning_user=user, name=newPlayerName, sorting_algo=algo) newPlayer.save() #If password provided, create and save password if 'password' in newPlayerJSON: PlayerPassword(player=newPlayer, password_hash=hashPlayerPassword(newPlayerJSON['password'])).save() #If locaiton provided, geocode it and save it if 'location' in newPlayerJSON: location = newPlayerJSON['location'] if isValidLocation(location): try: doLocationSet(location['address'], location['city'], location['state'], location['zipcode'], newPlayer) except LocationNotFoundError: return HttpResponseBadRequest('Location not found') else: return HttpResponseBadRequest('Bad location') return HttpResponse(json.dumps({'player_id' : newPlayer.id}), status=201, content_type="text/json")
def createPlayer(request): user = getUserForTicket(request) try: newPlayerJSON = json.loads(request.raw_post_data) except ValueError: return HttpResponseBadRequest('Bad JSON') #Ensure the name attribute was provided with the JSON try: newPlayerName = newPlayerJSON['name'] except KeyError: return HttpResponseBadRequest('No name given') #Determine which sorting algorithm to use if 'sorting_algorithm_id' in newPlayerJSON: try: sortingAlgo = SortingAlgorithm.objects.get(pk=newPlayerJSON['sorting_algorithm_id']) except ObjectDoesNotExist: toReturn = HttpResponseNotFound() toReturn[MISSING_RESOURCE_HEADER] = 'sorting-algorithm' return toReturn else: try: sortingAlgo = SortingAlgorithm.objects.get(function_name=DEFAULT_SORTING_ALGO) except ObjectDoesNotExist: raise ImproperlyConfigured('Default sorting algorithm is not in database') #Ensure that the suers doesn't already have a player with the given name conflictingPlayer = Player.objects.filter(owning_user=user, name=newPlayerName) if conflictingPlayer.exists(): return HttpResponse('A player with that name already exists', status=409) #Create and save new player newPlayer = Player( owning_user=user, name=newPlayerName, sorting_algo=sortingAlgo) newPlayer.save() #If password provided, create and save password if 'password' in newPlayerJSON: PlayerPassword(player=newPlayer, password_hash=hashPlayerPassword(newPlayerJSON['password'])).save() #If locaiton provided, geocode it and save it if 'location' in newPlayerJSON: location = newPlayerJSON['location'] if isValidLocation(location): try: setPlayerLocation(location, newPlayer) except LocationNotFoundError as e: return HttpResponseBadRequest('Location not found. Geocoder error: ' + str(e)) else: return HttpResponseBadRequest('Bad location') #create default library for new player new_library = Library(name="Default library", description="default library", pub_key="") new_library.save() new_default = DefaultLibrary(library=new_library, player=newPlayer) new_default.save() new_owned = OwnedLibrary(library=new_library, owner=user) new_owned.save() new_associated = AssociatedLibrary(library=new_library, player=newPlayer) new_associated.save() #Create Owner Permissions Group owner_group = PlayerPermissionGroup(player=newPlayer, name="owner") owner_group.save() owner_group.add_member(user) #Create Admin Permissions Group admin_group = PlayerPermissionGroup(player=newPlayer, name="admin") admin_group.save() #Add owner_group and admin group to select permissions set_default_player_permissions(newPlayer, owner_group) set_default_player_permissions(newPlayer, admin_group) return HttpJSONResponse(json.dumps(newPlayer, cls=UDJEncoder), status=201)
def createPlayer(request): user = getUserForTicket(request) try: newPlayerJSON = json.loads(request.raw_post_data) except ValueError: return HttpResponseBadRequest('Bad JSON') #Ensure the name attribute was provided with the JSON try: newPlayerName = newPlayerJSON['name'] except KeyError: return HttpResponseBadRequest('No name given') #Determine which sorting algorithm to use if 'sorting_algorithm_id' in newPlayerJSON: try: sortingAlgo = SortingAlgorithm.objects.get( pk=newPlayerJSON['sorting_algorithm_id']) except ObjectDoesNotExist: toReturn = HttpResponseNotFound() toReturn[MISSING_RESOURCE_HEADER] = 'sorting_algorithm' return toReturn else: try: sortingAlgo = SortingAlgorithm.objects.get( function_name=default_sorting_algo) except ObjectDoesNotExist: raise ImproperlyConfigured( 'Default sorting algorithm is not in database') #Determine external library externalLib = None if 'external_library_id' in newPlayerJSON: try: externalLib = ExternalLibrary.objects.get( pk=newPlayerJSON['external_library_id']) except ObjectDoesNotExist: toReturn = HttpResponseNotFound() toReturn[MISSING_RESOURCE_HEADER] = 'external_library' return toReturn #Ensure that the suers doesn't already have a player with the given name conflictingPlayer = Player.objects.filter(owning_user=user, name=newPlayerName) if conflictingPlayer.exists(): return HttpResponse('A player with that name already exists', status=409) #Create and save new player newPlayer = Player(owning_user=user, name=newPlayerName, sorting_algo=sortingAlgo, external_library=externalLib) newPlayer.save() #If password provided, create and save password if 'password' in newPlayerJSON: PlayerPassword(player=newPlayer, password_hash=hashPlayerPassword( newPlayerJSON['password'])).save() #If locaiton provided, geocode it and save it if 'location' in newPlayerJSON: location = newPlayerJSON['location'] if isValidLocation(location): try: setPlayerLocation(location, newPlayer) except LocationNotFoundError: return HttpResponseBadRequest('Location not found') else: return HttpResponseBadRequest('Bad location') return HttpJSONResponse(json.dumps({'player_id': newPlayer.id}), status=201)
def createPlayer(request, json_params): user = request.udjuser newPlayerName = json_params['name'] #Determine which sorting algorithm to use if 'sorting_algorithm_id' in json_params: try: sortingAlgo = SortingAlgorithm.objects.get(pk=json_params['sorting_algorithm_id']) except ObjectDoesNotExist: return HttpResponseMissingResource('sorting-algorithm') else: try: sortingAlgo = SortingAlgorithm.objects.get(function_name=DEFAULT_SORTING_ALGO) except ObjectDoesNotExist: raise ImproperlyConfigured('Default sorting algorithm is not in database') #If locaiton provided, attempted to geocode it. if 'location' in json_params: location = json_params['location'] if isValidLocation(location): try: address = location.get('address', "") locality = location.get('locality', "") region = location.get('region', "") postal_code = location['postal_code'] country = location['country'] lat, lon = geocodeLocation(postal_code, country, address, locality, region) except LocationNotFoundError as e: return HttpResponseBadRequest('Location not found. Geocoder error: ' + str(e)) else: return HttpResponseBadRequest('Bad location') #Create and save new player try: newPlayer = Player(owning_user=user, name=newPlayerName, sorting_algo=sortingAlgo) newPlayer.save() except IntegrityError: return HttpResponse('A player with that name already exists', status=409) #If password provided, create and save password if 'password' in json_params: newPlayer.setPassword(json_params['password']) #Set location if provided if 'location' in json_params: playerLocation = PlayerLocation(player=newPlayer, point=Point(lon, lat), address=address, locality=locality, region=region, postal_code=postal_code, country=country) playerLocation.save() #Create Owner Permissions Group owner_group = PlayerPermissionGroup(player=newPlayer, name="owner") owner_group.save() owner_group.add_member(user) #Add owner_group to select permissiosn set_default_player_permissions(newPlayer, owner_group) return HttpJSONResponse(json.dumps(newPlayer, cls=UDJEncoder), status=201)