浏览代码

fix in install_docs

version-14
Anand Doshi 13 年前
父节点
当前提交
cdee01ed80
共有 2 个文件被更改,包括 28 次插入11 次删除
  1. +16
    -5
      py/webnotes/db.py
  2. +12
    -6
      py/webnotes/model/sync.py

+ 16
- 5
py/webnotes/db.py 查看文件

@@ -343,14 +343,25 @@ class Database:
""" """
return self.sql("select name from tabDocField where fieldname=%s and parent=%s", (dt, fn)) return self.sql("select name from tabDocField where fieldname=%s and parent=%s", (dt, fn))


def exists(self, dt, dn):
def exists(self, dt, dn=None):
""" """
Returns true if the record exists Returns true if the record exists
""" """
try:
return self.sql('select name from `tab%s` where name=%s' % (dt, '%s'), dn)
except:
return None
if isinstance(dt, basestring):
try:
return self.sql('select name from `tab%s` where name=%s' % (dt, '%s'), dn)
except:
return None
elif isinstance(dt, dict) and dt.get('doctype'):
try:
conditions = []
for d in dt:
if d == 'doctype': continue
conditions.append('`%s` = "%s"' % (d, dt[d].replace('"', '\"')))
return self.sql('select name from `tab%s` where %s' % \
(dt['doctype'], " and ".join(conditions)))
except:
return None


# ====================================================================================== # ======================================================================================
def close(self): def close(self):


+ 12
- 6
py/webnotes/model/sync.py 查看文件

@@ -126,23 +126,29 @@ def save_perms_if_none_exist(doclist):


def load_install_docs(modules): def load_install_docs(modules):
import os import os
from webnotes.model.doc import Document
if isinstance(modules, basestring): modules = [modules] if isinstance(modules, basestring): modules = [modules]
for module_name in modules: for module_name in modules:
module = __import__(module_name) module = __import__(module_name)
if hasattr(module, 'install_docs'): if hasattr(module, 'install_docs'):
webnotes.conn.begin() webnotes.conn.begin()

for data in module.install_docs: for data in module.install_docs:
if data.get('name'): if data.get('name'):
if not webnotes.conn.exists(data['doctype'], data.get('name')): if not webnotes.conn.exists(data['doctype'], data.get('name')):
d = Document(data['doctype'])
d.fields.update(data)
d.save()
print 'Created %(doctype)s %(name)s' % d.fields
create_doc(data)
elif not webnotes.conn.exists(data):
create_doc(data)
webnotes.conn.commit() webnotes.conn.commit()


def create_doc(data):
from webnotes.model.doc import Document
d = Document(data['doctype'])
d.fields.update(data)
d.save()
print 'Created %(doctype)s %(name)s' % d.fields

import unittest import unittest
class TestSync(unittest.TestCase): class TestSync(unittest.TestCase):
def setUp(self): def setUp(self):


正在加载...
取消
保存