Beispiel #1
0
    def create(self, validated_data: Dict, *args: Any, **kwargs: Any) -> Dashboard:
        request = self.context["request"]
        validated_data["created_by"] = request.user
        team = Team.objects.get(id=self.context["team_id"])
        use_template: str = validated_data.pop("use_template", None)
        dashboard = Dashboard.objects.create(team=team, **validated_data)

        if use_template:
            try:
                create_dashboard_from_template(use_template, dashboard)
            except AttributeError:
                raise serializers.ValidationError({"use_template": "Invalid value provided."})

        elif request.data.get("items"):
            for item in request.data["items"]:
                DashboardItem.objects.create(
                    **{key: value for key, value in item.items() if key not in ("id", "deleted", "dashboard", "team")},
                    dashboard=dashboard,
                    team=team,
                )

        posthoganalytics.capture(
            request.user.distinct_id,
            "dashboard created",
            {**dashboard.get_analytics_metadata(), "from_template": bool(use_template), "template_key": use_template},
        )

        return dashboard
Beispiel #2
0
    def create(self, validated_data: Dict, *args: Any,
               **kwargs: Any) -> Dashboard:
        request = self.context["request"]
        validated_data["created_by"] = request.user
        team = Team.objects.get(id=self.context["team_id"])
        use_template: str = validated_data.pop("use_template", None)
        use_dashboard: int = validated_data.pop("use_dashboard", None)
        validated_data = self._update_creation_mode(validated_data,
                                                    use_template,
                                                    use_dashboard)
        tags = validated_data.pop(
            "tags", None
        )  # tags are created separately below as global tag relationships
        dashboard = Dashboard.objects.create(team=team, **validated_data)

        if use_template:
            try:
                create_dashboard_from_template(use_template, dashboard)
            except AttributeError:
                raise serializers.ValidationError(
                    {"use_template": "Invalid value provided."})

        elif use_dashboard:
            try:
                from posthog.api.insight import InsightSerializer

                existing_dashboard = Dashboard.objects.get(id=use_dashboard,
                                                           team=team)
                existing_dashboard_items = existing_dashboard.items.all()
                for dashboard_item in existing_dashboard_items:
                    override_dashboard_item_data = {
                        "id": None,  # to create a new Insight
                        "dashboard": dashboard.pk,
                        "last_refresh": now(),
                    }
                    new_data = {
                        **InsightSerializer(
                            dashboard_item,
                            context=self.context,
                        ).data,
                        **override_dashboard_item_data,
                    }
                    new_tags = new_data.pop("tags", None)
                    insight_serializer = InsightSerializer(
                        data=new_data,
                        context=self.context,
                    )
                    insight_serializer.is_valid()
                    insight_serializer.save()

                    # Create new insight's tags separately. Force create tags on dashboard duplication.
                    self._attempt_set_tags(new_tags,
                                           insight_serializer.instance,
                                           force_create=True)

            except Dashboard.DoesNotExist:
                raise serializers.ValidationError(
                    {"use_dashboard": "Invalid value provided"})

        elif request.data.get("items"):
            for item in request.data["items"]:
                Insight.objects.create(
                    **{
                        key: value
                        for key, value in item.items()
                        if key not in ("id", "deleted", "dashboard", "team")
                    },
                    dashboard=dashboard,
                    team=team,
                )

        # Manual tag creation since this create method doesn't call super()
        self._attempt_set_tags(tags, dashboard)

        report_user_action(
            request.user,
            "dashboard created",
            {
                **dashboard.get_analytics_metadata(),
                "from_template":
                bool(use_template),
                "template_key":
                use_template,
                "duplicated":
                bool(use_dashboard),
                "dashboard_id":
                use_dashboard,
            },
        )

        return dashboard
Beispiel #3
0
    def create(self, validated_data: Dict, *args: Any,
               **kwargs: Any) -> Dashboard:
        request = self.context["request"]
        validated_data["created_by"] = request.user
        team = Team.objects.get(id=self.context["team_id"])
        use_template: str = validated_data.pop("use_template", None)
        use_dashboard: int = validated_data.pop("use_dashboard", None)
        validated_data = self._update_creation_mode(validated_data,
                                                    use_template,
                                                    use_dashboard)
        dashboard = Dashboard.objects.create(team=team, **validated_data)

        if use_template:
            try:
                create_dashboard_from_template(use_template, dashboard)
            except AttributeError:
                raise serializers.ValidationError(
                    {"use_template": "Invalid value provided."})

        elif use_dashboard:
            try:
                from posthog.api.insight import InsightSerializer

                existing_dashboard = Dashboard.objects.get(id=use_dashboard,
                                                           team=team)
                existing_dashboard_items = existing_dashboard.items.all()
                for dashboard_item in existing_dashboard_items:
                    override_dashboard_item_data = {
                        "id": None,  # to create a new Insight
                        "dashboard": dashboard.pk,
                        "last_refresh": now(),
                    }
                    insight_serializer = InsightSerializer(
                        data={
                            **InsightSerializer(
                                dashboard_item,
                                context=self.context,
                            ).data,
                            **override_dashboard_item_data,
                        },
                        context=self.context,
                    )
                    insight_serializer.is_valid()
                    insight_serializer.save()

            except Dashboard.DoesNotExist:
                raise serializers.ValidationError(
                    {"use_dashboard": "Invalid value provided"})

        elif request.data.get("items"):
            for item in request.data["items"]:
                Insight.objects.create(
                    **{
                        key: value
                        for key, value in item.items()
                        if key not in ("id", "deleted", "dashboard", "team")
                    },
                    dashboard=dashboard,
                    team=team,
                )

        posthoganalytics.capture(
            request.user.distinct_id,
            "dashboard created",
            {
                **dashboard.get_analytics_metadata(),
                "from_template":
                bool(use_template),
                "template_key":
                use_template,
                "duplicated":
                bool(use_dashboard),
                "dashboard_id":
                use_dashboard,
            },
        )

        return dashboard