81 lines
2.7 KiB
Python
81 lines
2.7 KiB
Python
|
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)
|