-
Notifications
You must be signed in to change notification settings - Fork 291
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
fix PUT /api/v1/escalation_policies/<id> issue related to updating from_time and to_time #3581
Conversation
issue related to updating from_time and to_time
class CustomTimeField(fields.TimeField): | ||
def to_representation(self, value): | ||
result = super().to_representation(value) | ||
if result[-1] != "Z": | ||
result += "Z" | ||
return result | ||
|
||
def to_internal_value(self, data): | ||
TIME_FORMAT_LEN = len("00:00:00Z") | ||
if len(data) == TIME_FORMAT_LEN: | ||
try: | ||
time.strptime(data, "%H:%M:%SZ") | ||
except ValueError: | ||
raise BadRequest(detail="Invalid time format, should be '00:00:00Z'") | ||
else: | ||
raise BadRequest(detail="Invalid time format, should be '00:00:00Z'") | ||
return data | ||
|
||
|
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.
this is only used by EscalationPolicySerializer
raise BadRequest(detail="Invalid time format, should be '00:00:00Z'") | ||
else: | ||
raise BadRequest(detail="Invalid time format, should be '00:00:00Z'") | ||
return data |
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.
returning a str
is the main issue here. If you look at fields.TimeField
it returns a time.time
object (which solves the issue seen in the stack trace).
Additionally, we don't need to subclass fields.TimeField
just to validate the time format, we can use the format
and input_format
kwargs instead.
…:grafana/oncall into jorlando/fix-put-escalation-policies-bug
notify_if_time_from = CustomTimeField(required=False, source="from_time") | ||
notify_if_time_to = CustomTimeField(required=False, source="to_time") | ||
|
||
TIME_FORMAT = "%H:%M:%SZ" | ||
notify_if_time_from = serializers.TimeField( | ||
required=False, source="from_time", format=TIME_FORMAT, input_formats=[TIME_FORMAT] | ||
) | ||
notify_if_time_to = serializers.TimeField( | ||
required=False, source="to_time", format=TIME_FORMAT, input_formats=[TIME_FORMAT] | ||
) |
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.
main focus of PR
…om_time and to_time (#3581) # Which issue(s) this PR fixes Closes grafana/oncall-private#2373 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [ ] Documentation added (or `pr:no public docs` PR label added if not required) - [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required)
Which issue(s) this PR fixes
Closes https://github.com/grafana/oncall-private/issues/2373
Checklist
pr:no public docs
PR label added if not required)CHANGELOG.md
updated (orpr:no changelog
PR label added if not required)