def import_from_csv(filename, action="status", category="all"):
	existing_titles = []
	with open(filename, 'rb') as csvfile:
		rows = csv.reader(csvfile)
		header = rows.next()
		for text in rows:
			if not len(text[2]) or not len(text[9]): 
				# Require a primary titl and something set in "ready to upload" field
				continue	
			new_index = {
				"title": text[2].strip(),
				"sectionNames": [s.strip() for s in text[8].split(",")],
				"categories": [s.strip() for s in text[7].split(", ")],
				"titleVariants": [text[2].strip()] + [s.strip() for s in  text[6].split(", ")],
			}
			if len(text[3]):
				new_index["heTitle"] = text[3].strip()
			if len(text[4]):
				new_index["transliteratedTitle"] = text[4].strip()
				new_index["titleVariants"] += [new_index["transliteratedTitle"]]
				new_index["titleVariants"] = [v for v in new_index["titleVariants"] if v]
			if len(text[10]):
				new_index["length"] = int(text[10])
			if len(text[12]):
				# Only import the last order field for now
				new_index["order"] = [map(int, text[12].split(","))[-1]] 

			existing = db.index.find_one({"titleVariants": new_index["title"]})

			if action == "status":
				# Print information about texts listed
				if not existing:
					print "NEW - " + new_index["title"]
				if existing:
					if new_index["title"] == existing["title"]:
						print "EXISTING - " + new_index["title"]
					else:
						print "EXISTING (title change) - " + new_index["title"]
					existing_titles.append(existing["title"])

				validation = texts.validate_index(new_index)
				if "error" in validation:
					print "*** %s" % validation["error"]


			# Add texts if their category is specified in command line
			if action in ("post", "update") and category:
				if category == "all" or category in new_index["categories"][:2]:
					print "Saving %s" % new_index["title"]

					if action == "update":
						# TOOD remove any fields that have empty values like []
						# before updating - don't overwrite with nothing
						new_index.update(existing)

					tracker.add(1, sefaria.model.index.Index, new_index)
			

			if action == "hebrew" and existing:
				if "heTitle" not in existing:
					print "Missing Hebrew: %s" % (existing["title"])
					existing_titles.append(existing["title"])


	if action == "status":
		indexes = db.index.find()
		for i in indexes:
			if i["title"] not in existing_titles:
				print "NOT ON SHEET - %s" % i["title"]

	if action == "hebrew":
		indexes = db.index.find()
		for i in indexes:
			if "heTitle" not in i and i["title"] not in existing_titles:
				print "Still no Hebrew:  %s" % i["title"]

	if action in ("post", "update"):
		from sefaria.model import library
		library.rebuild_toc()
def import_from_csv(filename, action="status", category="all"):
    existing_titles = []
    with open(filename, 'rb') as csvfile:
        rows = csv.reader(csvfile)
        header = rows.next()
        for text in rows:
            if not len(text[2]) or not len(text[9]):
                # Require a primary titl and something set in "ready to upload" field
                continue
            new_index = {
                "title":
                text[2].strip(),
                "sectionNames": [s.strip() for s in text[8].split(",")],
                "categories": [s.strip() for s in text[7].split(", ")],
                "titleVariants":
                [text[2].strip()] + [s.strip() for s in text[6].split(", ")],
            }
            if len(text[3]):
                new_index["heTitle"] = text[3].strip()
            if len(text[4]):
                new_index["transliteratedTitle"] = text[4].strip()
                new_index["titleVariants"] += [
                    new_index["transliteratedTitle"]
                ]
                new_index["titleVariants"] = [
                    v for v in new_index["titleVariants"] if v
                ]
            if len(text[10]):
                new_index["length"] = int(text[10])
            if len(text[12]):
                # Only import the last order field for now
                new_index["order"] = [map(int, text[12].split(","))[-1]]

            existing = db.index.find_one({"titleVariants": new_index["title"]})

            if action == "status":
                # Print information about texts listed
                if not existing:
                    print "NEW - " + new_index["title"]
                if existing:
                    if new_index["title"] == existing["title"]:
                        print "EXISTING - " + new_index["title"]
                    else:
                        print "EXISTING (title change) - " + new_index["title"]
                    existing_titles.append(existing["title"])

                validation = texts.validate_index(new_index)
                if "error" in validation:
                    print "*** %s" % validation["error"]

            # Add texts if their category is specified in command line
            if action in ("post", "update") and category:
                if category == "all" or category in new_index["categories"][:2]:
                    print "Saving %s" % new_index["title"]

                    if action == "update":
                        # TOOD remove any fields that have empty values like []
                        # before updating - don't overwrite with nothing
                        new_index.update(existing)

                    tracker.add(1, sefaria.model.index.Index, new_index)

            if action == "hebrew" and existing:
                if "heTitle" not in existing:
                    print "Missing Hebrew: %s" % (existing["title"])
                    existing_titles.append(existing["title"])

    if action == "status":
        indexes = db.index.find()
        for i in indexes:
            if i["title"] not in existing_titles:
                print "NOT ON SHEET - %s" % i["title"]

    if action == "hebrew":
        indexes = db.index.find()
        for i in indexes:
            if "heTitle" not in i and i["title"] not in existing_titles:
                print "Still no Hebrew:  %s" % i["title"]

    if action in ("post", "update"):
        from sefaria.model import library
        library.rebuild_toc()
import argparse
""" The main function, runs when called from the CLI"""
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("commentator_name", help="commentator's name")
    parser.add_argument(
        "existing_book",
        help="title of existing index record for the comemntary")
    parser.add_argument("language",
                        help="version language",
                        choices=['en', 'he'])
    parser.add_argument("version_title",
                        help="version title for the new version")
    parser.add_argument("version_source",
                        help="version source for the new version")
    parser.add_argument("-ht",
                        "--he_commentator_name",
                        help="version source for the new version")
    args = parser.parse_args()
    print(args)
    try:
        from sefaria.helper.text import create_commentator_and_commentary_version
        from sefaria.model import library
        create_commentator_and_commentary_version(
            args.commentator_name, args.existing_book, args.language,
            args.version_title, args.version_source, args.he_commentator_name)
        library.rebuild_toc(
        )  # If we cache more than toc, call .rebuild() or something similar
    except Exception as e:
        print("{} exiting.".format(e))
Beispiel #4
0
from sefaria.model import library

library.rebuild_toc()
# -*- coding: utf-8 -*-

import argparse


""" The main function, runs when called from the CLI"""
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("commentator_name", help="commentator's name")
    parser.add_argument("existing_book", help="title of existing index record for the comemntary")
    parser.add_argument("language", help="version language", choices=['en', 'he'])
    parser.add_argument("version_title", help="version title for the new version")
    parser.add_argument("version_source", help="version source for the new version")
    parser.add_argument("-ht", "--he_commentator_name", help="version source for the new version")
    args = parser.parse_args()
    print args
    try:
        from sefaria.helper.text import create_commentator_and_commentary_version
        from sefaria.model import library
        create_commentator_and_commentary_version(args.commentator_name, args.existing_book, args.language, args.version_title,
                                                  args.version_source, args.he_commentator_name)
        library.rebuild_toc()  # If we cache more than toc, call .rebuild() or something similar
    except Exception as e:
        print "{} exiting.".format(e)
Beispiel #6
0
import django
django.setup()
from sefaria.model import library

library.rebuild_toc()