Last active
January 13, 2017 01:55
-
-
Save levivm/aa40c65d6c0d3bd3a89247ef32faa074 to your computer and use it in GitHub Desktop.
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
class UnixEpochDateField(serializers.DateTimeField): | |
def to_representation(self, value): | |
""" Return epoch time for a datetime object or ``None``""" | |
if type(value) is time: | |
d = date.today() | |
value = datetime.combine(d, value) | |
try: | |
return time_a.mktime(value.timetuple()) * 1000 | |
except (AttributeError, TypeError): | |
return None | |
def to_internal_value(self, value): | |
return datetime.fromtimestamp(value // 1000).replace(second=0) | |
class CalendarSerializer(RemovableSerializerFieldMixin, serializers.ModelSerializer): | |
activity = serializers.PrimaryKeyRelatedField(queryset=Activity.objects.all()) | |
initial_date = UnixEpochDateField() | |
assistants = serializers.SerializerMethodField() | |
schedules = HTMLField() | |
packages = CalendarPackageSerializer(many=True, required=False) | |
class Meta: | |
model = Calendar | |
fields = ( | |
'id', | |
'activity', | |
'initial_date', | |
'enroll_open', | |
'session_price', | |
'assistants', | |
'is_weekend', | |
'is_free', | |
'available_capacity', | |
'note', | |
'schedules', | |
'packages', | |
) | |
depth = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I noticed there was an issue with the UnixEpochDateField when it came to TimeZone aware objects.
I found the fix and put up a gist: https://gist.github.com/hammadzz/fe8f35c4b4344dd6ce9f73d23e825c96