123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- import json
- import requests
- import config
- import ast
- #Тестовый класс User и Address (ВАЖНО!!!) поля классов должны называться также как и поля json объекта
- '''
- #Full json order example
- https://wiki.sharix-app.org/doku.php/open/tech/dev/sharix_open_-_primery_json
- {
- "ticket": 123,
- "status": 111,
- "priority": 1,
- "title": "123",
- "note": "123",
- "due_date": 2023-05-15T17:16:22.955601Z,
- "id_metaservice": 1,
- "is_global": false,
- "is_visible": false,
- "service": 1,
- "service_type": 1,
- "status_changed_date": "2023-05-15T17:16:22.955601Z",
- "time_placed": "2023-05-15T16:40:32.179777Z",
- "time_start": "2023-05-15T16:40:32.179777Z",
- "time_start_predicted": "2023-05-15T16:40:32.179777Z",
- "time_finish_predicted": "2023-05-15T16:40:32.179777Z",
- "time_start_real": "2023-05-15T16:40:32.179777Z",
- "time_finish_real": "2023-05-15T16:40:32.179777Z",
- "order_place_type": "address",
- "order_place_start": "р-н Беговой",
- "order_place_start_gps": "55.751941, 37.607513",
- "order_place_start_sector": 4,
- "order_place_start_real": "р-н Беговой",
- "order_place_start_real_gps": "55.751941, 37.607513",
- "order_place_finish_predicted": "р-н Мещанский",
- "order_place_finish_predicted_gps": "55.751941, 37.607513",
- "order_place_finish_sector": 4,
- "order_place_finish_real": "р-н Мещанский",
- "order_place_finish_real_gps": "55.751941, 37.607513",
- "predicted_price": 1000,
- "real_price": 1001,
- "payment_transaction_id": 1,
- "rating_id": 1,
- "provider": 1,
- "provider_screen_name": "Vasya Ispolnitelev",
- "receiver": 1,
- "receiver_screen_name": "Marfa Clientovna",
- "client_id": 1,
- "client_screen_name": "Fedor Client",
- "resource_id": 1,
- "attempts": null
- }
- '''
- #надо адаптировать класс к новому формату JSON
- # class Order(object):
- # 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):
- # self.order_synced = order_synced
- # self.id = id
- # self.ticket = ticket
- # self.status = status
- # self.title = title
- # self.note = note
- # self.due_date = due_date
- # self.id_metaservice = id_metaservice
- # self.is_global = is_global
- # self.is_visible = is_visible
- # self.service = service
- # self.service_type = service_type
- # self.status_changed_date = status_changed_date
- # self.time_created = time_created
- # self.time_placed = time_placed
- # self.time_start = time_start
- # self.time_start_predicted = time_start_predicted
- # self.time_finish_predicted = time_finish_predicted
- # self.time_start_real = time_start_real
- # self.time_finish_real = time_finish_real
- # self.order_place_type =order_place_type
- # self.order_place_start = order_place_start
- # self.order_place_start_gps = order_place_start_gps
- # self.order_place_start_real = order_place_start_real
- # self.order_place_start_real_gps = order_place_start_real_gps
- # self.order_place_finish_predicted = order_place_finish_predicted
- # self.order_place_finish_predicted_gps = order_place_finish_predicted_gps
- # self.order_place_finish_real = order_place_finish_real
- # self.order_place_finish_real_gps = order_place_finish_real_gps
- # self.predicted_price = predicted_price
- # self.real_price = real_price
- # self.payment_transaction_id = payment_transaction_id
- # self.rating_id = rating_id
- # self.provider = provider
- # self.provider_screen_name = provider_screen_name
- # self.receiver = receiver
- # self.receiver_screen_name = receiver_screen_name
- # self.client_id = client_id
- # self.resource_id = resource_id
- # self.attempts = attempts
- # self.asap = asap
- # #доделать до конца по аналогии с закоммиченным json
- # def __str__(self):
- # 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)
- #\n добавляете в местах с пустой строкой в шаблоне
-
- class Order(object):
- 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):
- self.id = id
- self.ticket = ticket
- self.status = status
- self.title = title
- self.note = note
- self.id_metaservice = id_metaservice
- self.is_global = is_global
- self.is_visible = is_visible
- self.service = service
- self.service_type = service_type
- self.time_created = time_created
- self.time_placed = time_placed
- self.time_start = time_start
- self.time_start_predicted = time_start_predicted
- self.time_finish_predicted = time_finish_predicted
- self.time_start_real = time_start_real
- self.time_finish_real = time_finish_real
- self.order_place_type =order_place_type
- self.order_place_start = order_place_start
- self.order_place_start_gps = order_place_start_gps
- self.order_place_start_real = order_place_start_real
- self.order_place_start_real_gps = order_place_start_real_gps
- self.order_place_finish_predicted = order_place_finish_predicted
- self.order_place_finish_predicted_gps = order_place_finish_predicted_gps
- self.order_place_finish_real = order_place_finish_real
- self.order_place_finish_real_gps = order_place_finish_real_gps
- self.predicted_price = predicted_price
- self.real_price = real_price
- self.payment_transaction_id = payment_transaction_id
- self.rating_id = rating_id
- self.provider = provider
- self.provider_screen_name = provider_screen_name
- self.receiver = receiver
- self.receiver_screen_name = receiver_screen_name
- self.client_id = client_id
- self.resource_id = resource_id
- self.attempts = attempts
- self.asap = asap
- #доделать до конца по аналогии с закоммиченным json
- def __str__(self):
- 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)
- #ticket json example
- '''
- {
- "id": 2,
- "status": 111,
- "created_date": "2023-05-15",
- "status_changed_date": "2023-05-15T17:16:22.955601Z",
- "priority": 0,
- "title": "Test",
- "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}"
- "due_date": "2023-05-15",
- "task_list": 10,
- "created_by": 1,
- "type": 1,
- "assigned_to": 1
- }
- '''
- #ticket minimal example
- '''
- {
- "id": 2,
- "status": 111,
- "created_date": "2023-05-15",
- "status_changed_date": "2023-05-15T17:16:22.955601Z",
- "priority": 0,
- "title": null,
- "note": null,
- "due_date": null,
- "task_list": 10,
- "created_by": 1,
- "type": 1,
- "assigned_to": null
- }
- {
- "serviceType": 2,
- "driverSearchEngine": 0,
- "comment": "Yyyy",
- "arrivalTime": "2023-12-07T14:39:54Z",
- "additionalServices": [],
- "tasks": [
- {
- "text": "Eeee",
- "address": "Jjjj"
- }
- ],
- "spendingLimit": 6668,
- "isWithPaymentDocument": true,
- "id": 294830,
- "isProceedBeforeArrival": true
- }
- '''
- class OrderTicket(object):
- def __init__(self, serviceType, driverSearchEngine, comment, arrivalTime, additionalServices, tasks, spendingLimit, isWithPaymentDocument, id, isProceedBeforeArrival):
- self.serviceType = serviceType
- self.driverSearchEngine = driverSearchEngine
- self.comment = comment
- self.arrivalTime = arrivalTime
- self.additionalServices = additionalServices
- self.tasks = tasks
- self.spendingLimit = spendingLimit
- self.isWithPaymentDocument = isWithPaymentDocument
- self.id = id
- self.isProceedBeforeArrival = isProceedBeforeArrival
- # class OrderTicketOLD(object):
- # 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):
- # self.id = id
- # self.status = status
- # self.created_date = created_date
- # self.status_changed_date = status_changed_date
- # self.priority = priority
- # self.title = title
- # self.note = note
- # self.due_date = due_date
- # self.task_list = task_list
- # self.created_by = created_by
- # self.type = type
- # self.assigned_to = assigned_to
- # def __str__(self):
- # 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)
- #Функция преобразования 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"
- #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 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 answer(object):
- def __init__(self,order_id,provider,answer):
- self.order_id = order_id
- self.provider = provider
- self.answer = answer
-
- 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__)
- print(data)
- response = requests.patch(config.API_URL+f"/dbsynce/api/orders/{localtask.id}/", headers=headers, data=data)
- return response
|