-
-
Notifications
You must be signed in to change notification settings - 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
Open
mgaligniana
wants to merge
4
commits into
encode:main
Choose a base branch
from
mgaligniana:issue-5363
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+69
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
29e41f1
Fixed #5363 -- HTML5 datetime-local valid format HTMLFormRenderer
mgaligniana 85ca282
Update tests/test_renderers.py
auvipy b39982b
Apply changes requested by peterthomassen
mgaligniana 0c2b20c
Use datetime value to render datetime-local input_type
mgaligniana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import re | ||
from collections.abc import MutableMapping | ||
from datetime import datetime | ||
|
||
import pytest | ||
from django.core.cache import cache | ||
|
@@ -488,6 +489,68 @@ class TestSerializer(serializers.Serializer): | |
assert rendered == '' | ||
|
||
|
||
class TestDateTimeFieldHTMLFormRender(TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to add a couple of test cases with some non-naive datetimes, with a timezone specified. Could try with UTC and another timezone where the offset is non-zero |
||
def test_datetime_field_rendering_milliseconds(self): | ||
class TestSerializer(serializers.Serializer): | ||
appointment = serializers.DateTimeField() | ||
peterthomassen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
appointment = datetime(2024, 12, 24, 0, 55, 30, 345678) | ||
serializer = TestSerializer(data={"appointment": appointment}) | ||
serializer.is_valid() | ||
renderer = HTMLFormRenderer() | ||
field = serializer['appointment'] | ||
rendered = renderer.render_field(field, {}) | ||
self.assertInHTML( | ||
'<input name="appointment" class="form-control" type="datetime-local" value="2024-12-24T00:55:30.345">', | ||
rendered | ||
) | ||
|
||
def test_datetime_field_rendering_no_milliseconds(self): | ||
class TestSerializer(serializers.Serializer): | ||
appointment = serializers.DateTimeField() | ||
|
||
appointment = datetime(2024, 12, 24, 0, 55, 30, 0) | ||
serializer = TestSerializer(data={"appointment": appointment}) | ||
serializer.is_valid() | ||
renderer = HTMLFormRenderer() | ||
field = serializer['appointment'] | ||
rendered = renderer.render_field(field, {}) | ||
self.assertInHTML( | ||
'<input name="appointment" class="form-control" type="datetime-local" value="2024-12-24T00:55:30.000">', | ||
rendered | ||
) | ||
|
||
def test_datetime_field_rendering_no_seconds_and_no_milliseconds(self): | ||
class TestSerializer(serializers.Serializer): | ||
appointment = serializers.DateTimeField() | ||
|
||
appointment = datetime(2024, 12, 24, 0, 55, 0, 0) | ||
serializer = TestSerializer(data={"appointment": appointment}) | ||
serializer.is_valid() | ||
renderer = HTMLFormRenderer() | ||
field = serializer['appointment'] | ||
rendered = renderer.render_field(field, {}) | ||
self.assertInHTML( | ||
'<input name="appointment" class="form-control" type="datetime-local" value="2024-12-24T00:55:00.000">', | ||
rendered | ||
) | ||
|
||
def test_datetime_field_rendering_with_format(self): | ||
class TestSerializer(serializers.Serializer): | ||
appointment = serializers.DateTimeField(format='%a %d %b %Y, %I:%M%p') | ||
|
||
appointment = datetime(2024, 12, 24, 0, 55, 30, 345678) | ||
serializer = TestSerializer(data={"appointment": appointment}) | ||
serializer.is_valid() | ||
renderer = HTMLFormRenderer() | ||
field = serializer['appointment'] | ||
rendered = renderer.render_field(field, {}) | ||
self.assertInHTML( | ||
'<input name="appointment" class="form-control" type="datetime-local" value="2024-12-24T00:55:30.345">', | ||
rendered | ||
) | ||
|
||
|
||
class TestHTMLFormRenderer(TestCase): | ||
def setUp(self): | ||
class TestSerializer(serializers.Serializer): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It was harder than I thought!
Let me know if this is the correct way to get the datetime value.
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'm not sure. Is it guaranteed that there is always a parent?