Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions posthog/api/cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ class CSVConfig:
PERSON_ID_HEADERS = ["person_id", "person-id", "Person .id"]
DISTINCT_ID_HEADERS = ["distinct_id", "distinct-id"]
EMAIL_HEADERS = ["email", "e-mail"]
ENCODING = "utf-8"
# utf-8-sig strips the byte order mark that Excel and Google Sheets glue onto the first header
ENCODING = "utf-8-sig"

class ErrorMessages:
EMPTY_FILE = "CSV file is empty. Please upload a CSV file with at least one row of data."
Expand Down Expand Up @@ -1002,12 +1003,14 @@ def _calculate_static_by_csv(self, file, cohort: Cohort) -> None:
result = self._find_id_column(first_row)

if result is None:
available_headers = [h for h in first_row if h.strip()]
available_headers = [h.strip() for h in first_row if h.strip()]
raise ValidationError(
{
"csv": [
CSVConfig.ErrorMessages.MISSING_ID_COLUMN.format(
columns=", ".join(available_headers) if available_headers else "none"
columns=", ".join(f"'{h}'" for h in available_headers)
if available_headers
else "none"
)
]
}
Expand Down
69 changes: 67 additions & 2 deletions posthog/api/test/test_cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ def test_static_cohort_csv_upload_multicolumn_without_valid_identifier_fails(
response_data = response.json()
self.assertEqual(response_data["attr"], "csv")
self.assertIn("distinct_id", response_data["detail"])
self.assertIn("name, age", response_data["detail"])
self.assertIn("'name', 'age'", response_data["detail"])
self.assertEqual(patch_calculate_cohort_from_list.call_count, 0)

@parameterized.expand([("person-id",), ("person_id",), ("Person .id",)])
Expand Down Expand Up @@ -972,6 +972,71 @@ def test_static_cohort_csv_upload_with_person_uuid_column(
self.assertIn(str(person1.uuid), person_uuids_in_cohort)
self.assertIn(str(person2.uuid), person_uuids_in_cohort)

@patch(
"posthog.tasks.calculate_cohort.calculate_cohort_from_list.delay",
side_effect=calculate_cohort_from_list,
)
def test_static_cohort_csv_upload_multicolumn_with_bom_prefixed_header(self, patch_calculate_cohort_from_list):
person1 = create_person(team=self.team, distinct_ids=["user123"])
person2 = create_person(team=self.team, distinct_ids=["user456"])

csv = SimpleUploadedFile(
"excel_export.csv",
b"\xef\xbb\xbf"
+ f"""person_id,email
{person1.uuid},john@example.com
{person2.uuid},jane@example.com
""".encode(),
content_type="application/csv",
)

response = self.client.post(
f"/api/projects/{self.team.id}/cohorts/",
{"name": "test_bom_multicolumn", "csv": csv, "is_static": True},
format="multipart",
)

self.assertEqual(response.status_code, 201)
cohort = Cohort.objects.get(pk=response.json()["id"])

person_uuids_in_cohort = _cohort_member_uuids(cohort.team_id, cohort)
self.assertEqual(count_cohort_members(cohort.team_id, cohort.pk), 2)
self.assertIn(str(person1.uuid), person_uuids_in_cohort)
self.assertIn(str(person2.uuid), person_uuids_in_cohort)

@patch(
"posthog.tasks.calculate_cohort.calculate_cohort_from_list.delay",
side_effect=calculate_cohort_from_list,
)
def test_static_cohort_csv_upload_single_column_with_bom_prefixed_header(self, patch_calculate_cohort_from_list):
person1 = create_person(team=self.team, distinct_ids=["user123"])
person2 = create_person(team=self.team, distinct_ids=["user456"])

csv = SimpleUploadedFile(
"excel_single_column.csv",
b"\xef\xbb\xbf"
+ f"""person_id
{person1.uuid}
{person2.uuid}
""".encode(),
content_type="application/csv",
)

response = self.client.post(
f"/api/projects/{self.team.id}/cohorts/",
{"name": "test_bom_single_column", "csv": csv, "is_static": True},
format="multipart",
)

self.assertEqual(response.status_code, 201)
cohort = Cohort.objects.get(pk=response.json()["id"])

self.assertEqual(patch_calculate_cohort_from_list.call_args.kwargs["id_type"], "person_id")
person_uuids_in_cohort = _cohort_member_uuids(cohort.team_id, cohort)
self.assertEqual(count_cohort_members(cohort.team_id, cohort.pk), 2)
self.assertIn(str(person1.uuid), person_uuids_in_cohort)
self.assertIn(str(person2.uuid), person_uuids_in_cohort)

@patch(
"posthog.tasks.calculate_cohort.calculate_cohort_from_list.delay",
side_effect=calculate_cohort_from_list,
Expand Down Expand Up @@ -1239,7 +1304,7 @@ def test_static_cohort_csv_upload_multicolumn_without_any_id_fails(self):
self.assertIn("at least one column with a supported ID header", response_data["detail"])
self.assertIn("person_id", response_data["detail"])
self.assertIn("distinct_id", response_data["detail"])
self.assertIn("name, age", response_data["detail"])
self.assertIn("'name', 'age'", response_data["detail"])

@patch("posthog.tasks.calculate_cohort.calculate_cohort_from_list.delay")
def test_static_cohort_csv_upload_empty_file_fails(self, patch_calculate_cohort_from_list):
Expand Down
Loading