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.

пре 11 година
пре 13 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 13 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes, json
  5. from webnotes import _
  6. from webnotes.model.doc import Document
  7. """
  8. Model utilities, unclassified functions
  9. """
  10. def expand(docs):
  11. """
  12. Expand a doclist sent from the client side. (Internally used by the request handler)
  13. """
  14. def xzip(a,b):
  15. d = {}
  16. for i in range(len(a)):
  17. d[a[i]] = b[i]
  18. return d
  19. docs = json.loads(docs)
  20. clist = []
  21. for d in docs['_vl']:
  22. doc = xzip(docs['_kl'][d[0]], d);
  23. clist.append(doc)
  24. return clist
  25. def compress(doclist):
  26. """
  27. Compress a doclist before sending it to the client side. (Internally used by the request handler)
  28. """
  29. docs = [isinstance(d, Document) and d.fields or d for d in doclist]
  30. kl, vl = {}, []
  31. forbidden = ['server_code_compiled']
  32. # scan for keys & values
  33. for d in docs:
  34. dt = d['doctype']
  35. if not (dt in kl.keys()):
  36. kl[dt] = ['doctype','localname','__oldparent','__unsaved']
  37. # add client script for doctype, doctype due to ambiguity
  38. if dt=='DocType' and '__client_script' not in kl[dt]:
  39. kl[dt].append('__client_script')
  40. for f in d.keys():
  41. if not (f in kl[dt]) and not (f in forbidden):
  42. # if key missing, then append
  43. kl[dt].append(f)
  44. # build values
  45. tmp = []
  46. for f in kl[dt]:
  47. v = d.get(f)
  48. if type(v)==long:
  49. v=int(v)
  50. tmp.append(v)
  51. vl.append(tmp)
  52. return {'_vl':vl,'_kl':kl}
  53. def getlist(doclist, field):
  54. from webnotes.utils import cint
  55. l = []
  56. for d in doclist:
  57. if d.parentfield == field:
  58. l.append(d)
  59. l.sort(lambda a, b: cint(a.idx) - cint(b.idx))
  60. return l
  61. def copy_doclist(doclist, no_copy = []):
  62. """
  63. Save & return a copy of the given doclist
  64. Pass fields that are not to be copied in `no_copy`
  65. """
  66. cl = []
  67. # main doc
  68. c = Document(fielddata = doclist[0].fields.copy())
  69. # clear no_copy fields
  70. for f in no_copy:
  71. if c.fields.has_key(f):
  72. c.fields[f] = None
  73. c.name = None
  74. c.save(1)
  75. cl.append(c)
  76. # new parent name
  77. parent = c.name
  78. # children
  79. for d in doclist[1:]:
  80. c = Document(fielddata = d.fields.copy())
  81. c.name = None
  82. # clear no_copy fields
  83. for f in no_copy:
  84. if c.fields.has_key(f):
  85. c.fields[f] = None
  86. c.parent = parent
  87. c.save(1)
  88. cl.append(c)
  89. return cl
  90. def set_default(doc, key):
  91. if not doc.is_default:
  92. webnotes.conn.set(doc, "is_default", 1)
  93. webnotes.conn.sql("""update `tab%s` set `is_default`=0
  94. where `%s`=%s and name!=%s""" % (doc.doctype, key, "%s", "%s"),
  95. (doc.fields.get(key), doc.name))