async def post_login_infos(
        *,
        resp_: Response,
        request: Request,
        # access_token: str = p_access_token,
        user_login: UserLogin = Body(..., embed=True),
        api_key: APIKey = Depends(get_api_key),
):
    """ 
  Needs an anonymous access_token
  """
    ### DEBUGGING
    print()
    print("-+- " * 40)
    log_.debug("POST / %s", inspect.stack()[0][3])
    time_start = datetime.now()

    log_.debug("user_login : \n%s", pformat(user_login.dict()))
    log_.debug("request : \n%s", pformat(request.__dict__))

    resp_login = distantAuthCall(api_request=request,
                                 query={},
                                 payload={**user_login.dict()},
                                 func_name='login_user',
                                 token=api_key)

    return resp_login
def delete_one_dsr(as_test=True,
                   server_api=None,
                   access_token=None,
                   full_remove=False,
                   dsi_uuid=None):

    if server_api == None:
        server_api = get_server_api()

    ### get test user
    if access_token == None:
        test_user_access_token = client_login(as_test=False,
                                              only_access_token=True)
        log_.debug('=== delete_one_dsr / test_user_access_token : %s',
                   test_user_access_token)
    else:
        test_user_access_token = access_token

    ### get DSI uuid
    if dsi_uuid == None:
        test_dsi = create_one_dsi(as_test=False)
        assert test_dsi['data']['is_test_data'] == True
        test_dsi_uuid = test_dsi['data']['dsi_uuid']
        test_dsr_01 = create_one_dsr(as_test=False, dsi_uuid=test_dsi_uuid)
        log_.debug('=== delete_one_dsr / test_dsr_01 : \n%s',
                   pformat(test_dsr_01))
        # test_dsr_02 = create_one_dsr( as_test=False, dsi_uuid=test_dsi_uuid )
        # test_dsr_03 = create_one_dsr( as_test=False, dsi_uuid=test_dsi_uuid )
        test_dsr_uuid = test_dsr_01['data']['dsr_uuid']

    else:
        test_dsi_uuid = dsi_uuid
        test_dsr_list = get_list_dsr(dsi_uuid=test_dsi_uuid)
        log_.debug('=== delete_one_dsr / test_dsr_list : \n%s',
                   pformat(test_dsr_list))
        test_dsr_uuid = get_random_dsr_uuid(dsi_uuid=test_dsi_uuid)

    log_.debug('=== delete_one_dsr / test_dsi_uuid : %s', test_dsi_uuid)
    log_.debug('=== delete_one_dsr / test_dsr_uuid : %s', test_dsr_uuid)

    params_delete = {
        'full_remove': full_remove,
    }
    headers = {
        'accept': 'application/json',
        'access_token': test_user_access_token,
    }

    # url = f"{server_api}{config.API_V1_STR}/crud/dataset/{test_dsi_uuid}/dsr/remove/{test_dsr_uuid}"
    url = f"{config.API_V1_STR}/crud/dataset/{test_dsi_uuid}/dsr/remove/{test_dsr_uuid}"

    # response = requests.delete(
    response = client.delete(url, params=params_delete, headers=headers)
    log_.debug('=== delete_one_dsr / response : \n%s', pformat(response))

    if as_test:
        assert response.status_code == 200
    else:
        resp = response.json()
        return resp
def remove_mongodb_many_documents(
  m_client_db=get_mongodb_database(),
  collection=None,
  index_name=None,
  doc_type=None,
  ):
  """Function to delete a list of documents."""

  status = { 'status_code' : 200 }

  log_.debug( "function : %s", inspect.stack()[0][3] )
  log_.debug( "locals() : \n%s", pformat(locals()))

  db = m_client_db[ collection ]

  ### TO DO 
  # build query
  query = {}
  doc_query = build_mongodb_query( query )

  # find and delete many document
  try :
    res = db.delete_many( doc_query )
  except :
    res = None
    status = {
      'status_code' : 500,
      'error' : "",
      'info' : "",
    }

  log_.debug( "res : \n%s", pformat(res))
  print()
  return res, status
