jsonAndRequest.py 10 KB

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