Skip to content

Commit

Permalink
feat(cdp): Public templates API (#24802)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Sep 19, 2024
1 parent 059195a commit e331084
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
4 changes: 1 addition & 3 deletions posthog/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,8 @@ def register_grandfathered_environment_nested_viewset(
router.register(r"async_migrations", async_migration.AsyncMigrationsViewset, "async_migrations")
router.register(r"instance_settings", instance_settings.InstanceSettingsViewset, "instance_settings")
router.register(r"kafka_inspector", kafka_inspector.KafkaInspectorViewSet, "kafka_inspector")

router.register("debug_ch_queries/", debug_ch_queries.DebugCHQueries, "debug_ch_queries")


from posthog.api.action import ActionViewSet # noqa: E402
from posthog.api.cohort import CohortViewSet, LegacyCohortViewSet # noqa: E402
from posthog.api.web_experiment import WebExperimentViewSet # noqa: E402
Expand Down Expand Up @@ -502,7 +500,7 @@ def register_grandfathered_environment_nested_viewset(

projects_router.register(
r"hog_function_templates",
hog_function_template.HogFunctionTemplateViewSet,
hog_function_template.PublicHogFunctionTemplateViewSet,
"project_hog_function_templates",
["project_id"],
)
Expand Down
16 changes: 4 additions & 12 deletions posthog/api/hog_function_template.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import structlog
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets
from rest_framework import viewsets, permissions
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.exceptions import NotFound

from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.cdp.templates import HOG_FUNCTION_TEMPLATES
from posthog.cdp.templates.hog_function_template import HogFunctionTemplate, HogFunctionSubTemplate
from posthog.models.hog_functions.hog_function import HogFunction
from posthog.permissions import PostHogFeatureFlagPermission
from rest_framework_dataclasses.serializers import DataclassSerializer


Expand All @@ -28,19 +25,14 @@ class Meta:
dataclass = HogFunctionTemplate


class HogFunctionTemplateViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
scope_object = "INTERNAL" # Keep internal until we are happy to release this GA
queryset = HogFunction.objects.none()
# NOTE: There is nothing currently private about these values
class PublicHogFunctionTemplateViewSet(viewsets.GenericViewSet):
filter_backends = [DjangoFilterBackend]
filterset_fields = ["id", "team", "created_by", "enabled"]

permission_classes = [PostHogFeatureFlagPermission]
posthog_feature_flag = {"hog-functions": ["create", "partial_update", "update"]}

permission_classes = [permissions.AllowAny]
serializer_class = HogFunctionTemplateSerializer

def _get_templates(self):
# TODO: Filtering for status?
data = HOG_FUNCTION_TEMPLATES
return data

Expand Down
5 changes: 5 additions & 0 deletions posthog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
authentication,
capture,
decide,
hog_function_template,
router,
sharing,
signup,
Expand Down Expand Up @@ -228,6 +229,10 @@ def opt_slash_path(route: str, view: Callable, name: Optional[str] = None) -> UR
path("year_in_posthog/2022/<str:user_uuid>/", year_in_posthog.render_2022),
path("year_in_posthog/2023/<str:user_uuid>", year_in_posthog.render_2023),
path("year_in_posthog/2023/<str:user_uuid>/", year_in_posthog.render_2023),
opt_slash_path(
"api/public_hog_function_templates",
hog_function_template.PublicHogFunctionTemplateViewSet.as_view({"get": "list"}),
),
]

if settings.DEBUG:
Expand Down

0 comments on commit e331084

Please sign in to comment.