feat: added task

This commit is contained in:
Aerex 2019-03-09 23:00:45 -06:00
parent 0e992aaab9
commit 3d8b209fd8
3 changed files with 92 additions and 0 deletions

View File

@ -106,3 +106,12 @@ def chore(ctx):
chore = Chore(**cfg) chore = Chore(**cfg)
chores = chore.get_list() chores = chore.get_list()
click.echo(chores) click.echo(chores)
@main.command()
@click.pass_context
def task(ctx):
cfg = ctx.obj['cfg']
task = Task(**cfg)
tasks = task.get_list()
click.echo(tasks)

View File

@ -1,4 +1,5 @@
from grocy.commands.stock import Stock from grocy.commands.stock import Stock
from grocy.commands.recipe import Recipe from grocy.commands.recipe import Recipe
from grocy.commands.chore import Chore from grocy.commands.chore import Chore
from grocy.commands.task import Task
from grocy.commands.shopping import Shopping from grocy.commands.shopping import Shopping

82
grocy/commands/task.py Normal file
View File

@ -0,0 +1,82 @@
import re
from datetime import datetime
from grocy import RestService
from tabulate import tabulate
from os import path
class Task(object):
GET_CURRENT_TASKS = '/tasks/get-current'
GET_CATEGORIES = '/get-objects/task_categories'
GET_USERS = '/users/get'
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_tasks = self.rest_service.get(Task.GET_CURRENT_TASKS)
get_categories = self.rest_service.get(Task.GET_CATEGORIES)
get_users = self.rest_service.get(Task.GET_USERS)
users = {}
categories = {}
for category in get_categories:
categories[category.get('id')] = category.get('name')
for user in get_users:
users[user.get('id')] = user.get('username')
table_headers = ['ID', 'Name', 'Due', 'Assigned To']
table_entries = []
for task in get_current_tasks:
if task.get('category_id') == '':
category = ''
else:
category = categories[categories.get('category_id')]
if task.get('due_date') == '':
due_date = ''
else:
due_date = datetime.strptime(task.get('due_date'), '%Y-%m-%d').date()
if task.get('assigned_to_user_id') == '':
assigned_to = ''
else:
assigned_to = users[task.get('assigned_to_user_id')]
table_entry = [task.get('id'), task.get('name'), due_date, assigned_to]
table_entries.append(table_entry)
except Exception as e:
raise e
# Generate stock overview table
return tabulate(table_entries, headers=table_headers)