def ajax_action_publish(request): if request.method == 'POST': evaluation_sbid = request.POST.get('sbid', None) if evaluation_sbid is None: return Response('insufficient params') #TODO! catch exception try: evaluation = request.rel_db_session.query(rel_models.Evaluation).filter_by( monograph_sbid=evaluation_sbid).one() except NoResultFound: return Response('invalid action') if evaluation.status != 'accepted' and evaluation.status != 'accepted-with-condition': return Response('invalid action') if evaluation.is_published: return Response('nothing to do') try: parts = [update_part(part['doc'], {'visible': True, 'publication_date': str(date.today())}) for part in request.db.view('scielobooks/monographs_and_parts', include_docs=True, startkey=[evaluation.monograph_sbid, 0], endkey=[evaluation.monograph_sbid, 1])] except couchdbkit.ResourceNotFound: raise exceptions.NotFound() evaluation.is_published = True request.rel_db_session.add(evaluation) monograph = Monograph.get(request.db, evaluation.monograph_sbid) if not getattr(monograph, 'eisbn', None): # eISBN is required to publish a book return Response('invalid action') #TODO! catch exception try: request.rel_db_session.commit() request.db.save_docs(parts, all_or_nothing=True) except: request.rel_db_session.rollback() if hasattr(monograph, 'pdf_file'): # Transfer pdf file to static.scielo.org pdf_file = request.db.fetch_attachment(monograph._id, monograph.pdf_file['filename'], stream=True) if request.registry.settings.get('fileserver_sync_enable', 'false').lower() == 'true': #weird. need to find a better way to get boolean values from #settings. transfer_static_file(request, pdf_file, monograph._id, monograph.shortname, 'pdf', request.registry.settings['fileserver_remotebase']) return Response('done') return Response('nothing to do')
def pdf_file(request): sbid = request.matchdict['sbid'] req_part = request.matchdict['part'].split('-') monograph = Monograph.get(request.db, sbid) if len(req_part) == 2: if req_part[1] != getattr(monograph, 'isbn', None) and req_part[1] != getattr( monograph, 'eisbn', None): raise exceptions.NotFound() try: url = static_url( 'scielobooks:fileserver/{0}/pdf/{1}.pdf'.format( sbid, request.matchdict['part']), request) u = urllib2.urlopen(url) return HTTPFound(location=url) except (urllib2.HTTPError, urllib2.URLError): #cannot find in static file server, fetch from db try: pdf_file = request.db.fetch_attachment( monograph._id, monograph.pdf_file['filename'], stream=True) except (couchdbkit.ResourceNotFound, AttributeError): raise exceptions.NotFound() else: if asbool( request.registry.settings.get('fileserver_sync_enable', False)): if req_part[1] == getattr(monograph, 'eisbn', None) and getattr( monograph, 'isbn', None): #when the eisbn is registered at an already published book. The eisbn takes #precedence when generating the shortname. source_filename = '-'.join([ monograph.shortname.split('-')[0], monograph.isbn ]) try: url = static_url( 'scielobooks:fileserver/{0}/pdf/{1}.pdf'. format(sbid, source_filename), request) u = urllib2.urlopen(url) except (urllib2.HTTPError, urllib2.URLError): # there are no static files available for this book. fresh_pdf_file = request.db.fetch_attachment( monograph._id, monograph.pdf_file['filename'], stream=True) functions.transfer_static_file( request, fresh_pdf_file, monograph._id, monograph.shortname, 'pdf', request.registry. settings['fileserver_remotebase']) else: dest_filename = monograph.shortname functions.symlink_static_file( request, monograph._id, source_filename, dest_filename, 'pdf', request.registry. settings['fileserver_remotebase']) else: fresh_pdf_file = request.db.fetch_attachment( monograph._id, monograph.pdf_file['filename'], stream=True) functions.transfer_static_file( request, fresh_pdf_file, monograph._id, monograph.shortname, 'pdf', request.registry.settings['fileserver_remotebase']) else: parts = get_book_parts(monograph._id, request) try: selected_part = parts[int(req_part[2])] except (IndexError, ValueError): raise exceptions.NotFound() part = Part.get(request.db, selected_part['part_sbid']) try: pdf_file = request.db.fetch_attachment(part._id, part.pdf_file['filename'], stream=True) except (couchdbkit.ResourceNotFound, AttributeError): raise exceptions.NotFound() response = Response(content_type='application/pdf', expires=datetime_rfc822(365)) response.app_iter = pdf_file try: response.etag = str(hash(pdf_file)) except TypeError: #cannot generate a hash for the object, return it without the ETag pass return response
def pdf_file(request): sbid = request.matchdict['sbid'] req_part = request.matchdict['part'].split('-') monograph = Monograph.get(request.db, sbid) if len(req_part) == 2: if req_part[1] != getattr(monograph, 'isbn', None) and req_part[1] != getattr(monograph, 'eisbn', None): raise exceptions.NotFound() try: url = static_url('scielobooks:fileserver/{0}/pdf/{1}.pdf'.format(sbid, request.matchdict['part']), request) u = urllib2.urlopen(url) return HTTPFound(location=url) except (urllib2.HTTPError, urllib2.URLError): #cannot find in static file server, fetch from db try: pdf_file = request.db.fetch_attachment(monograph._id, monograph.pdf_file['filename'], stream=True) except (couchdbkit.ResourceNotFound, AttributeError): raise exceptions.NotFound() else: if asbool(request.registry.settings.get('fileserver_sync_enable', False)): if req_part[1] == getattr(monograph, 'eisbn', None) and getattr(monograph, 'isbn', None): #when the eisbn is registered at an already published book. The eisbn takes #precedence when generating the shortname. source_filename = '-'.join([monograph.shortname.split('-')[0], monograph.isbn]) try: url = static_url('scielobooks:fileserver/{0}/pdf/{1}.pdf'.format(sbid, source_filename), request) u = urllib2.urlopen(url) except (urllib2.HTTPError, urllib2.URLError): # there are no static files available for this book. fresh_pdf_file = request.db.fetch_attachment(monograph._id, monograph.pdf_file['filename'], stream=True) functions.transfer_static_file(request, fresh_pdf_file, monograph._id, monograph.shortname, 'pdf', request.registry.settings['fileserver_remotebase']) else: dest_filename = monograph.shortname functions.symlink_static_file(request, monograph._id, source_filename, dest_filename, 'pdf', request.registry.settings['fileserver_remotebase']) else: fresh_pdf_file = request.db.fetch_attachment(monograph._id, monograph.pdf_file['filename'], stream=True) functions.transfer_static_file(request, fresh_pdf_file, monograph._id, monograph.shortname, 'pdf', request.registry.settings['fileserver_remotebase']) else: parts = get_book_parts(monograph._id, request) try: selected_part = parts[int(req_part[2])] except (IndexError, ValueError): raise exceptions.NotFound() part = Part.get(request.db, selected_part['part_sbid']) try: pdf_file = request.db.fetch_attachment(part._id, part.pdf_file['filename'], stream=True) except (couchdbkit.ResourceNotFound, AttributeError): raise exceptions.NotFound() response = Response(content_type='application/pdf', expires=datetime_rfc822(365)) response.app_iter = pdf_file try: response.etag = str(hash(pdf_file)) except TypeError: #cannot generate a hash for the object, return it without the ETag pass return response
def ajax_action_publish(request): if request.method == 'POST': evaluation_sbid = request.POST.get('sbid', None) if evaluation_sbid is None: return Response('insufficient params') #TODO! catch exception try: evaluation = request.rel_db_session.query( rel_models.Evaluation).filter_by( monograph_sbid=evaluation_sbid).one() except NoResultFound: return Response('invalid action') if evaluation.status != 'accepted' and evaluation.status != 'accepted-with-condition': return Response('invalid action') if evaluation.is_published: return Response('nothing to do') try: parts = [ update_part(part['doc'], { 'visible': True, 'publication_date': str(date.today()) }) for part in request.db.view( 'scielobooks/monographs_and_parts', include_docs=True, startkey=[evaluation.monograph_sbid, 0], endkey=[evaluation.monograph_sbid, 1]) ] except couchdbkit.ResourceNotFound: raise exceptions.NotFound() evaluation.is_published = True request.rel_db_session.add(evaluation) monograph = Monograph.get(request.db, evaluation.monograph_sbid) if not getattr(monograph, 'eisbn', None): # eISBN is required to publish a book return Response('invalid action') #TODO! catch exception try: request.rel_db_session.commit() request.db.save_docs(parts, all_or_nothing=True) except: request.rel_db_session.rollback() if hasattr(monograph, 'pdf_file'): # Transfer pdf file to static.scielo.org pdf_file = request.db.fetch_attachment( monograph._id, monograph.pdf_file['filename'], stream=True) if request.registry.settings.get('fileserver_sync_enable', 'false').lower() == 'true': #weird. need to find a better way to get boolean values from #settings. transfer_static_file( request, pdf_file, monograph._id, monograph.shortname, 'pdf', request.registry.settings['fileserver_remotebase']) return Response('done') return Response('nothing to do')