Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sc 28125/backend for sheets with ref #1940

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions sefaria/sheets.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tabs throughout this PR seem way too big

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure github is formatting it strangely. The tabs don't look weird on my screen

Original file line number Diff line number Diff line change
Expand Up @@ -784,13 +784,32 @@ def get_top_sheets(limit=3):
query = {"status": "public", "views": {"$gte": 100}}
return sheet_list(query=query, limit=limit)


def get_sheets_for_ref(tref, uid=None, in_collection=None):
def get_collections_with_sheets(sheet_ids):
"""
Return every public collection that has a sheet in `sheet_ids`
"""
collections = CollectionSet({'sheets': {'$in': sheet_ids }, 'listed': True}, hint="sheets_listed")
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

def remove_slug_duplicates(topics):
slugs = []
normalizedTopics = []
for t in topics:
if t['slug'] not in slugs:
slugs.append(t['slug'])
normalizedTopics.append(t)
return normalizedTopics
def get_sheets_for_ref(tref, uid=None, in_collection=None, include_collections=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 @@ -827,6 +846,10 @@ def get_sheets_for_ref(tref, uid=None, in_collection=None):
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 All @@ -844,6 +867,7 @@ def get_sheets_for_ref(tref, uid=None, in_collection=None):
collection = Collection().load({"slug": sheet["displayedCollection"]})
sheet["collectionTOC"] = getattr(collection, "toc", None)
topics = add_langs_to_topics(sheet.get("topics", []))
topics = remove_slug_duplicates(topics)
for anchor_ref, anchor_ref_expanded in zip(anchor_ref_list, anchor_ref_expanded_list):
sheet_data = {
"owner": sheet["owner"],
Expand Down Expand Up @@ -873,7 +897,10 @@ def get_sheets_for_ref(tref, uid=None, in_collection=None):
"is_featured": sheet.get("is_featured", False),
"category": "Sheets", # ditto
"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
16 changes: 15 additions & 1 deletion sourcesheets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,9 +1020,23 @@ def sheets_by_ref_api(request, ref):
"""
API to get public sheets by ref.
"""
return jsonResponse(get_sheets_for_ref(ref))
include_collections = bool(int(request.GET.get("include_collections", 0)))
return jsonResponse(get_sheets_for_ref(ref, include_collections=include_collections))


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.
"""
is_range = bool(int(request.GET.get('range', 0)))
if is_range:
refs = [Ref(r) for r in tref.split(",")]
tref = refs[0].to(refs[-1]).normal()
he_tref = Ref(tref).he_normal()
normal_ref = tref if request.interfaceLang == "english" else he_tref
title = _(f"Sheets with ")+normal_ref+_(" on Sefaria")
return menu_page(request, page="sheetsWithRef", title=title, props={"sheetsWithRef": {"en": tref, "he": he_tref}})

def get_aliyot_by_parasha_api(request, parasha):
response = {"ref":[]};

Expand Down
1 change: 0 additions & 1 deletion static/js/ConnectionsPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,6 @@ ConnectionsPanel.propTypes = {
backButtonSettings: PropTypes.object,
};


const ResourcesList = ({ masterPanelMode, setConnectionsMode, counts }) => {
// A list of Resources in addition to connection
return (
Expand Down
Loading