feat: adding edit feature for recipe
This commit is contained in:
		| @@ -3,3 +3,4 @@ from grocy.commands.recipe import Recipe | ||||
| from grocy.commands.chore import Chore | ||||
| from grocy.commands.task import Task | ||||
| from grocy.commands.shopping import Shopping | ||||
| from grocy.commands.battery import Battery | ||||
|   | ||||
							
								
								
									
										64
									
								
								grocy/commands/battery.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								grocy/commands/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) | ||||
| @@ -5,7 +5,7 @@ from tabulate import tabulate | ||||
| from os import path | ||||
|  | ||||
| class Chore(object): | ||||
|     GET_CURRENT_CHORES = '/chores/get-current' | ||||
|     GET_BATTERIES = '/chores/get-current' | ||||
|     GET_CHORE_BY_ID = '/get-object/chores/{0}' | ||||
|     def __init__(self, **entries): | ||||
|         self.__dict__.update(entries) | ||||
|   | ||||
							
								
								
									
										3
									
								
								grocy/commands/product.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								grocy/commands/product.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| class Product(object): | ||||
|     def __init__(self, **entries): | ||||
|         self.__dict__.update(entries) | ||||
| @@ -1,13 +1,18 @@ | ||||
| from grocy import RestService | ||||
| from grocy.commands import products | ||||
| from tabulate import tabulate | ||||
| from os import path | ||||
|  | ||||
| class Recipe(object): | ||||
|     GET_RECIPES = '/get-objects/recipes' | ||||
|     GET_PRODUCT_BY_ID = '/get-object/products/{0}' | ||||
|     def __init__(self, **entries): | ||||
|     GET_RECIPE = '/get-object/recipes/{0}' | ||||
|     GET_PRODUCT = '/get-object/products/{0}' | ||||
|     GET_RECIPES_POS = '/get-object/recipes_pos' | ||||
|     def __init__(self, id, **entries): | ||||
|         self.id = id | ||||
|         self.__dict__.update(entries) | ||||
|         self._init_rest_service() | ||||
|         self.products = [] | ||||
|         #self._set_default_table_formats() | ||||
|         #if not hasattr('tablefmt', self): | ||||
|         #    self.tablefmt = None | ||||
| @@ -16,6 +21,17 @@ class Recipe(object): | ||||
|  | ||||
|          | ||||
|  | ||||
|     def _get_products_by_recipe_id(self): | ||||
|         recipe_products = self.rest_service.get(Recipe.GET_RECIPES_POS) | ||||
|         products_for_recipe = [] | ||||
|         for recipe_product in recipe_products: | ||||
|             if recipe_product.get('recipe_id') == self.id: | ||||
|                 ## TODO: need to find a better way to run a batch call to get  only products for recipe | ||||
|                 product = self.rest_service.get(Recipe.GET_PRODUCT.format(recipe_product.get('product_id')) | ||||
|                 ## combined dict into single dict | ||||
|                 product_recipie_info = {k: v for combined_dict in [product, recipe_product] for k, v in combined_dict.items()} | ||||
|                 self.products.append(product_recipie_info) | ||||
|  | ||||
|     def _set_default_table_formats(self): | ||||
|         if not hasattr('formats', self): | ||||
|             self.tablefmt = None | ||||
| @@ -50,3 +66,16 @@ class Recipe(object): | ||||
|             raise e | ||||
|             # Generate stock overview table | ||||
|         return tabulate(table_entries, headers=table_headers) | ||||
|  | ||||
|     def get(self, include_products=False): | ||||
|         try: | ||||
|             recipe = self.rest_service.get(Recipe.GET_RECIPE.format(self.id)) | ||||
|             if include_products: | ||||
|                 self.products = self._get_products_by_recipe_id() | ||||
|  | ||||
|         except Exception as e: | ||||
|             raise e | ||||
|  | ||||
|         return recipe.to_json() | ||||
|  | ||||
|               | ||||
|   | ||||
		Reference in New Issue
	
	Block a user