예제 #1
0
파일: player.py 프로젝트: hsingh23/UDJ
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")
예제 #2
0
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)