jsonAndRequest.py 10 KB

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