예제 #1
0
파일: cohort.py 프로젝트: PostHog/posthog
    def update(self, cohort: Cohort, validated_data: Dict, *args: Any,
               **kwargs: Any) -> Cohort:  # type: ignore
        request = self.context["request"]
        cohort.name = validated_data.get("name", cohort.name)
        cohort.description = validated_data.get("description",
                                                cohort.description)
        cohort.groups = validated_data.get("groups", cohort.groups)
        cohort.is_static = validated_data.get("is_static", cohort.is_static)
        deleted_state = validated_data.get("deleted", None)

        is_deletion_change = deleted_state is not None and cohort.deleted != deleted_state
        if is_deletion_change:
            cohort.deleted = deleted_state

        if not cohort.is_static and not is_deletion_change:
            cohort.is_calculating = True
        cohort.save()

        if not deleted_state:
            if cohort.is_static:
                # You can't update a static cohort using the trend/stickiness thing
                if request.FILES.get("csv"):
                    self._calculate_static_by_csv(request.FILES["csv"], cohort)
            else:
                # Increment based on pending versions
                pending_version = get_and_update_pending_version(cohort)

                calculate_cohort_ch.delay(cohort.id, pending_version)

        report_user_action(
            request.user,
            "cohort updated",
            {
                **cohort.get_analytics_metadata(), "updated_by_creator":
                request.user == cohort.created_by
            },
        )

        return cohort
예제 #2
0
    def update(self, cohort: Cohort, validated_data: Dict, *args: Any,
               **kwargs: Any) -> Cohort:  # type: ignore
        request = self.context["request"]
        cohort.name = validated_data.get("name", cohort.name)
        cohort.description = validated_data.get("description",
                                                cohort.description)
        cohort.groups = validated_data.get("groups", cohort.groups)
        cohort.is_static = validated_data.get("is_static", cohort.is_static)
        deleted_state = validated_data.get("deleted", None)

        is_deletion_change = deleted_state is not None and cohort.deleted != deleted_state
        if is_deletion_change:
            cohort.deleted = deleted_state

        if not cohort.is_static and not is_deletion_change:
            cohort.is_calculating = True
        cohort.save()

        if not deleted_state:
            if cohort.is_static:
                # You can't update a static cohort using the trend/stickiness thing
                if request.FILES.get("csv"):
                    self._calculate_static_by_csv(request.FILES["csv"], cohort)
            else:
                if is_clickhouse_enabled():
                    calculate_cohort_ch.delay(cohort.id)
                else:
                    calculate_cohort.delay(cohort.id)

        posthoganalytics.capture(
            request.user.distinct_id,
            "cohort updated",
            {
                **cohort.get_analytics_metadata(), "updated_by_creator":
                request.user == cohort.created_by
            },
        )

        return cohort