Skip to content

Commit

Permalink
fix shape api to use corpora when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nsantacruz committed Jun 13, 2022
1 parent 94c8909 commit 487e242
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions reader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,8 +1745,11 @@ def shape_api(request, title):
}
For complex texts or categories, returns a list of dicts.
:param title: A valid node title or a path to a category, separated by /.
"depth" parameter is DEPRECATED. I don't believe it's used but if so, below is the old documenation for it
The "depth" parameter in the query string indicates how many levels in the category tree to descend. Default is 2.
If depth == 0, descends to end of tree
The "dependents" parameter, if true, includes dependent texts. By default, they are filtered out.
"""
from sefaria.model.category import TocCollectionNode
Expand Down Expand Up @@ -1813,20 +1816,21 @@ def _collapse_book_leaf_shapes(leaf_shapes):

# Category
else:
cat = library.get_toc_tree().lookup(title.split("/"))

if not cat:
res = {"error": "No index or category found to match {}".format(title)}
else:
depth = request.GET.get("depth", 2)
include_dependents = request.GET.get("dependents", False)

leaves = cat.get_leaf_nodes() if depth == 0 else [n for n in cat.get_leaf_nodes_to_depth(depth)]
leaves = [n for n in leaves if not isinstance(n, TocCollectionNode)]
if not include_dependents:
leaves = [n for n in leaves if not getattr(n, "dependence", None)]

res = [_simple_shape(jan) for toc_index in leaves for jan in toc_index.get_index_object().nodes.get_leaf_nodes()]
cat_list = title.split("/")
depth = request.GET.get("depth", 2)
include_dependents = request.GET.get("dependents", False)
indexes = []
if len(cat_list) == 1:
# try as corpus
indexes = library.get_indexes_in_corpus(cat_list[0], include_dependant=include_dependents, full_records=True)
if len(indexes) == 0:
cat = library.get_toc_tree().lookup(cat_list) # just used for validating that the cat exists
if not cat:
res = {"error": "No index or category found to match {}".format(title)}
return jsonResponse(res, callback=request.GET.get("callback", None))
indexes = library.get_indexes_in_category_path(cat_list, include_dependant=include_dependents, full_records=True)

res = [_simple_shape(jan) for index in indexes for jan in index.nodes.get_leaf_nodes()]

res = _collapse_book_leaf_shapes(res)
return jsonResponse(res, callback=request.GET.get("callback", None))
Expand Down Expand Up @@ -4192,14 +4196,14 @@ def explore(request, topCat, bottomCat, book1, book2, lang=None):
"Bavli": {
"title": "Talmud",
"heTitle": "התלמוד",
"shapeParam": "Talmud/Bavli",
"shapeParam": "Bavli",
"linkCountParam": "Bavli",
"talmudAddressed": True,
},
"Yerushalmi": {
"title": "Jerusalem Talmud",
"heTitle": "התלמוד ירושלמי",
"shapeParam": "Talmud/Yerushalmi",
"shapeParam": "Yerushalmi",
"linkCountParam": "Yerushalmi",
"talmudAddressed": True,
},
Expand Down

0 comments on commit 487e242

Please sign in to comment.