feat: added edit for recipe
This commit is contained in:
9
grocy/models/__init__.py
Normal file
9
grocy/models/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from grocy.models.recipe_pos import RecipePos
|
||||
from grocy.models.stock import Stock
|
||||
from grocy.models.product import Product
|
||||
from grocy.models.recipe import Recipe
|
||||
from grocy.models.chore import Chore
|
||||
#
|
||||
from grocy.models.task import Task
|
||||
from grocy.models.shopping import Shopping
|
||||
from grocy.models.battery import Battery
|
64
grocy/models/battery.py
Normal file
64
grocy/models/battery.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 Battery(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)
|
63
grocy/models/chore.py
Normal file
63
grocy/models/chore.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import re
|
||||
from datetime import datetime
|
||||
from grocy import RestService
|
||||
from tabulate import tabulate
|
||||
from os import path
|
||||
|
||||
class Chore(object):
|
||||
GET_BATTERIES = '/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)
|
30
grocy/models/product.py
Normal file
30
grocy/models/product.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from grocy.models.schema import Schema
|
||||
|
||||
class Product(Schema):
|
||||
def __init__(self, **entries):
|
||||
self.fields = ['location_id', 'name', 'description', 'qu_id_purchase',
|
||||
'qu_id_stock', 'qu_factor_purchase_to_stock', 'barcode',
|
||||
'min_stock_amount', 'default_best_before_days', 'default_best_before_days_after_open'
|
||||
'tare_weight', 'enable_tare_weight_handling', 'picture_file_name','product_group_id',
|
||||
'allow_partial_units_in_stock']
|
||||
self.__dict__.update(entries)
|
||||
#self._init_rest_service()
|
||||
|
||||
def toJSON(self):
|
||||
obj = {}
|
||||
for attr, value in self.__dict__.items():
|
||||
if attr in self.fields and value is not None:
|
||||
obj[attr] = value
|
||||
return obj
|
||||
|
||||
#id:str = ''
|
||||
#location_id:str = ''
|
||||
#name:str = ''
|
||||
#description:str = ''
|
||||
#qu_id_purchase:str = ''
|
||||
#qu_id_stock:str = ''
|
||||
#qu_factor_purchase_to_stock:float = 0
|
||||
#barcode:str = ''
|
||||
#min_stock_amount:int = 0
|
||||
#default_best_before_days:int = 0
|
||||
#default_best_before_days_after_open:int = 0
|
113
grocy/models/recipe.py
Normal file
113
grocy/models/recipe.py
Normal file
@@ -0,0 +1,113 @@
|
||||
from grocy import RestService
|
||||
import html2text
|
||||
from grocy.models import Product, RecipePos
|
||||
|
||||
from grocy.models.schema import Schema
|
||||
from tabulate import tabulate
|
||||
from os import path
|
||||
|
||||
class Recipe(Schema):
|
||||
|
||||
def __init__(self, **entries):
|
||||
self.fields = [
|
||||
'name',
|
||||
'picture_file_name', 'description',
|
||||
'base_servings', 'desired_servings',
|
||||
'not_check_shoppinglist', 'recipes_pos', 'products'
|
||||
]
|
||||
self.__dict__.update(entries)
|
||||
self.recipes_pos = []
|
||||
self.products = []
|
||||
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 get_list(self):
|
||||
try:
|
||||
recipes = self.rest_service.get('recipe')
|
||||
table_headers = ['#', 'Name']
|
||||
table_entries = []
|
||||
for recipe in recipes:
|
||||
table_entry = [recipe.get('id'), recipe.get('name')]
|
||||
table_entries.append(table_entry)
|
||||
|
||||
except Exception as e:
|
||||
raise e
|
||||
# Generate stock overview table
|
||||
return tabulate(table_entries, headers=table_headers)
|
||||
|
||||
|
||||
def _get_products_by_recipe_id(self):
|
||||
recipe_products_info = self.rest_service.get('recipes_pos')
|
||||
for recipe_product_info in recipe_products_info:
|
||||
if recipe_product_info.get('recipe_id') == self.id:
|
||||
## TODO: need to find a better way to run a batch call to get only products for recipe
|
||||
product_info = self.rest_service.get('products', recipe_product_info.get('product_id'))
|
||||
product = Product(**product_info)
|
||||
product.id = recipe_product_info.get('product_id')
|
||||
self.products.append(product)
|
||||
recipe_pos = RecipePos(**recipe_product_info)
|
||||
recipe_pos.id = recipe_product_info.get('id')
|
||||
self.recipes_pos.append(recipe_pos)
|
||||
|
||||
|
||||
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 toJSON(self):
|
||||
obj = {}
|
||||
for attr, value in self.__dict__.items():
|
||||
isEmptyList = True if type(value) == list and len(value) == 0 else False
|
||||
if attr in self.fields and not isEmptyList:
|
||||
obj[attr] = value
|
||||
return obj
|
||||
|
||||
def update(self):
|
||||
try:
|
||||
for item in self.products:
|
||||
product = Product(**item)
|
||||
self.rest_service.put('products', product, product.id)
|
||||
#for item in self.recipes_pos:
|
||||
# self.rest_service.put('recipes_pos', item)
|
||||
updated_recipe = {
|
||||
'description': self.description,
|
||||
'name': self.name,
|
||||
'base_servings': self.base_servings,
|
||||
'desired_servings': self.desired_servings,
|
||||
'not_check_shoppinglist': self.not_check_shoppinglist
|
||||
}
|
||||
self.rest_service.put('recipes', updated_recipe, self.id)
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
def get(self, include_products=False):
|
||||
try:
|
||||
recipe = self.rest_service.get('recipes', id=self.id)
|
||||
if 'description' in recipe:
|
||||
recipe['description'] = html2text.html2text(recipe['description'].strip())
|
||||
self.__dict__.update(recipe)
|
||||
if include_products:
|
||||
self._get_products_by_recipe_id()
|
||||
except Exception as e:
|
||||
raise e
|
19
grocy/models/recipe_pos.py
Normal file
19
grocy/models/recipe_pos.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from grocy.models.schema import Schema
|
||||
|
||||
class RecipePos(Schema):
|
||||
def __init__(self, **entries):
|
||||
self.fields = [
|
||||
'id', 'recipe_id',
|
||||
'product_id', 'amount',
|
||||
'note', 'qu_id',
|
||||
'only_check_single_unit_in_stock',
|
||||
'not_check_stock_fulfillment', 'ingredient_group'
|
||||
]
|
||||
self.__dict__.update(entries)
|
||||
|
||||
def toJSON(self):
|
||||
obj = {}
|
||||
for attr, value in self.__dict__.items():
|
||||
if attr in self.fields:
|
||||
obj[attr] = value
|
||||
return obj
|
15
grocy/models/schema.py
Normal file
15
grocy/models/schema.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from grocy import RestService
|
||||
import logging
|
||||
|
||||
class Schema(object):
|
||||
|
||||
def _init_rest_service(self):
|
||||
if hasattr(self, 'api'):
|
||||
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.addHeader('Accept', 'application/json')
|
||||
self.rest_service.addToken(self.token)
|
72
grocy/models/shopping.py
Normal file
72
grocy/models/shopping.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from grocy import RestService
|
||||
import json
|
||||
from tabulate import tabulate
|
||||
from os import path
|
||||
|
||||
class Shopping(object):
|
||||
GET_SHOPPING_LIST = '/get-objects/shopping_list'
|
||||
GET_PRODUCT_BY_ID = '/get-object/products/{0}'
|
||||
GET_QUANTITY_UNIIT_BY_ID = '/get-object/quantity_units/{0}'
|
||||
GET_PRODUCT_GROUP_ID_BY_ID ='/get-object/product_groups/{0}'
|
||||
|
||||
def __init__(self, **entries):
|
||||
self.__dict__.update(entries)
|
||||
self._init_rest_service()
|
||||
# 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_shopping_list = self.rest_service.get(Shopping.GET_SHOPPING_LIST)
|
||||
table_headers = ['Group', 'Product', 'Amount']
|
||||
|
||||
# Get product names and location from ids and replace
|
||||
product_ids = [entry['product_id'] for entry in get_shopping_list]
|
||||
products = []
|
||||
location_ids = []
|
||||
table_entries = []
|
||||
for index in range(len(product_ids)):
|
||||
product_id = product_ids[index]
|
||||
path = Shopping.GET_PRODUCT_BY_ID.format(product_id)
|
||||
product = self.rest_service.get(path)
|
||||
|
||||
path = Shopping.GET_QUANTITY_UNIIT_BY_ID.format(product['qu_id_purchase'])
|
||||
quantity_unit = self.rest_service.get(path)
|
||||
min_amount = '{} {}'.format(product['min_stock_amount'], quantity_unit['name'])
|
||||
if product['product_group_id'] == '':
|
||||
product_group_name = 'Uncategorized'
|
||||
else:
|
||||
path = Shopping.GET_PRODUCT_GROUP_ID_BY_ID.format(product['product_group_id'])
|
||||
product_group = self.rest_service.get(path)
|
||||
product_group_name = product_group['name']
|
||||
current_shopping_item = [product_group_name, product['name'], min_amount]
|
||||
table_entries.append(current_shopping_item)
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
# Generate stock overview table
|
||||
return tabulate(table_entries, headers=table_headers)
|
61
grocy/models/stock.py
Normal file
61
grocy/models/stock.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from grocy import RestService
|
||||
from tabulate import tabulate
|
||||
from os import path
|
||||
|
||||
class Stock(object):
|
||||
GET_CURRENT_STOCK = '/stock/get-current-stock'
|
||||
GET_PRODUCT_BY_ID = '/get-object/products/{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_entries(self):
|
||||
try:
|
||||
get_current_stock = self.rest_service.get(Stock.GET_CURRENT_STOCK)
|
||||
|
||||
# Get product names from ids and replace
|
||||
product_ids = [entry['product_id'] for entry in get_current_stock]
|
||||
table_entries = []
|
||||
try:
|
||||
for index in range(len(product_ids)):
|
||||
product_id = product_ids[index]
|
||||
path = Stock.GET_PRODUCT_BY_ID.format(product_id)
|
||||
product = self.rest_service.get(path)
|
||||
get_current_stock[index]['product_id'] = product['name']
|
||||
table_entry = list(dict.values(get_current_stock[index]))
|
||||
table_entries.append(table_entry)
|
||||
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
# Generate stock overview table
|
||||
table_headers = ['Product', 'Amount', 'Best Before Date', 'Amount Opened']
|
||||
return tabulate(table_entries, headers=table_headers)
|
||||
except Exception as e:
|
||||
raise e
|
80
grocy/models/task.py
Normal file
80
grocy/models/task.py
Normal file
@@ -0,0 +1,80 @@
|
||||
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)
|
Reference in New Issue
Block a user