jsonAndRequest.py 12 KB

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