Skip to content

Commit

Permalink
Mailing Lists JSON: Support batching
Browse files Browse the repository at this point in the history
  • Loading branch information
reinhardt authored and thet committed Jan 13, 2025
1 parent f1ea0b2 commit bd97720
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/osha/oira/client/browser/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ def validate_ticket(self):
if ticket != expected:
raise Unauthorized("Invalid ticket")

@property
def page(self):
return int(self.request.get("page", "1"))

@property
def page_limit(self):
return int(self.request.get("page_limit", "10"))

def __call__(self):
"""Returns a json meant to be consumed by pat-autosuggest.
Expand Down Expand Up @@ -178,7 +186,8 @@ def results(self):
user = api.user.get_current()

if (
not q or q in all_users["id"] or q in all_users["text"].lower()
self.page == 1
and (not q or q in all_users["id"] or q in all_users["text"].lower())
) and api.user.has_permission("Manage portal"):
results.append(all_users)

Expand All @@ -198,7 +207,9 @@ def results(self):
else:
query["path"] = "/".join((query["path"], q))

brains = self.filter_permission(catalog(**query), query, user)
b_start = (self.page - 1) * self.page_limit
brains = catalog(**query, b_start=b_start, b_size=self.page_limit)
brains = self.filter_permission(brains, query, user)

for brain in brains:
results.extend(self._get_mailing_lists_for(brain))
Expand Down
58 changes: 58 additions & 0 deletions src/osha/oira/client/tests/test_mailing_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,64 @@ def test_mailing_lists(self):
results,
)

def test_mailing_lists_batching(self):
request = self.request.clone()
request.form = {"user_id": "admin", "page_limit": "2"}
with mock.patch.object(
MailingListsJson,
"addresses_view",
return_value=GroupToAddresses(context=self.portal.client, request=request),
):
view = MailingListsJson(context=self.portal.client, request=request)
results = view.results

# Result = 3, because the "general" mailing list is always added
# on page one for admin users.
self.assertEqual(len(results), 3)
self.assertEqual(
results[0],
{"id": "general|QWxsIHVzZXJz", "text": "All users [0 subscribers]"},
)
self.assertEqual(
results[1],
{
"id": "nl/test/second-survey|U2Vjb25kIFN1cnZleQ==",
"text": "Second Survey (nl/test/second-survey) [0 subscribers]",
},
)
self.assertEqual(
results[2],
{
"id": "nl/ict/software-development|U29mdHdhcmUgZGV2ZWxvcG1lbnQ=",
"text": "Software development (nl/ict/software-development) [0 subscribers]", # noqa: E501
},
)

request.form = {"user_id": "admin", "page_limit": "2", "page": "2"}
with mock.patch.object(
MailingListsJson,
"addresses_view",
return_value=GroupToAddresses(context=self.portal.client, request=request),
):
view = MailingListsJson(context=self.portal.client, request=request)
results = view.results

self.assertEqual(len(results), 2)
self.assertEqual(
results[0],
{
"id": "nl-nl|VGhlIE5ldGhlcmxhbmRzIChubCk=",
"text": "The Netherlands (nl) [0 subscribers]",
},
)
self.assertEqual(
results[1],
{
"id": "nl-fr|VGhlIE5ldGhlcmxhbmRzIChmcik=",
"text": "The Netherlands (fr) [0 subscribers]",
},
)

def test_group_to_addresses(self):
user = Account(loginname="[email protected]", password="secret")
Session.add(user)
Expand Down

0 comments on commit bd97720

Please sign in to comment.