2023-06-12 18:50:33 +02:00
|
|
|
import datetime
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
import typing
|
2023-06-12 18:50:33 +02:00
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
|
|
from apps.schedules.ical_utils import list_users_to_notify_from_ical
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
from common.api_helpers.custom_fields import (
|
|
|
|
|
SlackChannelsFilteredByOrganizationSlackWorkspaceField,
|
|
|
|
|
SlackUserGroupsFilteredByOrganizationSlackWorkspaceField,
|
|
|
|
|
TeamPrimaryKeyRelatedField,
|
|
|
|
|
)
|
2024-10-11 16:39:39 -03:00
|
|
|
from common.api_helpers.mixins import EagerLoadingMixin
|
2022-06-03 08:09:47 -06:00
|
|
|
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
|
from apps.schedules.models import OnCallSchedule
|
2022-06-03 08:09:47 -06:00
|
|
|
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
|
|
|
|
|
class SlackSerializer(serializers.Serializer):
|
|
|
|
|
channel_id = SlackChannelsFilteredByOrganizationSlackWorkspaceField(required=False, allow_null=True)
|
|
|
|
|
user_group_id = SlackUserGroupsFilteredByOrganizationSlackWorkspaceField(required=False, allow_null=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: update the following once we bump mypy to 1.11 (which supports generics)
|
|
|
|
|
# class ScheduleBaseSerializer[M: "OnCallSchedule"](EagerLoadingMixin, serializers.ModelSerializer[M]):
|
2024-10-11 16:39:39 -03:00
|
|
|
class ScheduleBaseSerializer(EagerLoadingMixin, serializers.ModelSerializer):
|
2022-06-03 08:09:47 -06:00
|
|
|
id = serializers.CharField(read_only=True, source="public_primary_key")
|
|
|
|
|
on_call_now = serializers.SerializerMethodField()
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
slack = SlackSerializer(required=False)
|
2022-06-03 08:09:47 -06:00
|
|
|
team_id = TeamPrimaryKeyRelatedField(required=False, allow_null=True, source="team")
|
|
|
|
|
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
SELECT_RELATED = ["team", "user_group", "slack_channel"]
|
2024-10-11 16:39:39 -03:00
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
def create(self, validated_data):
|
|
|
|
|
validated_data = self._correct_validated_data(validated_data)
|
|
|
|
|
validated_data["organization"] = self.context["request"].auth.organization
|
|
|
|
|
return super().create(validated_data)
|
|
|
|
|
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
def get_on_call_now(self, obj: "OnCallSchedule") -> typing.List[str]:
|
2024-10-16 10:01:21 -03:00
|
|
|
users_on_call = list_users_to_notify_from_ical(obj, datetime.datetime.now(datetime.timezone.utc))
|
2022-06-06 16:07:28 -03:00
|
|
|
if users_on_call is not None:
|
|
|
|
|
return [user.public_primary_key for user in users_on_call]
|
2022-06-03 08:09:47 -06:00
|
|
|
else:
|
2022-06-06 16:07:28 -03:00
|
|
|
return []
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def _correct_validated_data(self, validated_data):
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
if slack_field := validated_data.pop("slack", {}):
|
|
|
|
|
if "channel_id" in slack_field:
|
|
|
|
|
validated_data["slack_channel"] = slack_field["channel_id"]
|
2022-06-03 08:09:47 -06:00
|
|
|
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
if "user_group_id" in slack_field:
|
|
|
|
|
validated_data["user_group"] = slack_field["user_group_id"]
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
return validated_data
|
|
|
|
|
|
feat: convert `schedule.channel` (char field) to `schedule.slack_channel` (foreign key) (#5199)
# What this PR does
`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.
**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)
### Migration
```bash
Running migrations: │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1 │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7). │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel. │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0. │
│ Applying schedules.0019_auto_20241021_1735... OK
```
### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json
{
"name": "Demo testy testy2",
"type": "web",
"time_zone": "America/Los_Angeles",
"slack": {
"channel_id": "C05PPLYN1U1"
}
}
HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
{
"id": "SBBN73UTUTVCE",
"team_id": null,
"name": "Demo testy testy2",
"time_zone": "America/Los_Angeles",
"on_call_now": [],
"shifts": [],
"slack": {
"channel_id": "C05PPLYN1U1",
"user_group_id": null
},
"type": "web"
}
```
### Tested via UI (eg; internal API)
https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00
|
|
|
def to_representation(self, instance: "OnCallSchedule"):
|
|
|
|
|
return {
|
|
|
|
|
**super().to_representation(instance),
|
|
|
|
|
"slack": {
|
|
|
|
|
"channel_id": instance.slack_channel_slack_id,
|
|
|
|
|
"user_group_id": instance.user_group.slack_id if instance.user_group is not None else None,
|
|
|
|
|
},
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
2023-06-05 16:06:10 +02:00
|
|
|
|
|
|
|
|
class FinalShiftQueryParamsSerializer(serializers.Serializer):
|
2023-10-30 22:00:39 +10:00
|
|
|
start_date = serializers.DateTimeField(required=True, input_formats=["%Y-%m-%dT%H:%M", "%Y-%m-%d"])
|
|
|
|
|
end_date = serializers.DateTimeField(required=True, input_formats=["%Y-%m-%dT%H:%M", "%Y-%m-%d"])
|
2023-06-05 16:06:10 +02:00
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
|
if attrs["start_date"] > attrs["end_date"]:
|
|
|
|
|
raise serializers.ValidationError("start_date must be less than or equal to end_date")
|
2023-06-12 18:50:33 +02:00
|
|
|
if attrs["end_date"] - attrs["start_date"] > datetime.timedelta(days=365):
|
2023-06-05 16:06:10 +02:00
|
|
|
raise serializers.ValidationError(
|
|
|
|
|
"The difference between start_date and end_date must be less than one year (365 days)"
|
|
|
|
|
)
|
|
|
|
|
return attrs
|