Exemple #1
0
def index_api(request, title):
	"""
	API for manipulating text index records (aka "Text Info")
	"""
	if request.method == "GET":
		i = get_index(title)
		return jsonResponse(i)
	
	if request.method == "POST":
		# use the update function if update is in the params
		func = update_index if request.GET.get("update", False) else save_index
		j = json.loads(request.POST.get("json"))
		if not j:
			return jsonResponse({"error": "Missing 'json' parameter in post data."})
		j["title"] = title.replace("_", " ")	
		if not request.user.is_authenticated():
			key = request.POST.get("apikey")
			if not key:
				return jsonResponse({"error": "You must be logged in or use an API key to save texts."})
			apikey = db.apikeys.find_one({"key": key})
			if not apikey:
				return jsonResponse({"error": "Unrecognized API key."})
			return jsonResponse(func(j, apikey["uid"], method="API"))
		else:
			@csrf_protect
			def protected_index_post(request):
				return jsonResponse(func(j, request.user.id))
			return protected_index_post(request)

	return jsonResponse({"error": "Unsuported HTTP method."})
def text_category(text):
	"""Returns the top level category for text"""
	i = get_index(text)
	return mark_safe(i.get("categories", ["[no cats]"])[0])
#sys.path.insert(0, p)
sys.path.insert(0, p + "/sefaria")
import sefaria.texts as t

connection = pymongo.Connection()
db = connection[t.SEFARIA_DB]
if t.SEFARIA_DB_USER and t.SEFARIA_DB_PASSWORD:
    db.authenticate(t.SEFARIA_DB_USER, t.SEFARIA_DB_PASSWORD)

user = 28
texts = db.texts.find({"language": "he"})

text_total = {}
text_order = []
for text in texts:
    index = t.get_index(text["title"])
    if not index or not index.get("categories") or not (
        "Tanach" in index['categories']
        and ("Targum" in index['categories'] or "Commentary" in index['categories'])
    ):
        print "Skipping " + text["title"]
        continue

    if text['title'] not in text_total:
        text_total[text["title"]] = 0
        text_order.append(text["title"])
    print text["title"]

    for i in range(len(text['chapter'])):
        chap = i + 1
        ref = text['title'] + " " + str(chap)
#sys.path.insert(0, p)
sys.path.insert(0, p + "/sefaria")
import sefaria.texts as t

connection = pymongo.Connection()
db = connection[t.SEFARIA_DB]
if t.SEFARIA_DB_USER and t.SEFARIA_DB_PASSWORD:
    db.authenticate(t.SEFARIA_DB_USER, t.SEFARIA_DB_PASSWORD)

user = 28
texts = db.texts.find({"language": "he"})

text_total = {}
text_order = []
for text in texts:
    index = t.get_index(text["title"])
    if not index or not index.get("categories") or not (
            "Tanach" in index['categories'] and
        ("Targum" in index['categories']
         or "Commentary" in index['categories'])):
        print "Skipping " + text["title"]
        continue

    if text['title'] not in text_total:
        text_total[text["title"]] = 0
        text_order.append(text["title"])
    print text["title"]

    for i in range(len(text['chapter'])):
        chap = i + 1
        ref = text['title'] + " " + str(chap)