Skip to content

Commit

Permalink
Merge pull request #110 from RDFLib/edmond/fix-vocab-listing-ordering
Browse files Browse the repository at this point in the history
Fix ordering of listing items that are "top level items"
  • Loading branch information
edmondchuc authored Jul 14, 2023
2 parents 526df0f + 7f12130 commit d40e299
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions prez/models/profiles_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ProfileItem(BaseModel):
classes: Optional[Set[URIRef]] = frozenset([PROF.Profile])
id: Optional[str] = None
link_constructor: str = "/profiles"
label: str = None

# general_class: Optional[URIRef] = None
# url_path: Optional[str] = None
Expand All @@ -37,4 +38,6 @@ def populate(cls, values):
r = profiles_graph_cache.query(q)
if len(r.bindings) > 0:
values["classes"] = frozenset([prof.get("class") for prof in r.bindings])

values["label"] = profiles_graph_cache.value(URIRef(values["uri"]), URIRef("http://www.w3.org/ns/dx/conneg/altr-ext#hasLabelPredicate"))
return values
14 changes: 11 additions & 3 deletions prez/sparql/objects_listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
VocabMembers,
SearchMethod,
)
from prez.models.profiles_item import ProfileItem
from prez.models.profiles_listings import ProfilesMembers
from prez.services.curie_functions import get_uri_for_curie_id

Expand All @@ -27,14 +28,16 @@

def generate_listing_construct(
focus_item,
profile,
profile: URIRef,
page: Optional[int] = 1,
per_page: Optional[int] = 20,
):
"""
For a given URI, finds items with the specified relation(s).
Generates a SPARQL construct query for a listing of items
"""
profile_item = ProfileItem(uri=str(profile))

if isinstance(
focus_item, (ProfilesMembers, CatalogMembers, SpatialMembers, VocabMembers)
): # listings can include
Expand Down Expand Up @@ -112,10 +115,15 @@ def generate_listing_construct(
{generate_relative_properties("select", relative_properties, inbound_children, inbound_parents,
outbound_children, outbound_parents)}\
{{
SELECT ?top_level_item
SELECT ?top_level_item ?child_item
WHERE {{
{f'{uri_or_tl_item} a <{focus_item.general_class}> .{chr(10)}' if focus_item.top_level_listing else ""}\
{f'{uri_or_tl_item} a <{focus_item.general_class}> .{chr(10)}' if focus_item.top_level_listing else generate_outbound_predicates(uri_or_tl_item, outbound_children, outbound_parents)}\
OPTIONAL {{
{f'{uri_or_tl_item} <{profile_item.label}> ?label .' if focus_item.top_level_listing else ""}\
}}
}}
{'ORDER BY ASC(?label)' if profile_item.label else "ORDER BY ?top_level_item"}
{f"LIMIT {per_page}{chr(10)}"
f"OFFSET {(page - 1) * per_page}" if page is not None and per_page is not None else ""}
}}
Expand Down

0 comments on commit d40e299

Please sign in to comment.