-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
stevekaplan123
wants to merge
11
commits into
modularization-main
from
feature/sc-28125/backend-for-sheets-with-ref
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1b0cb82
chore: refactor Card component to use InterfaceText
stevekaplan123 0fd9b61
Merge branch 'modularization-main' of https://github.com/Sefaria/Sefa…
stevekaplan123 04b9b2a
chore: add collections to sheet_data in get_sheets_for_ref
stevekaplan123 c94ce25
feat(Sheets API): for /api/sheets/ref added get_collections_with_shee…
stevekaplan123 0a932a3
chore: remove GenericComponents
stevekaplan123 b007ab5
chore: only return dateCreated if include_collections is True
stevekaplan123 76b309b
chore: /api/sheets/ref takes include_collections as param
stevekaplan123 3a9511b
chore: remove duplicates from SheetWithRef filter counts and re-facto…
stevekaplan123 bd90e48
chore: sheets_with_ref route accepts range param
stevekaplan123 4090e99
chore: duplicates only in topics case not in collections
stevekaplan123 d826e6b
Merge branch 'modularization-main' into feature/sc-28125/backend-for-…
stevekaplan123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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': ''}) | ||
|
@@ -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"], | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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