import webnotes
from webnotes.model.doc import Document
[docs]def set_doc(doclist, ovr=0, ignore=1, onupdate=1):
dt = doclist[0]['doctype']
if webnotes.conn.exists(doclist[0]['doctype'], doclist[0]['name']):
# exists, merge if possible
if dt=='DocType':
ud = UpdateDocType(doclist)
elif dt == 'Module Def':
ud = UpdateModuleDef(doclist)
elif dt == 'DocType Mapper':
ud = UpdateDocTypeMapper(doclist)
else:
ud = UpdateDocument(doclist)
else:
ud = UpdateDocument(doclist)
ud.sync()
return '\n'.join(ud.log)
#
# Class to sync incoming document
#
[docs]class UpdateDocument:
def __init__(self, in_doclist=[]):
self.in_doclist = in_doclist
self.doc = Document(fielddata = in_doclist[0])
self.modified = self.doc.modified # make a copy
self.doclist = []
self.log = []
self.exists = 0
# sync
[docs] def sync(self):
is_mod = self.is_modified()
if (not self.exists) or (is_mod):
webnotes.conn.begin()
if self.exists:
self.delete_existing()
self.save()
self.update_modified()
self.run_on_update()
webnotes.conn.commit()
# check modified
[docs] def is_modified(self):
try:
timestamp = webnotes.conn.sql("select modified from `tab%s` where name=%s" % (self.doc.doctype, '%s'), self.doc.name)
except Exception ,e:
if(e.args[0]==1146):
return
else:
raise e
if timestamp:
self.exists = 1
if str(timestamp[0][0]) == self.doc.modified:
self.log.append('%s %s, No change' % (self.doc.doctype, self.doc.name))
else: return 1
# delete existing
[docs] def delete_existing(self):
from webnotes.model import delete_doc
webnotes.conn.sql("set foreign_key_checks=0")
delete_doc(self.doc.doctype, self.doc.name)
webnotes.conn.sql("set foreign_key_checks=1")
# update modified timestamp
[docs] def update_modified(self):
webnotes.conn.set(self.doc, 'modified', self.modified)
[docs] def save(self):
# parent
self.doc.save(new = 1, ignore_fields = 1, check_links=0)
self.doclist = [self.doc]
self.save_children()
[docs] def save_children(self):
for df in self.in_doclist[1:]:
self.save_one_doc(df)
[docs] def save_one_doc(self, df, as_new=1):
d = Document(fielddata = df)
d.save(new = as_new, ignore_fields = 1, check_links=0)
self.doclist.append(d)
[docs] def run_on_update(self):
from webnotes.model.code import get_server_obj
so = get_server_obj(self.doc, self.doclist)
if hasattr(so, 'on_update'):
so.on_update()
#
# "Merge incoming doctype"
#
[docs]class UpdateDocumentMerge(UpdateDocument):
def __init__(self, in_doclist):
self.to_update_doctype = []
UpdateDocument.__init__(self, in_doclist)
[docs] def delete_existing(self):
pass
[docs] def get_id(self, d):
pass
[docs] def to_update(self, d):
return 1
[docs] def child_exists(self, d):
return self.get_id(d)
[docs] def on_save(self):
pass
[docs] def save(self):
if self.exists:
# save main doc
self.keep_values(self.doc)
self.doc.save(ignore_fields = 1, check_links=0)
self.doclist.append(self.doc)
self.save_children()
self.on_save()
self.log.append('Updated %s' % self.doc.name)
else:
UpdateDocument.save(self)
[docs] def save_children(self):
for df in self.in_doclist[1:]:
d = Document(fielddata = df)
# update doctype?
if d.doctype in self.to_update_doctype:
# update this record?
if self.to_update(d):
# is it new?
if self.child_exists(d):
self.keep_values(d)
d.save(ignore_fields = 1, check_links=0)
self.log.append('updated %s, %s' % (d.doctype, d.name))
else:
d.save(1, ignore_fields = 1, check_links=0)
self.log.append('new %s' % d.doctype)
self.doclist.append(d)
[docs] def keep_values(self, d):
if hasattr(self, 'get_orignal_values'):
ov = self.get_orignal_values(d)
if ov:
d.fields.update(ov)
#
# Class to sync incoming doctype
#
[docs]class UpdateDocType(UpdateDocumentMerge):
def __init__(self, in_doclist):
UpdateDocumentMerge.__init__(self, in_doclist)
self.to_update_doctype = ['DocType', 'DocField']
[docs] def to_udpate(self, d):
if (d.fieldtype not in ['Section Break', 'Column Break', 'HTML']) and (d.fieldname or d.label):
return 1
[docs] def get_id(self, d):
key = d.fieldname and 'fieldname' or 'label'
return webnotes.conn.sql("""select name, options, permlevel, reqd, print_hide, hidden
from tabDocField where %s=%s and parent=%s""" % (key, '%s', '%s'), (d.fields[key], d.parent))
[docs] def on_save(self):
self.renum()
[docs] def child_exists(self, d):
if d.doctype=='DocField':
return self.get_id(d)
[docs] def get_orignal_values(self, d):
if d.doctype=='DocField':
t = self.get_id(d)[0]
return {'name': t[0], 'options': t[1], 'reqd':t[3], 'print_hide':t[4], 'hidden':t[5]}
if d.doctype=='DocType':
return webnotes.conn.sql("select server_code, client_script from `tabDocType` where name=%s", d.name, as_dict = 1)[0]
# renumber the indexes
[docs] def renum(self):
extra = self.get_extra_fields()
self.clear_section_breaks()
self.add_section_breaks_and_renum()
self.fix_extra_fields(extra)
# get fields not in the incoming list (to preserve order)
[docs] def clear_section_breaks(self):
webnotes.conn.sql("delete from tabDocField where fieldtype in ('Section Break', 'Column Break', 'HTML') and parent=%s and ifnull(options,'')!='Custom'", self.doc.name)
# add section breaks
[docs] def add_section_breaks_and_renum(self):
for d in self.in_doclist:
if d.get('parentfield')=='fields':
if d.get('fieldtype') in ('Section Break', 'Column Break', 'HTML'):
tmp = Document(fielddata = d)
tmp.fieldname = ''
tmp.name = None
tmp.save(1, ignore_fields = 1, check_links=0)
else:
webnotes.conn.sql("update tabDocField set idx=%s where %s=%s and parent=%s" % \
('%s', d.get('fieldname') and 'fieldname' or 'label', '%s', '%s'), (d.get('idx'), d.get('fieldname') or d.get('label'), self.doc.name))
# adjust the extra fields
[docs]class UpdateModuleDef(UpdateDocumentMerge):
def __init__(self, in_doclist):
UpdateDocumentMerge.__init__(self, in_doclist)
self.to_update_doctype = ['Module Def', 'Module Def Item']
[docs] def get_id(self, d):
return webnotes.conn.sql("select name from `tabModule Def Item` where doc_type=%s and doc_name=%s and display_name=%s and parent=%s", (d.doc_type, d.doc_name, d.display_name, d.parent))
[docs] def to_update(self, d):
if d.doctype=='Module Def Item': return 1
[docs] def get_orignal_values(self, d):
if d.doctype=='Module Def Item':
return {'name': self.get_id(d)[0][0]}
if d.doctype=='Module Def':
return webnotes.conn.sql("select module_seq, disabled, is_hidden from `tabModule Def` where name=%s", d.name, as_dict = 1)[0]
#
# update module def
#
[docs]class UpdateDocTypeMapper(UpdateDocumentMerge):
def __init__(self, in_doclist):
UpdateDocumentMerge.__init__(self, in_doclist)
self.to_update_doctype = ['Field Mapper Detail', 'Table Mapper Detail']
[docs] def get_id(self, d):
if d.doctype=='Field Mapper Detail':
return webnotes.conn.sql("select name from `tabField Mapper Detail` where from_field=%s and to_field=%s and match_id=%s and parent=%s", (d.from_field, d.to_field, d.match_id, d.parent))
elif d.doctype=='Table Mapper Detail':
return webnotes.conn.sql("select name from `tabTable Mapper Detail` where from_table=%s and to_table = %s and match_id=%s and parent=%s", (d.from_table, d.to_table, d.match_id, d.parent))
[docs] def get_orignal_values(self, d):
if d.doctype in ['Field Mapper Detail', 'Table Mapper Detail']:
return {'name': self.get_id(d)[0][0]}