Esempio n. 1
0
def set_lock_api(request, ref, lang, version):
	"""
	API to set an edit lock on a text segment.
	"""
	user = request.user.id if request.user.is_authenticated() else 0
	locks.set_lock(norm_ref(ref), lang, version.replace("_", " "), user)
	return jsonResponse({"status": "ok"})
Esempio n. 2
0
def translation_flow(request, ref):
	"""
	Assign a user a paritcular bit of text to translate within 'ref',
	either a text title or category. 
	"""
	ref = ref.replace("_", " ")
	generic_response = { "title": "Help Translate %s" % ref, "content": "" }
	categories = get_text_categories()
	next_text = None
	next_section = None

	# expire old locks before checking for a currently unlocked text
	locks.expire_locks()

	pRef = parse_ref(ref, pad=False)
	if "error" not in pRef and len(pRef["sections"]) == 0:
		# ref is an exact text Title
		text = norm_ref(ref)

		# normalize URL
		if request.path != "/translate/%s" % url_ref(text):
			return redirect("/translate/%s" % url_ref(text), permanent=True)

		# Check for completion
		if get_percent_available(text) == 100:
			generic_response["content"] = "<h3>Sefaria now has a complete translation of %s</h3>But you can still contribute in other ways.</h3> <a href='/contribute'>Learn More.</a>" % ref
			return render_to_response('static/generic.html', generic_response, RequestContext(request))

		if "random" in request.GET:
			# choose a ref from a random section within this text
			skip = int(request.GET.get("skip")) if "skip" in request.GET else None
			assigned_ref = random_untranslated_ref_in_text(text, skip=skip)
			
			if assigned_ref:
				next_section = parse_ref(assigned_ref)["sections"][0]
		
		elif "section" in request.GET:
			# choose the next ref within the specified section
			next_section = int(request.GET["section"])
			assigned_ref = next_untranslated_ref_in_text(text, section=next_section)
		
		else:
			# choose the next ref in this text in order
			assigned_ref = next_untranslated_ref_in_text(text)
	
		if not assigned_ref:
			generic_response["content"] = "All remaining sections in %s are being worked on by other contributors. Work on <a href='/translate/%s'>another text</a> for now." % (text, ref)
			return render_to_response('static/generic.html', generic_response, RequestContext(request))
		
	elif "error" not in pRef and len(pRef["sections"]) > 0:
		# ref is a citation to a particular location in a text
		# for now, send this to the edit_text view
		return edit_text(request, ref)
		
	elif "error" in pRef and ref in categories:
		# ref is a text Category
		cat = ref

		# Check for completion
		if get_percent_available(cat) == 100:
			generic_response["content"] = "<h3>Sefaria now has a complete translation of %s</h3>But you can still contribute in other ways.</h3> <a href='/contribute'>Learn More.</a>" % ref
			return render_to_response('static/generic.html', generic_response, RequestContext(request))

		if "random" in request.GET:
			# choose a random text from this cateogory
			skip = int(request.GET.get("skip")) if "skip" in request.GET else None
			text = random_untranslated_text_in_category(cat, skip=skip)
			assigned_ref = next_untranslated_ref_in_text(text)
			next_text = text

		elif "text" in request.GET:
			# choose the next text requested in URL
			text = norm_ref(request.GET["text"])
			next_text = text
			if get_percent_available(text) == 100:
				generic_response["content"] = "%s is complete! Work on <a href='/translate/%s'>another text</a>." % (next, ref)
				return render_to_response('static/generic.html', generic_response, RequestContext(request))
			
			assigned_ref = next_untranslated_ref_in_text(text)
			if "error" in assigned_ref:
				generic_response["content"] = "All remaining sections in %s are being worked on by other contributors. Work on <a href='/translate/%s'>another text</a> for now." % (next, ref)
				return render_to_response('static/generic.html', generic_response, RequestContext(request))

		else:
			# choose the next text in order
			skip = 0
			assigned_ref = {"error": "haven't chosen yet"}
			# TODO -- need an escape valve here
			while "error" in assigned_ref:
				text = next_untranslated_text_in_category(cat, skip=skip)
				assigned_ref = next_untranslated_ref_in_text(text)
				skip += 1
	
	else:
		# we don't know what this is
		generic_response["content"] = "<b>%s</b> isn't a known text or category.<br>But you can still contribute in other ways.</h3> <a href='/contribute'>Learn More.</a>" % (ref)
		return render_to_response('static/generic.html', generic_response, RequestContext(request))


	# get the assigned text
	assigned = get_text(assigned_ref, context=0, commentary=False)

	# Put a lock on this assignment
	user = request.user.id if request.user.is_authenticated() else 0
	locks.set_lock(assigned_ref, "en", "Sefaria Community Translation", user)
	
	# if the assigned text is actually empty, run this request again
	# but leave the new lock in place to skip over it
	if "he" not in assigned or not len(assigned["he"]):
		return translation_flow(request, ref)

	# get percentage and remaining counts
	# percent   = get_percent_available(assigned["book"])
	translated = get_translated_count_by_unit(assigned["book"], unit=assigned["sectionNames"][-1])
	remaining = get_untranslated_count_by_unit(assigned["book"], unit=assigned["sectionNames"][-1])
	percent = 100 * translated / float(translated + remaining)


	return render_to_response('translate_campaign.html', 
									{"title": "Help Translate %s" % ref,
									"base_ref": ref,
									"assigned_ref": assigned_ref,
									"assigned_ref_url": url_ref(assigned_ref),
									"assigned_text": assigned["he"],
									"assigned_segment_name": assigned["sectionNames"][-1],
									"assigned": assigned,
									"translated": translated,
									"remaining": remaining,
									"percent": percent,
									"thanks": "thank" in request.GET,
									"random_param": "&skip=%d" % assigned["sections"][0] if request.GET.get("random") else "",
									"next_text": next_text,
									"next_section": next_section,
									},
									RequestContext(request))