Skip to content
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

Merged
merged 8 commits into from
Dec 19, 2023

Conversation

joeyorlando
Copy link
Contributor

Which issue(s) this PR fixes

Closes https://github.com/grafana/oncall-private/issues/2373

Checklist

  • Unit, integration, and e2e (if applicable) tests updated
  • Documentation added (or pr:no public docs PR label added if not required)
  • CHANGELOG.md updated (or pr:no changelog PR label added if not required)

issue related to updating from_time and to_time
@joeyorlando joeyorlando added the pr:no public docs Added to a PR that does not require public documentation updates label Dec 18, 2023
@joeyorlando joeyorlando requested a review from a team December 18, 2023 15:28
Comment on lines -105 to -123
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


Copy link
Contributor Author

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
Copy link
Contributor Author

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.

@joeyorlando joeyorlando requested a review from a team December 19, 2023 13:34
Comment on lines -81 to +87
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]
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main focus of PR

@joeyorlando joeyorlando merged commit 006682d into dev Dec 19, 2023
21 checks passed
@joeyorlando joeyorlando deleted the jorlando/fix-put-escalation-policies-bug branch December 19, 2023 14:13
brojd pushed a commit that referenced this pull request Sep 18, 2024
…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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:no public docs Added to a PR that does not require public documentation updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants