Skip to content

Commit

Permalink
Closes #975 add prroperties to person and fix pagination (#976)
Browse files Browse the repository at this point in the history
* Closes #975 add prroperties to person and fix pagination

* Fix typing
  • Loading branch information
timgl committed Jun 16, 2020
1 parent da9f125 commit 5cdb11b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
8 changes: 4 additions & 4 deletions posthog/api/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.db.models.expressions import Window
from django.db import connection
from django.utils.timezone import now
from typing import Any, Dict, List, Union
from typing import Any, Dict, List, Optional
from django.utils.timezone import now
import json
import pandas as pd
Expand Down Expand Up @@ -145,14 +145,14 @@ def list(self, request: request.Request, *args: Any, **kwargs: Any) -> response.

reverse = request.GET.get('orderBy', '-timestamp') != '-timestamp'
if len(events) > 100:
next_url: Union[bool, str] = '{}{}{}={}'.format(
next_url: Optional[str] = request.build_absolute_uri('{}{}{}={}'.format(
path,
'&' if '?' in path else '?',
'after' if reverse else 'before',
events[99].timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
)
))
else:
next_url = False
next_url = None

return response.Response({
'next': next_url,
Expand Down
5 changes: 4 additions & 1 deletion posthog/api/person.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from posthog.models import Event, Team, Person, PersonDistinctId, Cohort
from posthog.models import Event, Team, Person, PersonDistinctId, Cohort, Filter
from posthog.utils import convert_property_value
from rest_framework import serializers, viewsets, response, request
from rest_framework.decorators import action
Expand All @@ -8,6 +8,7 @@
from .event import EventSerializer
from typing import Union
from .base import CursorPagination as BaseCursorPagination
import json

class PersonSerializer(serializers.HyperlinkedModelSerializer):
last_event = serializers.SerializerMethodField()
Expand Down Expand Up @@ -64,6 +65,8 @@ def _filter_request(self, request: request.Request, queryset: QuerySet, team: Te
queryset = queryset.filter(properties__icontains=' '.join(contains))
if request.GET.get('cohort'):
queryset = queryset.filter(cohort__id=request.GET['cohort'])
if request.GET.get('properties'):
queryset = queryset.filter(Filter(data={'properties': json.loads(request.GET['properties'])}).properties_to_Q())

queryset = queryset.prefetch_related(Prefetch('persondistinctid_set', to_attr='distinct_ids_cache'))
return queryset
Expand Down
2 changes: 1 addition & 1 deletion posthog/api/test/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_pagination(self):
events.append(Event(team=self.team, event='some event', distinct_id='1'))
Event.objects.bulk_create(events)
response = self.client.get('/api/event/?distinct_id=1').json()
self.assertIn('distinct_id=1', response['next'])
self.assertIn('http://testserver/api/event/?distinct_id=1&before=', response['next'])

page2 = self.client.get(response['next']).json()
self.assertEqual(len(page2['results']), 50)
12 changes: 12 additions & 0 deletions posthog/api/test/test_person.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .base import BaseTest
from posthog.models import Person, Event, Cohort
import json

class TestPerson(BaseTest):
TESTS_API = True
Expand All @@ -26,6 +27,17 @@ def test_search(self):
response = self.client.get('/api/person/?search=another@gm').json()
self.assertEqual(len(response['results']), 1)

def test_properties(self):
Person.objects.create(team=self.team, distinct_ids=['distinct_id'], properties={'email': 'someone@gmail.com'})
Person.objects.create(team=self.team, distinct_ids=['distinct_id_2'], properties={'email': 'another@gmail.com'})
Person.objects.create(team=self.team, distinct_ids=['distinct_id_3'], properties={})

response = self.client.get('/api/person/?properties=%s' % json.dumps([{'key': 'email', 'operator': 'is_set', 'value': 'true'}])).json()
self.assertEqual(len(response['results']), 2)

response = self.client.get('/api/person/?properties=%s' % json.dumps([{'key': 'email', 'operator': 'icontains', 'value': 'another@gm'}])).json()
self.assertEqual(len(response['results']), 1)

def test_person_property_names(self):
Person.objects.create(team=self.team, properties={'$browser': 'whatever', '$os': 'Mac OS X'})
Person.objects.create(team=self.team, properties={'random_prop': 'asdf'})
Expand Down

0 comments on commit 5cdb11b

Please sign in to comment.