Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

58 righe
1.4 KiB

  1. import frappe, random
  2. settings = frappe._dict(
  3. prob = {
  4. "default": { "make": 0.6, "qty": (1,5) },
  5. }
  6. )
  7. def add_random_children(doc, fieldname, rows, randomize, unique=None):
  8. nrows = rows
  9. if rows > 1:
  10. nrows = random.randrange(1, rows)
  11. for i in range(nrows):
  12. d = {}
  13. for key, val in randomize.items():
  14. if isinstance(val[0], str):
  15. d[key] = get_random(*val)
  16. else:
  17. d[key] = random.randrange(*val)
  18. if unique:
  19. if not doc.get(fieldname, {unique:d[unique]}):
  20. doc.append(fieldname, d)
  21. else:
  22. doc.append(fieldname, d)
  23. def get_random(doctype, filters=None, doc=False):
  24. condition = []
  25. if filters:
  26. for key, val in filters.items():
  27. condition.append("%s='%s'" % (key, str(val).replace("'", "\'")))
  28. if condition:
  29. condition = " where " + " and ".join(condition)
  30. else:
  31. condition = ""
  32. out = frappe.db.multisql({
  33. 'mariadb': """select name from `tab%s` %s
  34. order by RAND() limit 1 offset 0""" % (doctype, condition),
  35. 'postgres': """select name from `tab%s` %s
  36. order by RANDOM() limit 1 offset 0""" % (doctype, condition)
  37. })
  38. out = out and out[0][0] or None
  39. if doc and out:
  40. return frappe.get_doc(doctype, out)
  41. else:
  42. return out
  43. def can_make(doctype):
  44. return random.random() < settings.prob.get(doctype, settings.prob["default"])["make"]
  45. def how_many(doctype):
  46. return random.randrange(*settings.prob.get(doctype, settings.prob["default"])["qty"])