def run_background(prepared_report): instance = dataent.get_doc("Prepared Report", prepared_report) report = dataent.get_doc("Report", instance.ref_report_doctype) try: report.custom_columns = [] if report.report_type == 'Custom Report': custom_report_doc = report reference_report = custom_report_doc.reference_report report = dataent.get_doc("Report", reference_report) report.custom_columns = custom_report_doc.json result = generate_report_result(report, filters=instance.filters, user=instance.owner) create_json_gz_file(result['result'], 'Prepared Report', instance.name) instance.status = "Completed" instance.columns = json.dumps(result["columns"]) instance.report_end_time = dataent.utils.now() instance.save(ignore_permissions=True) except Exception: dataent.log_error(dataent.get_traceback()) instance = dataent.get_doc("Prepared Report", prepared_report) instance.status = "Error" instance.error_message = dataent.get_traceback() instance.save(ignore_permissions=True) dataent.publish_realtime('report_generated', { "report_name": instance.report_name, "name": instance.name }, user=dataent.session.user)
def get_context(context): if dataent.flags.in_migrate: return print(dataent.get_traceback().encode("utf-8")) return { "error": dataent.get_traceback().replace("<", "<").replace(">", ">") }
def auth(self): try: self.client.Auth.get(self.access_token) print("Authentication successful.....") except ItemError as e: if e.code == 'ITEM_LOGIN_REQUIRED': pass else: pass except APIError as e: if e.code == 'PLANNED_MAINTENANCE': pass else: pass except requests.Timeout: pass except Exception as e: print(e) dataent.log_error(dataent.get_traceback(), _("Plaid authentication error")) dataent.msgprint({ "title": _("Authentication Failed"), "message": e, "raise_exception": 1, "indicator": 'red' })
def order(*args, **kwargs): try: _order(*args, **kwargs) except Exception: error_message = dataent.get_traceback()+"\n\n Request Data: \n"+json.loads(dataent.request.data).__str__() dataent.log_error(error_message, "WooCommerce Error") raise
def get_transactions(self, start_date, end_date, account_id=None): try: self.auth() if account_id: account_ids = [account_id] response = self.client.Transactions.get( self.access_token, start_date=start_date, end_date=end_date, account_ids=account_ids) else: response = self.client.Transactions.get(self.access_token, start_date=start_date, end_date=end_date) transactions = response['transactions'] while len(transactions) < response['total_transactions']: response = self.client.Transactions.get( self.access_token, start_date=start_date, end_date=end_date, offset=len(transactions)) transactions.extend(response['transactions']) return transactions except Exception: dataent.log_error(dataent.get_traceback(), _("Plaid transactions sync error"))
def create_subscription_on_stripe(stripe_settings): items = [] for payment_plan in stripe_settings.payment_plans: plan = dataent.db.get_value("Subscription Plan", payment_plan.plan, "payment_plan_id") items.append({"plan": plan, "quantity": payment_plan.qty}) try: customer = stripe.Customer.create( description=stripe_settings.data.payer_name, email=stripe_settings.data.payer_email, source=stripe_settings.data.stripe_token_id) subscription = stripe.Subscription.create(customer=customer, items=items) if subscription.status == "active": stripe_settings.integration_request.db_set('status', 'Completed', update_modified=False) stripe_settings.flags.status_changed_to = "Completed" else: stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False) dataent.log_error('Subscription N°: ' + subscription.id, 'Stripe Payment not completed') except Exception: stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False) dataent.log_error(dataent.get_traceback()) return stripe_settings.finalize_request()
def mark_email_as_seen(name=None): try: if name and dataent.db.exists( "Communication", name) and not dataent.db.get_value( "Communication", name, "read_by_recipient"): dataent.db.set_value("Communication", name, "read_by_recipient", 1) dataent.db.set_value("Communication", name, "delivery_status", "Read") dataent.db.set_value("Communication", name, "read_by_recipient_on", get_datetime()) dataent.db.commit() except Exception: dataent.log_error(dataent.get_traceback()) finally: # Return image as response under all circumstances from PIL import Image import io im = Image.new('RGBA', (1, 1)) im.putdata([(255, 255, 255, 0)]) buffered_obj = io.BytesIO() im.save(buffered_obj, format="PNG") dataent.response["type"] = 'binary' dataent.response["filename"] = "imaginary_pixel.png" dataent.response["filecontent"] = buffered_obj.getvalue()
def import_csv(context, path, only_insert=False, submit_after_import=False, ignore_encoding_errors=False, no_email=True): "Import CSV using data import" from dataent.core.doctype.data_import import importer from dataent.utils.csvutils import read_csv_content site = get_site(context) if not os.path.exists(path): path = os.path.join('..', path) if not os.path.exists(path): print('Invalid path {0}'.format(path)) sys.exit(1) with open(path, 'r') as csvfile: content = read_csv_content(csvfile.read()) dataent.init(site=site) dataent.connect() try: importer.upload(content, submit_after_import=submit_after_import, no_email=no_email, ignore_encoding_errors=ignore_encoding_errors, overwrite=not only_insert, via_console=True) dataent.db.commit() except Exception: print(dataent.get_traceback()) dataent.destroy()
def finalize_request(self): redirect_to = self.data.get('redirect_to') or None redirect_message = self.data.get('redirect_message') or None status = self.integration_request.status if self.flags.status_changed_to == "Completed": if self.data.reference_doctype and self.data.reference_docname: custom_redirect_to = None try: custom_redirect_to = dataent.get_doc(self.data.reference_doctype, self.data.reference_docname).run_method("on_payment_authorized", self.flags.status_changed_to) except Exception: dataent.log_error(dataent.get_traceback()) if custom_redirect_to: redirect_to = custom_redirect_to redirect_url = 'payment-success' if self.redirect_url: redirect_url = self.redirect_url redirect_to = None else: redirect_url = 'payment-failed' if redirect_to: redirect_url += '?' + urlencode({'redirect_to': redirect_to}) if redirect_message: redirect_url += '&' + urlencode({'redirect_message': redirect_message}) return { "redirect_to": redirect_url, "status": status }
def sync(): try: gcalendar_settings = dataent.get_doc('GCalendar Settings') if gcalendar_settings.enable == 1: gcalendar_settings.sync() except Exception: dataent.log_error(dataent.get_traceback())
def create_mandate(data): data = dataent._dict(data) dataent.logger().debug(data) mandate = data.get('mandate') if dataent.db.exists("GoCardless Mandate", mandate): return else: reference_doc = dataent.db.get_value( data.get('reference_doctype'), data.get('reference_docname'), ["reference_doctype", "reference_name"], as_dict=1) epaas_customer = dataent.db.get_value(reference_doc.reference_doctype, reference_doc.reference_name, ["customer_name"], as_dict=1) try: dataent.get_doc({ "doctype": "GoCardless Mandate", "mandate": mandate, "customer": epaas_customer.customer_name, "gocardless_customer": data.get('customer') }).insert(ignore_permissions=True) except Exception: dataent.log_error(dataent.get_traceback())
def create_stripe_subscription(gateway_controller, data): stripe_settings = dataent.get_doc("Stripe Settings", gateway_controller) stripe_settings.data = dataent._dict(data) stripe.api_key = stripe_settings.get_password(fieldname="secret_key", raise_exception=False) stripe.default_http_client = stripe.http_client.RequestsClient() try: stripe_settings.integration_request = create_request_log( stripe_settings.data, "Host", "Stripe") stripe_settings.payment_plans = dataent.get_doc( "Payment Request", stripe_settings.data.reference_docname).subscription_plans return create_subscription_on_stripe(stripe_settings) except Exception: dataent.log_error(dataent.get_traceback()) return { "redirect_to": dataent.redirect_to_message( _('Server Error'), _("It seems that there is an issue with the server's stripe configuration. In case of failure, the amount will get refunded to your account." )), "status": 401 }
def repost_dn(dn_failed_list): dn_list = get_affected_delivery_notes() if dn_list: print("-"*40) print("Reposting Delivery Notes") for dn in dn_list: if dn.docstatus == 0: continue print(dn.name) try: dn_doc = dataent.get_doc("Delivery Note", dn.name) dn_doc.docstatus = 2 dn_doc.update_prevdoc_status() dn_doc.update_stock_ledger() dn_doc.cancel_packing_slips() dataent.db.sql("""delete from `tabGL Entry` where voucher_type='Delivery Note' and voucher_no=%s""", dn.name) dataent.db.sql("update `tabDelivery Note Item` set target_warehouse='' where parent=%s", dn.name) dn_doc = dataent.get_doc("Delivery Note", dn.name) dn_doc.docstatus = 1 dn_doc.on_submit() dataent.db.commit() except Exception: dn_failed_list.append(dn.name) dataent.local.stockledger_exceptions = None print(dataent.get_traceback()) dataent.db.rollback() dataent.db.sql("update `tabDelivery Note Item` set target_warehouse='' where docstatus=0")
def repost_si(si_failed_list): si_list = get_affected_sales_invoice() if si_list: print("-"*40) print("Reposting Sales Invoice") for si in si_list: if si.docstatus == 0: continue print(si.name) try: si_doc = dataent.get_doc("Sales Invoice", si.name) si_doc.docstatus = 2 si_doc.update_stock_ledger() dataent.db.sql("""delete from `tabGL Entry` where voucher_type='Sales Invoice' and voucher_no=%s""", si.name) dataent.db.sql("update `tabSales Invoice Item` set target_warehouse='' where parent=%s", si.name) si_doc = dataent.get_doc("Sales Invoice", si.name) si_doc.docstatus = 1 si_doc.update_stock_ledger() si_doc.make_gl_entries() dataent.db.commit() except Exception: si_failed_list.append(si.name) dataent.local.stockledger_exceptions = None print(dataent.get_traceback()) dataent.db.rollback() dataent.db.sql("update `tabSales Invoice Item` set target_warehouse='' where docstatus=0")
def execute(): from dataent.core.doctype.file.file import make_home_folder if not dataent.db.exists("DocType", "File"): dataent.rename_doc("DocType", "File Data", "File") dataent.reload_doctype("File") if not dataent.db.exists("File", {"is_home_folder": 1}): make_home_folder() # make missing folders and set parent folder for file in dataent.get_all("File", filters={"is_folder": 0}): file = dataent.get_doc("File", file.name) file.flags.ignore_folder_validate = True file.flags.ignore_file_validate = True file.flags.ignore_duplicate_entry_error = True file.flags.ignore_links = True file.set_folder_name() try: file.save() except: print(dataent.get_traceback()) raise from dataent.utils.nestedset import rebuild_tree rebuild_tree("File", "folder") # reset file size for folder in dataent.db.sql("""select name from tabFile f1 where is_folder = 1 and (select count(*) from tabFile f2 where f2.folder = f1.name and f2.is_folder = 1) = 0"""): folder = dataent.get_doc("File", folder[0]) folder.save()
def render_template(template, context, is_path=None, safe_render=True): '''Render a template using Jinja :param template: path or HTML containing the jinja template :param context: dict of properties to pass to the template :param is_path: (optional) assert that the `template` parameter is a path :param safe_render: (optional) prevent server side scripting via jinja templating ''' from dataent import get_traceback, throw from jinja2 import TemplateError if not template: return "" # if it ends with .html then its a freaking path, not html if (is_path or template.startswith("templates/") or (template.endswith('.html') and '\n' not in template)): return get_jenv().get_template(template).render(context) else: if safe_render and ".__" in template: throw("Illegal template") try: return get_jenv().from_string(template).render(context) except TemplateError: throw(title="Jinja Template Error", msg="<pre>{template}</pre><pre>{tb}</pre>".format( template=template, tb=get_traceback()))
def create_bank_entries(columns, data, bank_account): header_map = get_header_mapping(columns, bank_account) success = 0 errors = 0 for d in json.loads(data): if all(item is None for item in d) is True: continue fields = {} for key, value in iteritems(header_map): fields.update({key: d[int(value) - 1]}) try: bank_transaction = dataent.get_doc({"doctype": "Bank Transaction"}) bank_transaction.update(fields) bank_transaction.date = getdate(parse_date(bank_transaction.date)) bank_transaction.bank_account = bank_account bank_transaction.insert() bank_transaction.submit() success += 1 except Exception: dataent.log_error(dataent.get_traceback()) errors += 1 return {"success": success, "errors": errors}
def confirm_payment(token): try: custom_redirect_to = None data, params, url = get_paypal_and_transaction_details(token) params.update({ "METHOD": "DoExpressCheckoutPayment", "PAYERID": data.get("payerid"), "TOKEN": token, "PAYMENTREQUEST_0_PAYMENTACTION": "SALE", "PAYMENTREQUEST_0_AMT": data.get("amount"), "PAYMENTREQUEST_0_CURRENCYCODE": data.get("currency").upper() }) response = make_post_request(url, data=params) if response.get("ACK")[0] == "Success": update_integration_request_status(token, { "transaction_id": response.get("PAYMENTINFO_0_TRANSACTIONID")[0], "correlation_id": response.get("CORRELATIONID")[0] }, "Completed") if data.get("reference_doctype") and data.get("reference_docname"): custom_redirect_to = dataent.get_doc(data.get("reference_doctype"), data.get("reference_docname")).run_method("on_payment_authorized", "Completed") dataent.db.commit() redirect_url = '/integrations/payment-success?doctype={0}&docname={1}'.format(data.get("reference_doctype"), data.get("reference_docname")) else: redirect_url = "/integrations/payment-failed" setup_redirect(data, redirect_url, custom_redirect_to) except Exception: dataent.log_error(dataent.get_traceback())
def upload_file_to_dropbox(filename, folder, dropbox_client): """upload files with chunk of 15 mb to reduce session append calls""" if not os.path.exists(filename): return create_folder_if_not_exists(folder, dropbox_client) chunk_size = 15 * 1024 * 1024 file_size = os.path.getsize(encode(filename)) mode = (dropbox.files.WriteMode.overwrite) f = open(encode(filename), 'rb') path = "{0}/{1}".format(folder, os.path.basename(filename)) try: if file_size <= chunk_size: dropbox_client.files_upload(f.read(), path, mode) else: upload_session_start_result = dropbox_client.files_upload_session_start(f.read(chunk_size)) cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id, offset=f.tell()) commit = dropbox.files.CommitInfo(path=path, mode=mode) while f.tell() < file_size: if ((file_size - f.tell()) <= chunk_size): dropbox_client.files_upload_session_finish(f.read(chunk_size), cursor, commit) else: dropbox_client.files_upload_session_append(f.read(chunk_size), cursor.session_id,cursor.offset) cursor.offset = f.tell() except dropbox.exceptions.ApiError as e: if isinstance(e.error, dropbox.files.UploadError): error = "File Path: {path}\n".format(path=path) error += dataent.get_traceback() dataent.log_error(error) else: raise
def get_express_checkout_details(token): try: doc = dataent.get_doc("PayPal Settings") doc.setup_sandbox_env(token) params, url = doc.get_paypal_params_and_url() params.update({ "METHOD": "GetExpressCheckoutDetails", "TOKEN": token }) response = make_post_request(url, data=params) if response.get("ACK")[0] != "Success": dataent.respond_as_web_page(_("Something went wrong"), _("Looks like something went wrong during the transaction. Since we haven't confirmed the payment, Paypal will automatically refund you this amount. If it doesn't, please send us an email and mention the Correlation ID: {0}.").format(response.get("CORRELATIONID", [None])[0]), indicator_color='red', http_status_code=dataent.ValidationError.http_status_code) return doc = dataent.get_doc("Integration Request", token) update_integration_request_status(token, { "payerid": response.get("PAYERID")[0], "payer_email": response.get("EMAIL")[0] }, "Authorized", doc=doc) dataent.local.response["type"] = "redirect" dataent.local.response["location"] = get_redirect_uri(doc, token, response.get("PAYERID")[0]) except Exception: dataent.log_error(dataent.get_traceback())
def sync_transactions(bank, bank_account): last_sync_date = dataent.db.get_value("Bank Account", bank_account, "last_integration_date") if last_sync_date: start_date = formatdate(last_sync_date, "YYYY-MM-dd") else: start_date = formatdate(add_months(today(), -12), "YYYY-MM-dd") end_date = formatdate(today(), "YYYY-MM-dd") try: transactions = get_transactions(bank=bank, bank_account=bank_account, start_date=start_date, end_date=end_date) result = [] if transactions: for transaction in transactions: result.append(new_bank_transaction(transaction)) dataent.db.set_value("Bank Account", bank_account, "last_integration_date", getdate(end_date)) return result except Exception: dataent.log_error(dataent.get_traceback(), _("Plaid transactions sync error"))
def add_account_subtype(account_subtype): try: dataent.get_doc({ "doctype": "Account Subtype", "account_subtype": account_subtype }).insert() except Exception: dataent.throw(dataent.get_traceback())
def take_backups_s3(): try: backup_to_s3() send_email(True, "S3 Backup Settings") except Exception: error_message = dataent.get_traceback() dataent.errprint(error_message) send_email(False, "S3 Backup Settings", error_message)
def create_recurring_profile(token, payerid): try: custom_redirect_to = None updating = False data, params, url = get_paypal_and_transaction_details(token) addons = data.get("addons") subscription_details = data.get("subscription_details") if data.get('subscription_id') and addons: updating = True manage_recurring_payment_profile_status(data['subscription_id'], 'Cancel', params, url) params.update({ "METHOD": "CreateRecurringPaymentsProfile", "PAYERID": payerid, "TOKEN": token, "DESC": data.get("description"), "BILLINGPERIOD": subscription_details.get("billing_period"), "BILLINGFREQUENCY": subscription_details.get("billing_frequency"), "AMT": data.get("amount") if data.get("subscription_amount") == data.get("amount") else data.get("subscription_amount"), "CURRENCYCODE": data.get("currency").upper(), "INITAMT": data.get("upfront_amount") }) status_changed_to = 'Completed' if data.get("starting_immediately") or updating else 'Verified' starts_at = get_datetime(subscription_details.get("start_date")) or dataent.utils.now_datetime() starts_at = starts_at.replace(tzinfo=pytz.timezone(dataent.utils.get_time_zone())).astimezone(pytz.utc) #"PROFILESTARTDATE": datetime.utcfromtimestamp(get_timestamp(starts_at)).isoformat() params.update({ "PROFILESTARTDATE": starts_at.isoformat() }) response = make_post_request(url, data=params) if response.get("ACK")[0] == "Success": update_integration_request_status(token, { "profile_id": response.get("PROFILEID")[0], }, "Completed") if data.get("reference_doctype") and data.get("reference_docname"): data['subscription_id'] = response.get("PROFILEID")[0] dataent.flags.data = data custom_redirect_to = dataent.get_doc(data.get("reference_doctype"), data.get("reference_docname")).run_method("on_payment_authorized", status_changed_to) dataent.db.commit() redirect_url = '/integrations/payment-success?doctype={0}&docname={1}'.format(data.get("reference_doctype"), data.get("reference_docname")) else: redirect_url = "/integrations/payment-failed" setup_redirect(data, redirect_url, custom_redirect_to) except Exception: dataent.log_error(dataent.get_traceback())
def insert_local_doc(mapping, doc): try: # insert new doc if not doc.doctype: doc.doctype = mapping.local_doctype doc = dataent.get_doc(doc).insert() return doc except Exception: print('Data Migration Run failed: Error in Pull insert') print(dataent.get_traceback()) return None
def setup_app_type(): try: shopify_settings = dataent.get_doc("Shopify Settings") shopify_settings.app_type = 'Private' shopify_settings.update_price_in_epaas_price_list = 0 if getattr( shopify_settings, 'push_prices_to_shopify', None) else 1 shopify_settings.flags.ignore_mandatory = True shopify_settings.ignore_permissions = True shopify_settings.save() except Exception: dataent.db.set_value("Shopify Settings", None, "enable_shopify", 0) dataent.log_error(dataent.get_traceback())
def run_sync(account): exists = dataent.db.exists('Data Migration Run', dict(status=('in', ['Fail', 'Error']))) if exists: failed_run = dataent.get_doc("Data Migration Run", dict(status=('in', ['Fail', 'Error']))) failed_run.delete() started = dataent.db.exists('Data Migration Run', dict(status=('in', ['Started']))) if started: return try: doc = dataent.get_doc({ 'doctype': 'Data Migration Run', 'data_migration_plan': 'GCalendar Sync', 'data_migration_connector': 'Calendar Connector-' + account.name }).insert() try: doc.run() except Exception: dataent.log_error(dataent.get_traceback()) except Exception: dataent.log_error(dataent.get_traceback())
def execute(): dataent.reload_doc("utilities", "doctype", "address_template") if not dataent.db.sql("select name from `tabAddress Template`"): try: d = dataent.new_doc("Address Template") d.update({ "country": dataent.db.get_default("country") or dataent.db.get_value( "Global Defaults", "Global Defaults", "country") }) d.insert() except: print(dataent.get_traceback())
def update_local_doc(mapping, remote_doc, migration_id_value): try: # migration id value is set in migration_id_field in mapping.local_doctype docname = dataent.db.get_value(mapping.local_doctype, filters={ mapping.migration_id_field: migration_id_value }) doc = dataent.get_doc(mapping.local_doctype, docname) doc.update(remote_doc) doc.save() return doc except Exception: print('Data Migration Run failed: Error in Pull update') print(dataent.get_traceback()) return None
def process(data): """ Checks a `Subscription` and updates it status as necessary """ if data: try: subscription = dataent.get_doc('Subscription', data['name']) subscription.process() dataent.db.commit() except dataent.ValidationError: dataent.db.rollback() dataent.db.begin() dataent.log_error(dataent.get_traceback()) dataent.db.commit()