jsonAndRequest.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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, id,order_synced, ticket, status, title, note, due_date, id_metaservice, is_global, is_visible, service, service_type, status_changed_date,time_created, 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.id = id
  58. # self.ticket = ticket
  59. # self.status = status
  60. # self.title = title
  61. # self.note = note
  62. # self.due_date = due_date
  63. # self.id_metaservice = id_metaservice
  64. # self.is_global = is_global
  65. # self.is_visible = is_visible
  66. # self.service = service
  67. # self.service_type = service_type
  68. # self.status_changed_date = status_changed_date
  69. # self.time_created = time_created
  70. # self.time_placed = time_placed
  71. # self.time_start = time_start
  72. # self.time_start_predicted = time_start_predicted
  73. # self.time_finish_predicted = time_finish_predicted
  74. # self.time_start_real = time_start_real
  75. # self.time_finish_real = time_finish_real
  76. # self.order_place_type =order_place_type
  77. # self.order_place_start = order_place_start
  78. # self.order_place_start_gps = order_place_start_gps
  79. # self.order_place_start_real = order_place_start_real
  80. # self.order_place_start_real_gps = order_place_start_real_gps
  81. # self.order_place_finish_predicted = order_place_finish_predicted
  82. # self.order_place_finish_predicted_gps = order_place_finish_predicted_gps
  83. # self.order_place_finish_real = order_place_finish_real
  84. # self.order_place_finish_real_gps = order_place_finish_real_gps
  85. # self.predicted_price = predicted_price
  86. # self.real_price = real_price
  87. # self.payment_transaction_id = payment_transaction_id
  88. # self.rating_id = rating_id
  89. # self.provider = provider
  90. # self.provider_screen_name = provider_screen_name
  91. # self.receiver = receiver
  92. # self.receiver_screen_name = receiver_screen_name
  93. # self.client_id = client_id
  94. # self.resource_id = resource_id
  95. # self.attempts = attempts
  96. # self.asap = asap
  97. # #доделать до конца по аналогии с закоммиченным json
  98. # def __str__(self):
  99. # 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} , {33} ,{34}".format(self.order_synced, self.id, 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_created,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, self.attempts, self.asap)
  100. #\n добавляете в местах с пустой строкой в шаблоне
  101. class Order(object):
  102. def __init__(self, id, ticket, status, title, note, id_metaservice, is_global, is_visible, service, service_type,time_created, 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):
  103. self.id = id
  104. self.ticket = ticket
  105. self.status = status
  106. self.title = title
  107. self.note = note
  108. self.id_metaservice = id_metaservice
  109. self.is_global = is_global
  110. self.is_visible = is_visible
  111. self.service = service
  112. self.service_type = service_type
  113. self.time_created = time_created
  114. self.time_placed = time_placed
  115. self.time_start = time_start
  116. self.time_start_predicted = time_start_predicted
  117. self.time_finish_predicted = time_finish_predicted
  118. self.time_start_real = time_start_real
  119. self.time_finish_real = time_finish_real
  120. self.order_place_type =order_place_type
  121. self.order_place_start = order_place_start
  122. self.order_place_start_gps = order_place_start_gps
  123. self.order_place_start_real = order_place_start_real
  124. self.order_place_start_real_gps = order_place_start_real_gps
  125. self.order_place_finish_predicted = order_place_finish_predicted
  126. self.order_place_finish_predicted_gps = order_place_finish_predicted_gps
  127. self.order_place_finish_real = order_place_finish_real
  128. self.order_place_finish_real_gps = order_place_finish_real_gps
  129. self.predicted_price = predicted_price
  130. self.real_price = real_price
  131. self.payment_transaction_id = payment_transaction_id
  132. self.rating_id = rating_id
  133. self.provider = provider
  134. self.provider_screen_name = provider_screen_name
  135. self.receiver = receiver
  136. self.receiver_screen_name = receiver_screen_name
  137. self.client_id = client_id
  138. self.resource_id = resource_id
  139. self.attempts = attempts
  140. self.asap = asap
  141. #доделать до конца по аналогии с закоммиченным json
  142. def __str__(self):
  143. 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}".format( self.id, self.ticket, self.status, self.title, self.note, self.id_metaservice, self.is_global, self.is_visible, self.service, self.service_type, self.time_created,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, self.attempts, self.asap)
  144. #ticket json example
  145. '''
  146. {
  147. "id": 2,
  148. "status": 111,
  149. "created_date": "2023-05-15",
  150. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  151. "priority": 0,
  152. "title": "Test",
  153. "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}"
  154. "due_date": "2023-05-15",
  155. "task_list": 10,
  156. "created_by": 1,
  157. "type": 1,
  158. "assigned_to": 1
  159. }
  160. '''
  161. #ticket minimal example
  162. '''
  163. {
  164. "id": 2,
  165. "status": 111,
  166. "created_date": "2023-05-15",
  167. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  168. "priority": 0,
  169. "title": null,
  170. "note": null,
  171. "due_date": null,
  172. "task_list": 10,
  173. "created_by": 1,
  174. "type": 1,
  175. "assigned_to": null
  176. }
  177. {
  178. "serviceType": 2,
  179. "driverSearchEngine": 0,
  180. "comment": "Yyyy",
  181. "arrivalTime": "2023-12-07T14:39:54Z",
  182. "additionalServices": [],
  183. "tasks": [
  184. {
  185. "text": "Eeee",
  186. "address": "Jjjj"
  187. }
  188. ],
  189. "spendingLimit": 6668,
  190. "isWithPaymentDocument": true,
  191. "id": 294830,
  192. "isProceedBeforeArrival": true
  193. }
  194. '''
  195. class OrderTicket(object):
  196. def __init__(self, serviceType, driverSearchEngine, comment, arrivalTime, additionalServices, tasks, spendingLimit, isWithPaymentDocument, id, isProceedBeforeArrival):
  197. self.serviceType = serviceType
  198. self.driverSearchEngine = driverSearchEngine
  199. self.comment = comment
  200. self.arrivalTime = arrivalTime
  201. self.additionalServices = additionalServices
  202. self.tasks = tasks
  203. self.spendingLimit = spendingLimit
  204. self.isWithPaymentDocument = isWithPaymentDocument
  205. self.id = id
  206. self.isProceedBeforeArrival = isProceedBeforeArrival
  207. # class OrderTicketOLD(object):
  208. # 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):
  209. # self.id = id
  210. # self.status = status
  211. # self.created_date = created_date
  212. # self.status_changed_date = status_changed_date
  213. # self.priority = priority
  214. # self.title = title
  215. # self.note = note
  216. # self.due_date = due_date
  217. # self.task_list = task_list
  218. # self.created_by = created_by
  219. # self.type = type
  220. # self.assigned_to = assigned_to
  221. # def __str__(self):
  222. # 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)
  223. #Функция преобразования json в объект класса Order
  224. def jsonToOrder(jsonstring) -> Order:
  225. jsonObj = json.loads(jsonstring)
  226. print(jsonObj)
  227. u = Order(**jsonObj)
  228. return u
  229. #Функция преобразования json в объект класса OrderTicket
  230. def jsonToOrderTicket(jsonstring) -> OrderTicket:
  231. print (jsonstring)
  232. jsonObj = json.loads(jsonstring)
  233. print(jsonObj)
  234. u = OrderTicket(**jsonObj)
  235. return u
  236. #Функция обращения к апи и получение токена
  237. def requestGetToken(login, password, url) -> str:
  238. login = "79999999999"
  239. password = "12345"
  240. #url = "https://user.sharix-app.org/auth/token/login/"
  241. data = {
  242. "password":password,
  243. "phone_number":login
  244. }
  245. response = requests.post(url,json=data)
  246. result = response.json()
  247. return result['auth_token']
  248. #Функция обращения к апи и получение токена
  249. def createUser(username, phone, password, url) -> str:
  250. data = {
  251. "username": username,
  252. "phone_number": phone,
  253. "password": password
  254. }
  255. response = requests.post(url, json=data)
  256. result = response.json()
  257. return result
  258. #Функция обращения к апи и получение списка пользователей
  259. def requestGetListUser():
  260. auth_token=requestGetToken()
  261. print(auth_token)
  262. headers = {'Authorization': f'Token {auth_token}'}
  263. url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  264. response = requests.get(url, headers=headers)
  265. print(response)
  266. print(response.json())
  267. # def requestGetList(auth_token, url):
  268. # headers = {'Authorization': f'Token {auth_token}'}
  269. # response = requests.get(url, headers=headers)
  270. # #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  271. # ordertext=response.json()
  272. # startlist = []
  273. # if not ordertext:
  274. # print("Empty array")
  275. # else:
  276. # for i in ordertext:
  277. # print (i, "PROCESSING")
  278. # try:
  279. # orderticket = OrderTicket(**i)
  280. # print(i, "ORDER ", orderticket)
  281. # startlist.append(orderticket)
  282. # except Exception as ex:
  283. # print(ex)
  284. # print (startlist)
  285. # return startlist
  286. def requestGetList(auth_token, url):
  287. headers = {'Authorization': f'Token {auth_token}'}
  288. response = requests.get(url, headers=headers)
  289. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  290. ordertext=response.json()
  291. startlist = []
  292. if not ordertext:
  293. print("Empty array")
  294. else:
  295. for i in ordertext:
  296. print (i, "PROCESSING")
  297. try:
  298. order = Order(**i)
  299. print(i, "ORDER ", order)
  300. startlist.append(order)
  301. except Exception as ex:
  302. print(ex)
  303. print (startlist)
  304. return startlist
  305. class answer(object):
  306. def __init__(self,order_id,provider,answer):
  307. self.order_id = order_id
  308. self.provider = provider
  309. self.answer = answer
  310. def to_answer(jsonstring):
  311. json_answer = json.loads(jsonstring)
  312. print(json_answer)
  313. u = answer(**json_answer)
  314. return u
  315. def change_to_task(localtask,token):
  316. headers = {
  317. 'Authorization': f'Token {token}',
  318. 'Content-Type': 'application/json' # Установка заголовка Content-Type
  319. }
  320. data = json.dumps(localtask.__dict__)
  321. print(data)
  322. response = requests.patch(config.API_URL+f"/dbsynce/api/orders/{localtask.id}/", headers=headers, data=data)
  323. return response