Exemplo n.º 1
0

def view_process(conn, db, view):
    view_url = "/" + db + "/_design/" + os.path.basename(view)
    if not view_exists(conn, view_url):
        if not view_create(conn, "/" + db, open(view, "r")):
            print "... creation FAILED!"
    else:
        if not view_update(conn, view_url, open(view, "r")):
            print "... update FAILED!"


viewpath = "src/main/resources/views"

(db,
 conn) = couchconnection.arsnova_connection("/etc/arsnova/arsnova.properties")
try:
    if not database_exists(conn, db):
        print "Creating database '" + db + "'..."
        if not database_create(conn, db):
            print "... FAILED"
    else:
        print "Database '" + db + "' already exists."

    for view in os.listdir(viewpath):
        print "Creating view '" + view + "'..."
        view_process(conn, db, viewpath + "/" + view)
except socket.error as e:
    print "Could not connect to CouchDB <" + str(e) + ">! Exiting..."
    sys.exit(1)
Exemplo n.º 2
0
import couchconnection
import json
import sys

(db, conn) = couchconnection.arsnova_connection("/etc/arsnova/arsnova.properties")

db_url = "/" + db

# Iterate command line arguments (excluding the script file name).
# Each argument is assumed to be an ID that should be undeleted.
for arg in sys.argv[1:]:
	# TODO: Validate _id format
	url = db_url + "/" + arg
	res = conn.get(url)
	doc = json.loads(res.read())
	# only consider deleted documents
	if "error" in doc and doc["reason"] == "deleted":
		# retrieve document meta info
		url = url + "?revs=true&open_revs=all"
		res = conn.get(url)
		body = res.read()
		# remove non-json data, then create object
		clean_body = body[body.find("{"):body.rfind("}")+1]
		doc = json.loads(clean_body)
		# first id is the deleted document, second id (index=1) is the document before it was deleted
		rev_number = doc["_revisions"]["start"] - 1
		rev_hash = doc["_revisions"]["ids"][1]
		last_rev = str(rev_number) + "-" + rev_hash

		url = db_url + "/" + arg + "?rev=" + last_rev
		res = conn.get(url)