Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Django REST Framework serializer field
Closes #65 Squashed commit of the following: commit 1009c88 Author: Mike Fogel <mike@fogel.ca> Date: Fri Nov 20 11:08:00 2020 -0300 feat: add docs commit 7086221 Author: Mike Fogel <mike@fogel.ca> Date: Fri Nov 20 11:05:15 2020 -0300 chore: add test, allow for easier imports commit 9b9dc45 Author: starryrbs <1322096624@qq.com> Date: Mon Nov 16 14:45:29 2020 +0800 fix the problem: line too long commit 3506476 Merge: b425511 58542a9 Author: starryrbs <1322096624@qq.com> Date: Mon Nov 16 14:36:48 2020 +0800 Merge remote-tracking branch 'origin/master' commit b425511 Author: starryrbs <1322096624@qq.com> Date: Mon Nov 16 14:36:35 2020 +0800 add django_rest_framework TimeZoneField representation test commit 58542a9 Merge: 1f0de71 63b8828 Author: starryrbs <35923714+starryrbs@users.noreply.github.com> Date: Mon Nov 16 14:23:29 2020 +0800 Merge branch 'master' into master commit 1f0de71 Author: starryrbs <1322096624@qq.com> Date: Mon Nov 16 14:18:44 2020 +0800 add django_rest_framework TimeZoneField test commit c87d384 Author: starryrbs <1322096624@qq.com> Date: Mon Nov 9 17:59:41 2020 +0800 Add django rest framework support
- Loading branch information
Showing
7 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
'django.contrib.staticfiles', | ||
'timezone_field', | ||
'tests', | ||
'rest_framework' | ||
) | ||
|
||
MIDDLEWARE_CLASSES = ( | ||
|
This file contains 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 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 @@ | ||
from timezone_field.fields import TimeZoneField | ||
from timezone_field.forms import TimeZoneFormField | ||
from timezone_field.rest_framework import TimeZoneSerializerField | ||
|
||
__version__ = '4.0' | ||
__all__ = ['TimeZoneField', 'TimeZoneFormField'] | ||
__all__ = ['TimeZoneField', 'TimeZoneFormField', 'TimeZoneSerializerField'] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytz | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.utils.encoding import force_str | ||
from rest_framework.fields import Field | ||
|
||
|
||
class TimeZoneSerializerField(Field): | ||
default_error_messages = { | ||
'invalid': _('A valid timezone is required.'), | ||
} | ||
|
||
def to_internal_value(self, data): | ||
try: | ||
return pytz.timezone(force_str(data)) | ||
except pytz.UnknownTimeZoneError: | ||
self.fail('invalid') | ||
|
||
def to_representation(self, value): | ||
return str(value) |
This file contains 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