1
0

jsonAndRequest.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import json
  2. import requests
  3. import config
  4. import ast
  5. from classes.Answer import Answer
  6. from classes.Order import Order
  7. from classes.OrderTicket import OrderTicket
  8. from classes.Ticket_user import Ticket_user
  9. from classes.Ticket import Ticket
  10. #Функция преобразования json в объект класса Order
  11. def jsonToOrder(jsonstring) -> Order:
  12. jsonObj = json.loads(jsonstring)
  13. print(jsonObj)
  14. u = Order(**jsonObj)
  15. return u
  16. #Функция преобразования json в объект класса OrderTicket
  17. def jsonToOrderTicket(jsonstring) -> OrderTicket:
  18. print (jsonstring)
  19. jsonObj = json.loads(jsonstring)
  20. print(jsonObj)
  21. u = OrderTicket(**jsonObj)
  22. return u
  23. #Функция обращения к апи и получение токена
  24. def requestGetToken(login, password, url) -> str:
  25. login = "79999999999"
  26. password = "12345"
  27. # login = "11111111111"
  28. # password = "Sharix!"
  29. #url = "https://user.sharix-app.org/auth/token/login/"
  30. data = {
  31. "password":password,
  32. "phone_number":login
  33. }
  34. response = requests.post(url,json=data)
  35. result = response.json()
  36. return result['auth_token']
  37. #Функция обращения к апи и получение токена
  38. def msg_to_text(jsonstring):
  39. jsonObj = json.loads(jsonstring)
  40. u = jsonObj
  41. return u
  42. def createUser(username, phone, password, url) -> str:
  43. data = {
  44. "username": username,
  45. "phone_number": phone,
  46. "password": password
  47. }
  48. response = requests.post(url, json=data)
  49. result = response.json()
  50. return result
  51. #Функция обращения к апи и получение списка пользователей
  52. def requestGetListUser():
  53. auth_token=requestGetToken()
  54. print(auth_token)
  55. headers = {'Authorization': f'Token {auth_token}'}
  56. url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  57. response = requests.get(url, headers=headers)
  58. print(response)
  59. print(response.json())
  60. # def requestGetList(auth_token, url):
  61. # headers = {'Authorization': f'Token {auth_token}'}
  62. # response = requests.get(url, headers=headers)
  63. # #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  64. # ordertext=response.json()
  65. # startlist = []
  66. # if not ordertext:
  67. # print("Empty array")
  68. # else:
  69. # for i in ordertext:
  70. # print (i, "PROCESSING")
  71. # try:
  72. # orderticket = OrderTicket(**i)
  73. # print(i, "ORDER ", orderticket)
  74. # startlist.append(orderticket)
  75. # except Exception as ex:
  76. # print(ex)
  77. # print (startlist)
  78. # return startlist
  79. def requestGetList(auth_token, url):
  80. headers = {'Authorization': f'Token {auth_token}'}
  81. response = requests.get(url, headers=headers)
  82. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  83. ordertext=response.json()
  84. startlist = []
  85. if not ordertext:
  86. print("Empty array")
  87. else:
  88. for i in ordertext:
  89. print (i, "PROCESSING")
  90. try:
  91. order = Order(**i)
  92. print(i, "ORDER ", order)
  93. startlist.append(order)
  94. except Exception as ex:
  95. print(ex)
  96. print (startlist)
  97. return startlist
  98. class User(object):
  99. def __init__(self,
  100. group_name,
  101. last_login,
  102. is_superuser,
  103. username,
  104. is_staff,
  105. is_active,
  106. date_joined,
  107. middle_name,
  108. phone_number,
  109. email,
  110. avatar,
  111. groups,
  112. user_permissions,
  113. full_name="",
  114. last_name="",
  115. first_name="",
  116. ):
  117. self.full_name = full_name
  118. self.group_name = group_name
  119. self.last_login = last_login
  120. self.is_superuser = is_superuser
  121. self.username = username
  122. self.is_staff = is_staff
  123. self.is_active = is_active
  124. self.date_joined = date_joined
  125. self.last_name = last_name
  126. self.first_name = first_name
  127. self.middle_name = middle_name
  128. self.phone_number = phone_number
  129. self.email = email
  130. self.avatar = avatar
  131. self.groups = groups
  132. self.user_permissions = user_permissions
  133. def __str__(self):
  134. return ', '.join([
  135. str(self.full_name),
  136. str(self.group_name),
  137. str(self.last_login),
  138. str(self.is_superuser),
  139. str(self.username),
  140. str(self.is_staff),
  141. str(self.is_active),
  142. str(self.date_joined),
  143. str(self.last_name),
  144. str(self.first_name),
  145. str(self.middle_name),
  146. str(self.phone_number),
  147. str(self.email),
  148. str(self.avatar),
  149. str(self.groups),
  150. str(self.user_permissions)
  151. ]) + '\n'
  152. def requestGetUser(auth_token, url):
  153. headers = {'Authorization': f'Token {auth_token}'}
  154. response = requests.get(url, headers=headers)
  155. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  156. usertext=response.json()
  157. print(usertext)
  158. user = None
  159. if not usertext:
  160. print("Empty user")
  161. None
  162. else:
  163. try:
  164. user = User(**usertext)
  165. except Exception as ex:
  166. print(ex)
  167. print (user)
  168. return user
  169. def requestGetTicket_user(auth_token, url):
  170. headers = {'Authorization': f'Token {auth_token}'}
  171. response = requests.get(url, headers=headers)
  172. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  173. tickettext=response.json()
  174. startlist = []
  175. if not tickettext:
  176. print("Empty array")
  177. else:
  178. for i in tickettext:
  179. try:
  180. ticket = Ticket_user(**i)
  181. startlist.append(ticket)
  182. except Exception as ex:
  183. print(ex)
  184. print (startlist)
  185. return startlist
  186. def requestPatchTicketUser(token,url, data):
  187. headers = {'Authorization': f'Token {token}'}
  188. response = requests.patch(url, json=data, headers=headers)
  189. return response
  190. def change_groups(auth_token, url,group):
  191. headers = {
  192. 'Content-Type': 'application/json',
  193. 'Authorization': f'Token {auth_token}'
  194. }
  195. response = requests.get(url, headers=headers)
  196. user_data = response.json()
  197. current_groups = user_data.get('groups', [])
  198. updated_groups = list(set([group]+current_groups))
  199. data = {'groups': updated_groups}
  200. print(data)
  201. response = requests.put(url, json=data, headers=headers)
  202. print(url)
  203. print(user_data)
  204. return response
  205. def change_status_company(auth_token, url,id_metaservice):
  206. headers = {
  207. 'Content-Type': 'application/json',
  208. 'Authorization': f'Token {auth_token}'
  209. }
  210. response = requests.get(url, headers=headers)
  211. user_data = response.json()
  212. print(user_data)
  213. current_metaservice = user_data.get('id_metaservice', [])
  214. repsonse = requests.put(config.API_URL+f"dbsynce/api/company/{id_metaservice}", json=data, headers=headers)
  215. return response
  216. def to_answer(jsonstring):
  217. json_answer = json.loads(jsonstring)
  218. print(json_answer)
  219. u = Answer(**json_answer)
  220. return u
  221. def change_to_task(localtask,token):
  222. headers = {
  223. 'Authorization': f'Token {token}',
  224. 'Content-Type': 'application/json' # Установка заголовка Content-Type
  225. }
  226. data = json.dumps(localtask.__dict__)
  227. response = requests.patch(config.API_URL+f"/dbsynce/api/orders/{localtask.id}/", headers=headers, data=data)
  228. return response