From dd1a529cdf21c3294593d9939c871eb84cda6789 Mon Sep 17 00:00:00 2001 From: mabejoyb Date: Wed, 13 May 2026 18:22:54 -0300 Subject: [PATCH] Ajustes para unificar dependencia de current_url. --- agregar_planos.py | 10 ++++---- duplicar_plantilla_contrato.py | 4 +++- envio_contrato_docs_email.py | 39 ++++++++++++++++++++++++++++++-- plan_de_pago_contado.py | 6 ++--- reemplazar_variables_contrato.py | 38 +++++++++++++++++++++++++++++-- 5 files changed, 82 insertions(+), 15 deletions(-) diff --git a/agregar_planos.py b/agregar_planos.py index e40bbe2..7d1fab2 100644 --- a/agregar_planos.py +++ b/agregar_planos.py @@ -564,11 +564,9 @@ try: docs_service, drive_service, auth_mode = get_services(cred_path, impersonated_user) _sv("gdoc_auth_mode", auth_mode) - # doc_id desde URL del navegador - url_doc = "{current_url}" - #if url_doc == "": - # url_doc = _gvs("current_url", "") - #_sv("gdoc_url_used", url_doc) + # doc_id desde current_url, que lo setea duplicar_plantilla_contrato.py + url_doc = _gvs("current_url", "") + _sv("gdoc_url_used", url_doc) doc_id = extract_doc_id_from_url(url_doc) if doc_id == "": @@ -594,4 +592,4 @@ except HttpError as e: except Exception as e: _sv("gdoc_planos_status", "ERROR") _sv("gdoc_planos_error", str(e)) - raise \ No newline at end of file + raise diff --git a/duplicar_plantilla_contrato.py b/duplicar_plantilla_contrato.py index b0a458f..f1972d8 100644 --- a/duplicar_plantilla_contrato.py +++ b/duplicar_plantilla_contrato.py @@ -148,6 +148,8 @@ try: # Retornar valores (Rocketbot compatible) try: SetVar('current_url', doc_url) + SetVar('gdoc_id', copied_doc['id']) + SetVar('gdoc_url_final', doc_url) SetVar('doc_copy_created', True) except NameError: pass # Si no está en Rocketbot, solo imprime @@ -163,4 +165,4 @@ except Exception as e: SetVar('doc_copy_created', False) except NameError: pass - print(f"Error detectado: {str(e)}") \ No newline at end of file + print(f"Error detectado: {str(e)}") diff --git a/envio_contrato_docs_email.py b/envio_contrato_docs_email.py index 4020cd3..7710141 100644 --- a/envio_contrato_docs_email.py +++ b/envio_contrato_docs_email.py @@ -4,6 +4,7 @@ from googleapiclient.discovery import build import os import sys import json +import re import base64 from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart @@ -26,6 +27,30 @@ CREDENTIALS_PATH = '{gdoc_sa_json2}' IMPERSONATED_USER = '{gdoc_impersonated_user}' +def rb_get_var(name, required=True, default=''): + try: + value = GetVar(name) + except NameError: + value = os.environ.get(name, default) + + if value is None: + value = '' + + value = str(value).strip() + if value == f'{{{name}}}': + value = '' + + if required and not value: + raise RuntimeError(f'La variable Rocketbot "{name}" está vacía o no fue leída correctamente.') + + return value + + +def extract_doc_id_from_url(url): + match = re.search(r"/document/d/([a-zA-Z0-9_-]+)", url or "") + return match.group(1) if match else "" + + def _load_json(path): with open(path, 'r', encoding='utf-8') as f: return json.load(f) @@ -99,7 +124,17 @@ drive_service, gmail_service = get_drive_and_gmail_services( CREDENTIALS_PATH, IMPERSONATED_USER) # Id del documento de Google Drive a enviar por email -DOC_ID = '{gdoc_id}' +CURRENT_URL = rb_get_var('current_url') +DOC_ID = extract_doc_id_from_url(CURRENT_URL) +if not DOC_ID: + raise RuntimeError('No pude extraer documentId desde current_url: ' + CURRENT_URL) + +try: + SetVar('gdoc_id', DOC_ID) + SetVar('gdoc_url_used', CURRENT_URL) +except NameError: + pass + print(DOC_ID) # Obtener nombre del documento de Google @@ -131,4 +166,4 @@ pdf_data = export_doc_as_pdf(drive_service, DOC_ID) result = send_email(gmail_service, to_emails, SUBJECT, BODY_TEXT, pdf_data, pdf_filename) print('Mensaje enviado. ID:', result.get('id')) -SetVar('res_envio', True) \ No newline at end of file +SetVar('res_envio', True) diff --git a/plan_de_pago_contado.py b/plan_de_pago_contado.py index 2330455..62f7725 100644 --- a/plan_de_pago_contado.py +++ b/plan_de_pago_contado.py @@ -1239,9 +1239,7 @@ try: docs_service, drive_service, auth_mode = get_services(cred_path, impersonated_user) _sv("gdoc_auth_mode", auth_mode) - url = _gvs("gdoc_url", "") - if url == "": - url = _gvs("current_url", "") + url = _gvs("current_url", "") _sv("gdoc_url_used", url) doc_id = extract_doc_id_from_url(url) @@ -1454,4 +1452,4 @@ except Exception as e: SetVar("gdoc_error", str(e)) except Exception: pass - raise \ No newline at end of file + raise diff --git a/reemplazar_variables_contrato.py b/reemplazar_variables_contrato.py index 823d4c2..68865aa 100644 --- a/reemplazar_variables_contrato.py +++ b/reemplazar_variables_contrato.py @@ -3,6 +3,7 @@ from googleapiclient.discovery import build import os import sys import json +import re global Request, InstalledAppFlow, Credentials, service_account, build, os, sys, json, LISTA_BUSCAR_REEMPLAZAR, base_dir, libs_dir, SCOPES, CREDENTIALS_PATH, TOKEN_PATH, _load_json, get_google_services, replace_vars_doc, drive_service, docs_service, gdoc_id, lista_oficial_reemplazar, success @@ -26,6 +27,30 @@ CREDENTIALS_PATH = '{gdoc_sa_json2}' IMPERSONATED_USER = '{gdoc_impersonated_user}' +def rb_get_var(name, required=True, default=''): + try: + value = GetVar(name) + except NameError: + value = os.environ.get(name, default) + + if value is None: + value = '' + + value = str(value).strip() + if value == f'{{{name}}}': + value = '' + + if required and not value: + raise RuntimeError(f'La variable Rocketbot "{name}" está vacía o no fue leída correctamente.') + + return value + + +def extract_doc_id_from_url(url): + match = re.search(r"/document/d/([a-zA-Z0-9_-]+)", url or "") + return match.group(1) if match else "" + + def _load_json(path): with open(path, 'r', encoding='utf-8') as f: return json.load(f) @@ -92,11 +117,20 @@ def replace_vars_doc(docs_service, gdoc_id, replacements_values): drive_service, docs_service = get_google_services(CREDENTIALS_PATH, IMPERSONATED_USER) # Variables de entrada (esto usualmente viene de Rocketbot via GetVar) -gdoc_id = '{gdoc_id}' +current_url = rb_get_var('current_url') +gdoc_id = extract_doc_id_from_url(current_url) +if not gdoc_id: + raise RuntimeError('No pude extraer documentId desde current_url: ' + current_url) lista_oficial_reemplazar = {lista_oficial_reemplazar} try: + try: + SetVar('gdoc_id', gdoc_id) + SetVar('gdoc_url_used', current_url) + except NameError: + pass + # Ejecutar reemplazo si se cuenta con la información if gdoc_id and lista_oficial_reemplazar: success = replace_vars_doc(docs_service, gdoc_id, lista_oficial_reemplazar) @@ -114,4 +148,4 @@ except Exception as e: try: SetVar('error', {'error': 'Error al remplazar las variables en Google Docs', 'descripcion': str(e)}) except NameError: - pass \ No newline at end of file + pass