Skip to content

Commit

Permalink
feat(api texts): make api work with lexicons.
Browse files Browse the repository at this point in the history
  • Loading branch information
YishaiGlasner committed Sep 6, 2023
1 parent d2c7692 commit 3114834
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _handle_warnings(self, data):
return data

def get(self, request, *args, **kwargs):
if self.oref.is_empty():
if self.oref.is_empty() and not self.oref.index_node.is_virtual:
return jsonResponse({'error': f'We have no text for {self.oref}.'}, status=400)
versions_params = request.GET.getlist('version', [])
if not versions_params:
Expand Down
10 changes: 8 additions & 2 deletions sefaria/model/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ def __call__(cls, *args, **kwargs):
class TextRange:

def __init__(self, oref, lang, vtitle):
if isinstance(oref.index_node, JaggedArrayNode): #text cannot be SchemaNode
if isinstance(oref.index_node, JaggedArrayNode) or isinstance(oref.index_node, DictionaryEntryNode): #text cannot be SchemaNode
self.oref = oref
elif oref.has_default_child(): #use default child:
self.oref = oref.default_child_ref()
Expand Down Expand Up @@ -1787,7 +1787,10 @@ def _trim_text(self, text):
@property
def text(self):
if self._text is None:
self._text = self._trim_text(self.version.content_node(self.oref.index_node)) #todo if there is no version it will fail
if self.oref.index_node.is_virtual:
self._text = self.oref.index_node.get_text()
else:
self._text = self._trim_text(self.version.content_node(self.oref.index_node)) #todo if there is no version it will fail
return self._text


Expand Down Expand Up @@ -4497,6 +4500,9 @@ def part_projection(self):
# todo: reimplement w/ aggregation pipeline (see above)
# todo: special case string 0?

if self.index_node.is_virtual:
return

projection = {k: 1 for k in Version.required_attrs + Version.optional_attrs}
del projection[Version.content_attr] # Version.content_attr == "chapter"
projection["_id"] = 0
Expand Down
5 changes: 3 additions & 2 deletions sefaria/model/text_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _append_version(self, version):

def _append_required_versions(self, lang: str, vtitle: str) -> None:
if lang == self.BASE:
lang_condition = lambda v: getattr(v, 'isBaseText2', False) # temporal name
lang_condition = lambda v: getattr(v, 'isBaseText', False)
elif lang == self.SOURCE:
lang_condition = lambda v: getattr(v, 'isSource', False)
elif lang == self.TRANSLATION:
Expand Down Expand Up @@ -108,8 +108,9 @@ def _add_node_data_to_return_obj(self) -> None:
'heTitle': inode.full_title("he"),
'titleVariants': inode.all_tree_titles("en"),
'heTitleVariants': inode.all_tree_titles("he"),
'index_offsets_by_depth': inode.trim_index_offsets_by_sections(self.oref.sections, self.oref.toSections),
})
if not inode.is_virtual:
self.return_obj['index_offsets_by_depth'] = inode.trim_index_offsets_by_sections(self.oref.sections, self.oref.toSections)

def get_versions_for_query(self) -> dict:
for lang, vtitle in self.versions_params:
Expand Down

0 comments on commit 3114834

Please sign in to comment.