def edit_book(request): FORM_TITLE = _('%s') localizer = get_localizer(request) publishers = request.rel_db_session.query(rel_models.Publisher.name_slug, rel_models.Publisher.name).all() monograph_form = MonographForm.get_form(localizer, publisher_values=publishers) main = get_renderer(BASE_TEMPLATE).implementation() if request.method == 'POST': if 'btn_cancel' in request.POST: return HTTPFound(location=request.route_url('staff.book_details', sbid=request.matchdict['sbid'])) controls = request.POST.items() try: appstruct = monograph_form.validate(controls) except deform.ValidationFailure, e: monograph = Monograph.get(request.db, request.matchdict['sbid']) return {'content':e.render(), 'main':main, 'user':get_logged_user(request), 'general_stuff':{'form_title':FORM_TITLE % monograph.title, 'breadcrumb': [ (_('Dashboard'), request.route_url('staff.panel')), (monograph.title, request.route_url('staff.book_details', sbid=monograph._id)), (_('Edit'), None), ] }, } if appstruct['cover'] and appstruct['cover']['fp'] is not None: cover_thumbnail = {'fp': create_thumbnail(appstruct['cover']['fp']), 'filename': appstruct['cover']['filename'] + '.thumb.jpeg', 'uid':'', } appstruct['cover_thumbnail'] = cover_thumbnail existing_doc_appstruct = Monograph.get(request.db, appstruct['_id']).to_python() existing_doc_appstruct.update(appstruct) monograph = Monograph.from_python(existing_doc_appstruct) monograph.save(request.db) request.session.flash(_('Successfully updated.')) return HTTPFound(location=request.route_url('staff.book_details', sbid=monograph._id))
def edit_book(request): FORM_TITLE = _('Submission of %s') localizer = get_localizer(request) monograph_form = MonographForm.get_form(localizer) main = get_renderer(BASE_TEMPLATE).implementation() if request.method == 'POST': if 'btn_cancel' in request.POST: return HTTPFound(location=request.route_url('staff.book_details', sbid=request.matchdict['sbid'])) controls = request.POST.items() try: appstruct = monograph_form.validate(controls) except deform.ValidationFailure, e: return {'content':e.render(), 'main':main, 'user':get_logged_user(request), 'general_stuff':{}, } if appstruct['cover'] and appstruct['cover']['fp'] is not None: cover_thumbnail = {'fp': create_thumbnail(appstruct['cover']['fp']), 'filename': appstruct['cover']['filename'] + '.thumb.jpeg', 'uid':'', } appstruct['cover_thumbnail'] = cover_thumbnail existing_doc_appstruct = Monograph.get(request.db, appstruct['_id']).to_python() existing_doc_appstruct.update(appstruct) monograph = Monograph.from_python(existing_doc_appstruct) monograph.save(request.db) request.session.flash(_('Successfully updated.')) return HTTPFound(location=request.route_url('staff.book_details', sbid=monograph._id))
def edit_book(request): FORM_TITLE = '%s' localizer = get_localizer(request) publishers = request.rel_db_session.query(rel_models.Publisher.name_slug, rel_models.Publisher.name).all() monograph_form = MonographForm.get_form(localizer, publisher_values=publishers) main = get_renderer(BASE_TEMPLATE).implementation() if request.method == 'POST': if 'btn_cancel' in request.POST: return HTTPFound(location=request.route_path('staff.book_details', sbid=request.matchdict['sbid'])) controls = request.POST.items() try: appstruct = monograph_form.validate(controls) except deform.ValidationFailure, e: monograph = Monograph.get(request.db, request.matchdict['sbid']) return {'content':e.render(), 'main':main, 'user':get_logged_user(request), 'general_stuff':{'form_title':FORM_TITLE % monograph.title, 'breadcrumb': [ (_('Dashboard'), request.route_path('staff.panel')), (monograph.title, request.route_path('staff.book_details', sbid=monograph._id)), (_('Edit'), None), ] }, } if appstruct['cover'] and appstruct['cover']['fp'] is not None: cover_thumbnail = {'fp': create_thumbnail(appstruct['cover']['fp']), 'filename': appstruct['cover']['filename'] + '.thumb.jpeg', 'uid':'', } appstruct['cover_thumbnail'] = cover_thumbnail publisher_slug = appstruct.pop('publisher') publisher = request.rel_db_session.query(rel_models.Publisher).filter_by(name_slug=publisher_slug).one() appstruct['publisher'] = publisher.name existing_doc_appstruct = Monograph.get(request.db, appstruct['_id']).to_python() existing_doc_appstruct.update(appstruct) monograph = Monograph.from_python(existing_doc_appstruct) monograph.save(request.db) #update monographic data that lives on each part monograph_as_python = monograph.to_python() monographic_data = {'monograph_title': monograph_as_python['title'], 'monograph_isbn': monograph_as_python['isbn'], 'monograph_creators': monograph_as_python['creators'], 'monograph_publisher': monograph_as_python['publisher'],} try: parts = [update_part(part['doc'], monographic_data) for part in request.db.view('scielobooks/monographs_and_parts', include_docs=True, key=[monograph._id, 1])] except couchdbkit.ResourceNotFound: raise exceptions.NotFound() request.db.save_docs(parts, all_or_nothing=True) #update evaluation data evaluation = request.rel_db_session.query(rel_models.Evaluation).filter_by(monograph_sbid=monograph_as_python['_id']).one() for attr in ['title', 'isbn',]: setattr(evaluation, attr, monograph_as_python[attr]) request.rel_db_session.add(evaluation) try: request.rel_db_session.commit() except IntegrityError: request.rel_db_session.rollback() request.session.flash(_('Successfully updated.')) return HTTPFound(location=request.route_path('staff.book_details', sbid=monograph._id))
def edit_book(request): FORM_TITLE = '%s' localizer = get_localizer(request) publishers = request.rel_db_session.query(rel_models.Publisher.name_slug, rel_models.Publisher.name).all() monograph_form = MonographForm.get_form(localizer, publisher_values=publishers) main = get_renderer(BASE_TEMPLATE).implementation() if request.method == 'POST': if 'btn_cancel' in request.POST: return HTTPFound(location=request.route_path( 'staff.book_details', sbid=request.matchdict['sbid'])) controls = request.POST.items() try: appstruct = monograph_form.validate(controls) except deform.ValidationFailure, e: monograph = Monograph.get(request.db, request.matchdict['sbid']) return { 'content': e.render(), 'main': main, 'user': get_logged_user(request), 'general_stuff': { 'form_title': FORM_TITLE % monograph.title, 'breadcrumb': [ (_('Dashboard'), request.route_path('staff.panel')), (monograph.title, request.route_path('staff.book_details', sbid=monograph._id)), (_('Edit'), None), ] }, } if appstruct['cover'] and appstruct['cover']['fp'] is not None: cover_thumbnail = { 'fp': create_thumbnail(appstruct['cover']['fp']), 'filename': appstruct['cover']['filename'] + '.thumb.jpeg', 'uid': '', } appstruct['cover_thumbnail'] = cover_thumbnail publisher_slug = appstruct.pop('publisher') publisher = request.rel_db_session.query( rel_models.Publisher).filter_by(name_slug=publisher_slug).one() appstruct['publisher'] = publisher.name existing_doc_appstruct = Monograph.get(request.db, appstruct['_id']).to_python() existing_doc_appstruct.update(appstruct) monograph = Monograph.from_python(existing_doc_appstruct) monograph.save(request.db) #update monographic data that lives on each part monograph_as_python = monograph.to_python() monographic_data = { 'monograph_title': monograph_as_python['title'], 'monograph_creators': monograph_as_python['creators'], 'monograph_publisher': monograph_as_python['publisher'], } if monograph_as_python.get('isbn'): monographic_data.update( {'monograph_isbn': monograph_as_python['isbn']}) try: parts = [ update_part(part['doc'], monographic_data) for part in request.db.view('scielobooks/monographs_and_parts', include_docs=True, key=[monograph._id, 1]) ] except couchdbkit.ResourceNotFound: raise exceptions.NotFound() request.db.save_docs(parts, all_or_nothing=True) #update evaluation data evaluation = request.rel_db_session.query( rel_models.Evaluation).filter_by( monograph_sbid=monograph_as_python['_id']).one() for attr in [ 'title', 'isbn', ]: try: setattr(evaluation, attr, monograph_as_python[attr]) except KeyError: setattr(evaluation, attr, None) request.rel_db_session.add(evaluation) try: request.rel_db_session.commit() except IntegrityError: request.rel_db_session.rollback() request.session.flash(_('Successfully updated.')) return HTTPFound(location=request.route_path('staff.book_details', sbid=monograph._id))