Beispiel #1
0
def create_library(request, json_params):

  new_library = Library(name=json_params['name'],
                        description=json_params['description'],
                        pub_key=json_params['public_key']
                       )
  new_library.save()
  new_owned_library = OwnedLibrary(library=new_library, owner=request.udjuser)
  new_owned_library.save()
  return HttpJSONResponse(json.dumps(new_library, cls=UDJEncoder), status=201)
Beispiel #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')


  #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)