def view_mongodb_document(
  m_client_db=get_mongodb_database(),
  collection=None,
  index_name=None,
  doc_type=None,
  doc_uuid=None,
  ):
  """Function to view a MongoDB document."""

  status = { 'status_code' : 200 }

  log_.debug( "function : %s", inspect.stack()[0][3] )
  log_.debug( "locals() : \n%s", pformat(locals()))

  db = m_client_db[ collection ]

  ### TO DO 
  res = db.find_one(
    { "_id" : doc_uuid },
    { "_id": 0 } ### projection fields
  )
  log_.debug( "res : \n%s", pformat(res))
  res_list = list(res)

  log_.debug( "res_list : \n%s", pformat(res_list))
  print()
  return res_list, status
def create_dsr(
  dsi_uuid: str = None,
  dsr_uuid: str = None,
  query_params: dict = None,
  body = None,
  user: dict = None
  ):
  """ create a dsr in ES / MongoDB """

  ### check if DSI exists first 

  ### add document
  res, status = create_document(
    index_name = dsi_uuid,
    doc_type = DSR_DOC_TYPE,
    doc_uuid = dsr_uuid,
    params = query_params,
    body = body,
    user = user
  )

  log_.debug( "status : \n%s", pformat(status))
  log_.debug( "res : \n%s", pformat(res))

  return res, status
def view_dsr(
  dsi_uuid: str = None,
  dsr_uuid: str = None,
  query_params: dict = None,
  user: dict = None
  ):
  """ get a dsr from ES / MongoDB """

  ### TO DO 
  ### check user's auth 
  
  res, status = view_document(
    index_name = dsi_uuid,
    doc_type = DSR_DOC_TYPE,
    doc_uuid = dsr_uuid,
    query_params = query_params,
    user = user
  )
  log_.debug( "res : \n%s", pformat(res))
  log_.debug( "status : \n%s", pformat(status))

  ### TO DO 
  ### check user's auth 

  has_user_auth = is_user_authorized(
    user = user,
    doc_auth = DocAuthData( **res['_source'] ),
    level = 'read',
  )
  log_.debug( "has_user_auth : \n%s", pformat(has_user_auth))


  return res, status
def remove_dsr(
  dsi_uuid: str = None,
  dsr_uuid: str = None,
  query_params: dict = None,
  user: dict = None
  ):
  """ remove a dsr from ES / MongoDB """

  ### get dsr infos
  res_dsr, status_dsr = view_document(
    index_name = dsi_uuid,
    doc_type = DSR_DOC_TYPE,
    doc_uuid = dsr_uuid,
    query_params = query_params,
    user = user
  )
  log_.debug( "res_dsr : \n%s", pformat(res_dsr))
  log_.debug( "status_dsr : \n%s", pformat(status_dsr))

  ### TO DO 
  ### check user's auth 
  has_user_auth = is_user_authorized(
    user = user,
    doc_auth = DocAuthData( **res_dsr['_source'] ),
    level = 'delete',
  )
  log_.debug( "has_user_auth : \n%s", pformat(has_user_auth))


  ### remove doc
  if query_params['full_remove'] == True : 

    ### remove corresponding DSR doc
    res, status = remove_document(
      index_name = dsi_uuid,
      doc_type = DSR_DOC_TYPE,
      doc_uuid = dsr_uuid,
      params = query_params,
      user = user
    )
  
  else : 

    ### update corresponding DSR doc as 'deleted'
    res, status = update_document(
      index_name = dsi_uuid,
      doc_type = DSR_DOC_TYPE,
      doc_uuid = dsr_uuid,
      params = query_params,
      body = { 'is_deleted' : True },
      user = user
    )

  log_.debug( "res : \n%s", pformat(res))
  log_.debug( "status : \n%s", pformat(status))

  return res, status
