seitime-frappe/frappe/utils/make_random.py
Gavin D'souza e407b78506 chore: Drop dead and deprecated code
* Remove six for PY2 compatability since our dependencies are not, PY2
  is legacy.
* Removed usages of utils from future/past libraries since they are
  deprecated. This includes 'from __future__ ...' and 'from past...'
  statements.
* Removed compatibility imports for PY2, switched from six imports to
  standard library imports.
* Removed utils code blocks that handle operations depending on PY2/3
  versions.
* Removed 'from __future__ ...' lines from templates/code generators
* Used PY3 syntaxes in place of PY2 compatible blocks. eg: metaclass
2021-05-26 15:31:29 +05:30

53 lines
1.2 KiB
Python

import frappe, random
settings = frappe._dict(
prob = {
"default": { "make": 0.6, "qty": (1,5) },
}
)
def add_random_children(doc, fieldname, rows, randomize, unique=None):
nrows = rows
if rows > 1:
nrows = random.randrange(1, rows)
for i in range(nrows):
d = {}
for key, val in randomize.items():
if isinstance(val[0], str):
d[key] = get_random(*val)
else:
d[key] = random.randrange(*val)
if unique:
if not doc.get(fieldname, {unique:d[unique]}):
doc.append(fieldname, d)
else:
doc.append(fieldname, d)
def get_random(doctype, filters=None, doc=False):
condition = []
if filters:
for key, val in filters.items():
condition.append("%s='%s'" % (key, str(val).replace("'", "\'")))
if condition:
condition = " where " + " and ".join(condition)
else:
condition = ""
out = frappe.db.sql("""select name from `tab%s` %s
order by RAND() limit 0,1""" % (doctype, condition))
out = out and out[0][0] or None
if doc and out:
return frappe.get_doc(doctype, out)
else:
return out
def can_make(doctype):
return random.random() < settings.prob.get(doctype, settings.prob["default"])["make"]
def how_many(doctype):
return random.randrange(*settings.prob.get(doctype, settings.prob["default"])["qty"])