diff --git a/sefaria/sheets.py b/sefaria/sheets.py index aaa97d7789..1a657e8b32 100755 --- a/sefaria/sheets.py +++ b/sefaria/sheets.py @@ -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 @@ -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': 'test@sefaria.org', 'slug': 'Ploni-Almoni', 'id': None, 'profile_pic_url_small': ''}) @@ -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 diff --git a/sourcesheets/views.py b/sourcesheets/views.py index 37e528e0d3..207ba50532 100644 --- a/sourcesheets/views.py +++ b/sourcesheets/views.py @@ -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. diff --git a/static/js/SidebarSearch.jsx b/static/js/SidebarSearch.jsx index 6217d980a9..24ef47904d 100644 --- a/static/js/SidebarSearch.jsx +++ b/static/js/SidebarSearch.jsx @@ -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 }) => {