Skip to content

Commit 5f18114

Browse files
authored
Merge pull request #3288 from SEED-platform/3287-add-label-names-to-filter
add label_names filter to properties/labels
2 parents 449f0da + 4d279c7 commit 5f18114

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

seed/tests/test_labels_api_views.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
Unit tests for seed/views/labels.py
88
"""
99

10-
import json
1110
from collections import defaultdict
1211
from datetime import datetime
1312

@@ -149,35 +148,55 @@ def test_labels_inventory_specific_filter_endpoint_provides_IDs_for_records_wher
149148

150149
# Ensures that at least a single label exists to ensure that we are not
151150
# relying on auto-creation of labels for this test to pass.
152-
new_label = Label.objects.create(
151+
new_label_1 = Label.objects.create(
153152
color="red",
154153
name="test_label-a",
155154
super_organization=organization_a,
156155
)
156+
new_label_2 = Label.objects.create(
157+
color="blue",
158+
name="test_label-b",
159+
super_organization=organization_a,
160+
)
157161

158162
# Create 2 properties and 2 tax lots. Then, apply that label to one of each
159163
property_view_factory = FakePropertyViewFactory(organization=organization_a, user=user)
160164
p_view_1 = property_view_factory.get_property_view()
161-
p_view_1.labels.add(new_label)
165+
p_view_1.labels.add(new_label_1)
166+
p_view_2 = property_view_factory.get_property_view()
167+
p_view_2.labels.add(new_label_1)
168+
p_view_2.labels.add(new_label_2)
169+
170+
# create more random properties
171+
property_view_factory.get_property_view()
162172
property_view_factory.get_property_view()
163173

164174
taxlot_view_factory = FakeTaxLotViewFactory(organization=organization_a, user=user)
165175
tl_view_1 = taxlot_view_factory.get_taxlot_view()
166-
tl_view_1.labels.add(new_label)
176+
tl_view_1.labels.add(new_label_1)
177+
# more random tax lotx
167178
taxlot_view_factory.get_taxlot_view()
168179

169180
client = APIClient()
170181
client.login(username=user.username, password='secret')
171182

172183
url = reverse('api:v3:properties-labels')
173-
response_a = client.post(url + '?organization_id={}'.format(organization_a.pk))
174-
data = json.loads(response_a.content)
175-
184+
response_a = client.post(url + f'?organization_id={organization_a.pk}')
185+
data = response_a.json()
176186
for label in data:
177-
if label.get('name') != 'test_label-a':
178-
self.assertCountEqual(label.get('is_applied'), [])
187+
if label.get('name') == 'test_label-a':
188+
self.assertListEqual(label.get('is_applied'), [p_view_1.id, p_view_2.id])
189+
elif label.get('name') == 'test_label-b':
190+
self.assertCountEqual(label.get('is_applied'), [p_view_2.id])
179191
else:
180-
self.assertCountEqual(label.get('is_applied'), [p_view_1.id])
192+
self.assertCountEqual(label.get('is_applied'), [])
193+
194+
# check if we can filter to only label_2 and on p_view 2
195+
response_b = client.post(url + f'?organization_id={organization_a.pk}', data={'selected': [p_view_2.id], 'label_names': [new_label_1.name]})
196+
data = response_b.json()
197+
self.assertEqual(len(data), 1)
198+
self.assertEqual(data[0].get('name'), new_label_1.name)
199+
self.assertListEqual(data[0].get('is_applied'), [p_view_2.id])
181200

182201

183202
class TestUpdateInventoryLabelsAPIView(DeleteModelsTestCase):

seed/views/v3/properties.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ def _move_relationships(self, old_state, new_state):
188188
request_body=AutoSchemaHelper.schema_factory(
189189
{
190190
'selected': ['integer'],
191+
'label_names': ['string'],
191192
},
192-
description='IDs for properties to be checked for which labels are applied.'
193+
description='- selected: Property View IDs to be checked for which labels are applied\n'
194+
'- label_names: list of label names to query'
193195
)
194196
)
195197
@has_perm_class('requires_viewer')
@@ -199,12 +201,25 @@ def labels(self, request):
199201
Returns a list of all labels where the is_applied field
200202
in the response pertains to the labels applied to property_view
201203
"""
202-
labels = Label.objects.filter(
203-
super_organization=self.get_parent_org(self.request)
204-
).order_by("name").distinct()
205-
super_organization = self.get_organization(request)
204+
# labels are attached to the organization, but newly created ones in a suborg are
205+
# part of the suborg. A parent org's label should not be a factor of the current orgs labels,
206+
# but that isn't the current state of the system. This needs to be reworked when
207+
# we deal with accountability hierarchies.
208+
# organization = self.get_organization(request)
209+
super_organization = self.get_parent_org(request)
210+
211+
labels_qs = Label.objects.filter(
212+
super_organization=super_organization
213+
).order_by('name').distinct()
214+
215+
# if labels names is passes, then get only those labels
216+
if request.data.get('label_names', None):
217+
labels_qs = labels_qs.filter(
218+
name__in=request.data.get('label_names')
219+
).order_by('name')
220+
206221
# TODO: refactor to avoid passing request here
207-
return get_labels(request, labels, super_organization, 'property_view')
222+
return get_labels(request, labels_qs, super_organization, 'property_view')
208223

209224
@swagger_auto_schema(
210225
manual_parameters=[AutoSchemaHelper.query_org_id_field(required=True)],

0 commit comments

Comments
 (0)