prodev skill: add price/vendor/product_type fields to DB entries
This commit is contained in:
parent
2df14f1694
commit
c556d5fecd
2 changed files with 63 additions and 24 deletions
|
|
@ -2,12 +2,13 @@
|
|||
"""Manage prodev expense entries in wip.agent_tasks on DocDB.
|
||||
|
||||
Usage:
|
||||
prodev_tasks.py create "<note>"
|
||||
prodev_tasks.py update <document_id> "<note>"
|
||||
prodev_tasks.py create "<note>" [--price PRICE] [--vendor VENDOR] [--product-type TYPE]
|
||||
prodev_tasks.py update <document_id> "<note>" [--price PRICE] [--vendor VENDOR] [--product-type TYPE]
|
||||
prodev_tasks.py list
|
||||
"""
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
from pymongo import MongoClient
|
||||
|
||||
URI = "mongodb://root:3wwfoUjyk2E2zWELXFlLuHqfw1ALlOp4pb2H5Vq3TImbMIHL2h1u8Jej2mjzCPl@docdb.connorrhodes.com:35563?tls=true&tlsAllowInvalidCertificates=true"
|
||||
|
|
@ -18,15 +19,29 @@ def get_collection():
|
|||
client = MongoClient(URI, serverSelectionTimeoutMS=10000)
|
||||
return client[DB][COLL], client
|
||||
|
||||
def create(note):
|
||||
def create(note, price=None, vendor=None, product_type=None):
|
||||
coll, client = get_collection()
|
||||
result = coll.insert_one({"note": note, "type": "prodev expense", "status": "todo"})
|
||||
doc = {"note": note, "type": "prodev expense", "status": "todo"}
|
||||
if price is not None:
|
||||
doc["price"] = float(price)
|
||||
if vendor is not None:
|
||||
doc["vendor"] = vendor
|
||||
if product_type is not None:
|
||||
doc["product_type"] = product_type
|
||||
result = coll.insert_one(doc)
|
||||
print(result.inserted_id)
|
||||
client.close()
|
||||
|
||||
def update(doc_id, note):
|
||||
def update(doc_id, note, price=None, vendor=None, product_type=None):
|
||||
coll, client = get_collection()
|
||||
result = coll.update_one({"_id": doc_id}, {"$set": {"note": note}})
|
||||
sets = {"note": note}
|
||||
if price is not None:
|
||||
sets["price"] = float(price)
|
||||
if vendor is not None:
|
||||
sets["vendor"] = vendor
|
||||
if product_type is not None:
|
||||
sets["product_type"] = product_type
|
||||
result = coll.update_one({"_id": doc_id}, {"$set": sets})
|
||||
print(f"matched={result.matched_count} modified={result.modified_count}")
|
||||
client.close()
|
||||
|
||||
|
|
@ -34,22 +49,40 @@ def list_all():
|
|||
from bson import ObjectId
|
||||
coll, client = get_collection()
|
||||
for doc in coll.find({"type": "prodev expense"}):
|
||||
print(f"{doc['_id']} | {doc['status']} | {doc['note'][:80]}...")
|
||||
price = doc.get("price", "?")
|
||||
vendor = doc.get("vendor", "?")
|
||||
ptype = doc.get("product_type", "?")
|
||||
print(f"{doc['_id']} | ${price} | {vendor} | {ptype} | {doc['status']} | {doc['note'][:60]}...")
|
||||
client.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print(__doc__)
|
||||
sys.exit(1)
|
||||
parser = argparse.ArgumentParser(description="Manage prodev expense entries")
|
||||
sub = parser.add_subparsers(dest="cmd")
|
||||
|
||||
cmd = sys.argv[1]
|
||||
if cmd == "create" and len(sys.argv) >= 3:
|
||||
create(sys.argv[2])
|
||||
elif cmd == "update" and len(sys.argv) >= 4:
|
||||
p_create = sub.add_parser("create")
|
||||
p_create.add_argument("note")
|
||||
p_create.add_argument("--price", default=None)
|
||||
p_create.add_argument("--vendor", default=None)
|
||||
p_create.add_argument("--product-type", default=None)
|
||||
|
||||
p_update = sub.add_parser("update")
|
||||
p_update.add_argument("doc_id")
|
||||
p_update.add_argument("note")
|
||||
p_update.add_argument("--price", default=None)
|
||||
p_update.add_argument("--vendor", default=None)
|
||||
p_update.add_argument("--product-type", default=None)
|
||||
|
||||
sub.add_parser("list")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.cmd == "create":
|
||||
create(args.note, args.price, args.vendor, args.product_type)
|
||||
elif args.cmd == "update":
|
||||
from bson import ObjectId
|
||||
update(ObjectId(sys.argv[2]), sys.argv[3])
|
||||
elif cmd == "list":
|
||||
update(ObjectId(args.doc_id), args.note, args.price, args.vendor, args.product_type)
|
||||
elif args.cmd == "list":
|
||||
list_all()
|
||||
else:
|
||||
print(__doc__)
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue