Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
add userDto to ChoreDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
SebRut committed Jun 28, 2019
1 parent 7836161 commit b7b6962
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pygrocy/grocy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
CurrentStockResponse,
CurrentVolatilStockResponse, GrocyApiClient,
ProductData, ProductDetailsResponse,
TransactionType)
TransactionType, UserDto)


class Product(object):
Expand Down Expand Up @@ -44,11 +44,14 @@ def __init__(self, raw_chore: CurrentChoreResponse):
self._chore_id = raw_chore.chore_id
self._last_tracked_time = raw_chore.last_tracked_time
self._next_estimated_execution_time = raw_chore.next_estimated_execution_time

self._name = None
self._last_done_by = None

def get_details(self, api_client: GrocyApiClient):
details = api_client.get_chore(self.chore_id)
self._name = details.chore.name
self._last_done_by = details.last_done_by

@property
def chore_id(self) -> int:
Expand All @@ -66,6 +69,10 @@ def next_estimated_execution_time(self) -> datetime:
def name(self) -> str:
return self._name

@property
def last_done_by(self) -> UserDto:
return self._last_done_by


class Grocy(object):
def __init__(self, base_url, api_key):
Expand Down
27 changes: 27 additions & 0 deletions pygrocy/grocy_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ def __init__(self, parsed_json):
self._id = parse_int(parsed_json.get('id'))

self._username = parsed_json.get('username')
self._first_name = parsed_json.get('first_name')
self._last_name = parsed_json.get('last_name')
self._display_name = parsed_json.get('display_name')

@property
def id(self) -> int:
return self._id

@property
def username(self) -> str:
return self._username

@property
def first_name(self) -> str:
return self._first_name

@property
def last_name(self) -> str:
return self._last_name

@property
def display_name(self) -> str:
return self._display_name


class CurrentChoreResponse(object):
Expand Down Expand Up @@ -182,6 +205,10 @@ def __init__(self, parsed_json):
def chore(self) -> ChoreData:
return self._chore

@property
def last_done_by(self) -> UserDto:
return self._last_done_by


class TransactionType(Enum):
PURCHASE = "purchase"
Expand Down
36 changes: 36 additions & 0 deletions test/test_choreDetailsResponse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
from unittest import TestCase

from pygrocy.grocy_api_client import ChoreDetailsResponse


class TestChoreDetailsResponse(TestCase):
def test_parse(self):
input_json = """{
"chore": {
"id": 0,
"name": "string",
"description": "string",
"period_type": "manually",
"period_days": 0,
"row_created_timestamp": "2019-05-04T11:31:04.563Z"
},
"last_tracked": "2019-05-04T11:31:04.563Z",
"track_count": 0,
"last_done_by": {
"id": 0,
"username": "string",
"first_name": "string",
"last_name": "string",
"display_name": "string",
"row_created_timestamp": "2019-05-04T11:31:04.564Z"
},
"next_estimated_execution_time": "2019-05-04T11:31:04.564Z"
}"""

response = ChoreDetailsResponse(json.loads(input_json))

assert response.chore.id == 0
assert response.chore.name == "string"

assert response.last_done_by.display_name == "string"

0 comments on commit b7b6962

Please sign in to comment.