You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

169 line
4.9 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. import webnotes
  23. import webnotes.model
  24. from webnotes.model.doc import Document
  25. class DocList(list):
  26. """DocList object as a wrapper around a list"""
  27. def get(self, filters, limit=0):
  28. """pass filters as:
  29. {"key": "val", "key": ["!=", "val"],
  30. "key": ["in", "val"], "key": ["not in", "val"], "key": "^val"}"""
  31. # map reverse operations to set add = False
  32. import operator
  33. ops_map = {
  34. "!=": lambda (a, b): operator.ne(a, b),
  35. "in": lambda (a, b): operator.contains(b, a),
  36. "not in": lambda (a, b): not operator.contains(b, a)
  37. }
  38. out = []
  39. for doc in self:
  40. d = isinstance(getattr(doc, "fields", None), dict) and doc.fields or doc
  41. add = True
  42. for f in filters:
  43. fval = filters[f]
  44. if isinstance(fval, list):
  45. if fval[0] in ops_map and not ops_map[fval[0]]((d.get(f), fval[1])):
  46. add = False
  47. break
  48. elif isinstance(fval, basestring) and fval.startswith("^"):
  49. if not (d.get(f) or "").startswith(fval[1:]):
  50. add = False
  51. break
  52. elif d.get(f)!=fval:
  53. add = False
  54. break
  55. if add:
  56. out.append(doc)
  57. if limit and (len(out)-1)==limit:
  58. break
  59. return DocList(out)
  60. def remove_items(self, filters):
  61. for d in self.get(filters):
  62. self.remove(d)
  63. def getone(self, filters):
  64. return self.get(filters, limit=1)[0]
  65. def copy(self):
  66. out = []
  67. for d in self:
  68. if isinstance(d, dict):
  69. fielddata = d
  70. else:
  71. fielddata = d.fields
  72. fielddata.update({"name": None})
  73. out.append(Document(fielddata=fielddata))
  74. return DocList(out)
  75. def filter_valid_fields(self):
  76. import webnotes.model
  77. fieldnames = {}
  78. for d in self:
  79. remove = []
  80. for f in d:
  81. if f not in fieldnames.setdefault(d.doctype,
  82. webnotes.model.get_fieldnames(d.doctype)):
  83. remove.append(f)
  84. for f in remove:
  85. del d[f]
  86. def append(self, doc):
  87. if not isinstance(doc, Document):
  88. doc = Document(fielddata=doc)
  89. self._prepare_doc(doc)
  90. super(DocList, self).append(doc)
  91. def extend(self, doclist):
  92. doclist = objectify(doclist)
  93. for doc in doclist:
  94. self._prepare_doc(doc)
  95. super(DocList, self).extend(doclist)
  96. return self
  97. def _prepare_doc(self, doc):
  98. if not doc.name:
  99. doc.fields["__islocal"] = 1
  100. if doc.parentfield:
  101. if not doc.parenttype:
  102. doc.parenttype = self[0].doctype
  103. if not doc.parent:
  104. doc.parent = self[0].name
  105. def load(doctype, name):
  106. # load main doc
  107. return objectify(load_doclist(doctype, name))
  108. def load_doclist(doctype, name):
  109. doclist = DocList([load_main(doctype, name)])
  110. # load children
  111. table_fields = map(lambda f: (f.options, name, f.fieldname, doctype),
  112. webnotes.conn.get_table_fields(doctype))
  113. for args in table_fields:
  114. children = load_children(*args)
  115. if children: doclist += children
  116. return doclist
  117. def load_main(doctype, name):
  118. """retrieves doc from database"""
  119. if webnotes.conn.is_single(doctype):
  120. doc = webnotes.conn.sql("""select field, value from `tabSingles`
  121. where doctype=%s""", doctype, as_list=1)
  122. doc = dict(doc)
  123. doc["name"] = doctype
  124. else:
  125. doc = webnotes.conn.sql("""select * from `tab%s` where name = %s""" % \
  126. (doctype, "%s"), name, as_dict=1)
  127. if not doc:
  128. raise NameError, """%s: "%s" does not exist""" % (doctype, name)
  129. doc = doc[0]
  130. doc["doctype"] = doctype
  131. return doc
  132. def load_children(options, parent, parentfield, parenttype):
  133. """load children based on options, parentfield, parenttype and parent"""
  134. options = options.split("\n")[0].strip()
  135. return webnotes.conn.sql("""select *, "%s" as doctype from `tab%s` where parent = %s
  136. and parentfield = %s and parenttype = %s order by idx""" % (options, options, "%s", "%s", "%s"),
  137. (parent, parentfield, parenttype), as_dict=1)
  138. def objectify(doclist):
  139. from webnotes.model.doc import Document
  140. return map(lambda d: isinstance(d, Document) and d or Document(d), doclist)