def set_record_documents(record, *args, **kwargs): """Attach and treat all the documents embeded in the input filex.""" dirname = kwargs.get('dirname', None) or os.curdir def _check_path(source): """Check if the ``source`` path. If it is relative path than the directory path of original blob filename, if defined, or the current directory will be prepended. """ if not os.path.isabs(source): new_source = os.path.join(dirname, source) if os.path.exists(new_source): return new_source return source def _create_document(metadata, record): metadata['source'] = _check_path(metadata['source']) metadata.setdefault('recids', list()) record.setdefault('_documents', list()) model = metadata.pop('model', 'record_document_base') if record['recid'] not in metadata['recids']: metadata['recids'].append(record['recid']) document = api.Document.create(metadata, model=model) record['_documents'].append((document['title'], document['_id'])) return document if 'files_to_upload' in record: files_to_upload = record.get('files_to_upload', []) for file_to_upload in files_to_upload: document = _create_document(file_to_upload, record) set_document_contents.delay( document['_id'], document['source'], name_generator(document) ) del record['files_to_upload'] if 'files_to_link' in record: files_to_link = record.get('files_to_link', []) for file_to_link in files_to_link: _create_document(file_to_link, record) del record['files_to_link']
def set_record_documents(record, *args, **kwargs): """Attach and treat all the documents embeded in the input filex.""" dirname = kwargs.get('dirname', None) or os.curdir def _check_path(source): """Check if the ``source`` path. If it is relative path than the directory path of original blob filename, if defined, or the current directory will be prepended. """ if not os.path.isabs(source): new_source = os.path.join(dirname, source) if os.path.exists(new_source): return new_source return source def _create_document(metadata, record): metadata['source'] = _check_path(metadata['source']) metadata.setdefault('recids', list()) record.setdefault('_documents', list()) model = metadata.pop('model', 'record_document_base') if record['recid'] not in metadata['recids']: metadata['recids'].append(record['recid']) document = api.Document.create(metadata, model=model) record['_documents'].append((document['title'], document['_id'])) return document if 'files_to_upload' in record: files_to_upload = record.get('files_to_upload', []) for file_to_upload in files_to_upload: document = _create_document(file_to_upload, record) set_document_contents.delay(document['_id'], document['source'], name_generator(document)) del record['files_to_upload'] if 'files_to_link' in record: files_to_link = record.get('files_to_link', []) for file_to_link in files_to_link: _create_document(file_to_link, record) del record['files_to_link']
def _manage_attached_documents(obj, eng): record = obj[1] filename = eng.getVar('options').get('filename') dirname = os.path.abspath(os.path.dirname(filename)) \ if filename is not None else os.curdir def _check_path(source): """Check if the ``source`` path. If it is relative path than the directory path of original blob filename, if defined, or the current directory will be prepended. """ if not os.path.isabs(source): new_source = os.path.join(dirname, source) if os.path.exists(new_source): return new_source eng.log.error('File %s does not exist.', (new_source, )) return source eng.log.info('Look documents to manage') def _create_document(metadata, record): metadata['source'] = _check_path(metadata['source']) if '_documents' not in record: record['_documents'] = [] model = metadata.pop('model', 'record_document_base') if 'recids' not in metadata: metadata['recids'] = list() if record.get('recid', -1) not in metadata['recids']: metadata['recids'].append(record.get('recid', -1), ) document = api.Document.create(metadata, model=model) eng.log.info('Document %s created', (document['_id'], )) record['_documents'].append((document['title'], document['_id'])) return document if 'files_to_upload' in record: eng.log.info('Documents to upload found') files_to_upload = record.get('files_to_upload', []) for file_to_upload in files_to_upload: document = _create_document(file_to_upload, record) set_document_contents.delay(document['_id'], document['source'], name_generator(document)) eng.log.info('Finish creating documents, delete temporary key') del record['files_to_upload'] if 'files_to_link' in record: eng.log.info('Documents to link found') files_to_link = record.get('files_to_link', []) for file_to_link in files_to_link: _create_document(file_to_link, record) eng.log.info('Finish linking documents, delete temporary key') del record['files_to_link']
def _manage_attached_documents(obj, eng): record = obj[1] filename = eng.getVar('options').get('filename') dirname = os.path.abspath(os.path.dirname(filename)) \ if filename is not None else os.curdir def _check_path(source): """Check if the ``source`` path. If it is relative path than the directory path of original blob filename, if defined, or the current directory will be prepended. """ if not os.path.isabs(source): new_source = os.path.join(dirname, source) if os.path.exists(new_source): return new_source eng.log.error('File %s does not exist.', (new_source,)) return source eng.log.info('Look documents to manage') def _create_document(metadata, record): metadata['source'] = _check_path(metadata['source']) if '_documents' not in record: record['_documents'] = [] model = metadata.pop('model', 'record_document_base') if 'recids' not in metadata: metadata['recids'] = list() if record.get('recid', -1) not in metadata['recids']: metadata['recids'].append(record.get('recid', -1), ) document = api.Document.create(metadata, model=model) eng.log.info('Document %s created', (document['_id'],)) record['_documents'].append((document['title'], document['_id'])) return document if 'files_to_upload' in record: eng.log.info('Documents to upload found') files_to_upload = record.get('files_to_upload', []) for file_to_upload in files_to_upload: document = _create_document(file_to_upload, record) set_document_contents.delay( document['_id'], document['source'], name_generator(document) ) eng.log.info('Finish creating documents, delete temporary key') del record['files_to_upload'] if 'files_to_link' in record: eng.log.info('Documents to link found') files_to_link = record.get('files_to_link', []) for file_to_link in files_to_link: _create_document(file_to_link, record) eng.log.info('Finish linking documents, delete temporary key') del record['files_to_link']