Created
October 25, 2011 17:40
-
-
Save gustavofonseca/1313615 to your computer and use it in GitHub Desktop.
Script de definição do atributo ``visible`` para os capítulos de livros, com base no valor do mesmo atributo dos respectivos livros
This file contains 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']['visible']) for book in monographs) | |
def update_part(part, key, value): | |
part[key] = value | |
return part | |
def get_updated_parts(monograph_sbid, visibility): | |
try: | |
parts = [update_part(part['doc'], 'visible', visibility) 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, visibility in all_monographs.items(): | |
parts = get_updated_parts(sbid, visibility) | |
try: | |
db.save_docs(parts, all_or_nothing=True) | |
except: | |
#ugly, but get all exceptions | |
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