Skip to content

Commit

Permalink
Merge branch 'master' into add-cdp-airtable-destination
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloc authored Oct 17, 2024
2 parents e632ab9 + 85260e1 commit aa64a55
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 27 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { actions, connect, kea, key, path, props, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { lemonToast } from '@posthog/lemon-ui'
import { actions, connect, kea, key, listeners, path, props, reducers } from 'kea'
import api, { ApiError } from 'lib/api'

import { BooleanVariable, ListVariable, NumberVariable, StringVariable, Variable, VariableType } from '../../types'
import type { addVariableLogicType } from './addVariableLogicType'
Expand Down Expand Up @@ -30,6 +30,7 @@ export const addVariableLogic = kea<addVariableLogicType>([
openModal: (variableType: VariableType) => ({ variableType }),
closeModal: true,
updateVariable: (variable: Variable) => ({ variable }),
save: true,
}),
reducers({
variableType: [
Expand Down Expand Up @@ -97,20 +98,18 @@ export const addVariableLogic = kea<addVariableLogicType>([
},
],
}),
loaders(({ values, actions }) => ({
savedVariable: [
null as null | Variable,
{
save: async () => {
const variable = await api.insightVariables.create(values.variable)

actions.getVariables()
actions.addVariable({ variableId: variable.id, code_name: variable.code_name })
actions.closeModal()
listeners(({ values, actions }) => ({
save: async () => {
try {
const variable = await api.insightVariables.create(values.variable)

return variable
},
},
],
actions.getVariables()
actions.addVariable({ variableId: variable.id, code_name: variable.code_name })
actions.closeModal()
} catch (e: any) {
const error = e as ApiError
lemonToast.error(error.detail ?? error.message)
}
},
})),
])
4 changes: 0 additions & 4 deletions frontend/src/scenes/experiments/ExperimentView/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ export function PageHeaderCustom(): JSX.Element {
launchExperiment,
endExperiment,
archiveExperiment,
setEditExperiment,
loadExperimentResults,
loadSecondaryMetricResults,
createExposureCohort,
Expand All @@ -374,9 +373,6 @@ export function PageHeaderCustom(): JSX.Element {
<>
{experiment && !isExperimentRunning && (
<div className="flex items-center">
<LemonButton type="secondary" className="mr-2" onClick={() => setEditExperiment(true)}>
Edit
</LemonButton>
<LemonButton
type="primary"
data-attr="launch-experiment"
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/scenes/experiments/experimentLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,12 @@ export const experimentLogic = kea<experimentLogicType>([
},
{
key: [Scene.Experiment, experimentId],
name: experiment?.name || 'New',
path: urls.experiment(experimentId || 'new'),
name: experiment?.name || '',
onRename: async (name: string) => {
// :KLUDGE: work around a type error when using asyncActions accessed via a callback passed to selectors()
const logic = experimentLogic({ experimentId })
await logic.asyncActions.updateExperiment({ name })
},
},
],
],
Expand Down
8 changes: 8 additions & 0 deletions posthog/api/insight_variable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import serializers, viewsets
from rest_framework.exceptions import ValidationError

from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.models.insight_variable import InsightVariable
Expand All @@ -22,6 +23,13 @@ def create(self, validated_data):
"".join(n for n in validated_data["name"] if n.isalnum() or n == " ").replace(" ", "_").lower()
)

count = InsightVariable.objects.filter(
team_id=self.context["team_id"], code_name=validated_data["code_name"]
).count()

if count > 0:
raise ValidationError("Variable with name already exists")

return InsightVariable.objects.create(**validated_data)


Expand Down
7 changes: 4 additions & 3 deletions posthog/api/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def update(self, instance: Survey, validated_data):
instance.targeting_flag.active = False
instance.targeting_flag.save()

iteration_count = validated_data.get("iteration_count")
iteration_count = validated_data.get("iteration_count", None)
if (
instance.current_iteration is not None
and iteration_count is not None
Expand All @@ -396,8 +396,9 @@ def update(self, instance: Survey, validated_data):
f"Cannot change survey recurrence to {iteration_count}, should be at least {instance.current_iteration}"
)

instance.iteration_count = iteration_count
instance.iteration_frequency_days = validated_data.get("iteration_frequency_days")
if iteration_count is not None:
instance.iteration_count = iteration_count
instance.iteration_frequency_days = validated_data.get("iteration_frequency_days")

instance = super().update(instance, validated_data)

Expand Down
33 changes: 33 additions & 0 deletions posthog/api/test/test_insight_variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from posthog.models.insight_variable import InsightVariable
from posthog.test.base import APIBaseTest


class TestInsightVariable(APIBaseTest):
def test_create_insight_variable(self):
response = self.client.post(
f"/api/projects/{self.team.pk}/insight_variables/", data={"name": "Test 1", "type": "String"}
)

assert response.status_code == 201

variable = InsightVariable.objects.get(team_id=self.team.pk)

assert variable is not None
assert variable.created_by is not None
assert variable.created_at is not None
assert variable.name == "Test 1"
assert variable.type == "String"
assert variable.code_name == "test_1"

def test_no_duplicate_code_names(self):
InsightVariable.objects.create(team=self.team, name="Test 1", code_name="test_1")

response = self.client.post(
f"/api/projects/{self.team.pk}/insight_variables/", data={"name": "Test 1", "type": "String"}
)

assert response.status_code == 400

variable_count = InsightVariable.objects.filter(team_id=self.team.pk).count()

assert variable_count == 1
15 changes: 14 additions & 1 deletion posthog/api/test/test_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,19 @@ def test_can_create_recurring_survey(self):
assert len(response_data["iteration_start_dates"]) == 2
assert response_data["current_iteration"] == 1

def test_can_create_and_launch_recurring_survey(self):
survey = self._create_recurring_survey()
response = self.client.patch(
f"/api/projects/{self.team.id}/surveys/{survey.id}/",
data={
"start_date": datetime.now() - timedelta(days=1),
},
)
response_data = response.json()
assert response_data["iteration_start_dates"] is not None
assert len(response_data["iteration_start_dates"]) == 2
assert response_data["current_iteration"] == 1

def test_can_set_internal_targeting_flag(self):
survey = self._create_recurring_survey()
response = self.client.patch(
Expand Down Expand Up @@ -2493,7 +2506,7 @@ def test_guards_for_nil_iteration_count(self):
)
assert response.status_code == status.HTTP_200_OK
survey.refresh_from_db()
self.assertIsNone(survey.current_iteration)
self.assertIsNotNone(survey.current_iteration)
response = self.client.patch(
f"/api/projects/{self.team.id}/surveys/{survey.id}/",
data={
Expand Down

0 comments on commit aa64a55

Please sign in to comment.