Skip to content

Commit 68fe27c

Browse files
authored
Merge pull request ggrammar#15 from wrighterase/master
Misc. changes and updated for Python3
2 parents 799788e + 192ddf2 commit 68fe27c

File tree

13 files changed

+77
-47
lines changed

13 files changed

+77
-47
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,6 @@ $RECYCLE.BIN/
182182

183183
# .nfs files are created when an open file is removed but is still being accessed
184184
.nfs*
185+
186+
# ignoring a temp workspace
187+
workspace.py

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ It's a port of [the pizzapi node.js module](https://github.com/RIAEvangelist/nod
88

99
First construct a `Customer` object and set the customer's address.
1010
```python
11-
>>> customer = Customer('Barack', 'Obama', '[email protected]', '2024561111')
12-
>>> customer.set_address('700 Pennsylvania Avenue NW', 'Washington', 'DC', '20408')
11+
>>> customer = Customer('Barack', 'Obama', '[email protected]', '2024561111', '700 Pennsylvania Avenue NW, Washington, DC, 20408')
12+
>>> address = Address(*customer.address.split(','))
1313
```
1414

1515
Then, find a store that will deliver to the address.
1616
```python
17-
>>> store = find_closest_store(customer.address)
17+
>>> store = address.closest_store()
1818
```
1919

2020
Create an `Order` object.
2121
```python
22-
>>> order = Order(store, customer)
22+
>>> order = Order(store, customer, address)
2323
```
2424

