feat: added chore
This commit is contained in:
parent
fa936a96b9
commit
0e992aaab9
@ -97,3 +97,12 @@ def recipe(ctx):
|
||||
receipe = Recipe(**cfg)
|
||||
recipes = receipe.get_list()
|
||||
click.echo(recipes)
|
||||
|
||||
@main.command()
|
||||
@click.pass_context
|
||||
def chore(ctx):
|
||||
cfg = ctx.obj['cfg']
|
||||
|
||||
chore = Chore(**cfg)
|
||||
chores = chore.get_list()
|
||||
click.echo(chores)
|
||||
|
@ -1,3 +1,4 @@
|
||||
from grocy.commands.stock import Stock
|
||||
from grocy.commands.recipe import Recipe
|
||||
from grocy.commands.chore import Chore
|
||||
from grocy.commands.shopping import Shopping
|
||||
|
64
grocy/commands/chore.py
Normal file
64
grocy/commands/chore.py
Normal file
@ -0,0 +1,64 @@
|
||||
import re
|
||||
from datetime import datetime
|
||||
from grocy import RestService
|
||||
from tabulate import tabulate
|
||||
from os import path
|
||||
|
||||
class Chore(object):
|
||||
GET_CURRENT_CHORES = '/chores/get-current'
|
||||
GET_CHORE_BY_ID = '/get-object/chores/{0}'
|
||||
def __init__(self, **entries):
|
||||
self.__dict__.update(entries)
|
||||
self._init_rest_service()
|
||||
#self._set_default_table_formats()
|
||||
#if not hasattr('tablefmt', self):
|
||||
# self.tablefmt = None
|
||||
#if not hasattr('colalign', self):
|
||||
# self.colalign = None
|
||||
|
||||
|
||||
|
||||
def _set_default_table_formats(self):
|
||||
if not hasattr('formats', self):
|
||||
self.tablefmt = None
|
||||
self.colalign = None
|
||||
elif not hasattr('table', self.formats):
|
||||
self.tableformat = None
|
||||
elif not hasattr('col', self.formats):
|
||||
self.colalign = None
|
||||
|
||||
|
||||
|
||||
def _init_rest_service(self):
|
||||
if self.api.startswith == '/':
|
||||
self.api = self.api[1:]
|
||||
if self.api.endswith == '/':
|
||||
self.api = self.api[1:-1]
|
||||
self.rest_service = RestService(self.api, json=True)
|
||||
self.rest_service.addHeader('Content-Type', 'application/json')
|
||||
self.rest_service.addToken(self.token)
|
||||
|
||||
|
||||
def get_list(self):
|
||||
try:
|
||||
get_current_chores = self.rest_service.get(Chore.GET_CURRENT_CHORES)
|
||||
|
||||
table_headers = ['Name', 'Due']
|
||||
table_entries = []
|
||||
for chore in get_current_chores:
|
||||
path = Chore.GET_CHORE_BY_ID.format(chore['chore_id'])
|
||||
chore_info = self.rest_service.get(path)
|
||||
if chore.get('next_estimated_execution_time') is None:
|
||||
due_date = 'None'
|
||||
elif re.match('2999',chore.get('next_estimated_execution_time')):
|
||||
due_date = 'None'
|
||||
else:
|
||||
due_date = datetime.strptime(chore.get('next_estimated_execution_time'), '%Y-%m-%d')
|
||||
|
||||
table_entry = [chore_info.get('name'), due_date]
|
||||
table_entries.append(table_entry)
|
||||
|
||||
except Exception as e:
|
||||
raise e
|
||||
# Generate stock overview table
|
||||
return tabulate(table_entries, headers=table_headers)
|
Loading…
Reference in New Issue
Block a user