def get_one_dsr(
    as_test=True,
    only_test_data=False,
    dsi_uuid=None,
    access_token=None,
):

    server_api = get_server_api()
    # log_.debug('=== server_api : %s', server_api)

    ### get test user
    if access_token == None:
        test_user_access_token = client_login(as_test=False,
                                              only_access_token=True)
    else:
        test_user_access_token = access_token
    log_.debug('=== update_one_dsr / test_user_access_token : %s',
               test_user_access_token)

    ### get DSI UUID
    if dsi_uuid == None:
        test_dsi = create_one_dsi(as_test=False)
        assert test_dsi['data']['is_test_data'] == True
        test_dsi_uuid = test_dsi['data']['dsi_uuid']
    else:
        test_dsi_uuid = dsi_uuid
    log_.debug('=== test_get_one_dsr / test_dsi_uuid : %s', test_dsi_uuid)

    ### get DSR UUID
    test_dsr = create_one_dsr(as_test=False, dsi_uuid=test_dsi_uuid)
    log_.debug('=== test_get_one_dsr / test_dsr : \n%s', pformat(test_dsr))
    test_dsr_uuid = test_dsr['data']['dsr_uuid']
    log_.debug('=== test_get_one_dsr / test_dsr_uuid : %s', test_dsr_uuid)

    headers = {
        'accept': 'application/json',
        'access_token': test_user_access_token,
    }

    # url = f"{server_api}{config.API_V1_STR}/crud/dataset/{test_dsi_uuid}/dsr/get_one/{test_dsr_uuid}"
    url = f"{config.API_V1_STR}/crud/dataset/{test_dsi_uuid}/dsr/get_one/{test_dsr_uuid}"

    ### get DSRs list
    # response = requests.get(
    response = client.get(url, headers=headers)
    resp = response.json()
    log_.debug('=== test_get_one_dsr / resp : \n%s', pformat(resp))

    if as_test:
        assert response.status_code == 200
    else:
        return resp
def create_one_dsr(
    as_test=True,
    dsi_uuid=None,
    access_token=None,
    payload=None,
):

    # server_api = get_server_api()

    ### ACCESS TOKEN
    if access_token == None:
        test_user_access_token = client_login(as_test=False,
                                              only_access_token=True)
    else:
        test_user_access_token = access_token
    log_.debug('=== test_user_access_token : %s', test_user_access_token)

    ### DSI UUID
    if dsi_uuid == None:
        try:
            test_dsi_uuid = get_random_dsi_uuid(only_test_dsi=True)
        except:
            test_dsi = create_one_dsi(as_test=False)
            test_dsi_uuid = test_dsi['data']['dsi_uuid']

    else:
        test_dsi_uuid = dsi_uuid

    ### PAYLOAD
    if payload == None:
        random_int = random.randint(0, 1000)
        dsr_test_payload = {
            "is_test_data": True,
            "auth_preview": "opendata",
            "auth_modif": "private",
            "data": {
                "field_01": f"test DSR - {random_int}"
            }
        }
    else:
        dsr_test_payload = payload

    ### REQUEST
    headers = {
        'accept': 'application/json',
        'access_token': test_user_access_token,
    }

    # url = f"{server_api}{config.API_V1_STR}/crud/dataset/{test_dsi_uuid}/dsr/create"
    url = f"{config.API_V1_STR}/crud/dataset/{test_dsi_uuid}/dsr/create"

    # response = requests.post(
    response = client.post(url, json=dsr_test_payload, headers=headers)
    resp = response.json()
    log_.debug('=== resp : \n%s', pformat(resp))

    if as_test:
        assert response.status_code == 200
    else:
        return resp
def view_dsi(
  dsi_uuid: str = None,
  query_params: dict = {},
  user: dict = {},
  ):
  """ get a dsi from ES / MongoDB """

  ### TO DO 
  ### check user's auth 
  
  res_dsi, status_dsi = view_document(
    index_name = DSI_DOC_TYPE,
    doc_type = 'metadata',
    doc_uuid = dsi_uuid,
    query_params = query_params,
    user = user,
  )
  # log_.debug( "res_dsi : \n%s", pformat(res_dsi))
  # log_.debug( "status_dsi : \n%s", pformat(status_dsi))

  ### TO DO 
  ### check user's auth 

  has_user_auth = is_user_authorized(
    user = user,
    doc_auth = DocAuthData(**res_dsi['_source']),
    level = 'read',
  )
    # level = level,
  log_.debug( "has_user_auth : \n%s", pformat(has_user_auth))


  return res_dsi, status_dsi
