def get_sheet_for_panel(id=None): sheet = get_sheet(id) if "error" in sheet: return sheet if "assigner_id" in sheet: asignerData = public_user_data(sheet["assigner_id"]) sheet["assignerName"] = asignerData["name"] if "viaOwner" in sheet: viaOwnerData = public_user_data(sheet["viaOwner"]) sheet["viaOwnerName"] = viaOwnerData["name"] ownerData = public_user_data(sheet["owner"]) sheet["ownerName"] = ownerData["name"] sheet["ownerProfileUrl"] = public_user_data(sheet["owner"])["profileUrl"] sheet["ownerImageUrl"] = public_user_data(sheet["owner"])["imageUrl"] sheet["naturalDateCreated"] = naturaltime(datetime.strptime(sheet["dateCreated"], "%Y-%m-%dT%H:%M:%S.%f")) sheet["sources"] = annotate_user_links(sheet["sources"]) sheet["topics"] = add_langs_to_topics(sheet.get("topics", [])) if "displayedCollection" in sheet: collection = Collection().load({"slug": sheet["displayedCollection"]}) if collection: sheet["collectionImage"] = getattr(collection, "imageUrl", None) sheet["collectionName"] = collection.name else: del sheet["displayedCollection"] return sheet
def get_sheet_for_panel(id=None): sheet = get_sheet(id) if "error" in sheet: return sheet if "assigner_id" in sheet: asignerData = public_user_data(sheet["assigner_id"]) sheet["assignerName"] = asignerData["name"] if "viaOwner" in sheet: viaOwnerData = public_user_data(sheet["viaOwner"]) sheet["viaOwnerName"] = viaOwnerData["name"] ownerData = public_user_data(sheet["owner"]) sheet["ownerName"] = ownerData["name"] sheet["ownerProfileUrl"] = public_user_data(sheet["owner"])["profileUrl"] sheet["ownerImageUrl"] = public_user_data(sheet["owner"])["imageUrl"] sheet["naturalDateCreated"] = naturaltime( datetime.strptime(sheet["dateCreated"], "%Y-%m-%dT%H:%M:%S.%f")) sheet["sources"] = annotate_user_links(sheet["sources"]) sheet["topics"] = add_langs_to_topics(sheet.get("topics", [])) if "group" in sheet: group = Group().load({"name": sheet["group"]}) try: sheet["groupLogo"] = group.imageUrl except: sheet["groupLogo"] = None return sheet
def contents(self, with_content=False, authenticated=False): from sefaria.sheets import group_sheets, sheet_tag_counts contents = super(Group, self).contents() if with_content: contents["sheets"] = group_sheets(self, authenticated)["sheets"] contents["tags"] = sheet_tag_counts({"group": self.name}) contents["admins"] = [public_user_data(uid) for uid in contents["admins"]] contents["publishers"] = [public_user_data(uid) for uid in contents["publishers"]] contents["members"] = [public_user_data(uid) for uid in contents["members"]] contents["invitations"] = getattr(self, "invitations", []) if authenticated else [] contents["pinnedSheets"] = getattr(self, "pinned_sheets", []) return contents
def contents(self, with_content=False, authenticated=False): from sefaria.sheets import group_sheets, sheet_tag_counts contents = super(Group, self).contents() if with_content: contents["sheets"] = group_sheets(self.name, authenticated)["sheets"] contents["tags"] = sheet_tag_counts({"group": self.name}) contents["admins"] = [public_user_data(uid) for uid in contents["admins"]] contents["publishers"] = [public_user_data(uid) for uid in contents["publishers"]] contents["members"] = [public_user_data(uid) for uid in contents["members"]] contents["invitations"] = getattr(self, "invitations", []) if authenticated else [] contents["pinnedSheets"] = getattr(self, "pinned_sheets", []) return contents
def get_sheet_for_panel(id=None): sheet = get_sheet(id) if "assigner_id" in sheet: asignerData = public_user_data(sheet["assigner_id"]) sheet["assignerName"] = asignerData["name"] if "viaOwner" in sheet: viaOwnerData = public_user_data(sheet["viaOwner"]) sheet["viaOwnerName"] = viaOwnerData["name"] ownerData = public_user_data(sheet["owner"]) sheet["ownerName"] = ownerData["name"] sheet["naturalDateCreated"] = naturaltime(datetime.strptime(sheet["dateCreated"], "%Y-%m-%dT%H:%M:%S.%f")) return sheet
def get_sheet_for_panel(id=None): sheet = get_sheet(id) if "assigner_id" in sheet: asignerData = public_user_data(sheet["assigner_id"]) sheet["assignerName"] = asignerData["name"] if "viaOwner" in sheet: viaOwnerData = public_user_data(sheet["viaOwner"]) sheet["viaOwnerName"] = viaOwnerData["name"] ownerData = public_user_data(sheet["owner"]) sheet["ownerName"] = ownerData["name"] sheet["naturalDateCreated"] = naturaltime( datetime.strptime(sheet["dateCreated"], "%Y-%m-%dT%H:%M:%S.%f")) return sheet
def contents(self, with_content=False, authenticated=False): from sefaria.sheets import sheet_topics_counts contents = super(Collection, self).contents() if with_content: contents["sheets"] = self.sheet_contents(authenticated=authenticated) contents["topics"] = sheet_topics_counts({"id": {"$in": self.sheets}}) contents["admins"] = [public_user_data(uid) for uid in contents["admins"]] contents["members"] = [public_user_data(uid) for uid in contents["members"]] contents["lastModified"] = str(self.lastModified) contents["invitations"] = getattr(self, "invitations", []) if authenticated else [] contents["pinnedSheets"] = getattr(self, "pinned_sheets", []) contents["pinnedTags"] = getattr(self, "pinnedTags", []) return contents
def index_sheet(index_name, id): """ Index source sheet with 'id'. """ sheet = db.sheets.find_one({"id": id}) if not sheet: return False pud = public_user_data(sheet["owner"]) try: doc = { "title": strip_tags(sheet["title"]), "content": make_sheet_text(sheet, pud), "owner_id": sheet["owner"], "owner_name": pud["name"], "owner_image": pud["imageUrl"], "profile_url": pud["profileUrl"], "version": "Source Sheet by " + user_link(sheet["owner"]), "tags": sheet.get("tags", []), "sheetId": id, "summary": sheet.get("summary", None), "group": sheet.get("group", None), "datePublished": sheet.get("datePublished", None), "dateCreated": sheet.get("dateCreated", None), "dateModified": sheet.get("dateCreated", None), "views": sheet.get("views", 0) } es_client.create(index=index_name, doc_type='sheet', id=id, body=doc) global doc_count doc_count += 1 return True except Exception, e: print "Error indexing sheet %d" % id print e return False
def index_sheet(index_name, id): """ Index source sheet with 'id'. """ sheet = db.sheets.find_one({"id": id}) if not sheet: return False pud = public_user_data(sheet["owner"]) doc = { "title": sheet["title"], "content": make_sheet_text(sheet, pud), "owner_id": sheet["owner"], "owner_name": pud["name"], "owner_image": pud["imageUrl"], "profile_url": pud["profileUrl"], "version": "Source Sheet by " + user_link(sheet["owner"]), "tags": ",".join(sheet.get("tags",[])), "sheetId": id, } try: es.index(index_name, 'sheet', doc, id) global doc_count doc_count += 1 except Exception, e: print "Error indexing sheet %d" % id print e
def sheet_to_dict(sheet): """ Returns a JSON serializable dictionary of Mongo document `sheet`. Annotates sheet with user profile info that is useful to client. """ profile = public_user_data(sheet["owner"]) sheet_dict = { "id": sheet["id"], "title": strip_tags(sheet["title"]) if "title" in sheet else "Untitled Sheet", "status": sheet["status"], "author": sheet["owner"], "ownerName": profile["name"], "ownerImageUrl": profile["imageUrl"], "views": sheet["views"], "group": sheet.get("group", None), "modified": dateutil.parser.parse(sheet["dateModified"]).strftime("%m/%d/%Y"), "created": sheet.get("dateCreated", None), "topics": add_langs_to_topics(sheet.get("topics", [])), "tags": [t['asTyped'] for t in sheet.get("topics", []) ], # for backwards compatibility with mobile "options": sheet["options"] if "options" in sheet else [], } return sheet_dict
def sheet_to_dict(sheet): """ Returns a JSON serializable dictionary of Mongo document `sheet`. Annotates sheet with user profile info that is useful to client. """ profile = public_user_data(sheet["owner"]) sheet_dict = { "id": sheet["id"], "title": strip_tags(sheet["title"]) if "title" in sheet else "Untitled Sheet", "status": sheet["status"], "author": sheet["owner"], "ownerName": profile["name"], "ownerImageUrl": profile["imageUrl"], "views": sheet["views"], "modified": dateutil.parser.parse(sheet["dateModified"]).strftime("%m/%d/%Y"), "tags": sheet["tags"] if "tags" in sheet else [], "options": sheet["options"] if "options" in sheet else [], } return sheet_dict
def format_note_object_for_client(note): """ Returns an object that represents note in the format expected by the reader client, matching the format of links, which are currently handled together. """ anchor_oref = Ref(note.ref).padded_ref() ownerData = public_user_data(note.owner) com = { "category": "Notes", "type": "note", "owner": note.owner, "_id": str(note._id), "anchorRef": note.ref, "anchorVerse": anchor_oref.sections[-1], "anchorText": getattr(note, "anchorText", ""), "public": getattr(note, "public", False), "commentator": user_link(note.owner), "text": note.text, "title": getattr(note, "title", ""), "ownerName": ownerData["name"], "ownerProfileUrl": ownerData["profileUrl"], "ownerImageUrl": ownerData["imageUrl"], } return com
def index_sheet(index_name, id): """ Index source sheet with 'id'. """ sheet = db.sheets.find_one({"id": id}) if not sheet: return False pud = public_user_data(sheet["owner"]) doc = { "title": sheet["title"], "content": make_sheet_text(sheet, pud), "owner_id": sheet["owner"], "owner_name": pud["name"], "owner_image": pud["imageUrl"], "profile_url": pud["profileUrl"], "version": "Source Sheet by " + user_link(sheet["owner"]), "tags": ",".join(sheet.get("tags",[])), "sheetId": id, } try: es.index(index_name, 'sheet', doc, id) global doc_count doc_count += 1 return True except Exception, e: print "Error indexing sheet %d" % id print e return False
def get_sheets_for_ref(tref, pad=True, context=1): """ Returns a list of sheets that include ref, formating as need for the Client Sidebar. """ oref = model.Ref(tref) if pad: oref = oref.padded_ref() if context: oref = oref.context_ref(context) ref_re = oref.regex() results = [] regex_list = oref.regex(as_list=True) ref_clauses = [{"sources.ref": {"$regex": r}} for r in regex_list] sheets = db.sheets.find({"$or": ref_clauses, "status": "public"}, {"id": 1, "title": 1, "owner": 1, "sources.ref": 1, "views": 1}).sort([["views", -1]]) for sheet in sheets: matched_refs = [] if "sources" in sheet: for source in sheet["sources"]: if "ref" in source: matched_refs.append(source["ref"]) matched_refs = [r for r in matched_refs if regex.match(ref_re, r)] for match in matched_refs: try: match = model.Ref(match) except InputError: continue ownerData = public_user_data(sheet["owner"]) com = { "category": "Sheets", "type": "sheet", "owner": sheet["owner"], "_id": str(sheet["_id"]), "anchorRef": match.normal(), "anchorVerse": match.sections[-1] if len(match.sections) else 1, "public": True, "commentator": user_link(sheet["owner"]), # legacy, used in S1 "text": "<a class='sheetLink' href='/sheets/%d'>%s</a>" % (sheet["id"], strip_tags(sheet["title"])), # legacy, used in S1 "title": strip_tags(sheet["title"]), "sheetUrl": "/sheets/" + str(sheet["id"]), "ownerName": ownerData["name"], "ownerProfileUrl": ownerData["profileUrl"], "ownerImageUrl": ownerData["imageUrl"], "views": sheet["views"] } results.append(com) return results
def get_sheet_for_panel(id=None): sheet = get_sheet(id) if "assigner_id" in sheet: asignerData = public_user_data(sheet["assigner_id"]) sheet["assignerName"] = asignerData["name"] if "viaOwner" in sheet: viaOwnerData = public_user_data(sheet["viaOwner"]) sheet["viaOwnerName"] = viaOwnerData["name"] ownerData = public_user_data(sheet["owner"]) sheet["ownerName"] = ownerData["name"] sheet["ownerProfileUrl"] = public_user_data(sheet["owner"])["profileUrl"] sheet["ownerImageUrl"] = public_user_data(sheet["owner"])["imageUrl"] sheet["naturalDateCreated"] = naturaltime(datetime.strptime(sheet["dateCreated"], "%Y-%m-%dT%H:%M:%S.%f")) sheet["sources"] = annotate_user_links(sheet["sources"]) if "group" in sheet: group = Group().load({"name": sheet["group"]}) try: sheet["groupLogo"] = group.imageUrl except: sheet["groupLogo"] = None return sheet
def index_sheet(index_name, id): """ Index source sheet with 'id'. """ sheet = db.sheets.find_one({"id": id}) if not sheet: return False pud = public_user_data(sheet["owner"]) tag_terms_simple = make_sheet_tags(sheet) tags = [t["en"] for t in tag_terms_simple] topics = [] for t in sheet.get('topics', []): topic_obj = Topic.init(t['slug']) if not topic_obj: continue topics += [topic_obj] collections = CollectionSet({"sheets": id, "listed": True}) collection_names = [c.name for c in collections] try: doc = { "title": strip_tags(sheet["title"]), "content": make_sheet_text(sheet, pud), "owner_id": sheet["owner"], "owner_name": pud["name"], "owner_image": pud["imageUrl"], "profile_url": pud["profileUrl"], "version": "Source Sheet by " + user_link(sheet["owner"]), "tags": tags, "topic_slugs": [topic_obj.slug for topic_obj in topics], "topics_en": [topic_obj.get_primary_title('en') for topic_obj in topics], "topics_he": [topic_obj.get_primary_title('he') for topic_obj in topics], "sheetId": id, "summary": sheet.get("summary", None), "collections": collection_names, "datePublished": sheet.get("datePublished", None), "dateCreated": sheet.get("dateCreated", None), "dateModified": sheet.get("dateModified", None), "views": sheet.get("views", 0) } es_client.create(index=index_name, doc_type='sheet', id=id, body=doc) global doc_count doc_count += 1 return True except Exception as e: print("Error indexing sheet %d" % id) print(e) return False
def index_sheet(index_name, id): """ Index source sheet with 'id'. """ sheet = db.sheets.find_one({"id": id}) if not sheet: return False pud = public_user_data(sheet["owner"]) tag_terms_simple = make_sheet_tags(sheet) tags = [t["en"] for t in tag_terms_simple] tags_he_and_en = [ u"{}|||{}".format(t["en"], t["he"]) for t in tag_terms_simple ] try: doc = { "title": strip_tags(sheet["title"]), "content": make_sheet_text(sheet, pud), "owner_id": sheet["owner"], "owner_name": pud["name"], "owner_image": pud["imageUrl"], "profile_url": pud["profileUrl"], "version": "Source Sheet by " + user_link(sheet["owner"]), "tags": tags, "tags_he_and_en": tags_he_and_en, "sheetId": id, "summary": sheet.get("summary", None), "group": sheet.get("group", None), "datePublished": sheet.get("datePublished", None), "dateCreated": sheet.get("dateCreated", None), "dateModified": sheet.get("dateCreated", None), "views": sheet.get("views", 0) } es_client.create(index=index_name, doc_type='sheet', id=id, body=doc) global doc_count doc_count += 1 return True except Exception, e: print "Error indexing sheet %d" % id print e return False
def user_message_path(uid): """Returns the relative path to send a message to `uid`""" data = public_user_data(uid) return mark_safe(data["profileUrl"] + "?message=1")
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. """ oref = model.Ref(tref) # perform initial search with context to catch ranges that include a segment ref segment_refs = [r.normal() for r in oref.all_segment_refs()] query = {"expandedRefs": {"$in": segment_refs}} if uid: query["owner"] = uid else: query["status"] = "public" if in_collection: collections = CollectionSet({"slug": {"$in": in_collection}}) sheets_list = [collection.sheets for collection in collections] sheets_ids = [sheet for sublist in sheets_list for sheet in sublist] query["id"] = {"$in": sheets_ids} sheetsObj = db.sheets.find( query, { "id": 1, "title": 1, "owner": 1, "viaOwner": 1, "via": 1, "dateCreated": 1, "includedRefs": 1, "expandedRefs": 1, "views": 1, "topics": 1, "status": 1, "summary": 1, "attribution": 1, "assigner_id": 1, "likes": 1, "displayedCollection": 1, "options": 1 }).sort([["views", -1]]) sheetsObj.hint("expandedRefs_1") sheets = [s for s in sheetsObj] user_ids = list({s["owner"] for s in sheets}) django_user_profiles = User.objects.filter(id__in=user_ids).values( 'email', 'first_name', 'last_name', 'id') user_profiles = {item['id']: item for item in django_user_profiles} mongo_user_profiles = list( db.profiles.find({"id": { "$in": user_ids }}, { "id": 1, "slug": 1, "profile_pic_url_small": 1 })) mongo_user_profiles = {item['id']: item for item in mongo_user_profiles} for profile in user_profiles: try: user_profiles[profile]["slug"] = mongo_user_profiles[profile][ "slug"] except: user_profiles[profile]["slug"] = "/" try: user_profiles[profile][ "profile_pic_url_small"] = mongo_user_profiles[profile].get( "profile_pic_url_small", '') except: user_profiles[profile]["profile_pic_url_small"] = "" results = [] 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': '*****@*****.**', 'slug': 'Ploni-Almoni', 'id': None, 'profile_pic_url_small': '' }) if "assigner_id" in sheet: asignerData = public_user_data(sheet["assigner_id"]) sheet["assignerName"] = asignerData["name"] sheet["assignerProfileUrl"] = asignerData["profileUrl"] if "viaOwner" in sheet: viaOwnerData = public_user_data(sheet["viaOwner"]) sheet["viaOwnerName"] = viaOwnerData["name"] sheet["viaOwnerProfileUrl"] = viaOwnerData["profileUrl"] if "displayedCollection" in sheet: collection = Collection().load( {"slug": sheet["displayedCollection"]}) sheet["collectionTOC"] = getattr(collection, "toc", None) topics = add_langs_to_topics(sheet.get("topics", [])) for anchor_ref, anchor_ref_expanded in zip(anchor_ref_list, anchor_ref_expanded_list): sheet_data = { "owner": sheet["owner"], "_id": str(sheet["_id"]), "id": str(sheet["id"]), "public": sheet["status"] == "public", "title": strip_tags(sheet["title"]), "sheetUrl": "/sheets/" + str(sheet["id"]), "anchorRef": anchor_ref.normal(), "anchorRefExpanded": [r.normal() for r in anchor_ref_expanded], "options": sheet["options"], "collectionTOC": sheet.get("collectionTOC", None), "ownerName": ownerData["first_name"] + " " + ownerData["last_name"], "via": sheet.get("via", None), "viaOwnerName": sheet.get("viaOwnerName", None), "assignerName": sheet.get("assignerName", None), "viaOwnerProfileUrl": sheet.get("viaOwnerProfileUrl", None), "assignerProfileUrl": sheet.get("assignerProfileUrl", None), "ownerProfileUrl": "/profile/" + ownerData["slug"], "ownerImageUrl": ownerData.get('profile_pic_url_small', ''), "status": sheet["status"], "views": sheet["views"], "topics": topics, "likes": sheet.get("likes", []), "summary": sheet.get("summary", None), "attribution": sheet.get("attribution", None), "is_featured": sheet.get("is_featured", False), "category": "Sheets", # ditto "type": "sheet", # ditto } results.append(sheet_data) return results
def get_sheets_for_ref(tref, uid=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. """ oref = model.Ref(tref) # perform initial search with context to catch ranges that include a segment ref regex_list = oref.context_ref().regex(as_list=True) ref_clauses = [{"includedRefs": {"$regex": r}} for r in regex_list] query = {"$or": ref_clauses} if uid: query["owner"] = uid else: query["status"] = "public" sheetsObj = db.sheets.find( query, { "id": 1, "title": 1, "owner": 1, "viaOwner": 1, "via": 1, "dateCreated": 1, "includedRefs": 1, "views": 1, "tags": 1, "status": 1, "summary": 1, "attribution": 1, "assigner_id": 1, "likes": 1, "group": 1, "options": 1 }).sort([["views", -1]]) sheets = list((s for s in sheetsObj)) user_ids = list(set([s["owner"] for s in sheets])) django_user_profiles = User.objects.filter(id__in=user_ids).values( 'email', 'first_name', 'last_name', 'id') user_profiles = {item['id']: item for item in django_user_profiles} mongo_user_profiles = list( db.profiles.find({"id": { "$in": user_ids }}, { "id": 1, "slug": 1 })) mongo_user_profiles = {item['id']: item for item in mongo_user_profiles} for profile in user_profiles: user_profiles[profile]["slug"] = mongo_user_profiles[profile]["slug"] ref_re = "(" + '|'.join(regex_list) + ")" results = [] for sheet in sheets: potential_matches = [ r for r in sheet["includedRefs"] if r.startswith(oref.index.title) ] matched_refs = [r for r in potential_matches if regex.match(ref_re, r)] for match in matched_refs: try: match = model.Ref(match) except InputError: continue ownerData = user_profiles.get( sheet["owner"], { 'first_name': u'Ploni', 'last_name': u'Almoni', 'email': u'*****@*****.**', 'slug': 'Ploni-Almoni', 'id': None }) default_image = "https://www.sefaria.org/static/img/profile-default.png" gravatar_base = "https://www.gravatar.com/avatar/" + hashlib.md5( ownerData["email"].lower()).hexdigest() + "?" gravatar_url_small = gravatar_base + urllib.urlencode({ 'd': default_image, 's': str(80) }) if "assigner_id" in sheet: asignerData = public_user_data(sheet["assigner_id"]) sheet["assignerName"] = asignerData["name"] sheet["assignerProfileUrl"] = asignerData["profileUrl"] if "viaOwner" in sheet: viaOwnerData = public_user_data(sheet["viaOwner"]) sheet["viaOwnerName"] = viaOwnerData["name"] sheet["viaOwnerProfileUrl"] = viaOwnerData["profileUrl"] if "group" in sheet: group = Group().load({"name": sheet["group"]}) try: sheet["groupLogo"] = group.imageUrl except: sheet["groupLogo"] = None sheet_data = { "owner": sheet["owner"], "_id": str(sheet["_id"]), "id": str(sheet["id"]), "anchorRef": match.normal(), "anchorVerse": match.sections[-1] if len(match.sections) else 1, "public": sheet["status"] == "public", "title": strip_tags(sheet["title"]), "sheetUrl": "/sheets/" + str(sheet["id"]), "options": sheet["options"], "naturalDateCreated": naturaltime( datetime.strptime(sheet["dateCreated"], "%Y-%m-%dT%H:%M:%S.%f")), "groupLogo": sheet.get("groupLogo", None), "ownerName": ownerData["first_name"] + " " + ownerData["last_name"], "via": sheet.get("via", None), "viaOwnerName": sheet.get("viaOwnerName", None), "assignerName": sheet.get("assignerName", None), "viaOwnerProfileUrl": sheet.get("viaOwnerProfileUrl", None), "assignerProfileUrl": sheet.get("assignerProfileUrl", None), "ownerProfileUrl": "/profile/" + ownerData["slug"], "ownerImageUrl": gravatar_url_small, "status": sheet["status"], "views": sheet["views"], "tags": sheet.get("tags", []), "likes": sheet.get("likes", []), "summary": sheet.get("summary", None), "attribution": sheet.get("attribution", None), "category": "Sheets", # ditto "type": "sheet", # ditto } results.append(sheet_data) return results
def get_sheets_for_ref(tref, uid=None, in_group=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_group` (list) is present, only return sheets in one of the listed groups. """ oref = model.Ref(tref) # perform initial search with context to catch ranges that include a segment ref segment_refs = [r.normal() for r in oref.all_segment_refs()] query = {"expandedRefs": {"$in": segment_refs}} if uid: query["owner"] = uid else: query["status"] = "public" if in_group: query["group"] = {"$in": in_group} sheetsObj = db.sheets.find( query, { "id": 1, "title": 1, "owner": 1, "viaOwner": 1, "via": 1, "dateCreated": 1, "includedRefs": 1, "expandedRefs": 1, "views": 1, "topics": 1, "status": 1, "summary": 1, "attribution": 1, "assigner_id": 1, "likes": 1, "group": 1, "options": 1 }).sort([["views", -1]]) sheetsObj.hint("expandedRefs_1") sheets = [s for s in sheetsObj] user_ids = list({s["owner"] for s in sheets}) django_user_profiles = User.objects.filter(id__in=user_ids).values( 'email', 'first_name', 'last_name', 'id') user_profiles = {item['id']: item for item in django_user_profiles} mongo_user_profiles = list( db.profiles.find({"id": { "$in": user_ids }}, { "id": 1, "slug": 1, "profile_pic_url_small": 1 })) mongo_user_profiles = {item['id']: item for item in mongo_user_profiles} for profile in user_profiles: try: user_profiles[profile]["slug"] = mongo_user_profiles[profile][ "slug"] except: user_profiles[profile]["slug"] = "/" try: user_profiles[profile][ "profile_pic_url_small"] = mongo_user_profiles[profile].get( "profile_pic_url_small", '') except: user_profiles[profile]["profile_pic_url_small"] = "" results = [] 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': '*****@*****.**', 'slug': 'Ploni-Almoni', 'id': None, 'profile_pic_url_small': '' }) if len(ownerData.get('profile_pic_url_small', '')) == 0: default_image = "https://www.sefaria.org/static/img/profile-default.png" gravatar_base = "https://www.gravatar.com/avatar/" + hashlib.md5( ownerData["email"].lower().encode('utf8')).hexdigest() + "?" gravatar_url_small = gravatar_base + urllib.parse.urlencode( { 'd': default_image, 's': str(80) }) ownerData['profile_pic_url_small'] = gravatar_url_small if "assigner_id" in sheet: asignerData = public_user_data(sheet["assigner_id"]) sheet["assignerName"] = asignerData["name"] sheet["assignerProfileUrl"] = asignerData["profileUrl"] if "viaOwner" in sheet: viaOwnerData = public_user_data(sheet["viaOwner"]) sheet["viaOwnerName"] = viaOwnerData["name"] sheet["viaOwnerProfileUrl"] = viaOwnerData["profileUrl"] if "group" in sheet: group = Group().load({"name": sheet["group"]}) sheet["groupLogo"] = getattr(group, "imageUrl", None) sheet["groupTOC"] = getattr(group, "toc", None) natural_date_created = naturaltime( datetime.strptime(sheet["dateCreated"], "%Y-%m-%dT%H:%M:%S.%f")) topics = add_langs_to_topics(sheet.get("topics", [])) for anchor_ref, anchor_ref_expanded in zip(anchor_ref_list, anchor_ref_expanded_list): sheet_data = { "owner": sheet["owner"], "_id": str(sheet["_id"]), "id": str(sheet["id"]), "public": sheet["status"] == "public", "title": strip_tags(sheet["title"]), "sheetUrl": "/sheets/" + str(sheet["id"]), "anchorRef": anchor_ref.normal(), "anchorRefExpanded": [r.normal() for r in anchor_ref_expanded], "options": sheet["options"], "naturalDateCreated": natural_date_created, "group": sheet.get("group", None), "groupLogo": sheet.get("groupLogo", None), "groupTOC": sheet.get("groupTOC", None), "ownerName": ownerData["first_name"] + " " + ownerData["last_name"], "via": sheet.get("via", None), "viaOwnerName": sheet.get("viaOwnerName", None), "assignerName": sheet.get("assignerName", None), "viaOwnerProfileUrl": sheet.get("viaOwnerProfileUrl", None), "assignerProfileUrl": sheet.get("assignerProfileUrl", None), "ownerProfileUrl": "/profile/" + ownerData["slug"], "ownerImageUrl": ownerData.get('profile_pic_url_small', ''), "status": sheet["status"], "views": sheet["views"], "topics": topics, "likes": sheet.get("likes", []), "summary": sheet.get("summary", None), "attribution": sheet.get("attribution", None), "is_featured": sheet.get("is_featured", False), "category": "Sheets", # ditto "type": "sheet", # ditto } results.append(sheet_data) return results
def get_sheets_for_ref(tref, uid=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. """ oref = model.Ref(tref) # perform initial search with context to catch ranges that include a segment ref regex_list = oref.context_ref().regex(as_list=True) ref_clauses = [{"sources.ref": {"$regex": r}} for r in regex_list] query = {"$or": ref_clauses} if uid: query["owner"] = uid else: query["status"] = "public" sheets = db.sheets.find( query, { "id": 1, "title": 1, "owner": 1, "sources.ref": 1, "views": 1, "tags": 1, "status": 1 }).sort([["views", -1]]) results = [] for sheet in sheets: matched_refs = [] for source in sheet.get("sources", []): if "ref" in source: matched_refs.append(source["ref"]) for match in matched_refs: try: match = model.Ref(match) if not oref.overlaps(match): continue except InputError: continue ownerData = public_user_data(sheet["owner"]) sheet_data = { "owner": sheet["owner"], "_id": str(sheet["_id"]), "anchorRef": match.normal(), "anchorVerse": match.sections[-1] if len(match.sections) else 1, "public": sheet["status"] == "public", "text": "<a class='sheetLink' href='/sheets/%d'>%s</a>" % (sheet["id"], strip_tags( sheet["title"])), # legacy, used in S1 "title": strip_tags(sheet["title"]), "sheetUrl": "/sheets/" + str(sheet["id"]), "ownerName": ownerData["name"], "ownerProfileUrl": ownerData["profileUrl"], "ownerImageUrl": ownerData["imageUrl"], "status": sheet["status"], "views": sheet["views"], "tags": sheet.get("tags", []), "commentator": user_link(sheet["owner"]), # legacy, used in S1 "category": "Sheets", # ditto "type": "sheet", # ditto } results.append(sheet_data) return results
def get_sheets_for_ref(tref, uid=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. """ oref = model.Ref(tref) # perform initial search with context to catch ranges that include a segment ref regex_list = oref.context_ref().regex(as_list=True) ref_clauses = [{"includedRefs": {"$regex": r}} for r in regex_list] query = {"$or": ref_clauses } if uid: query["owner"] = uid else: query["status"] = "public" sheetsObj = db.sheets.find(query, {"id": 1, "title": 1, "owner": 1, "viaOwner":1, "via":1, "dateCreated": 1, "includedRefs": 1, "views": 1, "tags": 1, "status": 1, "summary":1, "attribution":1, "assigner_id":1, "likes":1, "options":1}).sort([["views", -1]]) sheets = list((s for s in sheetsObj)) user_ids = list(set([s["owner"] for s in sheets])) django_user_profiles = User.objects.filter(id__in=user_ids).values('email','first_name','last_name','id') user_profiles = {item['id']: item for item in django_user_profiles} mongo_user_profiles = list(db.profiles.find({"id": {"$in": user_ids}},{"id":1,"slug":1})) mongo_user_profiles = {item['id']: item for item in mongo_user_profiles} for profile in user_profiles: user_profiles[profile]["slug"] = mongo_user_profiles[profile]["slug"] ref_re = "("+'|'.join(regex_list)+")" results = [] for sheet in sheets: potential_matches = [r for r in sheet["includedRefs"] if r.startswith(oref.index.title)] matched_refs = [r for r in potential_matches if regex.match(ref_re, r)] for match in matched_refs: try: match = model.Ref(match) except InputError: continue ownerData = user_profiles.get(sheet["owner"], {'first_name': u'Ploni', 'last_name': u'Almoni', 'email': u'*****@*****.**', 'slug': 'Ploni-Almoni', 'id': None}) default_image = "https://www.sefaria.org/static/img/profile-default.png" gravatar_base = "https://www.gravatar.com/avatar/" + hashlib.md5(ownerData["email"].lower()).hexdigest() + "?" gravatar_url_small = gravatar_base + urllib.urlencode({'d': default_image, 's': str(80)}) if "assigner_id" in sheet: asignerData = public_user_data(sheet["assigner_id"]) sheet["assignerName"] = asignerData["name"] sheet["assignerProfileUrl"] = asignerData["profileUrl"] if "viaOwner" in sheet: viaOwnerData = public_user_data(sheet["viaOwner"]) sheet["viaOwnerName"] = viaOwnerData["name"] sheet["viaOwnerProfileUrl"] = viaOwnerData["profileUrl"] sheet_data = { "owner": sheet["owner"], "_id": str(sheet["_id"]), "id": str(sheet["id"]), "anchorRef": match.normal(), "anchorVerse": match.sections[-1] if len(match.sections) else 1, "public": sheet["status"] == "public", "title": strip_tags(sheet["title"]), "sheetUrl": "/sheets/" + str(sheet["id"]), "options": sheet["options"], "naturalDateCreated": naturaltime(datetime.strptime(sheet["dateCreated"], "%Y-%m-%dT%H:%M:%S.%f")), "ownerName": ownerData["first_name"]+" "+ownerData["last_name"], "via": sheet.get("via", None), "viaOwnerName": sheet.get("viaOwnerName", None), "assignerName": sheet.get("assignerName", None), "viaOwnerProfileUrl": sheet.get("viaOwnerProfileUrl", None), "assignerProfileUrl": sheet.get("assignerProfileUrl", None), "ownerProfileUrl": "/profile/" + ownerData["slug"], "ownerImageUrl": gravatar_url_small, "status": sheet["status"], "views": sheet["views"], "tags": sheet.get("tags", []), "likes": sheet.get("likes", []), "summary": sheet.get("summary", None), "attribution": sheet.get("attribution", None), "category": "Sheets", # ditto "type": "sheet", # ditto } results.append(sheet_data) return results