Skip to content

Commit

Permalink
Merge pull request #1091 from centerofci/mathesar-433-duration-displa…
Browse files Browse the repository at this point in the history
…y-options

Implement display options for Duration type
  • Loading branch information
kgodey committed Feb 25, 2022
2 parents 8591a14 + e124ca2 commit 02e22cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions mathesar/api/display_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
{"name": "locale", "type": "string"}]
},
MathesarTypeIdentifier.DATETIME.value:
{
"options": [{"name": "format", "type": "string"}]
},
MathesarTypeIdentifier.DURATION.value:
{
"options": [{"name": "format", "type": "string"}]
}
Expand Down
16 changes: 15 additions & 1 deletion mathesar/api/serializers/shared_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def validate(self, datetime_obj, display_format, serializer_field):
class TimeWithTimeZoneFormatValidator(AbstractDateTimeFormatValidator):

def validate(self, datetime_obj, display_format, serializer_field):
time_only_format = 'HH:mm:ssZZ'
time_only_format = 'HH:mm:ss.SSSZZ'
time_str = arrow.get('2013-09-30T15:34:00.000-07:00').format(time_only_format)
parsed_time_str = arrow.get(time_str, time_only_format)
if parsed_time_str.date() != datetime_obj.date():
Expand All @@ -180,6 +180,15 @@ def validate(self, datetime_obj, display_format, serializer_field):
return super().validate(datetime_obj, display_format, serializer_field)


class DurationFormatValidator(AbstractDateTimeFormatValidator):

def validate(self, datetime_obj, display_format, serializer_field):
if 'z' in display_format.lower():
raise serializers.ValidationError(
"Duration column cannot contain timezone display format"
)


class DateDisplayOptionSerializer(MathesarErrorMessageMixin, OverrideRootPartialMixin, serializers.Serializer):
format = serializers.CharField(validators=[DateFormatValidator()])

Expand Down Expand Up @@ -216,6 +225,10 @@ class TimeWithoutTimezoneDisplayOptionSerializer(
format = serializers.CharField(validators=[TimeWithoutTimeZoneFormatValidator()])


class DurationDisplayOptionSerializer(MathesarErrorMessageMixin, OverrideRootPartialMixin, serializers.Serializer):
format = serializers.CharField(validators=[DurationFormatValidator()])


class DisplayOptionsMappingSerializer(
MathesarErrorMessageMixin,
ReadWritePolymorphicSerializerMappingMixin,
Expand All @@ -231,6 +244,7 @@ class DisplayOptionsMappingSerializer(
('date', MathesarTypeIdentifier.DATETIME.value): DateDisplayOptionSerializer,
('time with time zone', MathesarTypeIdentifier.DATETIME.value): TimeWithTimezoneDisplayOptionSerializer,
('time without time zone', MathesarTypeIdentifier.DATETIME.value): TimeWithoutTimezoneDisplayOptionSerializer,
MathesarTypeIdentifier.DURATION.value: DurationDisplayOptionSerializer,
}

def get_mapping_field(self):
Expand Down
1 change: 1 addition & 0 deletions mathesar/tests/api/test_column_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def test_column_create_invalid_default(column_test_table, client):
("BOOLEAN", {"input": "dropdown"}),
("BOOLEAN", {"input": "checkbox", "custom_labels": {"TRUE": "yes", "FALSE": "no"}}),
("DATE", {'format': 'YYYY-MM-DD'}),
("INTERVAL", {'format': 'DD HH:mm:ss.SSS'}),
("NUMERIC", {"show_as_percentage": True}),
("NUMERIC", {"show_as_percentage": True, "locale": "en_US"}),
("TIMESTAMP WITH TIME ZONE", {'format': 'YYYY-MM-DD hh:mm'}),
Expand Down

0 comments on commit 02e22cb

Please sign in to comment.