def execute(self, db_name, event, now): """ Executes event in the specifed db """ import webnotes, webnotes.defs, webnotes.db try: webnotes.conn = webnotes.db.Database(user=db_name, password=webnotes.get_db_password(db_name)) webnotes.session = {'user':'******'} module = '.'.join(event.split('.')[:-1]) method = event.split('.')[-1] exec 'from %s import %s' % (module, method) webnotes.conn.begin() ret = locals()[method]() webnotes.conn.commit() webnotes.conn.close() self.log(db_name, event, ret or 'Ok') except Exception, e: if now: print webnotes.getTraceback() else: self.log(db_name, event, webnotes.getTraceback())
def execute(self, db_name, event, now): """ Executes event in the specifed db """ import webnotes, webnotes.defs, webnotes.db try: webnotes.conn = webnotes.db.Database( user=db_name, password=webnotes.get_db_password(db_name)) webnotes.session = {'user': '******'} module = '.'.join(event.split('.')[:-1]) method = event.split('.')[-1] exec 'from %s import %s' % (module, method) webnotes.conn.begin() ret = locals()[method]() webnotes.conn.commit() webnotes.conn.close() self.log(db_name, event, ret or 'Ok') except Exception, e: if now: print webnotes.getTraceback() else: self.log(db_name, event, webnotes.getTraceback())
def respond(): import webnotes import webnotes.webutils import MySQLdb try: return webnotes.webutils.render(webnotes.form_dict.get('page')) except webnotes.SessionStopped: print "Content-type: text/html" print print session_stopped % { "app_name": webnotes.get_config().app_name, "trace": webnotes.getTraceback(), "title": "Upgrading..." } except MySQLdb.ProgrammingError, e: if e.args[0] == 1146: print "Content-type: text/html" print print session_stopped % { "app_name": webnotes.get_config().app_name, "trace": webnotes.getTraceback(), "title": "Installing..." } else: raise e
def respond(): import webnotes import webnotes.webutils import MySQLdb try: return webnotes.webutils.render(webnotes.form_dict.get('page')) except webnotes.SessionStopped: print "Content-type: text/html" print print session_stopped % { "app_name": webnotes.get_config().app_name, "trace": webnotes.getTraceback(), "title": "Upgrading..." } except MySQLdb.ProgrammingError, e: if e.args[0]==1146: print "Content-type: text/html" print print session_stopped % { "app_name": webnotes.get_config().app_name, "trace": webnotes.getTraceback(), "title": "Installing..." } else: raise e
def sync_all(force=0): modules = [] modules += sync_core_doctypes(force) modules += sync_modules(force) try: webnotes.clear_cache() except Exception, e: if e.args[0] != 1146: print webnotes.getTraceback() raise e
def write_log(): import os import webnotes.defs import webnotes patch_log = open(os.path.join(webnotes.defs.modules_path, 'patches', 'patch.log'), 'a') patch_log.write(('\n\nError in %s:\n' % webnotes.conn.cur_db_name) + webnotes.getTraceback()) patch_log.close() if getattr(webnotes.defs,'admin_email_notification',0): from webnotes.utils import sendmail subj = 'Error in running patches in %s' % webnotes.conn.cur_db_name msg = subj + '<br><br>Login User: '******'<br><br>' + webnotes.getTraceback() sendmail(['*****@*****.**'], sender='*****@*****.**', subject= subj, parts=[['text/plain', msg]])
def write_log(): import os import webnotes.defs import webnotes patch_log = open(os.path.join(webnotes.defs.modules_path, 'patches', 'patch.log'), 'a') patch_log.write(('\n\nError in %s:\n' % webnotes.conn.cur_db_name) + webnotes.getTraceback()) patch_log.close() if getattr(webnotes.defs,'admin_email_notification',0): from webnotes.utils import sendmail subj = 'Patch Error. <br>Account: %s' % webnotes.conn.cur_db_name msg = subj + '<br><br>' + webnotes.getTraceback() print msg
def upload(): from webnotes.utils.datautils import read_csv_content_from_uploaded_file from webnotes.modules import scrub rows = read_csv_content_from_uploaded_file() if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} columns = [scrub(f) for f in rows[4]] columns[0] = "name" columns[3] = "att_date" ret = [] error = False from webnotes.utils.datautils import check_record, import_doc doctype_dl = webnotes.get_doctype("Attendance") for i, row in enumerate(rows[5:]): if not row: continue row_idx = i + 5 d = webnotes._dict(zip(columns, row)) d["doctype"] = "Attendance" if d.name: d["docstatus"] = webnotes.conn.get_value("Attendance", d.name, "docstatus") try: check_record(d, doctype_dl=doctype_dl) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=True)) except Exception, e: error = True ret.append("Error for row (#%d) %s : %s" % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) webnotes.errprint(webnotes.getTraceback())
def upload(): from webnotes.utils.datautils import read_csv_content_from_uploaded_file from webnotes.modules import scrub rows = read_csv_content_from_uploaded_file() if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} columns = [scrub(f) for f in rows[4]] columns[0] = "name" columns[3] = "att_date" ret = [] error = False from webnotes.utils.datautils import check_record, import_doc doctype_dl = webnotes.get_doctype("Attendance") for i, row in enumerate(rows[5:]): if not row: continue row_idx = i + 5 d = webnotes._dict(zip(columns, row)) d["doctype"] = "Attendance" if d.name: d["docstatus"] = webnotes.conn.get_value("Attendance", d.name, "docstatus") try: check_record(d, doctype_dl=doctype_dl) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=True)) except Exception, e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) webnotes.errprint(webnotes.getTraceback())
def execute_patch(patchmodule, method=None, methodargs=None): """execute the patch""" block_user(True) webnotes.conn.begin() log("Executing %s in %s" % (patchmodule or str(methodargs), webnotes.conn.cur_db_name)) try: if patchmodule: patch = __import__(patchmodule, fromlist=True) getattr(patch, "execute")() update_patch_log(patchmodule) log("Success") elif method: method(**methodargs) webnotes.conn.commit() except Exception, e: webnotes.conn.rollback() global has_errors has_errors = True tb = webnotes.getTraceback() log(tb) import os if os.environ.get("HTTP_HOST"): add_to_patch_log(tb)
def execute_patch(patchmodule, method=None, methodargs=None): """execute the patch""" success = False block_user(True) webnotes.conn.begin() try: log('Executing %s in %s' % (patchmodule or str(methodargs), webnotes.conn.cur_db_name)) if patchmodule: if patchmodule.startswith("execute:"): exec patchmodule.split("execute:")[1] in globals() else: webnotes.get_method(patchmodule + ".execute")() update_patch_log(patchmodule) elif method: method(**methodargs) webnotes.conn.commit() success = True except Exception, e: webnotes.conn.rollback() global has_errors has_errors = True tb = webnotes.getTraceback() log(tb) import os if os.environ.get('HTTP_HOST'): add_to_patch_log(tb)
def get_html(page_name): """get page html""" page_name = scrub_page_name(page_name) html = '' # load from cache, if auto cache clear is falsy if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0): html = webnotes.cache().get_value("page:" + page_name) from_cache = True if not html: html = load_into_cache(page_name) from_cache = False if not html: html = get_html("404") if page_name=="error": html = html.replace("%(error)s", webnotes.getTraceback()) else: comments = "\n\npage:"+page_name+\ "\nload status: " + (from_cache and "cache" or "fresh") html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) return html
def write_log(): import os import webnotes.defs import webnotes patch_log = open( os.path.join(webnotes.defs.modules_path, 'patches', 'patch.log'), 'a') patch_log.write(('\n\nError in %s:\n' % webnotes.conn.cur_db_name) + webnotes.getTraceback()) patch_log.close() if getattr(webnotes.defs, 'admin_email_notification', 0): from webnotes.utils import sendmail subj = 'Patch Error. <br>Account: %s' % webnotes.conn.cur_db_name msg = subj + '<br><br>' + webnotes.getTraceback() print msg
def render_page(page_name): """get page html""" page_name = scrub_page_name(page_name) html = '' if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0): html = webnotes.cache().get_value("page:" + page_name) from_cache = True if not html: from webnotes.auth import HTTPRequest webnotes.http_request = HTTPRequest() html = build_page(page_name) from_cache = False if not html: raise PageNotFoundError if page_name=="error": html = html.replace("%(error)s", webnotes.getTraceback()) else: comments = "\n\npage:"+page_name+\ "\nload status: " + (from_cache and "cache" or "fresh") html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) return html
def send(self): """ * Execute get method * Send email to recipients """ if not self.doc.recipient_list: return self.sending = True result, email_body = self.get() recipient_list = self.doc.recipient_list.split("\n") # before sending, check if user is disabled or not # do not send if disabled profile_list = webnotes.conn.sql("SELECT name, enabled FROM tabProfile", as_dict=1) for profile in profile_list: if profile['name'] in recipient_list and profile['enabled'] == 0: del recipient_list[recipient_list.index(profile['name'])] from webnotes.utils.email_lib import sendmail try: #webnotes.msgprint('in send') sendmail( recipients=recipient_list, sender='*****@*****.**', reply_to='*****@*****.**', subject=self.doc.frequency + ' Digest', msg=email_body, from_defs=1 ) except Exception, e: webnotes.msgprint('There was a problem in sending your email. Please contact [email protected]') webnotes.errprint(webnotes.getTraceback())
def execute_patch(patchmodule, method=None, methodargs=None): """execute the patch""" success = False block_user(True) webnotes.conn.begin() try: log('Executing %s in %s' % (patchmodule or str(methodargs), webnotes.conn.cur_db_name)) if patchmodule: patch = __import__(patchmodule, fromlist=True) getattr(patch, 'execute')() update_patch_log(patchmodule) elif method: method(**methodargs) webnotes.conn.commit() success = True except Exception, e: webnotes.conn.rollback() global has_errors has_errors = True tb = webnotes.getTraceback() log(tb) import os if os.environ.get('HTTP_HOST'): add_to_patch_log(tb)
def get_comments(page_name): if page_name == '404': comments = """error: %s""" % webnotes.getTraceback() else: comments = """page: %s""" % page_name return comments
def execute_patch(patchmodule, method=None, methodargs=None): """execute the patch""" success = False block_user(True) webnotes.conn.begin() try: log('Executing %s in %s' % (patchmodule or str(methodargs), webnotes.conn.cur_db_name)) if patchmodule: if patchmodule.startswith("execute:"): exec patchmodule.split("execute:")[1] in globals() else: webnotes.get_method(patchmodule + ".execute")() update_patch_log(patchmodule) elif method: method(**methodargs) webnotes.conn.commit() success = True except Exception, e: webnotes.conn.rollback() global has_errors has_errors = True tb = webnotes.getTraceback() log(tb) import os if os.environ.get('HTTP_HOST'): add_to_patch_log(tb)
def render_page(page_name): """get page html""" page_name = scrub_page_name(page_name) html = '' if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0): html = webnotes.cache().get_value("page:" + page_name) from_cache = True if not html: from webnotes.auth import HTTPRequest webnotes.http_request = HTTPRequest() html = build_page(page_name) from_cache = False if not html: raise PageNotFoundError if page_name == "error": html = html.replace("%(error)s", webnotes.getTraceback()) else: comments = "\n\npage:"+page_name+\ "\nload status: " + (from_cache and "cache" or "fresh") html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) return html
def get_html(page_name): """get page html""" page_name = scrub_page_name(page_name) html = '' # load from cache, if auto cache clear is falsy if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0): if not get_page_settings().get(page_name, {}).get("no_cache"): html = webnotes.cache().get_value("page:" + page_name) from_cache = True if not html: from webnotes.auth import HTTPRequest webnotes.http_request = HTTPRequest() #webnotes.connect() html = load_into_cache(page_name) from_cache = False if not html: html = get_html("404") if page_name == "error": html = html.replace("%(error)s", webnotes.getTraceback()) else: comments = "\n\npage:"+page_name+\ "\nload status: " + (from_cache and "cache" or "fresh") html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) return html
def render_page(page_name): """get page html""" set_content_type(page_name) if page_name.endswith('.html'): page_name = page_name[:-5] html = '' if not conf.auto_cache_clear: html = webnotes.cache().get_value("page:" + page_name) from_cache = True if not html: html = build_page(page_name) from_cache = False if not html: raise PageNotFoundError if page_name=="error": html = html.replace("%(error)s", webnotes.getTraceback()) elif "text/html" in webnotes._response.headers["Content-Type"]: comments = "\npage:"+page_name+\ "\nload status: " + (from_cache and "cache" or "fresh") html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) return html
def render_page(page_name): """get page html""" set_content_type(page_name) if page_name.endswith('.html'): page_name = page_name[:-5] html = '' if not conf.auto_cache_clear: html = webnotes.cache().get_value("page:" + page_name) from_cache = True if not html: html = build_page(page_name) from_cache = False if not html: raise PageNotFoundError if page_name == "error": html = html.replace("%(error)s", webnotes.getTraceback()) elif "text/html" in webnotes._response.headers["Content-Type"]: comments = "\npage:"+page_name+\ "\nload status: " + (from_cache and "cache" or "fresh") html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) return html
def send(self): """ * Execute get method * Send email to recipients """ if not self.doc.recipient_list: return self.sending = True result, email_body = self.get() recipient_list = self.doc.recipient_list.split("\n") # before sending, check if user is disabled or not # do not send if disabled profile_list = webnotes.conn.sql( "SELECT name, enabled FROM tabProfile", as_dict=1) for profile in profile_list: if profile['name'] in recipient_list and profile['enabled'] == 0: del recipient_list[recipient_list.index(profile['name'])] from webnotes.utils.email_lib import sendmail try: #webnotes.msgprint('in send') sendmail(recipients=recipient_list, sender='*****@*****.**', reply_to='*****@*****.**', subject=self.doc.frequency + ' Digest', msg=email_body, from_defs=1) except Exception, e: webnotes.msgprint( 'There was a problem in sending your email. Please contact [email protected]' ) webnotes.errprint(webnotes.getTraceback())
def manage_recurring_invoices(): """ Create recurring invoices on specific date by copying the original one and notify the concerned people """ recurring_invoices = webnotes.conn.sql("""select name, recurring_id from `tabSales Invoice` where ifnull(convert_into_recurring_invoice, 0)=1 and docstatus=1 and next_date=%s and next_date <= ifnull(end_date, '2199-12-31')""", nowdate()) exception_list = [] for ref_invoice, recurring_id in recurring_invoices: if not webnotes.conn.sql("""select name from `tabSales Invoice` where posting_date=%s and recurring_id=%s and docstatus=1""", (nowdate(), recurring_id)): try: ref_wrapper = webnotes.model_wrapper('Sales Invoice', ref_invoice) new_invoice_wrapper = make_new_invoice(ref_wrapper) send_notification(new_invoice_wrapper) webnotes.conn.commit() except Exception, e: webnotes.conn.rollback() webnotes.conn.begin() webnotes.conn.sql("update `tabSales Invoice` set \ convert_into_recurring_invoice = 0 where name = %s", ref_invoice) notify_errors(ref_invoice, ref_wrapper.doc.owner) webnotes.conn.commit() exception_list.append(webnotes.getTraceback()) finally:
def create_material_request(material_requests): """ Create indent on reaching reorder level """ mr_list = [] defaults = webnotes.defaults.get_defaults() exceptions_list = [] for request_type in material_requests: for company in material_requests[request_type]: try: items = material_requests[request_type][company] if not items: continue mr = [{ "doctype": "Material Request", "company": company, "fiscal_year": defaults.fiscal_year, "transaction_date": nowdate(), "material_request_type": request_type }] for d in items: item = webnotes.doc("Item", d.item_code) mr.append({ "doctype": "Material Request Item", "parenttype": "Material Request", "parentfield": "indent_details", "item_code": d.item_code, "schedule_date": add_days(nowdate(),cint(item.lead_time_days)), "uom": item.stock_uom, "warehouse": d.warehouse, "item_name": item.item_name, "description": item.description, "item_group": item.item_group, "qty": d.reorder_qty, "brand": item.brand, }) mr_bean = webnotes.bean(mr) mr_bean.insert() mr_bean.submit() mr_list.append(mr_bean) except: if webnotes.local.message_log: exceptions_list.append([] + webnotes.local.message_log) webnotes.local.message_log = [] else: exceptions_list.append(webnotes.getTraceback()) if mr_list: if getattr(webnotes.local, "reorder_email_notify", None) is None: webnotes.local.reorder_email_notify = cint(webnotes.conn.get_value('Stock Settings', None, 'reorder_email_notify')) if(webnotes.local.reorder_email_notify): send_email_notification(mr_list) if exceptions_list: notify_errors(exceptions_list)
def run(patch_list, overwrite=0, log_exception=1, conn='', db_name='', db_password=''): import webnotes, webnotes.defs print patch_list # db connection if not conn: dbn = db_name or webnotes.defs.default_db_name pwd = db_password or (hasattr(webnotes.defs, 'get_db_password') and webnotes.defs.get_db_password(dbn)) or ( hasattr(webnotes.defs, 'db_password') and webnotes.defs.db_password) connect_db(dbn, pwd) else: webnotes.conn = conn # session if not (webnotes.session and webnotes.session['user']): webnotes.session = {'user': '******'} # no patches on accounts if webnotes.conn.cur_db_name == 'accounts': return # check if already applied if not overwrite: patch_list = check_already_applied_patch(patch_list) block_user("Patches are being executed, please try again in a few minutes") try: for p in patch_list: # execute patch execute_patch(p, log_exception) except Exception, e: webnotes.conn.rollback() if log_exception: write_log() else: print webnotes.getTraceback()
def update_file_list(doctype, singles): if doctype in singles: doc = webnotes.doc(doctype, doctype) if doc.file_list: update_for_doc(doctype, doc) webnotes.conn.set_value(doctype, None, "file_list", None) else: try: for doc in webnotes.conn.sql("""select name, file_list from `tab%s` where ifnull(file_list, '')!=''""" % doctype, as_dict=True): update_for_doc(doctype, doc) webnotes.conn.commit() webnotes.conn.sql("""alter table `tab%s` drop column `file_list`""" % doctype) except Exception, e: print webnotes.getTraceback() if (e.args and e.args[0]!=1054) or not e.args: raise e
def concat(self, filelist, outfile=None): """ Concat css and js files into a bundle """ from cStringIO import StringIO out_type = outfile and outfile.split('.')[-1] or 'js' outtxt = '' for f in filelist: suffix = None if ':' in f: f, suffix = f.split(':') if not os.path.exists(f) or os.path.isdir(f): continue self.timestamps[f] = os.path.getmtime(f) # get datas try: with open(f, 'r') as infile: # get file type ftype = f.split('.')[-1] data = unicode(infile.read(), 'utf-8', errors='ignore') outtxt += ('\n/*\n *\t%s\n */' % f) # append if suffix == 'concat' or out_type != 'js' or self.no_compress or ( '.min.' in f): outtxt += '\n' + data + '\n' else: jsm = JavascriptMinify() tmpin = StringIO(data.encode('utf-8')) tmpout = StringIO() jsm.minify(tmpin, tmpout) tmpmin = unicode(tmpout.getvalue() or '', 'utf-8') tmpmin.strip('\n') outtxt += tmpmin except Exception, e: print "--Error in:" + f + "--" print webnotes.getTraceback()
def get_comments(page_name): import webnotes if page_name == '404': comments = """error: %s""" % webnotes.getTraceback() else: comments = """page: %s""" % page_name return comments
def concat(self, filelist, outfile=None): """ Concat css and js files into a bundle """ from cStringIO import StringIO out_type = outfile and outfile.split('.')[-1] or 'js' outtxt = '' for f in filelist: suffix = None if ':' in f: f, suffix = f.split(':') if not os.path.exists(f) or os.path.isdir(f): continue self.timestamps[f] = os.path.getmtime(f) # get datas try: with open(f, 'r') as infile: # get file type ftype = f.split('.')[-1] data = unicode(infile.read(), 'utf-8', errors='ignore') outtxt += ('\n/*\n *\t%s\n */' % f) # append if suffix=='concat' or out_type != 'js' or self.no_compress or ('.min.' in f): outtxt += '\n' + data + '\n' else: jsm = JavascriptMinify() tmpin = StringIO(data.encode('utf-8')) tmpout = StringIO() jsm.minify(tmpin, tmpout) tmpmin = unicode(tmpout.getvalue() or '', 'utf-8') tmpmin.strip('\n') outtxt += tmpmin except Exception, e: print "--Error in:" + f + "--" print webnotes.getTraceback()
def write_log(): import os import webnotes.defs import webnotes patch_log = open(os.path.join(webnotes.defs.modules_path, 'patches', 'patch.log'), 'a') patch_log.write(('\n\nError in %s:\n' % webnotes.conn.cur_db_name) + webnotes.getTraceback()) patch_log.close() webnotes.msgprint("There were errors in running patches, please call the Administrator")
def log(method): """log error in patch_log""" import webnotes webnotes.conn.rollback() traceback = webnotes.getTraceback() import webnotes.utils webnotes.conn.sql("""insert into __SchedulerLog (`timestamp`, method, error) values (%s, %s, %s)""", (webnotes.utils.now_datetime(), method, traceback)) return traceback
def validate_parent(parent_list, parenttype, ret, error): if parent_list: parent_list = list(set(parent_list)) for p in parent_list: try: obj = webnotes.bean(parenttype, p) obj.run_method("validate") obj.run_method("on_update") except Exception, e: error = True ret.append('Validation Error for %s %s: %s' % (parenttype, p, cstr(e))) webnotes.errprint(webnotes.getTraceback())
def backup_to_dropbox(): from dropbox import client, session from conf import dropbox_access_key, dropbox_secret_key from webnotes.utils.backups import new_backup from webnotes.utils import get_files_path, get_backups_path if not webnotes.conn: webnotes.connect() sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder") sess.set_token( webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"), webnotes.conn.get_value("Backup Manager", None, "dropbox_access_secret")) dropbox_client = client.DropboxClient(sess) # upload database backup = new_backup() filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) upload_file_to_dropbox(filename, "/database", dropbox_client) webnotes.conn.close() response = dropbox_client.metadata("/files") # upload files to files folder did_not_upload = [] error_log = [] path = get_files_path() for filename in os.listdir(path): filename = cstr(filename) if filename in ignore_list: continue found = False filepath = os.path.join(path, filename) for file_metadata in response["contents"]: if os.path.basename(filepath) == os.path.basename( file_metadata["path"]) and os.stat( filepath).st_size == int(file_metadata["bytes"]): found = True break if not found: try: upload_file_to_dropbox(filepath, "/files", dropbox_client) except Exception: did_not_upload.append(filename) error_log.append(webnotes.getTraceback()) webnotes.connect() return did_not_upload, list(set(error_log))
def take_backups_dropbox(): did_not_upload, error_log = [], [] try: from setup.doctype.backup_manager.backup_dropbox import backup_to_dropbox did_not_upload, error_log = backup_to_dropbox() if did_not_upload: raise Exception send_email(True, "Dropbox") except Exception: file_and_error = [" - ".join(f) for f in zip(did_not_upload, error_log)] error_message = ("\n".join(file_and_error) + "\n" + webnotes.getTraceback()) print error_message send_email(False, "Dropbox", error_message)
def validate_parent(parent_list, parenttype, ret, error): if parent_list: parent_list = list(set(parent_list)) for p in parent_list: try: obj = webnotes.bean(parenttype, p) obj.run_method("validate") obj.run_method("on_update") except Exception, e: error = True ret.append('Validation Error for %s %s: %s' % (parenttype, p, cstr(e))) webnotes.errprint(webnotes.getTraceback())
def take_backups_gdrive(): did_not_upload, error_log = [], [] try: from setup.doctype.backup_manager.backup_googledrive import backup_to_gdrive did_not_upload, error_log = backup_to_gdrive() if did_not_upload: raise Exception send_email(True, "Google Drive") except Exception: file_and_error = [" - ".join(f) for f in zip(did_not_upload, error_log)] error_message = ("\n".join(file_and_error) + "\n" + webnotes.getTraceback()) webnotes.errprint(error_message) send_email(False, "Google Drive", error_message)
def backup_to_dropbox(): from dropbox import client, session from conf import dropbox_access_key, dropbox_secret_key from webnotes.utils.backups import new_backup if not webnotes.conn: webnotes.connect() sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder") sess.set_token( webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"), webnotes.conn.get_value("Backup Manager", None, "dropbox_access_secret"), ) dropbox_client = client.DropboxClient(sess) # upload database backup = new_backup() filename = os.path.join(get_base_path(), "public", "backups", os.path.basename(backup.backup_path_db)) upload_file_to_dropbox(filename, "/database", dropbox_client) webnotes.conn.close() response = dropbox_client.metadata("/files") # upload files to files folder did_not_upload = [] error_log = [] path = os.path.join(get_base_path(), "public", "files") for filename in os.listdir(path): filename = cstr(filename) if filename in ignore_list: continue found = False filepath = os.path.join(path, filename) for file_metadata in response["contents"]: if os.path.basename(filepath) == os.path.basename(file_metadata["path"]) and os.stat( filepath ).st_size == int(file_metadata["bytes"]): found = True break if not found: try: upload_file_to_dropbox(filepath, "/files", dropbox_client) except Exception: did_not_upload.append(filename) error_log.append(webnotes.getTraceback()) webnotes.connect() return did_not_upload, list(set(error_log))
def run(patch_list, overwrite = 0, log_exception=1, conn = '', db_name = '', db_password = ''): import webnotes, webnotes.defs print patch_list # db connection if not conn: dbn = db_name or webnotes.defs.default_db_name pwd = db_password or (hasattr(webnotes.defs, 'get_db_password') and webnotes.defs.get_db_password(dbn)) or (hasattr(webnotes.defs, 'db_password') and webnotes.defs.db_password) connect_db(dbn, pwd) else: webnotes.conn = conn # session if not (webnotes.session and webnotes.session['user']): webnotes.session = {'user':'******'} # no patches on accounts if webnotes.conn.cur_db_name=='accounts': return # check if already applied if not overwrite: patch_list = check_already_applied_patch(patch_list) block_user("Patches are being executed, please try again in a few minutes") try: for p in patch_list: # execute patch execute_patch(p, log_exception) except Exception, e: webnotes.conn.rollback() if log_exception: write_log() else: print webnotes.getTraceback()
def log(method): """log error in patch_log""" import webnotes webnotes.conn.rollback() traceback = webnotes.getTraceback() import webnotes.utils webnotes.conn.begin() webnotes.conn.sql( """insert into __SchedulerLog (`timestamp`, method, error) values (%s, %s, %s)""", (webnotes.utils.now_datetime(), method, traceback)) webnotes.conn.commit() return traceback
def log(method): """log error in patch_log""" import webnotes webnotes.conn.rollback() traceback = webnotes.getTraceback() import webnotes.utils webnotes.conn.begin() d = webnotes.doc("Scheduler Log") d.method = method d.error = traceback d.save() webnotes.conn.commit() return traceback
def log(method): """log error in patch_log""" import webnotes webnotes.conn.rollback() traceback = webnotes.getTraceback() import webnotes.utils webnotes.conn.begin() d = webnotes.doc("Scheduler Log") d.method = method d.error = traceback d.save() webnotes.conn.commit() return traceback
def manage_recurring_invoices(next_date=None, commit=True): """ Create recurring invoices on specific date by copying the original one and notify the concerned people """ next_date = next_date or nowdate() recurring_invoices = webnotes.conn.sql( """select name, recurring_id from `tabSales Invoice` where ifnull(convert_into_recurring_invoice, 0)=1 and docstatus=1 and next_date=%s and next_date <= ifnull(end_date, '2199-12-31')""", next_date, ) exception_list = [] for ref_invoice, recurring_id in recurring_invoices: if not webnotes.conn.sql( """select name from `tabSales Invoice` where posting_date=%s and recurring_id=%s and docstatus=1""", (next_date, recurring_id), ): try: ref_wrapper = webnotes.bean("Sales Invoice", ref_invoice) new_invoice_wrapper = make_new_invoice(ref_wrapper, next_date) send_notification(new_invoice_wrapper) if commit: webnotes.conn.commit() except: if commit: webnotes.conn.rollback() webnotes.conn.begin() webnotes.conn.sql( "update `tabSales Invoice` set \ convert_into_recurring_invoice = 0 where name = %s", ref_invoice, ) notify_errors(ref_invoice, ref_wrapper.doc.owner) webnotes.conn.commit() exception_list.append(webnotes.getTraceback()) finally: if commit: webnotes.conn.begin() if exception_list: exception_message = "\n\n".join([cstr(d) for d in exception_list]) raise Exception, exception_message
def take_backups_gdrive(): did_not_upload, error_log = [], [] try: from setup.doctype.backup_manager.backup_googledrive import backup_to_gdrive did_not_upload, error_log = backup_to_gdrive() if did_not_upload: raise Exception send_email(True, "Google Drive") except Exception: file_and_error = [ " - ".join(f) for f in zip(did_not_upload, error_log) ] error_message = ("\n".join(file_and_error) + "\n" + webnotes.getTraceback()) webnotes.errprint(error_message) send_email(False, "Google Drive", error_message)
def __init__(self, **kwargs): """ Gets connection to database Arguments can be: * db_name * verbose --> to enabled printing """ self.verbose = kwargs.get('verbose') try: self.db_name = kwargs.get('db_name') webnotes.conn = Database(user=self.db_name) webnotes.conn.use(self.db_name) if not (webnotes.session and webnotes.session['user']): webnotes.session = {'user': '******'} except Exception, e: self.log(log_type='error', msg=webnotes.getTraceback()) raise e
def delete_items(): """delete selected items""" import json from webnotes.model import delete_doc from webnotes.model.code import get_obj il = json.loads(webnotes.form_dict.get('items')) doctype = webnotes.form_dict.get('doctype') for d in il: try: dt_obj = get_obj(doctype, d) if hasattr(dt_obj, 'on_trash'): dt_obj.on_trash() delete_doc(doctype, d) except Exception, e: webnotes.errprint(webnotes.getTraceback()) pass
def delete_items(): """delete selected items""" import json from webnotes.model import delete_doc from webnotes.model.code import get_obj il = json.loads(webnotes.form_dict.get('items')) doctype = webnotes.form_dict.get('doctype') for d in il: try: dt_obj = get_obj(doctype, d) if hasattr(dt_obj, 'on_trash'): dt_obj.on_trash() delete_doc(doctype, d) except Exception, e: webnotes.errprint(webnotes.getTraceback()) pass
def log(method, message=None): """log error in patch_log""" message = webnotes.utils.cstr(message) + "\n" if message else "" message += webnotes.getTraceback() if not (webnotes.conn and webnotes.conn._conn): webnotes.connect() webnotes.conn.rollback() webnotes.conn.begin() d = webnotes.doc("Scheduler Log") d.method = method d.error = message d.save() webnotes.conn.commit() return message
def upload(): messages = [] try: from webnotes.utils.datautils import read_csv_content_from_uploaded_file rows = read_csv_content_from_uploaded_file() common_values = get_common_values(rows) company_abbr = webnotes.conn.get_value("Company", common_values.company, "abbr") if not company_abbr: webnotes.msgprint(_("Company is missing or entered incorrect value"), raise_exception=1) data, start_idx = get_data(rows, company_abbr, rows[0][0]) except Exception, e: err_msg = webnotes.message_log and "<br>".join(webnotes.message_log) or cstr(e) messages.append("""<p style='color: red'>%s</p>""" % (err_msg or "No message")) webnotes.errprint(webnotes.getTraceback()) webnotes.message_log = [] return messages
def manage_recurring_invoices(next_date=None, commit=True): """ Create recurring invoices on specific date by copying the original one and notify the concerned people """ next_date = next_date or nowdate() recurring_invoices = webnotes.conn.sql( """select name, recurring_id from `tabSales Invoice` where ifnull(convert_into_recurring_invoice, 0)=1 and docstatus=1 and next_date=%s and next_date <= ifnull(end_date, '2199-12-31')""", next_date) exception_list = [] for ref_invoice, recurring_id in recurring_invoices: if not webnotes.conn.sql( """select name from `tabSales Invoice` where posting_date=%s and recurring_id=%s and docstatus=1""", (next_date, recurring_id)): try: ref_wrapper = webnotes.bean('Sales Invoice', ref_invoice) new_invoice_wrapper = make_new_invoice(ref_wrapper, next_date) send_notification(new_invoice_wrapper) if commit: webnotes.conn.commit() except: if commit: webnotes.conn.rollback() webnotes.conn.begin() webnotes.conn.sql( "update `tabSales Invoice` set \ convert_into_recurring_invoice = 0 where name = %s", ref_invoice) notify_errors(ref_invoice, ref_wrapper.doc.owner) webnotes.conn.commit() exception_list.append(webnotes.getTraceback()) finally: if commit: webnotes.conn.begin() if exception_list: exception_message = "\n\n".join([cstr(d) for d in exception_list]) raise Exception, exception_message
def upload(): messages = [] try: from webnotes.utils.datautils import read_csv_content_from_uploaded_file rows = read_csv_content_from_uploaded_file() common_values = get_common_values(rows) company_abbr = webnotes.conn.get_value("Company", common_values.company, "abbr") if not company_abbr: webnotes.msgprint(_("Company is missing or entered incorrect value"), raise_exception=1) data, start_idx = get_data(rows, company_abbr, rows[0][0]) except Exception, e: err_msg = webnotes.local.message_log and "<br>".join(webnotes.local.message_log) or cstr(e) messages.append("""<p style='color: red'>%s</p>""" % (err_msg or "No message")) webnotes.errprint(webnotes.getTraceback()) webnotes.local.message_log = [] return messages
def take_backups_dropbox(): did_not_upload, error_log = [], [] try: from setup.doctype.backup_manager.backup_dropbox import backup_to_dropbox did_not_upload, error_log = backup_to_dropbox() if did_not_upload: raise Exception send_email(True, "Dropbox") except Exception: file_and_error = [ " - ".join(f) for f in zip(did_not_upload, error_log) ] error_message = ("\n".join(file_and_error) + "\n" + webnotes.getTraceback()) webnotes.errprint(error_message) if not webnotes.conn: webnotes.connect() send_email(False, "Dropbox", error_message)
def render_page(page_name): """get page html""" page_name = scrub_page_name(page_name) html = '' if not ('auto_cache_clear') and conf.auto_cache_clear or 0 in conf: html = webnotes.cache().get_value("page:" + page_name) from_cache = True if not html: html = build_page(page_name) from_cache = False if not html: raise PageNotFoundError if page_name == "error": html = html.replace("%(error)s", webnotes.getTraceback()) elif not webnotes.response.content_type: comments = "\npage:"+page_name+\ "\nload status: " + (from_cache and "cache" or "fresh") html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) return html
def create_material_request(material_requests): """ Create indent on reaching reorder level """ mr_list = [] defaults = webnotes.defaults.get_defaults() exceptions_list = [] for request_type in material_requests: for company in material_requests[request_type]: try: items = material_requests[request_type][company] if not items: continue mr = [{ "doctype": "Material Request", "company": company, "fiscal_year": defaults.fiscal_year, "transaction_date": nowdate(), "material_request_type": request_type, "remark": _("This is an auto generated Material Request.") + \ _("""It was raised because the (actual + ordered + indented - reserved) quantity reaches re-order level when the following record was created""") }] for d in items: item = webnotes.doc("Item", d.item_code) mr.append({ "doctype": "Material Request Item", "parenttype": "Material Request", "parentfield": "indent_details", "item_code": d.item_code, "schedule_date": add_days(nowdate(), cint(item.lead_time_days)), "uom": item.stock_uom, "warehouse": d.warehouse, "item_name": item.item_name, "description": item.description, "item_group": item.item_group, "qty": d.reorder_qty, "brand": item.brand, }) mr_bean = webnotes.bean(mr) mr_bean.insert() mr_bean.submit() mr_list.append(mr_bean) except: if webnotes.message_log: exceptions_list.append([] + webnotes.message_log) webnotes.message_log = [] else: exceptions_list.append(webnotes.getTraceback()) if mr_list: if not hasattr(webnotes, "reorder_email_notify"): webnotes.reorder_email_notify = cint( webnotes.conn.get_value('Stock Settings', None, 'reorder_email_notify')) if (webnotes.reorder_email_notify): send_email_notification(mr_list) if exceptions_list: notify_errors(exceptions_list)
def import_vouchers(common_values, data, start_idx, import_type): from webnotes.model.doc import Document from webnotes.model.bean import Bean from accounts.utils import get_fiscal_year from webnotes.utils.dateutils import parse_date messages = [] def get_account_details(account): acc_details = webnotes.conn.sql("""select is_pl_account, master_name from tabAccount where name=%s""", account, as_dict=1) if not acc_details: webnotes.msgprint("%s is not an Account" % account, raise_exception=1) return acc_details[0] def apply_cost_center_and_against_invoice(detail, d): account = get_account_details(detail.account) if account.is_pl_account == "Yes": detail.cost_center = d.cost_center if account.master_name: map_fields([ "against_sales_invoice:against_invoice", "against_purhase_invoice:against_voucher", "against_journal_voucher:against_jv" ], d, detail.fields) webnotes.conn.commit() try: jv = Document("Journal Voucher") webnotes.conn.begin() for i in xrange(len(data)): jv = Document("Journal Voucher") d = data[i][0] if import_type == "Voucher Import: Two Accounts" and flt( d.get("amount")) == 0: webnotes.message_log = ["Amount not specified"] raise Exception elif import_type == "Voucher Import: Multiple Accounts" and \ (flt(d.get("total_debit")) == 0 or flt(d.get("total_credit")) == 0): webnotes.message_log = [ "Total Debit and Total Credit amount can not be zero" ] raise Exception else: d.posting_date = parse_date(d.posting_date) d.due_date = d.due_date and parse_date(d.due_date) or None if d.ref_number: if not d.ref_date: raise webnotes.ValidationError, \ """Ref Date is Mandatory if Ref Number is specified""" d.ref_date = parse_date(d.ref_date) d.company = common_values.company map_fields([ "voucher_type", "posting_date", "naming_series", "remarks:user_remark", "ref_number:cheque_no", "ref_date:cheque_date", "is_opening", "due_date", "company" ], d, jv.fields) jv.fiscal_year = get_fiscal_year(jv.posting_date)[0] details = [] if import_type == "Voucher Import: Two Accounts": map_fields(["amount:total_debit", "amount:total_credit"], d, jv.fields) detail1 = Document("Journal Voucher Detail") detail1.parent = True detail1.parentfield = "entries" map_fields(["debit_account:account", "amount:debit"], d, detail1.fields) apply_cost_center_and_against_invoice(detail1, d) detail2 = Document("Journal Voucher Detail") detail2.parent = True detail2.parentfield = "entries" map_fields(["credit_account:account", "amount:credit"], d, detail2.fields) apply_cost_center_and_against_invoice(detail2, d) details = [detail1, detail2] elif import_type == "Voucher Import: Multiple Accounts": map_fields(["total_debit", "total_credit"], d, jv.fields) accounts = data[i][1] for acc in accounts: detail = Document("Journal Voucher Detail") detail.parent = True detail.parentfield = "entries" detail.account = acc detail.debit = flt(accounts[acc]) > 0 and flt( accounts[acc]) or 0 detail.credit = flt( accounts[acc]) < 0 and -1 * flt(accounts[acc]) or 0 apply_cost_center_and_against_invoice(detail, d) details.append(detail) if not details: webnotes.message_log = [ """No accounts found. If you entered accounts correctly, please check template once""" ] raise Exception doclist = Bean([jv] + details) # validate datatype from core.page.data_import_tool.data_import_tool import check_record for d in doclist: check_record(d.fields, d.parenttype) doclist.submit() messages.append("""<p style='color: green'>[row #%s] <a href=\"#Form/Journal Voucher/%s\">%s</a> imported</p>""" \ % ((start_idx + 1) + i, jv.name, jv.name)) webnotes.conn.commit() except Exception, e: webnotes.conn.rollback() err_msg = webnotes.message_log and "<br>".join( webnotes.message_log) or unicode(e) messages.append( """<p style='color: red'>[row #%s] %s failed: %s</p>""" % ((start_idx + 1) + i, jv.name or "", err_msg or "No message")) messages.append( "<p style='color: red'>All transactions rolled back</p>") webnotes.errprint(webnotes.getTraceback()) webnotes.message_log = []
def create_material_request(material_requests): """ Create indent on reaching reorder level """ mr_list = [] defaults = webnotes.defaults.get_defaults() exceptions_list = [] from accounts.utils import get_fiscal_year current_fiscal_year = get_fiscal_year(nowdate())[0] or defaults.fiscal_year for request_type in material_requests: for company in material_requests[request_type]: try: items = material_requests[request_type][company] if not items: continue mr = [{ "doctype": "Material Request", "company": company, "fiscal_year": current_fiscal_year, "transaction_date": nowdate(), "material_request_type": request_type }] for d in items: item = webnotes.doc("Item", d.item_code) mr.append({ "doctype": "Material Request Item", "parenttype": "Material Request", "parentfield": "indent_details", "item_code": d.item_code, "schedule_date": add_days(nowdate(), cint(item.lead_time_days)), "uom": item.stock_uom, "warehouse": d.warehouse, "item_name": item.item_name, "description": item.description, "item_group": item.item_group, "qty": d.reorder_qty, "brand": item.brand, }) mr_bean = webnotes.bean(mr) mr_bean.insert() mr_bean.submit() mr_list.append(mr_bean) except: if webnotes.local.message_log: exceptions_list.append([] + webnotes.local.message_log) webnotes.local.message_log = [] else: exceptions_list.append(webnotes.getTraceback()) if mr_list: if getattr(webnotes.local, "reorder_email_notify", None) is None: webnotes.local.reorder_email_notify = cint( webnotes.conn.get_value('Stock Settings', None, 'reorder_email_notify')) if (webnotes.local.reorder_email_notify): send_email_notification(mr_list) if exceptions_list: notify_errors(exceptions_list)
(getlink(parenttype, doc.parent), unicode(doc.idx))) parent_list.append(doc.parent) else: ret.append( import_doc(doclist[0], doctype, overwrite, row_idx, submit_after_import, ignore_links)) except Exception, e: error = True if bean: webnotes.errprint(bean.doclist) err_msg = webnotes.message_log and "<br>".join( webnotes.message_log) or cstr(e) ret.append('Error for row (#%d) %s : %s' % (row_idx + 1, len(row) > 1 and row[1] or "", err_msg)) webnotes.errprint(webnotes.getTraceback()) ret, error = validate_parent(parent_list, parenttype, ret, error) if error: webnotes.conn.rollback() else: webnotes.conn.commit() webnotes.mute_emails = False return {"messages": ret, "error": error} def validate_parent(parent_list, parenttype, ret, error): if parent_list: