예제 #1
0
def dsCreate(request):
  """View Handler for creating data sources."""
  clientDsObj = json.loads(request.body)
  try:
    result = createDataSource(request, clientDsObj)
    if   result['status'] == 'success':
      return jsonResponse({'key': result['key']})
    elif result['status'] == 'error':
      return jsonResponse(result['error'], status = 400)
  except RedirectRequired as red:
    return jsonResponse({'redirect': red.url})
예제 #2
0
def _verifyPendingDs(request, pendingDsKey):
  """
  Helper function to verify pending data source creation.

  Args:
    request: A Django request object; used for secureStorageKey and sessionKey.
    pendingDsKey: A data source key corresponding to the one needing verification.

  Returns:
    A dictionary detailing whether or not the verification was a success or failure.

    In the case of success, the 'status' field of the return dictionary is 'success',
    and the data source key is passed back in the 'dsKey' field.

    In the case of failure, the 'status' field is set to 'error' and a 'error' field
    is filled with a description of the error.

    In the special case that the data source was not found, the 'status' field has
    value 'notFound'.
  """
  # pylint: disable = E1101
  # Pylint does not recognize Django models as having 'objects' attribute.
  # Pylint does not recognize Django model as having 'DoesNotExist' attribute.
  try:
    pendingDs = PendingDataSource.objects.get(key=pendingDsKey)
  except PendingDataSource.DoesNotExist:
    return {'status': 'notFound'}

  if pendingDs.user:
    assert pendingDs.user == user

  dsParams = pendingDs.params_json
  result = createDataSource(request, dsParams)

  if result['status'] == 'success':
    # Move over the local data source object, if it exists
    try:
      lds = LocalDataSource.objects.get(pendingdatasource=pendingDs)
      lds.datasource = DataSource.objects.get(key=result['key'])
      lds.pendingdatasource = None
      lds.save()
    except LocalDataSource.DoesNotExist:
      pass
    return { 'status': 'success'
           , 'dsKey': result['key']}

  if result['status'] == 'error':
    return { 'status': 'error'
           , 'error': result['error']}

  raise Exception('unknown createDs status: ' + result['status'])