jsonAndRequest.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import json
  2. import requests
  3. import config
  4. import ast
  5. #Тестовый класс User и Address (ВАЖНО!!!) поля классов должны называться также как и поля json объекта
  6. '''
  7. #Full json order example
  8. https://wiki.sharix-app.org/doku.php/open/tech/dev/sharix_open_-_primery_json
  9. {
  10. "ticket": 123,
  11. "status": 111,
  12. "priority": 1,
  13. "title": "123",
  14. "note": "123",
  15. "due_date": 2023-05-15T17:16:22.955601Z,
  16. "id_metaservice": 1,
  17. "is_global": false,
  18. "is_visible": false,
  19. "service": 1,
  20. "service_type": 1,
  21. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  22. "time_placed": "2023-05-15T16:40:32.179777Z",
  23. "time_start": "2023-05-15T16:40:32.179777Z",
  24. "time_start_predicted": "2023-05-15T16:40:32.179777Z",
  25. "time_finish_predicted": "2023-05-15T16:40:32.179777Z",
  26. "time_start_real": "2023-05-15T16:40:32.179777Z",
  27. "time_finish_real": "2023-05-15T16:40:32.179777Z",
  28. "order_place_type": "address",
  29. "order_place_start": "р-н Беговой",
  30. "order_place_start_gps": "55.751941, 37.607513",
  31. "order_place_start_sector": 4,
  32. "order_place_start_real": "р-н Беговой",
  33. "order_place_start_real_gps": "55.751941, 37.607513",
  34. "order_place_finish_predicted": "р-н Мещанский",
  35. "order_place_finish_predicted_gps": "55.751941, 37.607513",
  36. "order_place_finish_sector": 4,
  37. "order_place_finish_real": "р-н Мещанский",
  38. "order_place_finish_real_gps": "55.751941, 37.607513",
  39. "predicted_price": 1000,
  40. "real_price": 1001,
  41. "payment_transaction_id": 1,
  42. "rating_id": 1,
  43. "provider": 1,
  44. "provider_screen_name": "Vasya Ispolnitelev",
  45. "receiver": 1,
  46. "receiver_screen_name": "Marfa Clientovna",
  47. "client_id": 1,
  48. "client_screen_name": "Fedor Client",
  49. "resource_id": 1,
  50. "attempts": null
  51. }
  52. '''
  53. #надо адаптировать класс к новому формату JSON
  54. class Order(object):
  55. def __init__(self, order_synced, ticket, status, title, note, due_date, id_metaservice, is_global, is_visible, service, service_type, status_changed_date, time_placed, time_start=None, time_start_predicted=None, time_finish_predicted=None, time_start_real=None, time_finish_real=None, order_place_type=None, order_place_start=None, order_place_start_gps=None, order_place_start_real=None, order_place_start_real_gps=None, order_place_finish_predicted=None, order_place_finish_predicted_gps=None, order_place_finish_real=None, order_place_finish_real_gps=None, predicted_price=None, real_price=None, payment_transaction_id=None, rating_id=None, provider=None, provider_screen_name=None, receiver=None, receiver_screen_name=None, client_id=None, resource_id=None, attempts=None, asap=None):
  56. self.order_synced = order_synced
  57. self.ticket = ticket
  58. self.status = status
  59. self.title = title
  60. self.note = note
  61. self.due_date = due_date
  62. self.id_metaservice = id_metaservice
  63. self.is_global = is_global
  64. self.is_visible = is_visible
  65. self.service = service
  66. self.service_type = service_type
  67. self.status_changed_date = status_changed_date
  68. self.time_placed = time_placed
  69. self.time_start = time_start
  70. self.time_start_predicted = time_start_predicted
  71. self.time_finish_predicted = time_finish_predicted
  72. self.time_start_real = time_start_real
  73. self.time_finish_real = time_finish_real
  74. self.order_place_type =order_place_type
  75. self.order_place_start = order_place_start
  76. self.order_place_start_gps = order_place_start_gps
  77. self.order_place_start_real = order_place_start_real
  78. self.order_place_start_real_gps = order_place_start_real_gps
  79. self.order_place_finish_predicted = order_place_finish_predicted
  80. self.order_place_finish_predicted_gps = order_place_finish_predicted_gps
  81. self.order_place_finish_real = order_place_finish_real
  82. self.order_place_finish_real_gps = order_place_finish_real_gps
  83. self.predicted_price = predicted_price
  84. self.real_price = real_price
  85. self.payment_transaction_id = payment_transaction_id
  86. self.rating_id = rating_id
  87. self.provider = provider
  88. self.provider_screen_name = provider_screen_name
  89. self.receiver = receiver
  90. self.receiver_screen_name = receiver_screen_name
  91. self.client_id = client_id
  92. self.resource_id = resource_id
  93. self.attempts = attempts
  94. self.asap = asap
  95. #доделать до конца по аналогии с закоммиченным json
  96. def __str__(self):
  97. return "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}, {28}, {29}, {30}, {31}, {32}".format(self.order_synced, self.ticket, self.status, self.title, self.note, self.due_date, self.id_metaservice, self.is_global, self.is_visible, self.service, self.service_type, self.status_changed_date, self.time_placed, self.time_start, self.time_start_predicted, self.time_finish_predicted, self.time_start_real, self.time_finish_real, self.order_place_type, self.order_place_start, self.order_place_start_gps, self.order_place_start_real, self.order_place_start_real_gps, self.order_place_finish_predicted, self.order_place_finish_predicted_gps, self.order_place_finish_real, self.order_place_finish_real_gps, self.predicted_price, self.real_price, self.payment_transaction_id, self.rating_id, self.provider, self.provider_screen_name, self.receiver, self.receiver_screen_name, self.client_id, self.resource_id)
  98. #\n добавляете в местах с пустой строкой в шаблоне
  99. #ticket json example
  100. '''
  101. {
  102. "id": 2,
  103. "status": 111,
  104. "created_date": "2023-05-15",
  105. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  106. "priority": 0,
  107. "title": "Test",
  108. "note": "{\n \n \"ticket\": 1,\n \"state\": 1,\n \"id_metaservice\": 1,\n \"is_global\": false,\n \"is_visible\": false,\n \"service\": 1,\n \"service_type\": 1,\n\n \"time_placed\": 1}"
  109. "due_date": "2023-05-15",
  110. "task_list": 10,
  111. "created_by": 1,
  112. "type": 1,
  113. "assigned_to": 1
  114. }
  115. '''
  116. #ticket minimal example
  117. '''
  118. {
  119. "id": 2,
  120. "status": 111,
  121. "created_date": "2023-05-15",
  122. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  123. "priority": 0,
  124. "title": null,
  125. "note": null,
  126. "due_date": null,
  127. "task_list": 10,
  128. "created_by": 1,
  129. "type": 1,
  130. "assigned_to": null
  131. }
  132. '''
  133. class OrderTicket(object):
  134. def __init__(self, id, status, created_date, status_changed_date, priority, title=None, note=None, due_date=None, task_list=None, created_by=None, type=None, assigned_to=None):
  135. self.id = id
  136. self.status = status
  137. self.created_date = created_date
  138. self.status_changed_date = status_changed_date
  139. self.priority = priority
  140. self.title = title
  141. self.note = note
  142. self.due_date = due_date
  143. self.task_list = task_list
  144. self.created_by = created_by
  145. self.type = type
  146. self.assigned_to = assigned_to
  147. def __str__(self):
  148. return "{0}, {1}, {2}, {3}, {4}, {5}, {6},\n{7}, {8}, {9}, {10}, {11}\n".format(self.id, self.status, self.created_date, self.status_changed_date, self.priority, self.title, self.note, self.due_date, self.task_list, self.created_by, self.type, self.assigned_to)
  149. #Функция преобразования json в объект класса Order
  150. def jsonToOrder(jsonstring) -> Order:
  151. jsonObj = json.loads(jsonstring)
  152. print(jsonObj)
  153. u = Order(**jsonObj)
  154. return u
  155. #Функция преобразования json в объект класса OrderTicket
  156. def jsonToOrderTicket(jsonstring) -> OrderTicket:
  157. print (jsonstring)
  158. jsonObj = json.loads(jsonstring)
  159. print(jsonObj)
  160. u = OrderTicket(**jsonObj)
  161. return u
  162. #Функция обращения к апи и получение токена
  163. def requestGetToken(login, password, url) -> str:
  164. #login = "79876543210"
  165. #password = "admin12345@"
  166. #url = "https://user.sharix-app.org/auth/token/login/"
  167. data = {
  168. "password": password,
  169. "phone_number": login
  170. }
  171. response = requests.post(url, json=data)
  172. result = response.json()
  173. return result['auth_token']
  174. #Функция обращения к апи и получение токена
  175. def createUser(username, phone, password, url) -> str:
  176. data = {
  177. "username": username,
  178. "phone_number": phone,
  179. "password": password
  180. }
  181. response = requests.post(url, json=data)
  182. result = response.json()
  183. return result
  184. #Функция обращения к апи и получение списка пользователей
  185. def requestGetListUser():
  186. auth_token=requestGetToken()
  187. print(auth_token)
  188. headers = {'Authorization': f'Token {auth_token}'}
  189. url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  190. response = requests.get(url, headers=headers)
  191. print(response)
  192. print(response.json())
  193. def requestGetList(auth_token, url):
  194. #auth_token=requestGetToken(login, password, url)
  195. #print(auth_token)
  196. headers = {'Authorization': f'Token {auth_token}'}
  197. #url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  198. response = requests.get(config.API_URL+"/tickets/api/tickets/?list_id=10", headers=headers)
  199. #response = requests.get(config.API_URL+"/metaservicesynced/api/orders/", headers=headers)
  200. print(response)
  201. #return response.json()
  202. print(response.json())
  203. ordertext=response.json()
  204. startlist = []
  205. if not ordertext:
  206. print("Empty array")
  207. else:
  208. for i in ordertext:
  209. print (i, "PROCESSING")
  210. try:
  211. orderticket = OrderTicket(**i)
  212. print(i, "ORDER ", orderticket)
  213. startlist.append(orderticket)
  214. except Exception as ex:
  215. print(ex)
  216. print (startlist)
  217. #if ordertext is not None:
  218. # for order in ordertext:
  219. # print (order)
  220. # strorder = str(order).replace("'", '"')
  221. # strorder = strorder.replace("None", 'null')
  222. # #parsed_json = ast.literal_eval(order)
  223. # #print (parsed_json)
  224. # orderObj = jsonToOrderTicket(strorder)
  225. # print (orderObj)
  226. # startlist.append(orderObj)
  227. # #bot.bot_log(str(datetime.now())+"Заказ успешно добавлен в очередь обработки\n")
  228. #print (orderticket)
  229. return startlist