Created
October 31, 2011 16:43
-
-
Save gustavofonseca/1327960 to your computer and use it in GitHub Desktop.
Script que enriquece os registros de capítulos dos livros com atributos do livro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import couchdbkit | |
db_uri = 'http://localhost:5984' | |
db_name = 'scielobooks' | |
def get_monographs(): | |
try: | |
monographs = db.view('scielobooks/books') | |
except couchdbkit.ResourceNotFound: | |
raise exceptions.NotFound() | |
return dict((book['value']['_id'], book['value']) for book in monographs) | |
def update_part(part, data): | |
part['monograph'] = data['_id'] | |
part['visible'] = data['visible'] | |
part['monograph_title'] = data['title'] | |
part['monograph_isbn'] = data['isbn'] | |
part['monograph_creators'] = data['creators'] | |
part['monograph_publisher'] = data['publisher'] | |
return part | |
def get_updated_parts(monograph_sbid, data): | |
try: | |
parts = [update_part(part['doc'], data) for part in db.view('scielobooks/monographs_and_parts', | |
include_docs=True, key=[monograph_sbid, 1])] | |
except couchdbkit.ResourceNotFound: | |
raise exceptions.NotFound() | |
return parts | |
server = couchdbkit.Server(db_uri) | |
db = server.get_or_create_db(db_name) | |
all_monographs = get_monographs() | |
for sbid, data in all_monographs.items(): | |
parts = get_updated_parts(sbid, data) | |
try: | |
db.save_docs(parts, all_or_nothing=True) | |
except: | |
print 'Error saving parts of %s' % sbid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment