jsonAndRequest.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, 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):
  56. self.ticket = ticket
  57. self.state = state
  58. self.id_metaservice = id_metaservice
  59. self.is_global = is_global
  60. self.is_visible = is_visible
  61. self.service = service
  62. self.service_type = service_type
  63. self.time_placed = time_placed
  64. self.time_start = time_start
  65. self.time_start_predicted = time_start_predicted
  66. self.time_finish_predicted = time_finish_predicted
  67. self.time_start_real = time_start_real
  68. self.time_finish_real = time_finish_real
  69. self.order_place_start = order_place_start
  70. self.order_place_start_gps = order_place_start_gps
  71. self.order_place_start_real = order_place_start_real
  72. self.order_place_start_real_gps = order_place_start_real_gps
  73. self.order_place_finish_predicted = order_place_finish_predicted
  74. self.order_place_finish_predicted_gps = order_place_finish_predicted_gps
  75. self.order_place_finish_real = order_place_finish_real
  76. self.order_place_finish_real_gps = order_place_finish_real_gps
  77. self.predicted_price = predicted_price
  78. self.real_price = real_price
  79. self.payment_transaction_id = payment_transaction_id
  80. self.rating_id = rating_id
  81. self.provider = provider
  82. self.provider_screen_name = provider_screen_name
  83. self.receiver = receiver
  84. self.receiver_screen_name = receiver_screen_name
  85. self.client_id = client_id
  86. self.resource_id = resource_id
  87. #доделать до конца по аналогии с закоммиченным json
  88. def __str__(self):
  89. 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)
  90. #\n добавляете в местах с пустой строкой в шаблоне
  91. #ticket json example
  92. '''
  93. {
  94. "id": 2,
  95. "status": 111,
  96. "created_date": "2023-05-15",
  97. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  98. "priority": 0,
  99. "title": "Test",
  100. "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}"
  101. "due_date": "2023-05-15",
  102. "task_list": 10,
  103. "created_by": 1,
  104. "type": 1,
  105. "assigned_to": 1
  106. }
  107. '''
  108. #ticket minimal example
  109. '''
  110. {
  111. "id": 2,
  112. "status": 111,
  113. "created_date": "2023-05-15",
  114. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  115. "priority": 0,
  116. "title": null,
  117. "note": null,
  118. "due_date": null,
  119. "task_list": 10,
  120. "created_by": 1,
  121. "type": 1,
  122. "assigned_to": null
  123. }
  124. '''
  125. class OrderTicket(object):
  126. 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):
  127. self.id = id
  128. self.status = status
  129. self.created_date = created_date
  130. self.status_changed_date = status_changed_date
  131. self.priority = priority
  132. self.title = title
  133. self.note = note
  134. self.due_date = due_date
  135. self.task_list = task_list
  136. self.created_by = created_by
  137. self.type = type
  138. self.assigned_to = assigned_to
  139. def __str__(self):
  140. 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)
  141. #Функция преобразования json в объект класса Order
  142. def jsonToOrder(jsonstring) -> Order:
  143. jsonObj = json.loads(jsonstring)
  144. print(jsonObj)
  145. u = Order(**jsonObj)
  146. return u
  147. #Функция преобразования json в объект класса OrderTicket
  148. def jsonToOrderTicket(jsonstring) -> OrderTicket:
  149. print (jsonstring)
  150. jsonObj = json.loads(jsonstring)
  151. print(jsonObj)
  152. u = OrderTicket(**jsonObj)
  153. return u
  154. #Функция обращения к апи и получение токена
  155. def requestGetToken(login, password, url) -> str:
  156. #login = "79876543210"
  157. #password = "admin12345@"
  158. #url = "https://user.sharix-app.org/auth/token/login/"
  159. data = {
  160. "password": password,
  161. "phone_number": login
  162. }
  163. response = requests.post(url, json=data)
  164. result = response.json()
  165. return result['auth_token']
  166. #Функция обращения к апи и получение токена
  167. def createUser(username, phone, password, url) -> str:
  168. data = {
  169. "username": username,
  170. "phone_number": phone,
  171. "password": password
  172. }
  173. response = requests.post(url, json=data)
  174. result = response.json()
  175. return result
  176. #Функция обращения к апи и получение списка пользователей
  177. def requestGetListUser():
  178. auth_token=requestGetToken()
  179. print(auth_token)
  180. headers = {'Authorization': f'Token {auth_token}'}
  181. url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  182. response = requests.get(url, headers=headers)
  183. print(response)
  184. print(response.json())
  185. def requestGetList(auth_token, url):
  186. #auth_token=requestGetToken(login, password, url)
  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(config.API_URL+"/tickets/api/tickets/?list_id=10", headers=headers)
  191. #response = requests.get(config.API_URL+"/metaservicesynced/api/orders/", headers=headers)
  192. print(response)
  193. #return response.json()
  194. print(response.json())
  195. ordertext=response.json()
  196. startlist = []
  197. #if not ordertext:
  198. # print("Empty array")
  199. #else:
  200. # for i in ordertext:
  201. # print (i, "PROCESSING")
  202. # try:
  203. # orderticket = OrderTicket(**i)
  204. # print(i, "ORDER ", orderticket)
  205. # except Exception as ex:
  206. # print(ex)
  207. if ordertext is not None:
  208. for order in ordertext:
  209. print (order)
  210. strorder = str(order).replace("'", '"')
  211. strorder = strorder.replace("None", 'null')
  212. #parsed_json = ast.literal_eval(order)
  213. #print (parsed_json)
  214. orderObj = jsonToOrderTicket(strorder)
  215. print (orderObj)
  216. startlist.append(orderObj)
  217. #bot.bot_log(str(datetime.now())+"Заказ успешно добавлен в очередь обработки\n")
  218. #print (orderticket)
  219. return startlist