Skip to content

Commit

Permalink
Change initiative filters
Browse files Browse the repository at this point in the history
  • Loading branch information
gannetson committed Mar 4, 2022
1 parent db0404e commit c802cc9
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 79 deletions.
99 changes: 60 additions & 39 deletions bluebottle/initiatives/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@ class InitiativeSearchFilter(ElasticSearchFilter):
document = InitiativeDocument

sort_fields = {
'date': ('-created', ),
'activity_date': ({
'activities.status_score': {
'order': 'desc',
'mode': 'max',
'nested': {
'path': 'activities'
'date': ('-created',),
'activity_date': (
{
'activities.status_score': {
'order': 'desc',
'mode': 'max',
'nested': {
'path': 'activities'
}
},
'activities.activity_date': {
'order': 'desc',
'mode': 'max',
'nested': {
'path': 'activities'
}
}
},
'activities.activity_date': {
'order': 'desc',
'mode': 'max',
'nested': {
'path': 'activities'
}
}
}, ),
'alphabetical': ('title_keyword', ),
),
'alphabetical': ('title_keyword',),
}
default_sort_field = 'date'

Expand Down Expand Up @@ -56,37 +58,56 @@ def get_default_filters(self, request):

permission = 'initiatives.api_read_initiative'

user_id = None
if request.user.is_authenticated:
user_id = request.user.pk

public_filter = Term(status='approved') & (
Term(has_public_activities=True) |
Term(has_closed_activities=False) |
Nested(
path='segments',
query=(
Terms(
segments__id=[
segment.id for segment in request.user.segments.filter(closed=True)
] if request.user.is_authenticated else []
Nested(path='owner', query=Term(owner__id=user_id)) |
Nested(path='promoter', query=Term(promoter__id=user_id)) |
Nested(path='activity_managers', query=Term(activity_managers__id=user_id)) |
Nested(path='activity_owners', query=Term(activity_owners__id=user_id)) |
(
Term(has_public_activities=True) |
Term(has_closed_activities=False) |
Nested(
path='segments',
query=(
Terms(
segments__id=[
segment.id for segment in request.user.segments.filter(closed=True)
] if request.user.is_authenticated else []
)
)
)
)
)

if not request.user.has_perm(permission):
filters = [Term(owner_id=request.user.id)]
if 'owner.id' not in fields:
filters.append(public_filter)
elif 'owner.id' in fields and request.user.is_authenticated:
value = request.user.pk
owned_filter = ~Term(status='deleted') & (
Nested(path='owner', query=Term(owner__id=user_id)) |
Nested(path='promoter', query=Term(promoter__id=user_id)) |
Nested(path='activity_managers', query=Term(activity_managers__id=user_id)) |
Nested(path='activity_owners', query=Term(activity_owners__id=user_id))
)

if not request.user.has_perm(permission) and user_id:
# Not allowed to read initiatives through API unless owned.
filters = [owned_filter]
elif 'owner.id' in fields and user_id:
# Filter on user id.
# If owned then show also initiatives that are not approved.
filters = [owned_filter | public_filter]
elif user_id:
# Approved & owner or no closed segments or user has that segment
filters = [public_filter]
else:
# Guest user. Just approved projects without closed activities
filters = [
Nested(path='owner', query=Term(owner__id=value)) |
Nested(path='promoter', query=Term(promoter__id=value)) |
Nested(path='activity_managers', query=Term(activity_managers__id=value)) |
Nested(path='activity_owners', query=Term(activity_owners__id=value)) |
public_filter
Term(status='approved') & (
Term(has_public_activities=True) |
Term(has_closed_activities=False)
)
]
else:
filters = [public_filter]

return filters

def get_filters(self, request):
Expand Down
106 changes: 66 additions & 40 deletions bluebottle/initiatives/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ def test_only_owner_permission_owner(self):
)

response = self.client.get(
self.url + '?filter[owner.id]={}'.format(self.owner.pk),
self.url + f'?filter[owner.id]={self.owner.pk}',
HTTP_AUTHORIZATION="JWT {0}".format(self.owner.get_jwt_token())
)
data = json.loads(response.content)
Expand Down Expand Up @@ -855,84 +855,110 @@ def test_filter_segment(self):
self.assertEqual(data['meta']['pagination']['count'], 1)
self.assertEqual(data['data'][0]['id'], str(first.pk))

def test_filter_closed_segment(self):
closed_segment = SegmentFactory.create(
def _create_closed_segments_initiatives(self):
self.closed_segment = SegmentFactory.create(
closed=True
)
open_segment = SegmentFactory.create(
self.open_segment = SegmentFactory.create(
closed=False
)
self.another_closed_segment = SegmentFactory.create(
closed=True
)

first = InitiativeFactory.create(
self.first = InitiativeFactory.create(
status='approved',
owner=self.owner
)
activity = DateActivityFactory.create(
status='open',
title='hup',
initiative=first,
initiative=self.first,
)
activity.segments.add(closed_segment)
activity.segments.add(open_segment)
activity.save()
activity.segments.add(self.closed_segment)
activity.segments.add(self.another_closed_segment)
activity.segments.add(self.open_segment)

activity = DateActivityFactory.create(
status='open',
initiative=first,
initiative=self.first,
)
activity.segments.add(closed_segment)
activity.save()
activity.segments.add(self.closed_segment)

second = InitiativeFactory.create(
self.second = InitiativeFactory.create(
owner=self.owner,
status='approved'
)
activity = DateActivityFactory.create(
status='open',
initiative=second,
initiative=self.second,
)
activity.segments.add(closed_segment)
activity.segments.add(open_segment)
activity.save()
activity.segments.add(self.closed_segment)
activity.segments.add(self.open_segment)

DateActivityFactory.create(
activity = DateActivityFactory.create(
status='open',
initiative=second,
initiative=self.second,
)
activity.segments.add(open_segment)
activity.segments.add(self.open_segment)

InitiativeFactory.create(
status='approved'
self.third = InitiativeFactory.create(
owner=self.owner,
status='draft'
)
activity = DateActivityFactory.create(
status='draft',
initiative=self.third,
)
activity.segments.add(self.closed_segment)
activity.segments.add(self.open_segment)

user1 = BlueBottleUserFactory.create()
user1.segments.add(closed_segment)
user1.save()

user2 = BlueBottleUserFactory.create()
user2.segments.add(open_segment)
user2.save()
self.fourth = InitiativeFactory.create(
owner=self.owner,
status='approved'
)

def test_filter_closed_segment_owner(self):
self._create_closed_segments_initiatives()
response = self.client.get(
self.url,
user=self.owner
)
data = json.loads(response.content)
self.assertEqual(data['meta']['pagination']['count'], 2)
self.assertEqual(data['meta']['pagination']['count'], 3)
response = self.client.get(
self.url + f'?filter[owner.id]={self.owner.id}',
user=self.owner
)
data = json.loads(response.content)
self.assertEqual(data['meta']['pagination']['count'], 4)

def test_filter_closed_segment_user_without_segment(self):
self._create_closed_segments_initiatives()
user = BlueBottleUserFactory.create()
user.segments.add(self.open_segment)

response = self.client.get(
self.url,
user=user1
user=user
)
data = json.loads(response.content)
self.assertEqual(data['meta']['pagination']['count'], 3)
self.assertEqual(data['meta']['pagination']['count'], 2)

def test_filter_closed_segment_user_with_segment(self):
self._create_closed_segments_initiatives()
user = BlueBottleUserFactory.create()
user.segments.add(self.closed_segment)

response = self.client.get(
self.url,
user=user2
user=user
)
data = json.loads(response.content)
self.assertEqual(data['meta']['pagination']['count'], 2)
self.assertEqual(data['meta']['pagination']['count'], 3)

def test_filter_closed_segment_guest(self):
self._create_closed_segments_initiatives()
response = self.client.get(
self.url
)
Expand All @@ -950,7 +976,7 @@ def test_filter_owner(self):
InitiativeFactory.create_batch(4, status='submitted')

response = self.client.get(
self.url + '?filter[owner.id]={}'.format(self.owner.pk),
self.url + f'?filter[owner.id]={self.owner.pk}',
HTTP_AUTHORIZATION="JWT {0}".format(self.owner.get_jwt_token())
)

Expand All @@ -971,7 +997,7 @@ def test_filter_owner_activity(self):
activity = DateActivityFactory.create(owner=self.owner, initiative=with_activity)

response = self.client.get(
self.url + '?filter[owner.id]={}'.format(self.owner.pk),
self.url + f'?filter[owner.id]={self.owner.pk}',
HTTP_AUTHORIZATION="JWT {0}".format(self.owner.get_jwt_token())
)

Expand Down Expand Up @@ -1038,7 +1064,7 @@ def test_filter_not_owner(self):
InitiativeFactory.create_batch(3, status='approved')

response = self.client.get(
self.url + '?filter[owner.id]={}'.format(self.owner.pk),
self.url + f'?filter[owner.id]={self.owner.pk}',
user=self.visitor
)

Expand All @@ -1055,7 +1081,7 @@ def test_filter_activity_manager(self):
InitiativeFactory.create_batch(4, status='approved')

response = self.client.get(
self.url + '?filter[owner.id]={}'.format(self.owner.pk),
self.url + f'?filter[owner.id]={self.owner.pk}',
HTTP_AUTHORIZATION="JWT {0}".format(self.owner.get_jwt_token())
)

Expand All @@ -1072,7 +1098,7 @@ def test_filter_promoter(self):
InitiativeFactory.create_batch(4, status='approved')

response = self.client.get(
self.url + '?filter[owner.id]={}'.format(self.owner.pk),
self.url + f'?filter[owner.id]={self.owner.pk}',
HTTP_AUTHORIZATION="JWT {0}".format(self.owner.get_jwt_token())
)

Expand All @@ -1089,7 +1115,7 @@ def test_filter_owner_and_activity_manager(self):
InitiativeFactory.create_batch(4, status='approved')

response = self.client.get(
self.url + '?filter[owner.id]={}'.format(self.owner.pk),
self.url + f'?filter[owner.id]={self.owner.pk}',
HTTP_AUTHORIZATION="JWT {0}".format(self.owner.get_jwt_token())
)

Expand Down

0 comments on commit c802cc9

Please sign in to comment.