Exemple #11
0
def LocToDict(location_raw, src_geocoder=None) : 
	""" 
	location formater
	"""
	log.debug("... LocToDict / location_raw : \n%s ", pformat(location_raw) )
	if location_raw != None : 
		return {
			"src_geocoder"	: src_geocoder,
			"raw"       	: location_raw.raw,
			"address"   	: location_raw.address,
			"point"     	: location_raw.point,
			# "latitude"  	: location_raw.latitude,
			# "longitude" 	: location_raw.longitude,
			"lat"			  	: location_raw.latitude,
			"lon"				 	: location_raw.longitude,
		}
	else : 
		return {
			"src_geocoder"	: src_geocoder,
			"raw"        	: None,
			"address"    	: None,
			"point"      	: None,
			# "latitude"   	: None,
			# "longitude"  	: None,
			"lat"   			: None,
			"lon"  				: None,
		}
def create_mongodb_index(
  m_client=create_mongodb_client(),
  index_name=None
  ):
  """Functionality to create index."""

  status = { 'status_code' : 200 }

  log_.debug( "function : %s", inspect.stack()[0][3] )
  log_.debug( "locals() : \n%s", pformat(locals()))

  ### TO DO 
  res = {}

  log_.debug( "res : \n%s", pformat(res))
  print()
  return res, status
def delete_mongodb_index(
  m_client=create_mongodb_client(),
  index_name=None
  ):
  """Delete an index by specifying the index name"""
  
  status = { 'status_code' : 200 }

  log_.debug( "function : %s", inspect.stack()[0][3] )
  log_.debug( "locals() : \n%s", pformat(locals()))

  ### TO DO 
  res = {}
  
  log_.debug( "res : \n%s", pformat(res))
  print()
  return res, status
Exemple #14
0
def send_async_email(curr_app, msg):
    """
  send an email asynchronously due to the decorator
	"""
    log.debug("... sending async email...")
    log.debug("... msg : \n%s", pformat(msg))

    with curr_app.app_context():
        mail.send(msg)
def add_mongodb_document(
  m_client_db=get_mongodb_database(),
  collection=None,
  index_name=None,
  doc_type=None,
  doc_uuid=None,
  doc_body=None
  ):
  """
  Funtion to add a MongoDB document by providing index_name,
  document type, document contents as doc and document id.
  """

  status = { 'status_code' : 200 }

  log_.debug( "function : %s", inspect.stack()[0][3] )
  log_.debug( "locals() : \n%s", pformat(locals()))

  db = m_client_db[ collection ]

  ### TO DO 
  try : 
    res = db.insert_one(
      doc_body,
    )
    res_add = { 
      'item_id' : str(res.inserted_id),
      'operation' : "item added" 
    }
  except : 
    res = {}
    res_add = { 
      'item_id' : None,
      'operation' : 'not added...' 
    }
    status = {
      'status_code' : 500,
      'error' : "",
      'info' : ""
    }
  # log_.debug( "res : \n%s", pformat(res.__dict__))
  log_.debug( "res_add : \n%s", pformat(res_add))
  print()
  return res_add, status
def test_create_one_dsr(client_access_token):

    test_dsi = create_one_dsi(access_token=client_access_token, as_test=False)
    log_.debug('=== test_dsi : \n%s', pformat(test_dsi))
    assert test_dsi['data']['is_test_data'] == True

    test_dsi_uuid = test_dsi['data']['dsi_uuid']
    log_.debug('=== test_dsi_uuid : %s', test_dsi_uuid)

    create_one_dsr(dsi_uuid=test_dsi_uuid)
def remove_mongodb_document(
  m_client_db=get_mongodb_database(),
  collection=None,
  index_name=None,
  doc_type=None,
  doc_uuid=None
  ):
  """Function to delete a specific document."""

  status = { 'status_code' : 200 }

  log_.debug( "function : %s", inspect.stack()[0][3] )
  log_.debug( "locals() : \n%s", pformat(locals()))

  db = m_client_db[ collection ]

  ### TO DO 

  # build query
  # doc_query = build_mongodb_query( query, doc_uuid )
  doc_query = {
    f"{index_name}_uuid" : doc_uuid
  }
  log_.debug( "doc_query : \n%s", pformat( doc_query ))

  # find and delete document
  try :
    res = db.delete_one( 
      doc_query 
    )

  except : 
    res = {}
    status = {
      'status_code' : 500,
      'error' : "",
      'info' : "",
    }

  log_.debug( "res : \n%s", pformat(res))
  print()
  return res, status