documents = jstruct['docs']

    for doc in documents:        
        doc_appstruct = create_document(doc, lista_capitulos)
        if doc_appstruct['TYPE'] == 'Monograph':
            monograph = models.Monograph.from_python(doc_appstruct)

            try:
                monograph.cover = {'fp':get_monograph_cover(monograph._id),
                                   'uid':'coveruid',
                                   'filename':'cover.jpg'}
            except urllib2.HTTPError:
                monografias_sem_capa.append(monograph._id)
            else:
                try:
                    monograph.cover_thumbnail = {'fp': create_thumbnail(monograph.cover['fp']),
                                   'filename': 'cover.jpg' + '.thumb.jpeg',
                                   'uid':'',
                                   }
                except IOError:
                    monografias_sem_thumbnail.append(monograph._id)

            try:
                monograph.pdf_file = {'fp':get_pdf_file(monograph._id, monograph.isbn),
                                      'uid':'pdfuid',
                                      'filename':'fulltext.pdf'}
            except urllib2.HTTPError:
                monografias_sem_pdf.append(monograph._id)

            monograph.save(db)
        doc_appstruct = create_document(doc, lista_capitulos)
        if doc_appstruct['TYPE'] == 'Monograph':
            monograph = models.Monograph.from_python(doc_appstruct)

            try:
                monograph.cover = {
                    'fp': get_monograph_cover(monograph._id),
                    'uid': 'coveruid',
                    'filename': 'cover.jpg'
                }
            except urllib2.HTTPError:
                monografias_sem_capa.append(monograph._id)
            else:
                try:
                    monograph.cover_thumbnail = {
                        'fp': create_thumbnail(monograph.cover['fp']),
                        'filename': 'cover.jpg' + '.thumb.jpeg',
                        'uid': '',
                    }
                except IOError:
                    monografias_sem_thumbnail.append(monograph._id)

            try:
                monograph.pdf_file = {
                    'fp': get_pdf_file(monograph._id, monograph.isbn),
                    'uid': 'pdfuid',
                    'filename': 'fulltext.pdf'
                }
            except urllib2.HTTPError:
                monografias_sem_pdf.append(monograph._id)
Example #3
0
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))
Example #4
0
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))