Skip to content

Commit

Permalink
chore: refactor get_collections_for_sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
stevekaplan123 committed Aug 7, 2024
1 parent ae5f2e8 commit b6e493c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
26 changes: 12 additions & 14 deletions sefaria/sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,24 +784,29 @@ def get_top_sheets(limit=3):
query = {"status": "public", "views": {"$gte": 100}}
return sheet_list(query=query, limit=limit)

def get_collections_with_sheets(sheet_ids):
def get_collections_for_sheets(sheets):
"""
Return every public collection that has a sheet in `sheet_ids`
Annotate a list of `sheets` with a list of public collections that the sheet appears in.
"""
collections = CollectionSet({'sheets': {'$in': sheet_ids }, 'listed': True}, hint="sheets_listed")
ids = list({int(s['id']) for s in sheets})
collections = CollectionSet({'sheets': {'$in': ids}, 'listed': True}, hint="sheets_listed") #Return every public collection that has a sheet in `ids`

sheet_id_to_collections = defaultdict(list)
for collection in collections:
for sheet_id in collection.sheets:
sheet_id_to_collections[sheet_id].append({'name': collection.name, 'slug': collection.slug})
return sheet_id_to_collections
sheet_id_to_collections[sheet_id].append(collection)

for sheet in sheets:
collections = sheet_id_to_collections[int(sheet["id"])]
sheet["collections"] = [{'name': collection.name, 'slug': collection.slug} for collection in collections]
return sheets

def get_sheets_for_ref(tref, uid=None, in_collection=None, include_collections=None):
def get_sheets_for_ref(tref, uid=None, in_collection=None):
"""
Returns a list of sheets that include ref,
formating as need for the Client Sidebar.
If `uid` is present return user sheets, otherwise return public sheets.
If `in_collection` (list of slugs) is present, only return sheets in one of the listed collections.
If `include_collections` is present, given the list of sheets that include tref, return all public collections that have at least one of those sheets
"""
oref = model.Ref(tref)
# perform initial search with context to catch ranges that include a segment ref
Expand Down Expand Up @@ -840,10 +845,6 @@ def get_sheets_for_ref(tref, uid=None, in_collection=None, include_collections=N
user_profiles[profile]["profile_pic_url_small"] = ""

results = []
sheet_id_to_collection = {}
if include_collections:
sheet_id_to_collection = get_collections_with_sheets([s['id'] for s in sheets])

for sheet in sheets:
anchor_ref_list, anchor_ref_expanded_list = oref.get_all_anchor_refs(segment_refs, sheet.get("includedRefs", []), sheet.get("expandedRefs", []))
ownerData = user_profiles.get(sheet["owner"], {'first_name': 'Ploni', 'last_name': 'Almoni', 'email': '[email protected]', 'slug': 'Ploni-Almoni', 'id': None, 'profile_pic_url_small': ''})
Expand Down Expand Up @@ -892,9 +893,6 @@ def get_sheets_for_ref(tref, uid=None, in_collection=None, include_collections=N
"type": "sheet", # ditto
"dateCreated": sheet.get("dateCreated", None)
}
if include_collections:
sheet_data["collections"] = sheet_id_to_collection[sheet["id"]]

results.append(sheet_data)
return results

Expand Down
5 changes: 4 additions & 1 deletion sourcesheets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,10 @@ def sheets_by_ref_api(request, ref):
API to get public sheets by ref.
"""
include_collections = bool(int(request.GET.get("include_collections", 0)))
return jsonResponse(get_sheets_for_ref(ref, include_collections=include_collections))
sheets = get_sheets_for_ref(ref)
if include_collections:
sheets = get_collections_for_sheets(sheets)
return jsonResponse(sheets)
def sheets_with_ref(request, tref):
"""
Accepts tref as a string which is expected to be in the format of a ref or refs separated by commas, indicating a range.
Expand Down
5 changes: 1 addition & 4 deletions static/js/SidebarSearch.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { useState, useEffect } from "react";
import {InterfaceText, EnglishText, HebrewText} from "./Misc";
import Sefaria from "./sefaria/sefaria";
import SearchState from './sefaria/searchState';
import {SearchResultList} from './SearchResultList';
import DictionarySearch from './DictionarySearch';
import classNames from 'classnames';

import {ElasticSearchQuerier} from "./ElasticSearchQuerier";
import {
SearchButton,
} from './Misc';
import {ElasticSearchQuerier} from "./ElasticSearchQuerier";


const SidebarSearch = ({ title, updateAppliedOptionSort, navigatePanel, sidebarSearchQuery, setSidebarSearchQuery, onSidebarSearchClick }) => {
Expand Down

0 comments on commit b6e493c

Please sign in to comment.