Skip to content

Commit 450a428

Browse files
committed
[ADDED] Orders creations without geo-coordinates and Routes creations with order_id or contact_id
1 parent e9afa78 commit 450a428

File tree

4 files changed

+133
-16
lines changed

4 files changed

+133
-16
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# -*- coding: utf-8 -*-
2+
# codebeat:disable[SIMILARITY, LOC, ABC]
3+
4+
import argparse
5+
6+
from route4me import Route4Me
7+
8+
from route4me.constants import (
9+
ALGORITHM_TYPE,
10+
OPTIMIZE,
11+
DEVICE_TYPE,
12+
TRAVEL_MODE,
13+
DISTANCE_UNIT,
14+
)
15+
16+
17+
def main(api_key):
18+
route4me = Route4Me(api_key)
19+
20+
optimization = route4me.optimization
21+
address = route4me.address
22+
optimization.algorithm_type(ALGORITHM_TYPE.CVRP_TW_SD)
23+
optimization.share_route(0)
24+
optimization.store_route(0)
25+
optimization.route_time(0)
26+
optimization.rt(1)
27+
optimization.route_max_duration(86400)
28+
optimization.vehicle_max_distance_mi(10000)
29+
optimization.route_name('Single Depot Round Trip Orders Based')
30+
optimization.optimize(OPTIMIZE.TIME)
31+
optimization.distance_unit(DISTANCE_UNIT.MI)
32+
optimization.device_type(DEVICE_TYPE.WEB)
33+
optimization.travel_mode(TRAVEL_MODE.DRIVING)
34+
35+
order_manager = route4me.order
36+
37+
# Creating some Orders
38+
39+
orders = [
40+
{
41+
"address_1": "754 5th Ave New York, NY 10019, USA",
42+
"address_stop_type": "DELIVERY",
43+
"day_scheduled_for_YYMMDD": "2022-12-24",
44+
"EXT_FIELD_cost": 5,
45+
"EXT_FIELD_pieces": 1,
46+
"EXT_FIELD_custom_data":
47+
{
48+
"attached_barcode": "ORD0002ABC12301",
49+
}
50+
},
51+
{
52+
"address_1": "717 5th Ave New York, NY 10022",
53+
"address_stop_type": "DELIVERY",
54+
"day_scheduled_for_YYMMDD": "2022-12-24",
55+
"EXT_FIELD_cost": 1,
56+
"EXT_FIELD_pieces": 1,
57+
"EXT_FIELD_custom_data":
58+
{
59+
"attached_barcode": "ORD0002ABC12302",
60+
}
61+
},
62+
{
63+
"address_1": "888 Madison Ave New York, NY 10014",
64+
"address_stop_type": "DELIVERY",
65+
"day_scheduled_for_YYMMDD": "2022-12-24",
66+
"EXT_FIELD_cost": 2,
67+
"EXT_FIELD_pieces": 1,
68+
"EXT_FIELD_custom_data":
69+
{
70+
"attached_barcode": "ORD0002ABC12303",
71+
}
72+
},
73+
{
74+
"address_1": "1011 Madison Ave New York, NY 10075",
75+
"address_stop_type": "DELIVERY",
76+
"day_scheduled_for_YYMMDD": "2022-12-24",
77+
"EXT_FIELD_cost": 6,
78+
"EXT_FIELD_pieces": 1,
79+
"EXT_FIELD_custom_data":
80+
{
81+
"attached_barcode": "ORD0002ABC12304",
82+
}
83+
},
84+
{
85+
"address_1": "440 Columbus Ave New York, NY 10024",
86+
"address_stop_type": "DELIVERY",
87+
"day_scheduled_for_YYMMDD": "2022-12-24",
88+
"EXT_FIELD_cost": 3,
89+
"EXT_FIELD_pieces": 1,
90+
"EXT_FIELD_custom_data":
91+
{
92+
"attached_barcode": "ORD0002ABC12305",
93+
}
94+
},
95+
96+
]
97+
98+
for o in orders:
99+
new_order = order_manager.create_order(**o)
100+
address.add_address(order_id=new_order.get('order_id'))
101+
102+
# Adding Depot
103+
104+
address.add_address(
105+
address='57 W 57th St New York, NY 10019',
106+
lat=40.7558695,
107+
lng=-73.9862019,
108+
alias='Verizon Wireless',
109+
time=0,
110+
is_depot=True
111+
)
112+
113+
response = route4me.run_optimization()
114+
print('Optimization Link: {}'.format(response['links']['view']))
115+
for i, route in enumerate(response['routes']):
116+
print('\t{0}\tRoute Link: {1}'.format(i + 1, route['links']['route']))
117+
for address in route['addresses']:
118+
print('\t\t\tAddress: {0}'.format(address['address']))
119+
120+
121+
if __name__ == '__main__':
122+
parser = argparse.ArgumentParser(description='Single Driver Round Trip')
123+
parser.add_argument('--api_key', dest='api_key', help='Route4Me API KEY',
124+
type=str, required=True)
125+
args = parser.parse_args()
126+
main(args.api_key)
127+
128+
# codebeat:enable[SIMILARITY, LOC, ABC]

route4me/address.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ def add_address(self, **kwargs):
6363
:param kwargs:
6464
:return:
6565
"""
66-
if self.check_required_params(kwargs, self.REQUIRED_FIELDS):
66+
if self.check_required_params(kwargs, self.REQUIRED_FIELDS) or \
67+
self.check_required_params(kwargs, ["order_id"]) or \
68+
self.check_required_params(kwargs, ["contact_id"]):
6769
self.addresses.append(kwargs)
6870
self.api.optimization.data['addresses'] = self.addresses
6971
else:

route4me/api.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import requests
55
import json
66

7-
try:
8-
from urllib import urlencode
9-
except ImportError:
10-
from urllib.parse import urlencode
117
from .activity_feed import ActivityFeed
128
from .address import Address
139
from .address_book import AddressBook
@@ -76,21 +72,12 @@ def _make_request(self, url, params, data, request_method):
7672
:raise: APIException
7773
"""
7874
params['api_key'] = self.key
79-
request_params = self._transform_params(params)
80-
response = request_method(url, request_params, data)
75+
response = request_method(url, params, data)
8176
if not 200 <= response.status_code < 400:
8277
raise APIException(response.status_code, response.text,
8378
response.url)
8479
return response
8580

86-
def _transform_params(self, params):
87-
"""
88-
Convert params dict to url params
89-
:param params:
90-
:return:
91-
"""
92-
return urlencode(params)
93-
9481
def get(self, request_method):
9582
"""
9683
Execute optimization

route4me/orders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Order(Base):
1212
Orders are transactional events.
1313
"""
1414

15-
REQUIRED_FIELDS = ('address_1', 'cached_lat', 'cached_lng',)
15+
REQUIRED_FIELDS = ('address_1', )
1616

1717
def __init__(self, api):
1818
"""

0 commit comments

Comments
 (0)