import json import requests import config import ast from classes.Answer import Answer from classes.Order import Order from classes.OrderTicket import OrderTicket from classes.Ticket_user import Ticket_user from classes.Ticket import Ticket #Функция преобразования json в объект класса Order def jsonToOrder(jsonstring) -> Order: jsonObj = json.loads(jsonstring) print(jsonObj) u = Order(**jsonObj) return u #Функция преобразования json в объект класса OrderTicket def jsonToOrderTicket(jsonstring) -> OrderTicket: print (jsonstring) jsonObj = json.loads(jsonstring) print(jsonObj) u = OrderTicket(**jsonObj) return u #Функция обращения к апи и получение токена def requestGetToken(login, password, url) -> str: login = "79999999999" password = "12345" # login = "11111111111" # password = "Sharix!" #url = "https://user.sharix-app.org/auth/token/login/" data = { "password":password, "phone_number":login } response = requests.post(url,json=data) result = response.json() return result['auth_token'] #Функция обращения к апи и получение токена def msg_to_text(jsonstring): jsonObj = json.loads(jsonstring) u = jsonObj return u def createUser(username, phone, password, url) -> str: data = { "username": username, "phone_number": phone, "password": password } response = requests.post(url, json=data) result = response.json() return result #Функция обращения к апи и получение списка пользователей def requestGetListUser(): auth_token=requestGetToken() print(auth_token) headers = {'Authorization': f'Token {auth_token}'} url = 'https://user.sharix-app.org/platform/api/sharix-users/' response = requests.get(url, headers=headers) print(response) print(response.json()) # def requestGetList(auth_token, url): # headers = {'Authorization': f'Token {auth_token}'} # response = requests.get(url, headers=headers) # #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers) # ordertext=response.json() # startlist = [] # if not ordertext: # print("Empty array") # else: # for i in ordertext: # print (i, "PROCESSING") # try: # orderticket = OrderTicket(**i) # print(i, "ORDER ", orderticket) # startlist.append(orderticket) # except Exception as ex: # print(ex) # print (startlist) # return startlist def requestGetList(auth_token, url): headers = {'Authorization': f'Token {auth_token}'} response = requests.get(url, headers=headers) #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers) ordertext=response.json() startlist = [] if not ordertext: print("Empty array") else: for i in ordertext: print (i, "PROCESSING") try: order = Order(**i) print(i, "ORDER ", order) startlist.append(order) except Exception as ex: print(ex) print (startlist) return startlist class User(object): def __init__(self, group_name, last_login, is_superuser, username, is_staff, is_active, date_joined, middle_name, phone_number, email, avatar, groups, user_permissions, full_name="", last_name="", first_name="", ): self.full_name = full_name self.group_name = group_name self.last_login = last_login self.is_superuser = is_superuser self.username = username self.is_staff = is_staff self.is_active = is_active self.date_joined = date_joined self.last_name = last_name self.first_name = first_name self.middle_name = middle_name self.phone_number = phone_number self.email = email self.avatar = avatar self.groups = groups self.user_permissions = user_permissions def __str__(self): return ', '.join([ str(self.full_name), str(self.group_name), str(self.last_login), str(self.is_superuser), str(self.username), str(self.is_staff), str(self.is_active), str(self.date_joined), str(self.last_name), str(self.first_name), str(self.middle_name), str(self.phone_number), str(self.email), str(self.avatar), str(self.groups), str(self.user_permissions) ]) + '\n' def requestGetUser(auth_token, url): headers = {'Authorization': f'Token {auth_token}'} response = requests.get(url, headers=headers) #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers) usertext=response.json() print(usertext) user = None if not usertext: print("Empty user") None else: try: user = User(**usertext) except Exception as ex: print(ex) print (user) return user def requestGetTicket_user(auth_token, url): headers = {'Authorization': f'Token {auth_token}'} response = requests.get(url, headers=headers) #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers) tickettext=response.json() startlist = [] if not tickettext: print("Empty array") else: for i in tickettext: try: ticket = Ticket_user(**i) startlist.append(ticket) except Exception as ex: print(ex) print (startlist) return startlist def requestPatchTicketUser(token,url, data): headers = {'Authorization': f'Token {token}'} response = requests.patch(url, json=data, headers=headers) return response def change_groups(auth_token, url,group): headers = { 'Content-Type': 'application/json', 'Authorization': f'Token {auth_token}' } response = requests.get(url, headers=headers) user_data = response.json() current_groups = user_data.get('groups', []) updated_groups = list(set([group]+current_groups)) data = {'groups': updated_groups} print(data) response = requests.put(url, json=data, headers=headers) print(url) print(user_data) return response def change_status_company(auth_token, url,id_metaservice): headers = { 'Content-Type': 'application/json', 'Authorization': f'Token {auth_token}' } response = requests.get(url, headers=headers) user_data = response.json() print(user_data) current_metaservice = user_data.get('id_metaservice', []) repsonse = requests.put(config.API_URL+f"dbsynce/api/company/{id_metaservice}", json=data, headers=headers) return response def to_answer(jsonstring): json_answer = json.loads(jsonstring) print(json_answer) u = Answer(**json_answer) return u def change_to_task(localtask,token): headers = { 'Authorization': f'Token {token}', 'Content-Type': 'application/json' # Установка заголовка Content-Type } data = json.dumps(localtask.__dict__) response = requests.patch(config.API_URL+f"/dbsynce/api/orders/{localtask.id}/", headers=headers, data=data) return response