Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion ci/apiv2/test_hashlist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from hashtopolis import Hashlist, Helper, File
import requests

from hashtopolis import Hashlist, Helper, File, HashtopolisConnector, HashtopolisConfig
from utils import BaseTest


Expand Down Expand Up @@ -158,3 +160,27 @@ def test_bulk_archive(self):
def test_bulk_delete(self):
hashlists = [self.create_test_object(delete=False) for i in range(5)]
Hashlist.objects.delete_many(hashlists)

def test_superhashlist_edit_returns_meta_page_total_elements(self):
hashlist1 = self.create_test_object()
hashlist2 = self.create_test_object()

helper = Helper()
superhashlist = helper.create_superhashlist(
name="SuperhashList edit test",
hashlists=[hashlist1, hashlist2]
)
self.delete_after_test(superhashlist)

config = HashtopolisConfig()
connector = HashtopolisConnector('/ui/hashlists',config)
connector.authenticate()

uri = connector._api_endpoint + connector._model_uri + f'/{superhashlist.id}?include=hashlists,hashType'
req = requests.get(uri, headers=connector._headers)
response = req.json()

self.assertIn('meta', response)
self.assertIn('page', response['meta'])
self.assertIn('total_elements', response['meta']['page'])
self.assertEqual(response['meta']['page']['total_elements'], 2)
18 changes: 18 additions & 0 deletions src/inc/apiv2/common/AbstractBaseAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,24 @@ protected static function getOneResource(object $apiClass, object $object, Reque
$links = ["self" => $linksSelf];

$metaData = [];

$toManyRelations = $apiClass::getToManyRelationships();
$hasToManyExpand = false;
$totalExpanded = 0;
foreach ($expands as $expand) {
if (array_key_exists($expand, $toManyRelations)) {
$hasToManyExpand = true;
if (isset($expandResult[$expand])) {
foreach ($expandResult[$expand] as $objectList) {
$totalExpanded += count($objectList);
}
}
}
}
if ($hasToManyExpand) {
$metaData["page"] = ["total_elements" => $totalExpanded];
}

if ($apiClass->permissionErrors !== null) {
$metaData["Include errors"] = $apiClass->permissionErrors;
}
Expand Down
Loading