Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def run_command(command):
sys.exit(1)


####################################### Quantity tracking
print "Would you like oscar to track quantities of scanned items?"
print "If yes, every time an item is scanned Trello will increase a quantity displayed next to the item's name."
track_item_quantity = raw_input('y/n: ')
while track_item_quantity not in ['y', 'n']:
track_item_quantity = raw_input("Please input 'y' or 'n': ")
# Convert to boolean
track_item_quantity = {'y': True, 'n': False}[track_item_quantity]


######################################## Digit-Eyes
print "You need accounts with a few APIs to use Oscar. First of all,"
print "go to"
Expand Down Expand Up @@ -201,6 +211,8 @@ def run_command(command):

digiteyes_app_key: '{digiteyes_app_key}'
digiteyes_auth_key: '{digiteyes_auth_key}'

track_item_quantity: {track_item_quantity}
'''.format(**locals()))
oscar_yaml.close()

Expand Down
70 changes: 61 additions & 9 deletions scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,75 @@ def match_description_rule(trello_db, desc):
return r
return None


def matching_card(item, cards):
"""Return first card whose name matches item.
>>> cards = [{'name': 'random', 'id':1}, {'name': 'test', 'id': 2}, {'name': 'test', 'id': 3}]
>>> matching_card('test', cards)
{'name': 'test', 'id': 2}
"""
card = None
matches = filter(lambda c: c['name'] == item, cards)
if len(matches) > 0:
card = matches[0]
return card

def add_quantity(card_name, qty):
""" Return name with quantity.
>>> add_quantity('name', 2)
'name (2)'
"""
return card_name + " (%i)" % qty

def remove_quantity(card_name):
"""Return name without quantity.
>>> remove_quantity('name (2)')
'name'
"""
return card_name.split(' (')[0].strip()

def item_quantity(card_name):
"""Return quantity in card_name.
>>> item_quantity('name (2)')
2
>>> item_quantity('name')
1
"""
qty = 1
if card_name.find(' (') != -1:
qty = int(card_name.split('(')[1].split(')')[0])
return qty

def add_item_to_list(item):
print "Adding '{0}' to grocery list".format(item)
trello_api.lists.new_card(grocery_list['id'], item)

def add_grocery_item(trello_api, item):
"""Adds the given item to the grocery list (if it's not already present)."""
# Get the current grocery list
grocery_board_id = conf.get()['trello_grocery_board']
all_lists = trello_api.boards.get_list(grocery_board_id)
grocery_list = [x for x in all_lists if x['name'] == conf.get()['trello_grocery_list']][0]
cards = trello_api.lists.get_card(grocery_list['id'])
card_names = [card['name'] for card in cards]

# Add item if it's not there already
if item not in card_names:
print "Adding '{0}' to grocery list".format(item)
trello_api.lists.new_card(grocery_list['id'], item)
card_names = [remove_quantity(card['name']) for card in cards]

if bool(conf.get()['track_item_quantity']):
# Include item quantity in item name if oscar is configd
mcard = matching_card(item, cards)
if mcard is not None:
# Update existing item's quantity
qty = item_quantity(mcard['name'])
name_w_qty = add_quantity(item, qty + 1)
trello_api.cards.update_name(mcard['id'], name_w_qty)
else:
# Add new item with qty 1
name_w_qty = add_quantity(item, 1)
add_item_to_list(name_w_qty)
else:
print "Item '{0}' is already on the grocery list; not adding".format(item)

# Add item if it's not there already
if item not in card_names:
add_item_to_list(item)
else:
print "Item '{0}' is already on the grocery list; not adding".format(item)

trello_api = trello.TrelloApi(conf.get()['trello_app_key'])
trello_api.set_token(conf.get()['trello_token'])
Expand Down