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.
 
 
 
 
 
 

319 righe
8.6 KiB

  1. import frappe
  2. from frappe import _
  3. from frappe.utils import add_to_date, now
  4. @frappe.whitelist()
  5. def create_if_not_exists(doc):
  6. '''Create records if they dont exist.
  7. Will check for uniqueness by checking if a record exists with these field value pairs
  8. :param doc: dict of field value pairs. can be a list of dict for multiple records.
  9. '''
  10. if not frappe.local.dev_server:
  11. frappe.throw(_('This method can only be accessed in development'), frappe.PermissionError)
  12. doc = frappe.parse_json(doc)
  13. if not isinstance(doc, list):
  14. docs = [doc]
  15. else:
  16. docs = doc
  17. names = []
  18. for doc in docs:
  19. doc = frappe._dict(doc)
  20. filters = doc.copy()
  21. filters.pop('doctype')
  22. name = frappe.db.exists(doc.doctype, filters)
  23. if not name:
  24. d = frappe.get_doc(doc)
  25. d.insert(ignore_permissions=True)
  26. name = d.name
  27. names.append(name)
  28. return names
  29. @frappe.whitelist()
  30. def create_todo_records():
  31. if frappe.db.get_all('ToDo', {'description': 'this is first todo'}):
  32. return
  33. frappe.get_doc({
  34. "doctype": "ToDo",
  35. "date": add_to_date(now(), days=7),
  36. "description": "this is first todo"
  37. }).insert()
  38. frappe.get_doc({
  39. "doctype": "ToDo",
  40. "date": add_to_date(now(), days=-7),
  41. "description": "this is second todo"
  42. }).insert()
  43. frappe.get_doc({
  44. "doctype": "ToDo",
  45. "date": add_to_date(now(), months=2),
  46. "description": "this is third todo"
  47. }).insert()
  48. frappe.get_doc({
  49. "doctype": "ToDo",
  50. "date": add_to_date(now(), months=-2),
  51. "description": "this is fourth todo"
  52. }).insert()
  53. @frappe.whitelist()
  54. def create_communication_record():
  55. doc = frappe.get_doc({
  56. "doctype": "Communication",
  57. "recipients": "test@gmail.com",
  58. "subject": "Test Form Communication 1",
  59. "communication_date": frappe.utils.now_datetime(),
  60. })
  61. doc.insert()
  62. return doc
  63. @frappe.whitelist()
  64. def setup_workflow():
  65. from frappe.workflow.doctype.workflow.test_workflow import create_todo_workflow
  66. create_todo_workflow()
  67. create_todo_records()
  68. frappe.clear_cache()
  69. @frappe.whitelist()
  70. def create_contact_phone_nos_records():
  71. if frappe.db.get_all('Contact', {'first_name': 'Test Contact'}):
  72. return
  73. doc = frappe.new_doc('Contact')
  74. doc.first_name = 'Test Contact'
  75. for index in range(1000):
  76. doc.append('phone_nos', {'phone': '123456{}'.format(index)})
  77. doc.insert()
  78. @frappe.whitelist()
  79. def create_doctype(name, fields):
  80. fields = frappe.parse_json(fields)
  81. if frappe.db.exists('DocType', name):
  82. return
  83. frappe.get_doc({
  84. "doctype": "DocType",
  85. "module": "Core",
  86. "custom": 1,
  87. "fields": fields,
  88. "permissions": [{
  89. "role": "System Manager",
  90. "read": 1
  91. }],
  92. "name": name
  93. }).insert()
  94. @frappe.whitelist()
  95. def create_child_doctype(name, fields):
  96. fields = frappe.parse_json(fields)
  97. if frappe.db.exists('DocType', name):
  98. return
  99. frappe.get_doc({
  100. "doctype": "DocType",
  101. "module": "Core",
  102. "istable": 1,
  103. "custom": 1,
  104. "fields": fields,
  105. "permissions": [{
  106. "role": "System Manager",
  107. "read": 1
  108. }],
  109. "name": name
  110. }).insert()
  111. @frappe.whitelist()
  112. def create_contact_records():
  113. if frappe.db.get_all('Contact', {'first_name': 'Test Form Contact 1'}):
  114. return
  115. insert_contact('Test Form Contact 1', '12345')
  116. insert_contact('Test Form Contact 2', '54321')
  117. insert_contact('Test Form Contact 3', '12345')
  118. @frappe.whitelist()
  119. def create_multiple_todo_records():
  120. values = []
  121. if frappe.db.get_all('ToDo', {'description': 'Multiple ToDo 1'}):
  122. return
  123. for index in range(1, 1002):
  124. values.append(('100{}'.format(index), 'Multiple ToDo {}'.format(index)))
  125. frappe.db.bulk_insert('ToDo', fields=['name', 'description'], values=set(values))
  126. def insert_contact(first_name, phone_number):
  127. doc = frappe.get_doc({
  128. 'doctype': 'Contact',
  129. 'first_name': first_name
  130. })
  131. doc.append('phone_nos', {'phone': phone_number})
  132. doc.insert()
  133. @frappe.whitelist()
  134. def create_form_tour():
  135. if frappe.db.exists('Form Tour', {'name': 'Test Form Tour'}):
  136. return
  137. tour = frappe.get_doc({
  138. 'doctype': 'Form Tour',
  139. 'title': 'Test Form Tour',
  140. 'reference_doctype': 'Contact',
  141. 'save_on_complete': 1,
  142. 'steps': [{
  143. "title": "Test Title 1",
  144. "description": "Test Description 1",
  145. "has_next_condition": 1,
  146. "next_step_condition": "eval: doc.first_name",
  147. "fieldname": "first_name",
  148. "fieldtype": "Data"
  149. },{
  150. "title": "Test Title 2",
  151. "description": "Test Description 2",
  152. "has_next_condition": 1,
  153. "next_step_condition": "eval: doc.last_name",
  154. "fieldname": "last_name",
  155. "fieldtype": "Data"
  156. },{
  157. "title": "Test Title 3",
  158. "description": "Test Description 3",
  159. "fieldname": "phone_nos",
  160. "fieldtype": "Table"
  161. },{
  162. "title": "Test Title 4",
  163. "description": "Test Description 4",
  164. "is_table_field": 1,
  165. "parent_fieldname": "phone_nos",
  166. "next_step_condition": "eval: doc.phone",
  167. "has_next_condition": 1,
  168. "fieldname": "phone",
  169. "fieldtype": "Data"
  170. }]
  171. })
  172. tour.insert()
  173. @frappe.whitelist()
  174. def create_data_for_discussions():
  175. web_page = create_web_page("Test page for discussions", "test-page-discussions", False)
  176. create_topic_and_reply(web_page)
  177. create_web_page("Test single thread discussion", "test-single-thread", True)
  178. def create_web_page(title, route, single_thread):
  179. web_page = frappe.db.exists("Web Page", {"route": route})
  180. if not web_page:
  181. web_page = frappe.get_doc({
  182. "doctype": "Web Page",
  183. "title": title,
  184. "route": route,
  185. "published": True
  186. })
  187. web_page.save()
  188. web_page.append("page_blocks", {
  189. "web_template": "Discussions",
  190. "web_template_values": frappe.as_json({
  191. "title": "Discussions",
  192. "cta_title": "New Discussion",
  193. "docname": web_page.name,
  194. "single_thread": single_thread
  195. })
  196. })
  197. web_page.save()
  198. return web_page
  199. def create_topic_and_reply(web_page):
  200. topic = frappe.db.exists("Discussion Topic",{
  201. "reference_doctype": "Web Page",
  202. "reference_docname": web_page.name
  203. })
  204. if not topic:
  205. topic = frappe.get_doc({
  206. "doctype": "Discussion Topic",
  207. "reference_doctype": "Web Page",
  208. "reference_docname": web_page.name,
  209. "title": "Test Topic"
  210. })
  211. topic.save()
  212. reply = frappe.get_doc({
  213. "doctype": "Discussion Reply",
  214. "topic": topic.name,
  215. "reply": "This is a test reply"
  216. })
  217. reply.save()
  218. @frappe.whitelist()
  219. def update_webform_to_multistep():
  220. if not frappe.db.exists("Web Form", "update-profile-duplicate"):
  221. doc = frappe.get_doc("Web Form", "edit-profile")
  222. _doc = frappe.copy_doc(doc)
  223. _doc.is_multi_step_form = 1
  224. _doc.title = "update-profile-duplicate"
  225. _doc.route = "update-profile-duplicate"
  226. _doc.is_standard = False
  227. _doc.save()
  228. @frappe.whitelist()
  229. def update_child_table(name):
  230. doc = frappe.get_doc('DocType', name)
  231. if len(doc.fields) == 1:
  232. doc.append('fields', {
  233. 'fieldname': 'doctype_to_link',
  234. 'fieldtype': 'Link',
  235. 'in_list_view': 1,
  236. 'label': 'Doctype to Link',
  237. 'options': 'Doctype to Link'
  238. })
  239. doc.save()
  240. @frappe.whitelist()
  241. def insert_doctype_with_child_table_record(name):
  242. if frappe.db.get_all(name, {'title': 'Test Grid Search'}):
  243. return
  244. def insert_child(doc, data, barcode, check, rating, duration, date):
  245. doc.append('child_table_1', {
  246. 'data': data,
  247. 'barcode': barcode,
  248. 'check': check,
  249. 'rating': rating,
  250. 'duration': duration,
  251. 'date': date,
  252. })
  253. doc = frappe.new_doc(name)
  254. doc.title = 'Test Grid Search'
  255. doc.append('child_table', {'title': 'Test Grid Search'})
  256. insert_child(doc, 'Data', '09709KJKKH2432', 1, 0.5, 266851, "2022-02-21")
  257. insert_child(doc, 'Test', '09209KJHKH2432', 1, 0.8, 547877, "2021-05-27")
  258. insert_child(doc, 'New', '09709KJHYH1132', 0, 0.1, 3, "2019-03-02")
  259. insert_child(doc, 'Old', '09701KJHKH8750', 0, 0, 127455, "2022-01-11")
  260. insert_child(doc, 'Alpha', '09204KJHKH2432', 0, 0.6, 364, "2019-12-31")
  261. insert_child(doc, 'Delta', '09709KSPIO2432', 1, 0.9, 1242000, "2020-04-21")
  262. insert_child(doc, 'Update', '76989KJLVA2432', 0, 1, 183845, "2022-02-10")
  263. insert_child(doc, 'Delete', '29189KLHVA1432', 0, 0, 365647, "2021-05-07")
  264. insert_child(doc, 'Make', '09689KJHAA2431', 0, 0.3, 24, "2020-11-11")
  265. insert_child(doc, 'Create', '09709KLKKH2432', 1, 0.3, 264851, "2021-02-21")
  266. insert_child(doc, 'Group', '09209KJLKH2432', 1, 0.8, 537877, "2020-03-15")
  267. insert_child(doc, 'Slide', '01909KJHYH1132', 0, 0.5, 9, "2018-03-02")
  268. insert_child(doc, 'Drop', '09701KJHKH8750', 1, 0, 127255, "2018-01-01")
  269. insert_child(doc, 'Beta', '09204QJHKN2432', 0, 0.6, 354, "2017-12-30")
  270. insert_child(doc, 'Flag', '09709KXPIP2432', 1, 0, 1241000, "2021-04-21")
  271. insert_child(doc, 'Upgrade', '75989ZJLVA2432', 0.8, 1, 183645, "2020-08-13")
  272. insert_child(doc, 'Down', '28189KLHRA1432', 1, 0, 362647, "2020-06-17")
  273. insert_child(doc, 'Note', '09689DJHAA2431', 0, 0.1, 29, "2021-09-11")
  274. insert_child(doc, 'Click', '08189DJHAA2431', 1, 0.3, 209, "2020-07-04")
  275. insert_child(doc, 'Drag', '08189DIHAA2981', 0, 0.7, 342628, "2022-05-04")
  276. doc.insert()