From 3d8b209fd812cd90d3ff48c81b1f29809dacf17b Mon Sep 17 00:00:00 2001 From: Aerex Date: Sat, 9 Mar 2019 23:00:45 -0600 Subject: [PATCH] feat: added task --- grocy/cli.py | 9 +++++ grocy/commands/__init__.py | 1 + grocy/commands/task.py | 82 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 grocy/commands/task.py diff --git a/grocy/cli.py b/grocy/cli.py index ed19f3a..80be1bd 100644 --- a/grocy/cli.py +++ b/grocy/cli.py @@ -106,3 +106,12 @@ def chore(ctx): chore = Chore(**cfg) chores = chore.get_list() 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) diff --git a/grocy/commands/__init__.py b/grocy/commands/__init__.py index c426efa..b938150 100644 --- a/grocy/commands/__init__.py +++ b/grocy/commands/__init__.py @@ -1,4 +1,5 @@ from grocy.commands.stock import Stock from grocy.commands.recipe import Recipe from grocy.commands.chore import Chore +from grocy.commands.task import Task from grocy.commands.shopping import Shopping diff --git a/grocy/commands/task.py b/grocy/commands/task.py new file mode 100644 index 0000000..60d5385 --- /dev/null +++ b/grocy/commands/task.py @@ -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)