def savedocs(): """save / submit / cancel / update doclist""" try: from webnotes.model.wrapper import ModelWrapper form = webnotes.form_dict doclist = ModelWrapper() doclist.from_compressed(form.get('docs'), form.get('docname')) # action action = form.get('action') if action=='Update': action='update_after_submit' getattr(doclist, action.lower())() # update recent documents webnotes.user.update_recent(doclist.doc.doctype, doclist.doc.name) # send updated docs webnotes.response['saved'] = '1' webnotes.response['main_doc_name'] = doclist.doc.name webnotes.response['doctype'] = doclist.doc.doctype webnotes.response['docname'] = doclist.doc.name webnotes.response['docs'] = [doclist.doc] + doclist.children except Exception, e: webnotes.msgprint('Did not save') webnotes.errprint(webnotes.utils.getTraceback()) raise e
def testFailAssert(self): if docnotok: with self.assertRaises(Exception) as context: d = ModelWrapper() d.doc = docnotok[0] d.children = None d.doc.fields['__islocal']=1 d.save(1)
def testInsert(self): d = ModelWrapper() count_before = flt(sql("select count(*) from tab"+_doctype)[0][0]) if docok: for i in docok: d.doc = i d.children = None d.doc.fields['__islocal']=1 d.save(1) count_after = flt(sql("select count(*) from tab"+_doctype)[0][0]) self.assertTrue(count_before+len(docok)==count_after)
def save(): """insert or update from form query""" doclist = json.loads(webnotes.form_dict.doclist) from webnotes.model.wrapper import ModelWrapper if not webnotes.has_permission(doclist[0]["doctype"], "write"): webnotes.msgprint("No Write Permission", raise_exception=True) doclistobj = ModelWrapper(doclist) doclistobj.save() return [d.fields for d in doclist]
def cancel_packing_slips(self): """ Cancel submitted packing slips related to this delivery note """ res = webnotes.conn.sql("""\ SELECT name, count(*) FROM `tabPacking Slip` WHERE delivery_note = %s AND docstatus = 1 """, self.doc.name) if res and res[0][1]>0: from webnotes.model.wrapper import ModelWrapper for r in res: ps = ModelWrapper(dt='Packing Slip', dn=r[0]) ps.cancel() webnotes.msgprint("%s Packing Slip(s) Cancelled" % res[0][1])
def save_pages(): """save all web pages, blogs to create content""" query_map = { 'Web Page': """select name from `tabWeb Page` where docstatus=0""", 'Blog': """\ select name from `tabBlog` where docstatus = 0 and ifnull(published, 0) = 1""", 'Item': """\ select name from `tabItem` where docstatus = 0 and ifnull(show_in_website, 0) = 1""", } import webnotes from webnotes.model.wrapper import ModelWrapper import webnotes.modules.patch_handler for dt in query_map: for result in webnotes.conn.sql(query_map[dt], as_dict=1): try: ModelWrapper(dt, result['name'].encode('utf-8')).save() except Exception, e: webnotes.modules.patch_handler.log(unicode(e))
def cancel_packing_slips(self): """ Cancel submitted packing slips related to this delivery note """ res = webnotes.conn.sql( """\ SELECT name, count(*) FROM `tabPacking Slip` WHERE delivery_note = %s AND docstatus = 1 """, self.doc.name) if res and res[0][1] > 0: from webnotes.model.wrapper import ModelWrapper for r in res: ps = ModelWrapper(dt='Packing Slip', dn=r[0]) ps.cancel() webnotes.msgprint("%s Packing Slip(s) Cancelled" % res[0][1])
def testFailAssert(self): if docnotok: with self.assertRaises(Exception) as context: d = ModelWrapper() d.doc = docnotok[0] d.children = None d.doc.fields['__islocal'] = 1 d.save(1)
def testInsert(self): d = ModelWrapper() count_before = flt(sql("select count(*) from tab" + _doctype)[0][0]) if docok: for i in docok: d.doc = i d.children = None d.doc.fields['__islocal'] = 1 d.save(1) count_after = flt(sql("select count(*) from tab" + _doctype)[0][0]) self.assertTrue(count_before + len(docok) == count_after)
def import_doc(d, doctype, overwrite, row_idx): """import main (non child) document""" from webnotes.model.wrapper import ModelWrapper if webnotes.conn.exists(doctype, d['name']): if overwrite: doclist = webnotes.model.doc.get(doctype, d['name']) doclist[0].fields.update(d) model_wrapper = ModelWrapper(doclist) model_wrapper.save() return 'Updated row (#%d) %s' % (row_idx, getlink(doctype, d['name'])) else: return 'Ignored row (#%d) %s (exists)' % ( row_idx, getlink(doctype, d['name'])) else: d['__islocal'] = 1 dl = ModelWrapper([webnotes.model.doc.Document(fielddata=d)]) dl.save() return 'Inserted row (#%d) %s' % ( row_idx, getlink(doctype, dl.doc.fields['name']))
def dt_map(): import webnotes import webnotes.model.utils from webnotes.model.code import get_obj from webnotes.model.doc import Document from webnotes.model.wrapper import ModelWrapper form_dict = webnotes.form_dict dt_list = webnotes.model.utils.expand(form_dict.get('docs')) from_doctype = form_dict.get('from_doctype') to_doctype = form_dict.get('to_doctype') from_docname = form_dict.get('from_docname') from_to_list = form_dict.get('from_to_list') dm = get_obj('DocType Mapper', from_doctype + '-' + to_doctype) dl = dm.dt_map(from_doctype, to_doctype, from_docname, Document(fielddata=dt_list[0]), (len(dt_list) > 1) and ModelWrapper(dt_list).doclist or [], from_to_list) webnotes.response['docs'] = dl
def setUp(self): webnotes.conn.sql("delete from `tabItem Group`") self.data = [ ["t1", None, 1, 20], ["c0", "t1", 2, 3], ["c1", "t1", 4, 11], ["gc1", "c1", 5, 6], ["gc2", "c1", 7, 8], ["gc3", "c1", 9, 10], ["c2", "t1", 12, 17], ["gc4", "c2", 13, 14], ["gc5", "c2", 15, 16], ["c3", "t1", 18, 19] ] for d in self.data: self.__dict__[d[0]] = ModelWrapper([Document(fielddata = { "doctype": "Item Group", "item_group_name": d[0], "parent_item_group": d[1], "__islocal": 1 })]) self.save_all() self.reload_all()
def runserverobj(): """ Run server objects """ import webnotes.model.code from webnotes.model.wrapper import ModelWrapper from webnotes.utils import cint wrapper = None method = webnotes.form_dict.get('method') arg = webnotes.form_dict.get('arg') dt = webnotes.form_dict.get('doctype') dn = webnotes.form_dict.get('docname') if dt: # not called from a doctype (from a page) if not dn: dn = dt # single so = webnotes.model.code.get_obj(dt, dn) else: wrapper = ModelWrapper() wrapper.from_compressed(webnotes.form_dict.get('docs'), dn) if not wrapper.has_read_perm(): webnotes.msgprint(_("No Permission"), raise_exception = True) so = wrapper.make_obj() wrapper.check_if_latest() check_guest_access(so.doc) if so: r = webnotes.model.code.run_server_obj(so, method, arg) if r: #build output as csv if cint(webnotes.form_dict.get('as_csv')): make_csv_output(r, so.doc.doctype) else: webnotes.response['message'] = r webnotes.response['docs'] = so.doclist
def import_doc(d, doctype, overwrite, row_idx): """import main (non child) document""" from webnotes.model.wrapper import ModelWrapper if webnotes.conn.exists(doctype, d['name']): if overwrite: doclist = webnotes.model.doc.get(doctype, d['name']) doclist[0].fields.update(d) model_wrapper = ModelWrapper(doclist) model_wrapper.save() return 'Updated row (#%d) %s' % (row_idx, getlink(doctype, d['name'])) else: return 'Ignored row (#%d) %s (exists)' % (row_idx, getlink(doctype, d['name'])) else: d['__islocal'] = 1 dl = ModelWrapper([webnotes.model.doc.Document(fielddata = d)]) dl.save() return 'Inserted row (#%d) %s' % (row_idx, getlink(doctype, dl.doc.fields['name']))
def runserverobj(): """ Run server objects """ import webnotes.model.code from webnotes.model.wrapper import ModelWrapper from webnotes.utils import cint doclist = None method = webnotes.form_dict.get('method') arg = webnotes.form_dict.get('arg') dt = webnotes.form_dict.get('doctype') dn = webnotes.form_dict.get('docname') if dt: # not called from a doctype (from a page) if not dn: dn = dt # single so = webnotes.model.code.get_obj(dt, dn) else: doclist = ModelWrapper() doclist.from_compressed(webnotes.form_dict.get('docs'), dn) so = doclist.make_obj() doclist.check_if_latest() check_guest_access(so.doc) if so: r = webnotes.model.code.run_server_obj(so, method, arg) if r: #build output as csv if cint(webnotes.form_dict.get('as_csv')): make_csv_output(r, so.doc.doctype) else: webnotes.response['message'] = r webnotes.response['docs'] = [so.doc] + so.doclist
def runserverobj(): """ Run server objects """ import webnotes.model.code from webnotes.model.wrapper import ModelWrapper from webnotes.utils import cint doclist = None method = webnotes.form_dict.get('method') arg = webnotes.form_dict.get('arg') dt = webnotes.form_dict.get('doctype') dn = webnotes.form_dict.get('docname') if dt: # not called from a doctype (from a page) if not dn: dn = dt # single so = webnotes.model.code.get_obj(dt, dn) else: doclist = ModelWrapper() doclist.from_compressed(webnotes.form_dict.get('docs'), dn) so = doclist.make_obj() doclist.check_if_latest() check_guest_access(so.doc) if so: r = webnotes.model.code.run_server_obj(so, method, arg) if r: #build output as csv if cint(webnotes.form_dict.get('as_csv')): make_csv_output(r, so.doc.doctype) else: webnotes.response['message'] = r webnotes.response['docs'] =[so.doc] + so.doclist
def import_vouchers(common_values, data, start_idx, import_type): from webnotes.model.doc import Document from webnotes.model.wrapper import ModelWrapper 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 = ModelWrapper([jv]+details) 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 model_wrapper(doctype, name=None): from webnotes.model.wrapper import ModelWrapper return ModelWrapper(doctype, name)
def execute(): import webnotes from webnotes.model.wrapper import ModelWrapper ModelWrapper("Website Settings", "Website Settings").save()
def import_vouchers(common_values, data, start_idx, import_type): from webnotes.model.doc import Document from webnotes.model.wrapper import ModelWrapper 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 = ModelWrapper([jv] + details) 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 webnotes.message_log[0] 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 = []