Nriver / trilium-py

Python client for ETAPI of Trilium Note. Trilium 的 Python版 ETAPI 客户端
GNU Affero General Public License v3.0
118 stars 27 forks source link

Patch date created: handle both Local and UTC dates #43

Closed maphew closed 3 months ago

maphew commented 3 months ago

This patch is a bigger touch than the last one and I don't expect it to pass review without changes.

Patch_notes will now take the provided date, convert it to whichever of local or utc is missing, and set both properties on the target note. To use, call ea.patch_notes() using either of dateCreated or utcDateCreated optional parameters but not both. If timezone info is missing from the datetime object Patch_notes will use the tz of the local machine.

mydate= dateutil.parser.parse('1999.12.31') # or anything else that creates a datetime object
# use just one of these:
res = ea.patch_note(noteId, dateCreated=mydate)
res = ea.patch_note(noteId, utcDateCreated=mydate)

Example output for res = ea.patch_note(noteId, utcDateCreated=mydate) follows. Lines prefixed with --- are from my test script (so not emitted from trilium-py):

---parsed_date: 1899-01-15 00:00:00
utc date tzinfo was None, forced to UTC (1899-01-15 00:00:00+00:00)
Getting local_timezone...
timezone: Yukon Standard Time
Synchronized dates:
        local_date: 1899-01-14 17:00:00-07:00
        utc_date  : 1899-01-15 00:00:00+00:00
ETAPI Formatted date kind: local
        1899-01-14 17:00:00.143-0700
ETAPI Formatted date kind: utc
        1899-01-15 00:00:00.153Z
---Patched LOCAL response: 1899-01-14 17:00:00.143-0700
---Patched UTC response: 1899-01-15 00:00:00.153Z

The new functions are:

They could be collapsed into one function. I went back and forth a lot on that.

The pyproject.toml changes can be ignored/discarded. (After review I'll create a new branch with just the clean changes and a fresh PR.)

Known deficits:

I started this almost a full month ago and busted my brain several times sorting out a path that worked, and didn't reverse local/utc or some other logic error. I'm out of my comfort zone and will not be surprised at any mistakes pointed out!

maphew commented 3 months ago

My test script: patch-date-test.py.txt

Nriver commented 3 months ago

Thanks for the big PR! 😊

I have some feedback regarding your changes:

  1. Handling None Values for Dates: There could be cases where both dateCreated and utcDateCreated are None. For instance, if I need to correct a typo in the title and only want to change the title, I would use a command like this:

    ea.patch_note(
       noteId='some id',
       title="new title"
    )

    The handle_dates function should be updated to handle this scenario.

  2. Function Location: You can move the three functions to utils/time_util.py as they are static methods.

maphew commented 3 months ago

Thanks for the feedback! I was so deep in my local problem I overlooked that date changing is only sometimes the goal. Hah. I'll get on both of these suggestions.

maphew commented 3 months ago

1) patching note without any date changes is allowed (0aff1cc)

2) I've move all the new date functions except handle_dates() into _timeutil (cfa0670). I'm not that familiar with classes and static functions and utilities. This is my first time splitting code up like this so feel free to advise and critique.

Nriver commented 3 months ago

It works great. I've just made a new release.

maphew commented 3 months ago

oh! I expected some more changes needed. Wonderful that wasn't needed =) commit 7d8611cddd6f1edc4f070e62bb6445cc3499cd82 removes the disabled code (won't change the release).

Nriver commented 3 months ago

I don't mind commented code; I have a lot of commented code in my own work. I can merge the clean-up PR if you'd like.

maphew commented 3 months ago

it can wait until my next contribution (I know what that is yet. I just know that tri-py is useful enough to me that I'm building workflows around it, and will want something else changed too)