def upload_files(self): """ Upload files from configuration to the Sharepoint site from the configuration. """ ctx = ClientContext(self._url, self._context_auth) root = ctx.web.lists.get_by_title("Documents").root_folder target_folder = root.folders.get_by_url(self._fconfig.out_folder) file_size = 0 for f in self._fconfig.files: self._logger.info(f"File upload: {os.path.basename(f)}") file_size = os.path.getsize(f) with tqdm( total=file_size, file=sys.stdout, unit="B", unit_scale=True, unit_divisor=1024, ascii=True, ) as pbar: def display_upload_progress(offset): # pylint: disable=unused-argument # print_progress callback in create_upload_session requires # the offset parameter to show progress like: # (`offset` out of `file_size` uploaded) # but tqdm instead works with `chunk_size`, i.e. size of step to # update the progress bar. """ Callback used to print progress to stdout during file update. """ # pbar is only ever used here, so it's safe to silence # the warning from pylint. pbar.update( # pylint: disable=cell-var-from-loop self._runtime.chunk_size) uploaded_file = target_folder.files.create_upload_session( f, self._runtime.chunk_size, display_upload_progress) try: ctx.execute_query() except ClientRequestException as err: self._logger.error(err.args[self._MESSAGE]) return False self._logger.success( f"File {f} uploaded to: {uploaded_file.serverRelativeUrl}") return True
def add_folder(self): """ Add folder given in FilesConfig.out to the list collection of the Sharepoint site from the configuration. """ ctx = ClientContext(self._url, self._context_auth) target_folder = ctx.web.lists.get_by_title("Documents").root_folder new_folder = target_folder.add(self._fconfig.out_folder) try: ctx.execute_query() except ClientRequestException as err: self._logger.error(err.args[self._MESSAGE]) return False self._logger.success(f"Added folder: {new_folder.serverRelativeUrl}") return True
def get_site_users(): """ Fetch SharepointUsers users :return: """ def generate(entities): yield "[" for index, entity in enumerate(entities): if index > 0: yield "," yield json.dumps(entity.properties) yield ']' ctx_auth = AuthenticationContext(URL) if ctx_auth.acquire_token_for_user(USERNAME, PASSWORD): ctx = ClientContext(URL, ctx_auth) user_col = ctx.web.site_users ctx.load(user_col) ctx.execute_query() return Response(generate(user_col), mimetype='application/json')
def openShiftPlan(creds): ctx_auth = AuthenticationContext(siteURL) if ctx_auth.acquire_token_for_user(creds[0], creds[1]): ctx = ClientContext(siteURL, ctx_auth) web = ctx.web ctx.load(web) ctx.execute_query() print("Web title: {0}".format(web.properties['Title'])) else: print(ctx_auth.get_last_error()) response = File.open_binary(ctx, relativeURL) #print(ctx.service_root_url()) #save data to BytesIO stream bytes_file_obj = io.BytesIO() bytes_file_obj.write(response.content) bytes_file_obj.seek(0) #set file object to start #read file into pandas dataframe return pd.read_excel(bytes_file_obj, sheet_name='Daily - Infra')
def get_from_list(list_name): """ Fetch list of entities from given sharepoint list :param list_name: :return: """ def generate(entities): yield "[" for index, entity in enumerate(entities): if index > 0: yield "," yield json.dumps(entity.properties) yield ']' ctx_auth = AuthenticationContext(URL) if ctx_auth.acquire_token_for_user(USERNAME, PASSWORD): ctx = ClientContext(URL, ctx_auth) list_object = ctx.web.lists.get_by_title(list_name) items = list_object.get_items() ctx.load(items) ctx.execute_query() return Response(generate(items), mimetype='application/json')
def post_entities(entities: list): ctx_auth = None token_acquired = False ctx = None values_to_send = None item_properties = None list_object = None for index, entity in enumerate(entities): if ctx_auth is None: ctx_auth = AuthenticationContext(URL) if token_acquired is False: ctx_auth.acquire_token_for_user(USERNAME, PASSWORD) if ctx_auth.provider.token is not None: ctx = ClientContext(URL, ctx_auth) list_object = ctx.web.lists.get_by_title(entity[LIST_NAME]) token_acquired = True if token_acquired: try: list_item_name = entity.get(LIST_ITEM_NAME) if list_item_name is None: item_properties_metadata = {} else: item_properties_metadata = { '__metadata': { 'type': list_item_name } } keys_to_send = entity['Keys'] values_to_send = { key: str(entity[key]) for key in keys_to_send } item_properties = { **item_properties_metadata, **values_to_send } existing_item = None if entity.get('ID'): try: existing_item = list_object.get_item_by_id( entity.get('ID')) ctx.load(existing_item) ctx.execute_query() except Exception as ie: logging.warning( "Item lookup by ID resulted in an exception from Office 365 {}" .format(ie)) if ( hasattr(ie, 'code') and ie.code == "-2147024809, System.ArgumentException" ) or (hasattr(ie, 'message') and ie.message == "Item does not exist. It may have been deleted by another user." ): existing_item = None else: raise if not existing_item: logging.info("Creating new item") new_item = list_object.add_item(item_properties) ctx.execute_query() entity['status'] = "OK: Sent to {}".format( entity[LIST_NAME]) entity['sharepoint_item'] = new_item.properties else: logging.info("Existing item found") update_result = update_list_item( ctx, entity[LIST_NAME], entity.get('ID'), values_to_send) if update_result.status_code > 299: raise Exception(update_result.text) else: entity['status'] = 'OK: updated successfully' except Exception as e: logging.error("An exception has occurred: {}".format(e)) entity[ 'status'] = "ERROR: An exception has occurred: {}".format( e) error_message = "An exception occurred during processing of current entity: {0} {1} ({2}".format( e.errno, e.strerror, json.dumps(entity)) raise Exception(error_message) else: error = ctx_auth.get_last_error() logging.error(error) entity['status'] = "ERROR: {}".format(error) raise Exception(error) return True
def conn(numero_autorizacao): # Definindo parametros de conexão # ID e Senha criados pelo portal do sharepoint app_settings = { 'url': 'https://usinacoruripe.sharepoint.com/sites/FaturamentoTorta', 'client_id': 'c74022f1-d1b5-47e3-913f-84d7a98cf032', 'client_secret': 'qfHtOWl6YieOhGAAavzuzUDvuf9pl2ZvD/0JSqvZhsQ=' } # Chamando conexão com API Rest context_auth = AuthenticationContext(url=app_settings['url']) context_auth.acquire_token_for_app( client_id=app_settings['client_id'], client_secret=app_settings['client_secret']) ctx = ClientContext(app_settings['url'], context_auth) # Puxando valores da lista lista_share = ctx.web.lists.get_by_title("Autorizações") items = lista_share.get_items() ctx.load(items) ctx.execute_query() for item in items: _id = '{0}'.format(item.properties["ID"]) id_autorizacao = '{0}'.format( item.properties["NumAutoriza_x00e7__x00e3_o"]) if id_autorizacao == numero_autorizacao: id_autorizacao = '{0}'.format( item.properties["NumAutoriza_x00e7__x00e3_o"]) fornecedor = '{0}'.format(item.properties["Fornecedor"]).strip() numero_ordem = '{0}'.format(item.properties["uwih"]).strip() transportador = '{0}'.format(item.properties["r9n0"]).strip() nome_motorista = '{0}'.format(item.properties["yecy"]).strip() cpf = '{0}'.format(item.properties["jcvj"]).strip() rg = '{0}'.format(item.properties["OData__x006d_vk6"]).strip() cnh = '{0}'.format(item.properties["wwof"]).strip() cavalo = '{0}'.format(item.properties["qbkd"]).strip() cavalo = cavalo.replace('-', '') minicipio_cavalo = '{0}'.format(item.properties["hr0e"]).strip() carreta_1 = '{0}'.format( item.properties["OData__x006d_cb0"]).strip() carreta_1 = carreta_1.replace('-', '') if carreta_1.upper() == 'NONE': carreta_1 = '' municipio_carreta_1 = '{0}'.format(item.properties["a8fj"]).strip() if municipio_carreta_1.upper() == 'NONE': municipio_carreta_1 = '' carreta_2 = '{0}'.format(item.properties["qdqz"]).strip() carreta_2 = carreta_2.replace('-', '') if carreta_2.upper() == 'NONE': carreta_2 = '' municipio_carreta_2 = '{0}'.format( item.properties["OData__x0071_aw9"]).strip() if municipio_carreta_2.upper() == 'NONE': municipio_carreta_2 = '' tipo_veiculo = '{0}'.format(item.properties["OData__x0065_op5"]) procedimento_especial = '{0}'.format(item.properties["i0dv"]) numero_eixos = '{0}'.format(item.properties["ahpu"]) quantidade_ordens = '{0}'.format(item.properties["hpzf"]) geradas = '{0}'.format(item.properties["OData__x006d_kv6"]) data_inicio = '{0}'.format(item.properties["OData__x0068_qp8"]) data_final = '{0}'.format(item.properties["OData__x0078_od1"]) data_execucao = '{0}'.format(item.properties["ejtw"]) ativo = '{0}'.format(item.properties["ATIVA"]) autoriazacao = AutorizacaoSharePoint() autoriazacao.id_autorizacao = id_autorizacao autoriazacao.fornecedor = fornecedor autoriazacao.numero_ordem = numero_ordem autoriazacao.transportador = transportador autoriazacao.nome_motorista = nome_motorista autoriazacao.cpf = cpf autoriazacao.rg = rg autoriazacao.cnh = cnh autoriazacao.cavalo = cavalo autoriazacao.minicipio_cavalo = minicipio_cavalo autoriazacao.carreta_1 = carreta_1 autoriazacao.municipio_carreta_1 = municipio_carreta_1 autoriazacao.carreta_2 = carreta_2 autoriazacao.municipio_carreta_2 = municipio_carreta_2 autoriazacao.tipo_veiculo = tipo_veiculo autoriazacao.procedimento_especial = procedimento_especial autoriazacao.numero_eixos = numero_eixos autoriazacao.quantidade_ordens = quantidade_ordens autoriazacao.geradas = geradas autoriazacao.data_inicio = data_inicio autoriazacao.data_final = data_final autoriazacao.data_execucao = data_execucao autoriazacao.ativo = ativo autoriazacao._id = _id return autoriazacao return None
def post_entities(entities: list): ctx_auth = AuthenticationContext(URL) ctx_auth.acquire_token_for_user(USERNAME, PASSWORD) if ctx_auth.provider.token: ctx = ClientContext(URL, ctx_auth) else: error = ctx_auth.get_last_error() logging.error(error) raise Exception(error) for _, entity in enumerate(entities): if entity['_deleted'] and not PROCESS_DELETED: logging.debug( f"entity {entity['_id']} marked as deleted and will not be processed" ) continue list_object = ctx.web.lists.get_by_title(entity[LIST_NAME]) try: list_item_name = entity.get(LIST_ITEM_NAME) if list_item_name is None: item_properties_metadata = {} else: item_properties_metadata = { '__metadata': { 'type': list_item_name } } keys_to_send = entity['Keys'] values_to_send = { key: str(entity[key]) for key in keys_to_send } item_properties = { **item_properties_metadata, **values_to_send } existing_item = None if entity.get('ID'): try: existing_item = list_object.get_item_by_id( entity.get('ID')) ctx.load(existing_item) ctx.execute_query() except Exception as ie: logging.warning( "Item lookup by ID resulted in an exception from Office 365 {}" .format(ie)) if ( hasattr(ie, 'code') and ie.code == "-2147024809, System.ArgumentException" ) or (hasattr(ie, 'message') and ie.message == "Item does not exist. It may have been deleted by another user." ): existing_item = None else: raise Exception from ie if not existing_item: logging.info("Creating new item") list_object.add_item(item_properties) ctx.execute_query() else: logging.info("Existing item found") if entity.get('SHOULD_DELETE') is not None and bool( entity.get('SHOULD_DELETE')): response = delete_list_item(ctx, entity[LIST_NAME], entity.get('ID')) else: response = update_list_item(ctx, entity[LIST_NAME], entity.get('ID'), values_to_send) response.raise_for_status() except Exception as e: error_message = f"An exception occurred during processing of an entity: {e} ({json.dumps(entity)}" logging.error(error_message) raise Exception(error_message) from e
from settings import settings from office365.runtime.auth.user_credential import UserCredential from office365.sharepoint.client_context import ClientContext credentials = UserCredential(settings['user_credentials']['username'], settings['user_credentials']['password']) ctx = ClientContext(settings['url']).with_credentials(credentials) # create one folder only target_folder = "/Shared Documents/test_folder" target_folder = ctx.web.get_folder_by_server_relative_url(target_folder) target_folder.add("new_folder") ctx.execute_query() # have to execute # create relative folder tree, no execution required target_folder = "/Shared Documents/test_folder/20201116/1133/test" target_folder = ctx.web.ensure_folder_path(target_folder)