-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor get_collections_for_sheets
- Loading branch information
1 parent
ae5f2e8
commit b6e493c
Showing
3 changed files
with
17 additions
and
19 deletions.
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,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': '[email protected]', '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 | ||
|
||
|
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