parameters: (request, title, v2=False, raw=False)
return_statement: return jsonResponse({"error": "Unsupported HTTP method."}, callback=request.GET.get("callback", None))
docstring: API for manipulating text index records (aka "Text Info")
docstring_summary: API for manipulating text index records (aka "Text Info")
docstring_tokens: API for manipulating text index records ( aka Text Info )
function: def index_api(request, title, v2=False, raw=False):
      """
      API for manipulating text index records (aka "Text Info")
      """
      if request.method == "GET":
          try:
              with_content_counts = bool(request.GET.get("with_content_counts", False))
              i = library.get_index(title).contents(v2=v2, raw=raw, with_content_counts=with_content_counts)
          except InputError as e:
              node = library.get_schema_node(title)  # If the request were for v1 and fails, this falls back to v2.
              if not node:
                  raise e
              if node.is_default():
                  node = node.parent
              i = node.as_index_contents()
  
          if request.GET.get("with_related_topics", False):
              i["relatedTopics"] = get_topics_for_book(title, annotate=True)
  
          return jsonResponse(i, callback=request.GET.get("callback", None))
  
      if request.method == "POST":
          # use the update function if update is in the params
          func = tracker.update if request.GET.get("update", False) else tracker.add
          j = json.loads(request.POST.get("json"))
          if not j:
              return jsonResponse({"error": "Missing 'json' parameter in post data."})
          j["title"] = title.replace("_", " ")
          #todo: move this to texts_api, pass the changes down through the tracker and text chunk
          #if "versionTitle" in j:
          #    if j["versionTitle"] == "Sefaria Community Translation":
          #        j["license"] = "CC0"
          #        j["licenseVetter"] = True
          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(apikey["uid"], Index, j, method="API", v2=v2, raw=raw, force_complex=True).contents(v2=v2, raw=raw, force_complex=True))
          else:
              title = j.get("oldTitle", j.get("title"))
              try:
                  library.get_index(title)  # getting the index just to tell if it exists
                  # Only allow staff and the person who submitted a text to edit
                  if not request.user.is_staff and not user_started_text(request.user.id, title):
                     return jsonResponse({"error": "{} is protected from change.<br/><br/>See a mistake?<br/>Email hello@sefaria.org.".format(title)})
              except BookNameError:
                  pass  # if this is a new text, allow any logged in user to submit
          @csrf_protect
          def protected_index_post(request):
              return jsonResponse(
                  func(request.user.id, Index, j, v2=v2, raw=raw, force_complex=True).contents(v2=v2, raw=raw, force_complex=True)
              )
          return protected_index_post(request)
  
      if request.method == "DELETE":
          if not request.user.is_staff:
              return jsonResponse({"error": "Only moderators can delete texts indices."})
  
          title = title.replace("_", " ")
  
          i = library.get_index(title)
  
          i.delete()
          record_index_deletion(title, request.user.id)
  
          return jsonResponse({"status": "ok"})
  
      return jsonResponse({"error": "Unsupported HTTP method."}, callback=request.GET.get("callback", None))
function_tokens: def index_api ( request , title , v2 = False , raw = False ) : if request . method == "GET" : try : with_content_counts = bool ( request . GET . get ( "with_content_counts" , False ) ) i = library . get_index ( title ) . contents ( v2 = v2 , raw = raw , with_content_counts = with_content_counts ) except InputError as e : node = library . get_schema_node ( title ) # If the request were for v1 and fails, this falls back to v2. if not node : raise e if node . is_default ( ) : node = node . parent i = node . as_index_contents ( ) if request . GET . get ( "with_related_topics" , False ) : i [ "relatedTopics" ] = get_topics_for_book ( title , annotate = True ) return jsonResponse ( i , callback = request . GET . get ( "callback" , None ) ) if request . method == "POST" : # use the update function if update is in the params func = tracker . update if request . GET . get ( "update" , False ) else tracker . add j = json . loads ( request . POST . get ( "json" ) ) if not j : return jsonResponse ( { "error" : "Missing 'json' parameter in post data." } ) j [ "title" ] = title . replace ( "_" , " " ) #todo: move this to texts_api, pass the changes down through the tracker and text chunk #if "versionTitle" in j: #    if j["versionTitle"] == "Sefaria Community Translation": #        j["license"] = "CC0" #        j["licenseVetter"] = True 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 ( apikey [ "uid" ] , Index , j , method = "API" , v2 = v2 , raw = raw , force_complex = True ) . contents ( v2 = v2 , raw = raw , force_complex = True ) ) else : title = j . get ( "oldTitle" , j . get ( "title" ) ) try : library . get_index ( title ) # getting the index just to tell if it exists # Only allow staff and the person who submitted a text to edit if not request . user . is_staff and not user_started_text ( request . user . id , title ) : return jsonResponse ( { "error" : "{} is protected from change.<br/><br/>See a mistake?<br/>Email hello@sefaria.org." . format ( title ) } ) except BookNameError : pass # if this is a new text, allow any logged in user to submit @ csrf_protect def protected_index_post ( request ) : return jsonResponse ( func ( request . user . id , Index , j , v2 = v2 , raw = raw , force_complex = True ) . contents ( v2 = v2 , raw = raw , force_complex = True ) ) return protected_index_post ( request ) if request . method == "DELETE" : if not request . user . is_staff : return jsonResponse ( { "error" : "Only moderators can delete texts indices." } ) title = title . replace ( "_" , " " ) i = library . get_index ( title ) i . delete ( ) record_index_deletion ( title , request . user . id ) return jsonResponse ( { "status" : "ok" } ) return jsonResponse ( { "error" : "Unsupported HTTP method." } , callback = request . GET . get ( "callback" , None ) )