def texts_history_api(request, ref, lang=None, version=None): """ API for retrieving history information about a given text. """ if request.method != "GET": return jsonResponse({"error": "Unsuported HTTP method."}) ref = norm_ref(ref) refRe = '^%s$|^%s:' % (ref, ref) if lang and version: query = {"ref": {"$regex": refRe }, "language": lang, "version": version.replace("_", " ")} else: query = {"ref": {"$regex": refRe }} history = db.history.find(query) summary = {"copiers": Set(), "translators": Set(), "editors": Set(), "reviewers": Set() } for act in history: if act["rev_type"].startswith("edit"): summary["editors"].update([act["user"]]) elif act["rev_type"] == "review": summary["reviewers"].update([act["user"]]) elif act["version"] == "Sefaria Community Translation": summary["translators"].update([act["user"]]) else: summary["copiers"].update([act["user"]]) # Don't list copiers and translators as editors as well summary["editors"].difference_update(summary["copiers"]) summary["editors"].difference_update(summary["translators"]) for group in summary: uids = list(summary[group]) names = [] for uid in uids: try: user = User.objects.get(id=uid) name = "%s %s" % (user.first_name, user.last_name) link = user_link(uid) except User.DoesNotExist: name = "Someone" link = user_link(-1) u = { 'name': name, 'link': link } names.append(u) summary[group] = names return jsonResponse(summary)
def get_sheets_for_ref(ref, pad=True, context=1): """ Returns a list of sheets that include ref, formating as need for the Client Sidebar. """ ref = norm_ref(ref, pad=pad, context=context) ref_re = make_ref_re(ref) results = [] sheets = db.sheets.find({"included_refs": {"$regex": ref_re}, "status": {"$in": LISTED_SHEETS}}, {"id": 1, "title": 1, "owner": 1, "included_refs": 1}) for sheet in sheets: # Check for multiple matching refs within this sheet matched = [ref for ref in sheet["included_refs"] if regex.match(ref_re, ref)] for match in matched: com = {} anchorRef = parse_ref(match) com["category"] = "Sheets" com["type"] = "sheet" com["owner"] = sheet["owner"] com["_id"] = str(sheet["_id"]) com["anchorRef"] = match com["anchorVerse"] = anchorRef["sections"][-1] com["public"] = True com["commentator"] = user_link(sheet["owner"]) com["text"] = "<a class='sheetLink' href='/sheets/%d'>%s</a>" % (sheet["id"], strip_tags(sheet["title"])) results.append(com) return results
def annotate_user_links(sources): """ Search a sheet for any addedBy fields (containg a UID) and add corresponding user links. """ for source in sources: if "addedBy" in source: source["userLink"] = user_link(source["addedBy"]) if "subsources" in source: source["subsources"] = annotate_user_links(source["subsources"]) return sources
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. """ #tref = norm_ref(tref, pad=pad, context=context) #ref_re = make_ref_re(tref) oref = model.Ref(tref) if pad: oref = oref.padded_ref() if context: oref = oref.context_ref(context) ref_re = oref.regex() results = [] sheets = db.sheets.find( { "included_refs": { "$regex": ref_re }, "status": { "$in": LISTED_SHEETS } }, { "id": 1, "title": 1, "owner": 1, "included_refs": 1 }) for sheet in sheets: # Check for multiple matching refs within this sheet matched_orefs = [ model.Ref(r) for r in sheet["included_refs"] if regex.match(ref_re, r) ] for match in matched_orefs: com = {} com["category"] = "Sheets" com["type"] = "sheet" com["owner"] = sheet["owner"] com["_id"] = str(sheet["_id"]) com["anchorRef"] = match.normal() com["anchorVerse"] = match.sections[-1] com["public"] = True com["commentator"] = user_link(sheet["owner"]) com["text"] = "<a class='sheetLink' href='/sheets/%d'>%s</a>" % ( sheet["id"], strip_tags(sheet["title"])) results.append(com) return results
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 = [{"included_refs": {"$regex": r}} for r in regex_list] sheets = db.sheets.find({ "$or": ref_clauses, "status": "public" }, { "id": 1, "title": 1, "owner": 1, "included_refs": 1 }) for sheet in sheets: # Check for multiple matching refs within this sheet matched_refs = [ r for r in sheet["included_refs"] if regex.match(ref_re, r) ] for match in matched_refs: try: match = model.Ref(match) except InputError: continue com = {} com["category"] = "Sheets" com["type"] = "sheet" com["owner"] = sheet["owner"] com["_id"] = str(sheet["_id"]) com["anchorRef"] = match.normal() com["anchorVerse"] = match.sections[-1] if len( match.sections) else 1 com["public"] = True com["commentator"] = user_link(sheet["owner"]) com["text"] = "<a class='sheetLink' href='/sheets/%d'>%s</a>" % ( sheet["id"], strip_tags(sheet["title"])) results.append(com) return results
def get_reviews(tref, lang, version): """ Returns a list of reviews pertaining to ref/lang/version """ reviews = [] tref = model.Ref(tref).normal() refRe = '^%s$|^%s:' % (tref, tref) cursor = texts.db.history.find({"ref": {"$regex": refRe}, "language": lang, "version": version, "rev_type": "review"}).sort([["date", -1]]) for r in cursor: r["_id"] = str(r["_id"]) r["userLink"] = user_link(r["user"]) reviews.append(r) return reviews
def annotate_user_list(uids): """ Returns a list of dictionaries giving details (names, profile links) for the user ids list in uids. """ annotated_list = [] for uid in uids: annotated = { "userLink": user_link(uid), "imageUrl": UserProfile(id=uid).gravatar_url_small, } annotated_list.append(annotated) return annotated_list
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 = [{"included_refs": {"$regex": r}} for r in regex_list] sheets = db.sheets.find({"$or": ref_clauses, "status": "public"}, {"id": 1, "title": 1, "owner": 1, "included_refs": 1}) for sheet in sheets: # Check for multiple matching refs within this sheet matched_refs = [r for r in sheet["included_refs"] if regex.match(ref_re, r)] for match in matched_refs: try: match = model.Ref(match) except InputError: continue com = {} com["category"] = "Sheets" com["type"] = "sheet" com["owner"] = sheet["owner"] com["_id"] = str(sheet["_id"]) com["anchorRef"] = match.normal() com["anchorVerse"] = match.sections[-1] com["public"] = True com["commentator"] = user_link(sheet["owner"]) com["text"] = "<a class='sheetLink' href='/sheets/%d'>%s</a>" % (sheet["id"], strip_tags(sheet["title"])) results.append(com) return results
def get_reviews(tref, lang, version): """ Returns a list of reviews pertaining to ref/lang/version """ reviews = [] tref = model.Ref(tref).normal() refRe = '^%s$|^%s:' % (tref, tref) cursor = db.history.find({ "ref": { "$regex": refRe }, "language": lang, "version": version, "rev_type": "review" }).sort([["date", -1]]) for r in cursor: r["_id"] = str(r["_id"]) r["userLink"] = user_link(r["user"]) reviews.append(r) return reviews
def index_sheet(id): """ Index source sheet with 'id'. """ sheet = db.sheets.find_one({"id": id}) if not sheet: return False doc = { "title": sheet["title"], "content": make_sheet_text(sheet), "version": "Source Sheet by " + user_link(sheet["owner"]), "sheetId": id, } try: es.index('sefaria', 'sheet', doc, id) global doc_count doc_count += 1 except Exception, e: print "Error indexing sheet %d" % id print e
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. """ com = {} anchor_oref = Ref(note.ref).padded_ref() com["category"] = "Notes" com["type"] = "note" com["owner"] = note.owner com["_id"] = str(note._id) com["anchorRef"] = note.ref com["anchorVerse"] = anchor_oref.sections[-1] com["anchorText"] = getattr(note, "anchorText", "") com["public"] = getattr(note, "public", False) com["commentator"] = user_link(note.owner) com["text"] = note.text com["title"] = note.title # com["text"] = note.title + " - " + note.text if getattr(note, "title", None) else note.text return com
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. """ com = {} anchor_oref = model.Ref(note.ref).padded_ref() com["category"] = "Notes" com["type"] = "note" com["owner"] = note.owner com["_id"] = str(note._id) com["anchorRef"] = note.ref com["anchorVerse"] = anchor_oref.sections[-1] com["anchorText"] = getattr(note, "anchorText", "") com["public"] = getattr(note, "public", False) com["commentator"] = user_link(note.owner) com["text"] = note.text com["title"] = note.title # com["text"] = note.title + " - " + note.text if getattr(note, "title", None) else note.text return com