Skip to content

Commit 2b36723

Browse files
authored
Merge pull request #206 from canonical/multiple-filters
feat: Add third filter for querying engage pages
2 parents 5bffff9 + c32970b commit 2b36723

File tree

3 files changed

+43
-53
lines changed

3 files changed

+43
-53
lines changed

canonicalwebteam/discourse/app.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ def get_index(
312312
self,
313313
limit=50,
314314
offset=0,
315+
tag_value=None,
315316
key=None,
316317
value=None,
317318
second_key=None,
@@ -323,31 +324,29 @@ def get_index(
323324
- URL map
324325
And set those as properties on this object
325326
"""
326-
if key == "tag":
327+
328+
params = {
329+
"category_id": self.category_id,
330+
"limit": limit,
331+
"offset": offset,
332+
"tag_value": tag_value,
333+
"key": key,
334+
"value": value,
335+
"second_key": second_key,
336+
"second_value": second_value,
337+
}
338+
339+
params = {k: v for k, v in params.items() if v is not None}
340+
341+
if tag_value and not value and not second_value:
327342
list_topics = self.api.get_engage_pages_by_tag(
328343
category_id=self.category_id,
329344
limit=limit,
330345
offset=offset,
331-
tag=value,
332-
)
333-
elif key and second_key:
334-
list_topics = self.api.get_engage_pages_by_param(
335-
category_id=self.category_id,
336-
limit=limit,
337-
offset=offset,
338-
key=key,
339-
value=value,
340-
second_key=second_key,
341-
second_value=second_value,
346+
tag=tag_value,
342347
)
343348
else:
344-
list_topics = self.api.get_engage_pages_by_param(
345-
category_id=self.category_id,
346-
limit=limit,
347-
offset=offset,
348-
key=key,
349-
value=value,
350-
)
349+
list_topics = self.api.get_engage_pages_by_param(**params)
351350

352351
topics = []
353352
for topic in list_topics:

canonicalwebteam/discourse/models.py

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from canonicalwebteam.discourse.exceptions import DataExplorerError
2+
import json
23

34

45
class DiscourseAPI:
@@ -130,6 +131,7 @@ def get_engage_pages_by_param(
130131
offset=0,
131132
second_key=None,
132133
second_value=None,
134+
tag_value=None,
133135
):
134136
"""
135137
Uses data-explorer to query topics with the category
@@ -169,42 +171,31 @@ def get_engage_pages_by_param(
169171
# See https://discourse.ubuntu.com/admin/plugins/explorer?id=16
170172
data_explorer_id = 16
171173

172-
params = (
173-
{
174-
"params": (
175-
f'{{"category_id":"{category_id}", '
176-
f'"limit":"{limit}", "offset":"{offset}"}}'
177-
)
178-
},
179-
)
174+
params_dict = {
175+
"category_id": str(category_id),
176+
"limit": str(limit),
177+
"offset": str(offset),
178+
}
180179

181-
if key and value and second_key and second_value:
182-
params = (
183-
{
184-
"params": (
185-
f'{{"category_id":"{category_id}", '
186-
f'"keyword":"{key}", "value":"{value}", '
187-
f'"second_keyword":"{second_key}", '
188-
f'"second_value":"{second_value}", '
189-
f'"limit":"{limit}", "offset":"{offset}"}}'
190-
)
191-
},
192-
)
193-
elif key and value:
194-
params = (
195-
{
196-
"params": (
197-
f'{{"category_id":"{category_id}", '
198-
f'"keyword":"{key}", "value":"{value}", '
199-
f'"limit":"{limit}", "offset":"{offset}"}}'
200-
)
201-
},
202-
)
180+
# Tags have to be queried differently due to the way they are stored
181+
if tag_value:
182+
params_dict["tag_value"] = str(tag_value)
203183

184+
if key and value:
185+
params_dict["keyword"] = key
186+
params_dict["value"] = value
187+
188+
if second_key and second_value:
189+
params_dict["second_keyword"] = second_key
190+
params_dict["second_value"] = second_value
191+
192+
# Get all engage pages to compile list of tags
193+
# last resort if you need to get all pages, not performant
204194
if limit == -1:
205-
# Get all engage pages to compile list of tags
206-
# last resort if you need to get all pages, not performant
207-
params = ({"params": f'{{"category_id":"{category_id}"}}'},)
195+
params_dict.pop("limit")
196+
params_dict.pop("offset")
197+
198+
params = ({"params": json.dumps(params_dict)},)
208199

209200
response = self.session.post(
210201
f"{self.base_url}/admin/plugins/explorer/"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name="canonicalwebteam.discourse",
7-
version="5.8.0",
7+
version="5.8.1",
88
author="Canonical webteam",
99
author_email="webteam@canonical.com",
1010
url="https://github.com/canonical/canonicalwebteam.discourse",

0 commit comments

Comments
 (0)