Skip to content

Commit

Permalink
fix: temporary fix for nested things in activity
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Jun 30, 2023
1 parent 91beef5 commit b2a8050
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
4 changes: 3 additions & 1 deletion examples/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
token = oidc_client.authorize()
client = TimedAPIClient(token, URL, API_NAMESPACE)

r = client.activities.start("made with libtimed")
attributes = {"comment": "made with libtimed"}
relationships = {"task": "3605343"}
r = client.activities.start(attributes, relationships)
time.sleep(7)
r = client.activities.stop()
30 changes: 13 additions & 17 deletions src/libtimed/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,35 @@ def get(self, filters={}, include=None, id=None) -> dict:

return resp["data"]

def post(self, attributes={}, relationships={}) -> Response:
json = self.parse_post_json(attributes, relationships)
def post(self, attributes={}, relationships={}, raw=False) -> Response:
json = self._parse_patch_create_json(attributes, relationships, raw)
resp = self.client.session.post(self.url, json=json)
return resp

def patch(self, id, attributes={}, relationships={}) -> Response:
json = self.parse_patch_json(attributes, relationships, id=id)
def patch(self, id, attributes={}, relationships={}, raw=False) -> Response:
json = self._parse_patch_create_json(attributes, relationships, raw)
json["data"]["id"] = id
resp = self.client.session.patch(f"{self.url}/{id}", json=json)
return resp

def all(self, filters, include=None):
# self.get but with different defaults
raise NotImplementedError

def parse_post_json(self, attributes, relationships) -> dict:
def _parse_patch_create_json(self, attributes, relationships, raw: bool) -> dict:
cls = self.__class__
return {
"data": {
"attributes": self._parsed_defaults(cls.attribute_defaults, attributes),
"relationships": self._parsed_relationships(
cls.relationship_defaults, relationships
),
)
if not raw
else relationships,
"type": cls.resource_name,
}
}

def parse_patch_json(self, *args, id):
json = self.parse_post_json(*args)
json["data"]["id"] = id
return json

def _id_to_relationship(self, id, resource_name):
if not id:
return {"data": None}
Expand Down Expand Up @@ -234,19 +232,17 @@ class Activities(BaseModel):
def current(self):
return (self.get({"active": True}) or [[]])[0]

def start(self, comment=""):
def start(self, attributes: dict, relationships: dict):
if self.current:
self.stop()
return self.post({"comment": comment})
return self.post(attributes, relationships)

def stop(self):
if self.current:
attributes = self.current["attributes"]
relationships = self.current["relationships"]
attributes["to-time"] = datetime.now()
r = self.patch(
self.current["id"],
attributes,
)
r = self.patch(self.current["id"], attributes, relationships, raw=True)
return r


Expand Down

0 comments on commit b2a8050

Please sign in to comment.