2022-01-18 15:38:55 +01:00
|
|
|
from django.utils import timezone
|
2022-02-02 15:50:28 +01:00
|
|
|
from rest_framework import serializers
|
2021-06-03 15:22:16 +02:00
|
|
|
|
|
|
|
from ee.models.event_definition import EnterpriseEventDefinition
|
|
|
|
from posthog.api.shared import UserBasicSerializer
|
2022-02-18 17:47:05 +01:00
|
|
|
from posthog.api.tagged_item import TaggedItemSerializerMixin
|
2023-10-26 12:38:15 +02:00
|
|
|
from posthog.models.activity_logging.activity_log import (
|
|
|
|
dict_changes_between,
|
|
|
|
log_activity,
|
|
|
|
Detail,
|
|
|
|
)
|
2021-06-03 15:22:16 +02:00
|
|
|
|
2024-01-11 13:33:10 +01:00
|
|
|
from loginas.utils import is_impersonated_session
|
|
|
|
|
2021-06-03 15:22:16 +02:00
|
|
|
|
2022-02-18 17:47:05 +01:00
|
|
|
class EnterpriseEventDefinitionSerializer(TaggedItemSerializerMixin, serializers.ModelSerializer):
|
2021-06-10 01:45:42 +02:00
|
|
|
updated_by = UserBasicSerializer(read_only=True)
|
2022-01-18 15:38:55 +01:00
|
|
|
verified_by = UserBasicSerializer(read_only=True)
|
2022-06-17 17:18:41 +02:00
|
|
|
created_by = UserBasicSerializer(read_only=True)
|
|
|
|
is_action = serializers.SerializerMethodField(read_only=True)
|
|
|
|
action_id = serializers.IntegerField(read_only=True)
|
|
|
|
is_calculating = serializers.BooleanField(read_only=True)
|
|
|
|
last_calculated_at = serializers.DateTimeField(read_only=True)
|
2022-06-24 16:26:31 +02:00
|
|
|
last_updated_at = serializers.DateTimeField(read_only=True)
|
2022-06-17 17:18:41 +02:00
|
|
|
post_to_slack = serializers.BooleanField(default=False)
|
2021-06-03 15:22:16 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = EnterpriseEventDefinition
|
|
|
|
fields = (
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"owner",
|
|
|
|
"description",
|
|
|
|
"tags",
|
2021-12-08 17:03:34 +01:00
|
|
|
"created_at",
|
2021-06-03 15:22:16 +02:00
|
|
|
"updated_at",
|
|
|
|
"updated_by",
|
2021-12-08 17:03:34 +01:00
|
|
|
"last_seen_at",
|
2022-06-24 16:26:31 +02:00
|
|
|
"last_updated_at",
|
2022-01-18 15:38:55 +01:00
|
|
|
"verified",
|
|
|
|
"verified_at",
|
|
|
|
"verified_by",
|
2022-06-17 17:18:41 +02:00
|
|
|
# Action fields
|
|
|
|
"is_action",
|
|
|
|
"action_id",
|
|
|
|
"is_calculating",
|
|
|
|
"last_calculated_at",
|
|
|
|
"created_by",
|
|
|
|
"post_to_slack",
|
2021-06-03 15:22:16 +02:00
|
|
|
)
|
2021-12-08 17:03:34 +01:00
|
|
|
read_only_fields = [
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"created_at",
|
|
|
|
"updated_at",
|
|
|
|
"last_seen_at",
|
2022-06-24 16:26:31 +02:00
|
|
|
"last_updated_at",
|
2022-01-18 15:38:55 +01:00
|
|
|
"verified_at",
|
|
|
|
"verified_by",
|
2022-06-17 17:18:41 +02:00
|
|
|
# Action fields
|
|
|
|
"is_action",
|
|
|
|
"action_id",
|
|
|
|
"is_calculating",
|
|
|
|
"last_calculated_at",
|
|
|
|
"created_by",
|
2021-12-08 17:03:34 +01:00
|
|
|
]
|
2021-06-03 15:22:16 +02:00
|
|
|
|
|
|
|
def update(self, event_definition: EnterpriseEventDefinition, validated_data):
|
|
|
|
validated_data["updated_by"] = self.context["request"].user
|
2022-01-18 15:38:55 +01:00
|
|
|
|
|
|
|
if "verified" in validated_data:
|
|
|
|
if validated_data["verified"] and not event_definition.verified:
|
|
|
|
# Verify event only if previously unverified
|
|
|
|
validated_data["verified_by"] = self.context["request"].user
|
|
|
|
validated_data["verified_at"] = timezone.now()
|
|
|
|
validated_data["verified"] = True
|
|
|
|
elif not validated_data["verified"]:
|
|
|
|
# Unverifying event nullifies verified properties
|
|
|
|
validated_data["verified_by"] = None
|
|
|
|
validated_data["verified_at"] = None
|
|
|
|
validated_data["verified"] = False
|
|
|
|
else:
|
|
|
|
# Attempting to re-verify an already verified event, invalid action. Ignore attribute.
|
|
|
|
validated_data.pop("verified")
|
|
|
|
|
2023-04-20 21:28:38 +02:00
|
|
|
before_state = {
|
|
|
|
k: event_definition.__dict__[k] for k in validated_data.keys() if k in event_definition.__dict__
|
|
|
|
}
|
|
|
|
# KLUDGE: if we get a None value for tags, and we're not adding any
|
|
|
|
# then we get an activity log that we went from null to the empty array ¯\_(ツ)_/¯
|
|
|
|
if "tags" not in before_state or before_state["tags"] is None:
|
|
|
|
before_state["tags"] = []
|
|
|
|
|
|
|
|
changes = dict_changes_between("EventDefinition", before_state, validated_data, True)
|
|
|
|
|
|
|
|
log_activity(
|
|
|
|
organization_id=None,
|
|
|
|
team_id=self.context["team_id"],
|
|
|
|
user=self.context["request"].user,
|
|
|
|
item_id=str(event_definition.id),
|
|
|
|
scope="EventDefinition",
|
|
|
|
activity="changed",
|
2024-01-11 13:33:10 +01:00
|
|
|
was_impersonated=is_impersonated_session(self.context["request"]),
|
2023-04-20 21:28:38 +02:00
|
|
|
detail=Detail(name=str(event_definition.name), changes=changes),
|
|
|
|
)
|
2023-04-24 13:18:18 +02:00
|
|
|
|
2021-06-03 15:22:16 +02:00
|
|
|
return super().update(event_definition, validated_data)
|
2021-06-10 01:45:42 +02:00
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
representation = super().to_representation(instance)
|
2022-06-17 17:18:41 +02:00
|
|
|
representation["owner"] = (
|
|
|
|
UserBasicSerializer(instance=instance.owner).data if hasattr(instance, "owner") and instance.owner else None
|
|
|
|
)
|
2021-06-10 01:45:42 +02:00
|
|
|
return representation
|
2022-06-17 17:18:41 +02:00
|
|
|
|
|
|
|
def get_is_action(self, obj):
|
|
|
|
return hasattr(obj, "action_id") and obj.action_id is not None
|