first commit

This commit is contained in:
Eidan T.
2026-05-09 14:00:04 -04:00
commit e0ec11977c
42 changed files with 4596 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
import requests
import os
import msal
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.auth.client_credential import ClientCredential
class SharepointManager:
def __init__(self, site, userSharepoint, passwordSharepoint):#,client_id,tenant_id,client_secret):
self.site = site
self.userSharepoint = userSharepoint
self.passwordSharepoint = passwordSharepoint
#self.client_id = client_id
#self.tenant_id = tenant_id
#self.client_secret = client_secret
#print(self.client_secret)
def uploadFile2(self, task, sharepoint_path):
self._connect2()
file_name = os.path.basename(task.docsend_Path)
with open(task.docsend_Path, "rb") as file_content:
folder = self.ctx.web.get_folder_by_server_relative_url(sharepoint_path)
upload = folder.upload_file(file_name, file_content.read()) # solo prepara la query
self.ctx.execute_query() # ejecuta en el contexto
print(f"✅ Archivo {file_name} subido a {upload.serverRelativeUrl}")
self._disconnect()
def _connect2(self):
#"""Conexión a SharePoint con access_token"""
#token = self._get_token()
#self.ctx = ClientContext(self.site).with_access_token(token)
#self.ctx.load(self.ctx.web)
#self.ctx.execute_query()
self.ctx = ClientContext(self.site).with_credentials(ClientCredential(self.client_id, self.client_secret))
self.ctx.load(self.ctx.web)
self.ctx.execute_query()
def _connect(self):
# 👇 Nada de AuthenticationContext aquí
self.ctx = ClientContext(self.site).with_credentials(
UserCredential(self.userSharepoint, self.passwordSharepoint)
)
self.ctx.load(self.ctx.web)
self.ctx.execute_query()
def _disconnect(self):
self.ctx = None
def uploadFile(self, task, sharepoint_path):
self._connect()
file_name = os.path.basename(task.docsend_Path)
with open(task.docsend_Path, "rb") as file_content:
response = (
self.ctx.web.get_folder_by_server_relative_url(sharepoint_path)
.upload_file(file_name, file_content.read())
.execute_query()
)
print(f"Archivo {file_name} subido a {response.serverRelativeUrl}")
self._disconnect()
def uploadFile2(self, task, sharepoint_path):
"""Subida con MSAL (App Registration)"""
self._connect2()
file_name = os.path.basename(task.docsend_Path)
with open(task.docsend_Path, "rb") as file_content:
folder = self.ctx.web.get_folder_by_server_relative_url(sharepoint_path)
upload = folder.upload_file(file_name, file_content.read())
self.ctx.execute_query()
print(f"✅ Archivo {file_name} subido a {upload.serverRelativeUrl}")
self._disconnect()
def uploadFileTemp(self, task, data):
self._connect()
file_name = os.path.basename(task.docsend_Path)
with open(task.docsend_Path, "rb") as file_content:
response = (
self.ctx.web.get_folder_by_server_relative_url(data[6])
.upload_file(file_name, file_content.read())
.execute_query()
)
print(f"Archivo {file_name} subido a {response.serverRelativeUrl}")
self._disconnect()
def dropFilesFolder(self, data):
self._connect()
folder = (
self.ctx.web.get_folder_by_server_relative_url(data[6])
.expand(["Files"])
.get()
.execute_query()
)
for file in folder.files:
print(f"Eliminando archivo: {file.name}")
file.delete_object()
self.ctx.execute_query()
self._disconnect()