-
-
Couldn't load subscription status.
- Fork 7k
Fixed #5363 -- HTML5 datetime-local valid format HTMLFormRenderer #9365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Hi @auvipy! Following your comment on #5363 (comment) I ask if you could do the review! Thank you! |
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please address the review comment
|
Thanks @auvipy! I'll do it! |
|
Just an update here. I'm still active! I'll do as soon as possible |
|
I finally did it, sorry for the long delay! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes HTML5 datetime-local input formatting by truncating milliseconds to 3 digits to ensure browser compatibility. The HTML5 datetime-local input type specification only supports up to 3 digits of milliseconds precision, but DRF was potentially outputting more digits which caused browser validation errors.
- Modifies the datetime-local field value formatting in HTMLFormRenderer to limit milliseconds to 3 digits
- Adds a test case to verify the correct datetime-local input formatting
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| rest_framework/renderers.py | Updates datetime-local field formatting to truncate milliseconds to 3 digits |
| tests/test_renderers.py | Adds test case to verify datetime-local input formatting with milliseconds |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I have two more remarks:
- Timezones currently get dropped, but I assume that's fine because this is about
datetime-localwhich "represent[s] a local date and time, with no time-zone offset information". - The transformation assumes that the string format is iso-8601. It doesn't work if I specify, for example,
appointment = serializers.DateTimeField(format='%a %d %b %Y, %I:%M%p'). Shouldn't the internal value be used as an input?
|
Thank you Peter! Not sure if I'm following: do you mean use the datetime value instead of the input.value (string)? So let me know if you want to fix this in this pull request |
Yes, that's what I meant. Sorry for being unclear! |
|
I tried to use this branch on a Django project with |
|
Correct! I'll go for that approach and add one more test for that case Bruno! Thanks! |
6d5cff3 to
0c2b20c
Compare
|
@browniebroke and @peterthomassen thank you so much for your quick reviews and guidance! 🙏
Sure, I'll add a new test with timezone!
Good question. I didn't fully understand what is parent so I'll read more code to be sure and add new cases if needed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
bce2aa2 to
fee4af2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
d894aab to
a15dfb4
Compare
|
Hello team! I went for a new approach: get datetime string and transform to datetime object to better manipulation. I've added 7 tests in total! I think I covered all possible cases! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the new approach, but I don't understand the typing considerations. You probably have thought about that -- any insights on my comments?
Also, the tests have quite some code duplication (I'm not sure if that is a problem).
Yes!! Of course, I can improve that! |
Tests are prettier now! (I hope so) |
|
I've also renamed the |
5468bca to
89efdab
Compare
|
@peterthomassen I would need your email to add you as |
What makes you think that? -- I believe it is not. There are also multiple functions in DRF that define variables with that name (you can find some via
Thanks, but that's not necessary :-) Anyway, by address is "my first name" at desec dot io. |
|
I meant this builtin function: https://docs.python.org/3/library/functions.html#format I realized when editor highlighted to me |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I understand what you mean. (That's not reserved, but a built-in, which is shadowed when you redeclare it.)
Anyway, looks great to me now!
|
Ahhh correct! Thank you Peter!!! |
tests/test_renderers.py
Outdated
| @override_settings(TIME_ZONE='Asia/Tokyo') # +09:00 | ||
| def test_datetime_field_rendering_non_zero_timezone(self): | ||
| self._assert_datetime_rendering( | ||
| datetime(2024, 12, 24, 0, 55, 30, 345678), | ||
| "2024-12-24T00:55:30.345" | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I wasn't clear enough when I wrote "naive datetime". I was talking about the naive in the pure Python sense, which is outline in this paragraph of the Python docs: https://docs.python.org/3.9/library/datetime.html#aware-and-naive-objects
In practical sense, I think that this test should not only set the TIME_ZONE Django setting, but also set the tzinfo attribute on the datetime object itself:
from zoneinfo import ZoneInfo
datetime(2024, 12, 24, 0, 55, 30, 345678, tz_info=ZoneInfo('Asia/Tokyo'))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @browniebroke !
I've modified and re-named as this: test_datetime_field_rendering_timezone_aware_datetime
Let me if its what you had in mind!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, looks good. Not sure why the tests fail on Django 4.2 though... 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm, I found it! In Django 4.1 is returing value="2024-12-23T15:55:30.345" instead of value="2024-12-23T09:55:30.345"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we skip the test for that version as it is no longer maintenable? 🤔 That is Django 4.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I FOUND IT!
In django 4.1, 4.2 USE_TZ was False! https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-USE_TZ
89efdab to
dba1179
Compare
| # field.value is expected to be a string | ||
| # https://www.django-rest-framework.org/api-guide/fields/#datetimefield | ||
| field.value = ( | ||
| datetime.datetime.fromisoformat(field.value.rstrip('Z')) if format_ == ISO_8601 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you still need to remove the UTC timezone manually (with .rstrip('Z'))? It works as well without it, so I think that datetime.datetime.fromisoformat handles it out of the box.
The TZ info is stripped line 362 with field.value.replace(tzinfo=None) and that should handle any timezone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your fast response!
Mmm if I remove the .rstrip('Z') test_datetime_field_rendering_utc fails
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>>> import datetime
>>> value = '2024-12-24T00:55:30.345678Z'
>>> datetime.datetime.fromisoformat(value).replace(tzinfo=None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid isoformat string: '2024-12-24T00:55:30.345678Z'Co-authored-by: Peter Thomassen
dba1179 to
d275c59
Compare
Description
Hi!
This PR delete everything after the first 3 digits of the miliseconds part of a
DateTimeFieldinput atHTMLFormRendererto avoid break in browsers:https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/datetime-local#try_it
Please execute
./runtests.py TestDateTimeFieldHTMLFormRenderto run the new testsFix #5363