oncall-engine/engine/apps/api/serializers/resolution_note.py
Vadim Stepanov 16bbfbbe73
Alert list view & caching rework (#216)
* remove cache usage in AlertGroupView

* remove CustomSearchFilter

* remove caching for alerts

* remove readonly db setup

* render templates on alert creation

* serialize only necessary fields on alert groups list

* optimize AlertGroupListSerializer

* return on-demand templating for alerts

* return on-demand templating for alert groups

* use CursorPaginator

* remove templating on alert create

* pass alert to AlertGroupWebRenderer

* alert_count -> alerts_count

* make sql joins after pagination

* add migration

* bring alert.save() back

* fix tests

* fix tests

* fix tests

* add perpage query param

* add cursor pagination to incidents page

* remove cached_render_for_web usage

* post merge fix

* keep cursor

* lint

* remove get_alert_groups_and_days_for_previous_same_period

* fix pagination on navigate

* refine search_fields on AlertGroupView

Co-authored-by: Maxim <hello.makson@gmail.com>
Co-authored-by: Maxim <maxim.mordasov@grafana.com>
2022-07-14 15:19:25 +01:00

57 lines
2 KiB
Python

from rest_framework import serializers
from apps.alerts.models import AlertGroup, ResolutionNote
from apps.api.serializers.user import FastUserSerializer
from common.api_helpers.custom_fields import OrganizationFilteredPrimaryKeyRelatedField
from common.api_helpers.exceptions import BadRequest
from common.api_helpers.mixins import EagerLoadingMixin
class ResolutionNoteSerializer(EagerLoadingMixin, serializers.ModelSerializer):
id = serializers.CharField(read_only=True, source="public_primary_key")
alert_group = OrganizationFilteredPrimaryKeyRelatedField(
filter_field="channel__organization",
queryset=AlertGroup.unarchived_objects,
)
text = serializers.CharField(allow_null=False, source="message_text")
author = FastUserSerializer(read_only=True)
SELECT_RELATED = ["resolution_note_slack_message", "author"]
class Meta:
model = ResolutionNote
fields = [
"id",
"alert_group",
"source",
"author",
"created_at",
"text",
]
read_only_fields = [
"author",
"created_at",
"source",
]
def create(self, validated_data):
validated_data["author"] = self.context["request"].user
validated_data["source"] = ResolutionNote.Source.WEB
created_instance = super().create(validated_data)
return created_instance
def to_representation(self, instance):
result = super().to_representation(instance)
result["text"] = instance.text
result["source"] = {"id": instance.source, "display_name": instance.get_source_display()}
return result
class ResolutionNoteUpdateSerializer(ResolutionNoteSerializer):
alert_group = OrganizationFilteredPrimaryKeyRelatedField(read_only=True)
def update(self, instance, validated_data):
if instance.source != ResolutionNote.Source.WEB:
raise BadRequest(detail="Cannot update message with this source type")
return super().update(instance, validated_data)