Skip to content

Commit

Permalink
Modify existing drinks.
Browse files Browse the repository at this point in the history
  • Loading branch information
YtvwlD committed Sep 11, 2017
1 parent c44ca80 commit d1e8ac3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ $ metecli drinks show Mate
| active? | yes |
+-------------+--------+
```

### modify a drink

```
$ metecli drinks modify Mate
name [Mate]:
price [1.5]:
bottle size [0.5]:
caffeine [None]: 100
active? [yes]:
```
5 changes: 5 additions & 0 deletions metecli/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def drinks(self):
r = requests.get(urljoin(self._base_url, "/drinks.json"))
return r.json()

def modify_drink(self, drink):
"""Modifys an existing drink."""
r = requests.patch(urljoin(self._base_url, "/drinks/{}.json").format(drink["id"]), json={"drink": drink})
assert r.ok

def barcodes(self):
"""Lists all barcodes."""
r = requests.get(urljoin(self._base_url, "/barcodes.json"))
Expand Down
22 changes: 21 additions & 1 deletion metecli/drinks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from .connection import Connection
from .utils import true_false_to_yes_no, fuzzy_search, print_table
from .utils import true_false_to_yes_no, fuzzy_search, print_table, show_edit

import logging
log = logging.getLogger(__name__)

def setup_cmdline(global_subparsers):
parser = global_subparsers.add_parser("drinks", help="show or modify drinks")
Expand All @@ -9,6 +12,9 @@ def setup_cmdline(global_subparsers):
parser_show = subparsers.add_parser("show", help="display detailed information about a drink")
parser_show.add_argument("drink", help="the drink to display")
parser_show.set_defaults(func=show)
parser_modify = subparsers.add_parser("modify", help="edits a drink")
parser_modify.add_argument("drink", help="the drink to modify")
parser_modify.set_defaults(func=modify)
parser.set_defaults(func=list_drinks)

def list_drinks(args, config):
Expand Down Expand Up @@ -47,3 +53,17 @@ def show(args, config):
["caffeine", drink["caffeine"]],
["active?", true_false_to_yes_no(drink["active"])],
])

def modify(args, config):
conn = Connection(base_url=config.settings["connection"]["base_url"])
data = fuzzy_search(conn.drinks(), args.drink)
if not data:
return
log.debug("Editing drink. Old data: %s", data)
show_edit(data, "name", "name", str)
show_edit(data, "price", "price", float)
show_edit(data, "bottle_size", "bottle size", float)
show_edit(data, "caffeine", "caffeine", int)
show_edit(data, "active", "active?", bool)
log.info("Editing drink. New data: %s", data)
conn.modify_drink(data)

0 comments on commit d1e8ac3

Please sign in to comment.