diff --git a/credentials.json b/credentials.json new file mode 100644 index 0000000..a0c9505 --- /dev/null +++ b/credentials.json @@ -0,0 +1 @@ +{"installed":{"client_id":"249203512502-8ut4vkh77ns4rl40ia485t460niii2b8.apps.googleusercontent.com","project_id":"python-access-2025","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}} \ No newline at end of file diff --git a/readme.txt b/readme.txt index e25a864..ec862c3 100644 --- a/readme.txt +++ b/readme.txt @@ -6,7 +6,7 @@ Problems: Source does not send accurate unit price, shipping charge, tax, and discounts. To bring something accurate in we use the old Shopify integration. This brings up another issue that the old integration brings in orders at creation, if these fields -get modifed (like from a refund) we are bringing in the wrong amount. +get modifed (like from a refund) we are brining in the wrong amount. Questions: How does Source handle multiple lots on a line? @@ -28,7 +28,7 @@ To catch issues we need to look at: -missing sales orders, that came into the shipping table, but failed to import in -missing deliveries, that failed during import, e.g. not enough stock -To find these use the sql files in "sql reports" +To find these use the sql files in "daily sql reports" ---------------------------------------------------------------------------------------- Reimporting: diff --git a/source_ecommerce_file_alerts.py b/source_ecommerce_file_alerts.py index b1f6f09..351a025 100644 --- a/source_ecommerce_file_alerts.py +++ b/source_ecommerce_file_alerts.py @@ -11,6 +11,16 @@ from email.mime.text import MIMEText import shutil import os +import base64 +import google.auth +import pickle +# Gmail API utils +from googleapiclient.discovery import build +from google_auth_oauthlib.flow import InstalledAppFlow +from google.auth.transport.requests import Request + +SCOPES = ['https://mail.google.com/'] + THIS_DIRECTORY = pathlib.Path(__file__).parent INCOMING_DIRECTORY = THIS_DIRECTORY / "incoming_orders" @@ -49,19 +59,55 @@ def main(): if file_count: file_alert(file_count) + +def gmail_authenticate(): + creds = None + # the file token.pickle stores the user's access and refresh tokens, and is + # created automatically when the authorization flow completes for the first time + if os.path.exists("token.pickle"): + with open("token.pickle", "rb") as token: + creds = pickle.load(token) + # if there are no (valid) credentials availablle, let the user log in. + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) + creds = flow.run_local_server(port=0) + # save the credentials for the next run + with open("token.pickle", "wb") as token: + pickle.dump(creds, token) + return build('gmail', 'v1', credentials=creds) + + +def gmail_send_message(service, payload): + create_message = {"raw": payload} + # pylint: disable=E1101 + send_message = ( + service.users() + .messages() + .send(userId="me", body=create_message) + .execute() + ) + return send_message + + def file_alert(file_list): file_string = ', '.join(file_list) msg = MIMEMultipart() msg['Subject'] = 'Source ecommerce: Files Left Over' msg['Precedence'] = 'bulk' msg['From'] = 'x3report@stashtea.com' - msg['To'] = 'bleeson@stashtea.com' + msg['To'] = 'bleeson@stashtea.com,icarrera@yamamotoyama.com' #msg['CC'] = 'bleeson@stashtea.com' emailtext = f'files: {file_string}' msg.attach(MIMEText(emailtext, 'plain')) - with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp: - smtp.login(user='x3reportmk2@yamamotoyama.com', password=r'n