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.

пре 13 година
пре 14 година
пре 14 година
пре 14 година
пре 12 година
пре 12 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 getone(self, filters):
  61. return self.get(filters, limit=1)[0]
  62. def extend(self, n):
  63. list.extend(self, n)
  64. return self
  65. def copy(self):
  66. out = []
  67. for d in self:
  68. fielddata = d.fields
  69. fielddata.update({"name": None})
  70. out.append(Document(fielddata=fielddata))
  71. return DocList(out)
  72. def filter_valid_fields(self):
  73. import webnotes.model
  74. fieldnames = {}
  75. for d in self:
  76. remove = []
  77. for f in d:
  78. if f not in fieldnames.setdefault(d.doctype,
  79. webnotes.model.get_fieldnames(d.doctype)):
  80. remove.append(f)
  81. for f in remove:
  82. del d[f]
  83. def append(self, doc):
  84. if not isinstance(doc, Document):
  85. doc = Document(fielddata=doc)
  86. self._prepare_doc(doc)
  87. super(DocList, self).append(doc)
  88. def extend(self, doclist):
  89. doclist = objectify(doclist)
  90. for doc in doclist:
  91. self._prepare_doc(doc)
  92. super(DocList, self).extend(doclist)
  93. def _prepare_doc(self, doc):
  94. if not doc.name:
  95. doc.fields["__islocal"] = 1
  96. if doc.parentfield:
  97. if not doc.parenttype:
  98. doc.parenttype = self[0].doctype
  99. if not doc.parent:
  100. doc.parent = self[0].name
  101. def load(doctype, name):
  102. # load main doc
  103. return objectify(load_doclist(doctype, name))
  104. def load_doclist(doctype, name):
  105. doclist = DocList([load_main(doctype, name)])
  106. # load children
  107. table_fields = map(lambda f: (f.options, name, f.fieldname, doctype),
  108. webnotes.conn.get_table_fields(doctype))
  109. for args in table_fields:
  110. children = load_children(*args)
  111. if children: doclist += children
  112. return doclist
  113. def load_main(doctype, name):
  114. """retrieves doc from database"""
  115. if webnotes.conn.is_single(doctype):
  116. doc = webnotes.conn.sql("""select field, value from `tabSingles`
  117. where doctype=%s""", doctype, as_list=1)
  118. doc = dict(doc)
  119. doc["name"] = doctype
  120. else:
  121. doc = webnotes.conn.sql("""select * from `tab%s` where name = %s""" % \
  122. (doctype, "%s"), name, as_dict=1)
  123. if not doc:
  124. raise NameError, """%s: "%s" does not exist""" % (doctype, name)
  125. doc = doc[0]
  126. doc["doctype"] = doctype
  127. return doc
  128. def load_children(options, parent, parentfield, parenttype):
  129. """load children based on options, parentfield, parenttype and parent"""
  130. options = options.split("\n")[0].strip()
  131. return webnotes.conn.sql("""select *, "%s" as doctype from `tab%s` where parent = %s
  132. and parentfield = %s and parenttype = %s order by idx""" % (options, options, "%s", "%s", "%s"),
  133. (parent, parentfield, parenttype), as_dict=1)
  134. def objectify(doclist):
  135. from webnotes.model.doc import Document
  136. return map(lambda d: isinstance(d, Document) and d or Document(d), doclist)