Skip to content

Commit

Permalink
Add subcommand userfield
Browse files Browse the repository at this point in the history
  • Loading branch information
adamschmalhofer committed May 21, 2023
1 parent 3ba424a commit d329502
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
45 changes: 45 additions & 0 deletions grocy_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,16 @@ def get_user_fields(self, entity: str, object_id: int) -> GrocyUserFields:
self.assert_valid_response(response)
return cast(GrocyUserFields, response.json())

def set_userfields(self, entity: str, object_id: int,
user_fields: dict[str, object]) -> None:
''' Sets a Grocy user field '''
call = f'/userfields/{entity}/{object_id}'
response = requests.put(self.base_url + call,
headers=self.headers,
timeout=self.timeout,
data=json.dumps(user_fields))
self.assert_valid_response(response)


@dataclass
class AppArgs:
Expand Down Expand Up @@ -420,6 +430,7 @@ class CliArgs(AppArgs):
days: int
at: Optional[GrocyDateTime]
keep: Literal['later', 'earlier', 'old', 'new']
entity: str


@dataclass
Expand Down Expand Up @@ -1270,6 +1281,26 @@ def todotxt_chore_push(args: TodotxtArgs,
f' on {response["tracked_time"]}')


def userfield_cmd(args: CliArgs,
_: AppConfig,
grocy: GrocyApi) -> None:
''' Quickly add userfields '''
try:
for item in yaml.safe_load(args.file):
try:
item_id = item['id']
del item['id']
grocy.set_userfields(args.entity, item_id, item)
print(f'{args.entity} {item_id}')
except KeyError:
print('Error: missing id-field for entity in yaml file.',
file=sys.stderr)
except TypeError:
print('Error: list missing in yaml file.', file=sys.stderr)
except yaml.scanner.ScannerError:
print('Error: yaml invalid.', file=sys.stderr)


def chore_show_cmd(args: AppArgs,
_: AppConfig,
grocy: GrocyApi,
Expand Down Expand Up @@ -1456,6 +1487,20 @@ def get_argparser_cli(stores: Iterable[Store]) -> ArgumentParser:
help='Show given chore')
chore_show.set_defaults(func=chore_show_cmd)
add_chore_show_arguments(chore_show)
userfield = subparsers.add_parser('userfield',
description='Add userfield(s) to'
' (usally) many grocy'
' entities from a yaml'
' file.',
help='Quickly add userfields')
userfield.set_defaults(func=userfield_cmd)
userfield.add_argument('entity',
help='the type of entity that the user fields'
' should be added to. E.g. batteries, chores,'
' chores_log, ...')
userfield.add_argument('file',
type=FileType('r', encoding='utf-8'),
help='a yaml file with the user fields to set')
return parser


Expand Down
80 changes: 78 additions & 2 deletions tests/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ we only include cases that are not useful there.
$ grocy_importer.py --help
usage: grocy_importer.py [-h] [--timeout N] [--dry-run]
{whereis,shopping-list,recipe,purchase,chore} ...
{whereis,shopping-list,recipe,purchase,chore,userfield}
...
Help importing into Grocy
positional arguments:
{whereis,shopping-list,recipe,purchase,chore}
{whereis,shopping-list,recipe,purchase,chore,userfield}
whereis show location of a product
shopping-list export shopping list in todo.txt format
recipe assist importing recipes from the web
purchase import purchases
chore Prompt to do each overdue chore
userfield Quickly add userfields
options:
-h, --help show this help message and exit
Expand Down Expand Up @@ -73,3 +75,77 @@ Recipe
Unknown unit convertion:
Userfields
----------

.. code::
$ grocy_importer.py userfield --help
usage: grocy_importer.py userfield [-h] entity file
Add userfield(s) to (usally) many grocy entities from a yaml file.
positional arguments:
entity the type of entity that the user fields should be added to. E.g.
batteries, chores, chores_log, ...
file a yaml file with the user fields to set
options:
-h, --help show this help message and exit
.. code::
$ cat tests/userfields.yml
---
- id: 1
customfield1: Changed entity1, field 1
- id: 2
customfield1: Changed entity2, field 2
customfield2: Changed entity2, field 2
In addition to the user fields that should be set only the id of the entity for the fields needs to be specified.

.. code::
$ grocy_importer.py userfield userentity-exampleuserentity tests/userfields.yml
userentity-exampleuserentity 1
userentity-exampleuserentity 2
In case the userfields doesn't match the entity an error will be printed:

.. code::
$ grocy_importer.py userfield chores tests/userfields.yml
Error: Connection to Grocy failed with Bad Request: Field customfield1 is not a valid userfield of the given entity
In case the entity id doesn't exist, we would hope that grocy would complain with an error. Instead it just saves the userfields.

.. code::
$ grocy_importer.py userfield userentity-exampleuserentity tests/userfields-missing-entity.yml
userentity-exampleuserentity 10001
userentity-exampleuserentity 2
In case the given file isn't a yml an error will be printed:

.. code::
$ grocy_importer.py userfield userentity-exampleuserentity tests/README.rst
Error: yaml invalid.
In case the id of an item is missing an error will be printed:

.. code::
$ grocy_importer.py userfield userentity-exampleuserentity tests/userfields-missing-id.yml
Error: missing id-field for entity in yaml file.
userentity-exampleuserentity 2
In case the given list is empty

.. code::
$ grocy_importer.py userfield userentity-exampleuserentity /dev/null
Error: list missing in yaml file.
6 changes: 6 additions & 0 deletions tests/userfields-missing-entity.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- id: 10001
customfield1: ignored as entity-id missing
- id: 2
customfield1: Changed entity2, field 2
customfield2: Changed entity2, field 2
5 changes: 5 additions & 0 deletions tests/userfields-missing-id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- customfield1: Changed entity1, field 1
- id: 2
customfield1: Changed entity2, field 2
customfield2: Changed entity2, field 2
6 changes: 6 additions & 0 deletions tests/userfields.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- id: 1
customfield1: Changed entity1, field 1
- id: 2
customfield1: Changed entity2, field 2
customfield2: Changed entity2, field 2

0 comments on commit d329502

Please sign in to comment.