Skip to content

Commit

Permalink
feat(surveys): API for team level survey config (#25542)
Browse files Browse the repository at this point in the history
This PR adds a new survey_config property to the Team database table and updates its corresponding controllers and tests. The /surveys endpoint used by posthog-js is also updated to support retrieving any team level survey config that the library might need to use to customize appearance.


Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Phanatic and github-actions[bot] authored Oct 11, 2024
1 parent 1029715 commit 51253a0
Show file tree
Hide file tree
Showing 24 changed files with 333 additions and 7 deletions.
2 changes: 1 addition & 1 deletion latest_migrations.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ contenttypes: 0002_remove_content_type_name
ee: 0016_rolemembership_organization_member
otp_static: 0002_throttling
otp_totp: 0002_auto_20190420_0723
posthog: 0486_cohort_last_error_at
posthog: 0487_team_survey_config
sessions: 0001_initial
social_django: 0010_uid_db_index
two_factor: 0007_auto_20201201_1019
33 changes: 33 additions & 0 deletions posthog/api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Meta:
"session_recording_linked_flag", # Compat with TeamSerializer
"session_recording_network_payload_capture_config", # Compat with TeamSerializer
"session_replay_config", # Compat with TeamSerializer
"survey_config",
"access_control", # Compat with TeamSerializer
"week_start_day", # Compat with TeamSerializer
"primary_dashboard", # Compat with TeamSerializer
Expand Down Expand Up @@ -159,6 +160,7 @@ class Meta:
"session_recording_linked_flag",
"session_recording_network_payload_capture_config",
"session_replay_config",
"survey_config",
"access_control",
"week_start_day",
"primary_dashboard",
Expand Down Expand Up @@ -269,6 +271,37 @@ def update(self, instance: Project, validated_data: dict[str, Any]) -> Project:
team_before_update = team.__dict__.copy()
project_before_update = instance.__dict__.copy()

if "survey_config" in validated_data:
if team.survey_config is not None and validated_data.get("survey_config") is not None:
validated_data["survey_config"] = {
**team.survey_config,
**validated_data["survey_config"],
}

if validated_data.get("survey_config") is None:
del team_before_update["survey_config"]

survey_config_changes_between = dict_changes_between(
"Survey",
team_before_update.get("survey_config", {}),
validated_data.get("survey_config", {}),
use_field_exclusions=True,
)
if survey_config_changes_between:
log_activity(
organization_id=cast(UUIDT, instance.organization_id),
team_id=instance.pk,
user=cast(User, self.context["request"].user),
was_impersonated=is_impersonated_session(request),
scope="Survey",
item_id="#",
activity="updated",
detail=Detail(
name="Survey Config",
changes=survey_config_changes_between,
),
)

if (
"session_replay_config" in validated_data
and validated_data["session_replay_config"] is not None
Expand Down
20 changes: 19 additions & 1 deletion posthog/api/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,12 @@ def activity(self, request: request.Request, **kwargs):
return activity_page_response(activity_page, limit, page, request)


class SurveyConfigSerializer(serializers.ModelSerializer):
class Meta:
model = Team
fields = ["survey_config"]


class SurveyAPISerializer(serializers.ModelSerializer):
"""
Serializer for the exposed /api/surveys endpoint, to be used in posthog-js and for headless APIs.
Expand Down Expand Up @@ -732,7 +738,19 @@ def surveys(request: Request):
many=True,
).data

return cors_response(request, JsonResponse({"surveys": surveys}))
serialized_survey_config: dict[str, Any] = {}
if team.survey_config is not None:
serialized_survey_config = SurveyConfigSerializer(team).data

return cors_response(
request,
JsonResponse(
{
"surveys": surveys,
"survey_config": serialized_survey_config.get("survey_config", None),
}
),
)


@contextmanager
Expand Down
34 changes: 34 additions & 0 deletions posthog/api/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Meta:
"session_recording_linked_flag",
"session_recording_network_payload_capture_config",
"session_replay_config",
"survey_config",
"recording_domains",
"inject_web_apps",
"surveys_opt_in",
Expand Down Expand Up @@ -156,6 +157,7 @@ class Meta:
"session_recording_linked_flag",
"session_recording_network_payload_capture_config",
"session_replay_config",
"survey_config",
"effective_membership_level",
"access_control",
"week_start_day",
Expand Down Expand Up @@ -328,6 +330,38 @@ def create(self, validated_data: dict[str, Any], **kwargs) -> Team:
def update(self, instance: Team, validated_data: dict[str, Any]) -> Team:
before_update = instance.__dict__.copy()

if "survey_config" in validated_data:
if instance.survey_config is not None and validated_data.get("survey_config") is not None:
validated_data["survey_config"] = {
**instance.survey_config,
**validated_data["survey_config"],
}

if validated_data.get("survey_config") is None:
del before_update["survey_config"]

survey_config_changes_between = dict_changes_between(
"Survey",
before_update.get("survey_config", {}),
validated_data.get("survey_config", {}),
use_field_exclusions=True,
)

if survey_config_changes_between:
log_activity(
organization_id=cast(UUIDT, instance.organization_id),
team_id=instance.pk,
user=cast(User, self.context["request"].user),
was_impersonated=is_impersonated_session(request),
scope="Survey",
item_id="",
activity="updated",
detail=Detail(
name="Team Survey Config",
changes=survey_config_changes_between,
),
)

if (
"session_replay_config" in validated_data
and validated_data["session_replay_config"] is not None
Expand Down
3 changes: 3 additions & 0 deletions posthog/api/test/__snapshots__/test_action.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -147,6 +148,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -468,6 +470,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down
3 changes: 3 additions & 0 deletions posthog/api/test/__snapshots__/test_annotation.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -115,6 +116,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -369,6 +371,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down
12 changes: 12 additions & 0 deletions posthog/api/test/__snapshots__/test_decide.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -173,6 +174,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -239,6 +241,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -386,6 +389,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -461,6 +465,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -592,6 +597,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -668,6 +674,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -765,6 +772,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -920,6 +928,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -1017,6 +1026,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -1178,6 +1188,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -1292,6 +1303,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down
2 changes: 2 additions & 0 deletions posthog/api/test/__snapshots__/test_early_access_feature.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -168,6 +169,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down
1 change: 1 addition & 0 deletions posthog/api/test/__snapshots__/test_element.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down
8 changes: 8 additions & 0 deletions posthog/api/test/__snapshots__/test_feature_flag.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -675,6 +676,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -1043,6 +1045,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -1184,6 +1187,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -1484,6 +1488,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -1601,6 +1606,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -1672,6 +1678,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down Expand Up @@ -1736,6 +1743,7 @@
"posthog_team"."session_recording_linked_flag",
"posthog_team"."session_recording_network_payload_capture_config",
"posthog_team"."session_replay_config",
"posthog_team"."survey_config",
"posthog_team"."capture_console_log_opt_in",
"posthog_team"."capture_performance_opt_in",
"posthog_team"."surveys_opt_in",
Expand Down
Loading

0 comments on commit 51253a0

Please sign in to comment.