2525
In order to add items to your order, you'll need the items' product codes.
@@ -62,3 +62,11 @@ Or if you're just testing and don't want to actually order something, use `.pay_
6262
```python
6363
>>> order.pay_with(card)
6464
```
65+
```
66+
Notes:
67+
```
68+
The first steps seem unnecessary. It seems more efficient to initalize Customer info and Address info seperately.
69+
customer.address(...) has issues with the address coming across as a single string.
70+
If you .split(',') you can pass it to Address as
71+
```address = Address(*customer.address.split(','))``` to parse it properly.
72+
From there you can do```store = Address.closes_store(address)``` to get the nearest location.

pizzapi/__init__.py

100644100755
File mode changed.

pizzapi/address.py

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from store import Store
2-
from utils import request_json
3-
from urls import FIND_URL
1+
from .store import Store
2+
from .utils import request_json
3+
from .urls import FIND_URL
44

55
class Address(object):
6-
def __init__(self, street, city, region='', zip=''):
6+
def __init__(self, street, city, region='', zip='', *args):
77
self.street = street.strip()
88
self.city = city.strip()
99
self.region = region.strip()

pizzapi/coupon.py

100644100755
File mode changed.

pizzapi/customer.py

100644100755
File mode changed.

pizzapi/menu.py

100644100755
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from urls import MENU_URL
2-
from utils import request_json
1+
from __future__ import print_function
2+
from .urls import MENU_URL
3+
from .utils import request_json
34

45

56
# TODO: Get rid of this class
@@ -35,7 +36,7 @@ def __init__(self, data={}):
3536
self.products = self.parse_items(data['Products'])
3637
self.coupons = self.parse_items(data['Coupons'])
3738
self.preconfigured = self.parse_items(data['PreconfiguredProducts'])
38-
for key, value in data['Categorization'].iteritems():
39+
for key, value in data['Categorization'].items():
3940
self.root_categories[key] = self.build_categories(value)
4041

4142
@classmethod
@@ -60,7 +61,7 @@ def build_categories(self, category_data, parent=None):
6061

6162
def parse_items(self, parent_data):
6263
items = []
63-
for code in parent_data.iterkeys():
64+
for code in parent_data.keys():
6465
obj = MenuItem(parent_data[code])
6566
self.menu_by_code[obj.code] = obj
6667
items.append(obj)
@@ -71,30 +72,30 @@ def display(self):
7172
def print_category(category, depth=1):
7273
indent = " " * (depth + 1)
7374
if len(category.products) + len(category.subcategories) > 0:
74-
print indent + category.name
75+
print(indent + category.name)
7576
for subcategory in category.subcategories:
7677
print_category(subcategory, depth + 1)
7778
for product in category.products:
78-
print indent + " [%s]" % product.code, product.name
79-
print "************ Coupon Menu ************"
79+
print(indent + " [%s]" % product.code, product.name)
80+
print("************ Coupon Menu ************")
8081
print_category(self.root_categories['Coupons'])
81-
print "\n************ Preconfigured Menu ************"
82+
print("\n************ Preconfigured Menu ************")
8283
print_category(self.root_categories['PreconfiguredProducts'])
83-
print "\n************ Regular Menu ************"
84+
print("\n************ Regular Menu ************")
8485
print_category(self.root_categories['Food'])
8586

8687
# TODO: Find more pythonic way to format the menu
8788
# TODO: Format the menu after the variants have been filtered
8889
# TODO: Return the search results and print in different method
8990
# TODO: Import fuzzy search module or allow lists as search conditions
9091
def search(self, **conditions):
91-
max_len = lambda x: 2 + max(len(v[x]) for v in self.variants.values())
92-
for v in self.variants.itervalues():
92+
max_len = lambda x: 2 + max(len(v[x]) for v in list(self.variants.values()))
93+
for v in self.variants.values():
9394
v['Toppings'] = dict(x.split('=', 1) for x in v['Tags']['DefaultToppings'].split(',') if x)
94-
if all(y in e.get(x, '') for x, y in conditions.iteritems()):
95-
print v['Code'],
96-
print v['Name'],
97-
print '$' + v['Price'],
98-
print v['SizeCode'],
99-
print v['ProductCode'],
100-
print v['Toppings']
95+
if all(y in v.get(x, '') for x, y in conditions.items()):
96+
print(v['Code'], end=' ')
97+
print(v['Name'], end=' ')
98+
print('$' + v['Price'], end=' ')
99+
print(v['SizeCode'], end=' ')
100+
print(v['ProductCode'], end=' ')
101+
print(v['Toppings'])

pizzapi/order.py

100644100755
Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import requests
22

3-
from menu import Menu
4-
from urls import PRICE_URL, PLACE_URL, VALIDATE_URL
3+
from .menu import Menu
4+
from .urls import PRICE_URL, PLACE_URL, VALIDATE_URL
55

66

77
# TODO: Add add_coupon and remove_coupon methods
88
class Order(object):
9-
def __init__(self, store, customer):
9+
def __init__(self, store, customer, address):
1010
self.store = store
1111
self.menu = Menu.from_store(store_id=store.id)
1212
self.customer = customer
13+
self.address = address
1314
self.data = {
15+
'Address': {'Street': self.address.street,
16+
'City': self.address.city,
17+
'Region': self.address.region,
18+
'PostalCode': self.address.zip,
19+
'Type': 'House'},
1420
'Coupons': [], 'CustomerID': '', 'Extension': '',
1521
'OrderChannel': 'OLO', 'OrderID': '', 'NoCombine': True,
1622
'OrderMethod': 'Web', 'OrderTaker': None, 'Payments': [],
@@ -20,7 +26,7 @@ def __init__(self, store, customer):
2026
'Partners': {}, 'NewUser': True, 'metaData': {}, 'Amounts': {},
2127
'BusinessDate': '', 'EstimatedWaitMinutes': '',
2228
'PriceOrderTime': '', 'AmountsBreakdown': {}
23-
}
29+
}
2430

2531
# TODO: Implement item options
2632
# TODO: Add exception handling for KeyErrors
@@ -35,14 +41,25 @@ def remove_item(self, code):
3541
codes = [x['Code'] for x in self.data['Products']]
3642
return self.data['Products'].pop(codes.index(code))
3743

44+
def add_coupon(self, code, qty=1):
45+
item = self.menu.variants[code]
46+
item.update(ID=1, isNew=True, Qty=qty, AutoRemove=False)
47+
self.data['Coupons'].append(item)
48+
return item
49+
50+
def remove_coupon(self, code):
51+
codes = [x['Code'] for x in self.data['Coupons']]
52+
return self.data['Coupons'].pop(codes.index(code))
53+
3854
def _send(self, url, merge):
3955
self.data.update(
4056
StoreID=self.store.id,
4157
Email=self.customer.email,
4258
FirstName=self.customer.first_name,
4359
LastName=self.customer.last_name,
4460
Phone=self.customer.phone,
45-
Address=self.customer.address.data
61+
#Address=self.address.street
62+
4663
)
4764

4865
for key in ('Products', 'StoreID', 'Address'):
@@ -53,14 +70,15 @@ def _send(self, url, merge):
5370
'Referer': 'https://order.dominos.com/en/pages/order/',
5471
'Content-Type': 'application/json'
5572
}
73+
5674
r = requests.post(url=url, headers=headers, json={'Order': self.data})
5775
r.raise_for_status()
5876
json_data = r.json()
77+
5978
if merge:
60-
for key, value in json_data['Order'].iteritems():
79+
for key, value in json_data['Order'].items():
6180
if value or not isinstance(value, list):
6281
self.data[key] = value
63-
6482
return json_data
6583

6684
# TODO: Figure out if this validates anything that PRICE_URL does not
@@ -79,18 +97,18 @@ def pay_with(self, card):
7997
"""Use this instead of self.place when testing"""
8098
# get the price to check that everything worked okay
8199
response = self._send(PRICE_URL, True)
100+
82101
if response['Status'] == -1:
83102
raise Exception('get price failed: %r' % response)
84-
85103
self.credit_card = card
86104
self.data['Payments'] = [
87105
{
88106
'Type': 'CreditCard',
89-
'Experiation': self.card.expiration,
107+
'Expiration': self.credit_card.expiration,
90108
'Amount': self.data['Amounts'].get('Customer', 0),
91-
'CardType': self.card.card_type,
92-
'Number': self.card.number,
93-
'SecurityCode': self.card.cvv,
94-
'PostalCode': self.card.zip
109+
'CardType': self.credit_card.card_type,
110+
'Number': int(self.credit_card.number),
111+
'SecurityCode': int(self.credit_card.cvv),
112+
'PostalCode': int(self.credit_card.zip)
95113
}
96114
]

pizzapi/payment.py

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ def find_type(self):
2424
'DISCOVER': r'^6(?:011|5[0-9]{2})[0-9]{12}$',
2525
'JCB': r'^(?:2131|1800|35\d{3})\d{11}$',
2626
'ENROUTE': r'^(?:2014|2149)\d{11}$'}
27-
return next((card_type for card_type, pattern in patterns.items()
27+
return next((card_type for card_type, pattern in list(patterns.items())
2828
if re.match(pattern, self.number)), '')

pizzapi/store.py

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from menu import Menu
2-
from urls import INFO_URL, MENU_URL
3-
from utils import request_json
1+
from .menu import Menu
2+
from .urls import INFO_URL, MENU_URL
3+
from .utils import request_json
44

55

66
class Store(object):
77
def __init__(self, data={}):
8-
self.id = int(data.get('StoreID', -1))
8+
self.id = str(data.get('StoreID', -1))
99
self.data = data
1010

1111
def get_details(self):

pizzapi/track.py

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from urls import TRACK_BY_PHONE, TRACK_BY_ORDER
2-
from utils import request_xml, request_json
1+
from .urls import TRACK_BY_PHONE, TRACK_BY_ORDER
2+
from .utils import request_xml, request_json
33

44

55
def track_by_phone(phone):

pizzapi/urls.py

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
TRACK_BY_ORDER = 'https://trkweb.dominos.com/orderstorage/GetTrackerData?StoreID={store_id}&OrderKey={order_key}'
77
TRACK_BY_PHONE = 'https://trkweb.dominos.com/orderstorage/GetTrackerData?Phone={phone}'
88
VALIDATE_URL = 'https://order.dominos.com/power/validate-order'
9+
COUPON_URL = 'https://order.dominos.com/power/store/{store_id}/coupon/{couponid}?lang={lang}'

pizzapi/utils.py

100644100755
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import requests
22
import xmltodict
33

4-
54
# TODO: Find out why this occasionally hangs
65
def request_json(url, **kwargs):
76
r = requests.get(url.format(**kwargs))

0 commit comments

Comments
 (0)