def fix_bibdoc_bibdoc(id_bibdoc1, id_bibdoc2, logfile): """ Migrate an icon. """ try: the_bibdoc = BibDoc.create_instance(id_bibdoc1) except Exception as err: msg = "WARNING: when opening docid %s: %s" % (id_bibdoc1, err) print(msg, file=logfile) print(msg) return True try: msg = "Fixing icon for the document %s" % (id_bibdoc1, ) print(msg, end=' ') print(msg, end=' ', file=logfile) the_icon = BibDoc.create_instance(id_bibdoc2) for a_file in the_icon.list_latest_files(): the_bibdoc.add_icon(a_file.get_full_path(), format=a_file.get_format()) the_icon.delete() run_sql( "DELETE FROM bibdoc_bibdoc WHERE id_bibdoc1=%s AND id_bibdoc2=%s", (id_bibdoc1, id_bibdoc2)) print("OK") print("OK", file=logfile) return True except Exception as err: print("ERROR: %s" % err) print("ERROR: %s" % err, file=logfile) register_exception() return False
def fix_bibdoc_bibdoc(id_bibdoc1, id_bibdoc2, logfile): """ Migrate an icon. """ try: the_bibdoc = BibDoc.create_instance(id_bibdoc1) except Exception as err: msg = "WARNING: when opening docid %s: %s" % (id_bibdoc1, err) print(msg, file=logfile) print(msg) return True try: msg = "Fixing icon for the document %s" % (id_bibdoc1, ) print(msg, end=' ') print(msg, end=' ', file=logfile) the_icon = BibDoc.create_instance(id_bibdoc2) for a_file in the_icon.list_latest_files(): the_bibdoc.add_icon(a_file.get_full_path(), format=a_file.get_format()) the_icon.delete() run_sql("DELETE FROM bibdoc_bibdoc WHERE id_bibdoc1=%s AND id_bibdoc2=%s", (id_bibdoc1, id_bibdoc2)) print("OK") print("OK", file=logfile) return True except Exception as err: print("ERROR: %s" % err) print("ERROR: %s" % err, file=logfile) register_exception() return False
def clean_documents(): """Delete all the bibdocs that have been set as deleted and have not been modified since CFG_DELETED_BIBDOC_MAXLIFE days. Returns the number of bibdocs involved.""" write_message("""CLEANING OF OBSOLETED DELETED DOCUMENTS STARTED""") write_message("select id from bibdoc where status='DELETED' and NOW()>ADDTIME(modification_date, '%s 0:0:0')" % CFG_DELETED_BIBDOC_MAXLIFE, verbose=9) records = run_sql("select id from bibdoc where status='DELETED' and NOW()>ADDTIME(modification_date, '%s 0:0:0')", (CFG_DELETED_BIBDOC_MAXLIFE,)) for record in records: bibdoc = BibDoc.create_instance(record[0]) bibdoc.expunge() write_message("DELETE FROM bibdoc WHERE id=%i" % int(record[0]), verbose=9) run_sql("DELETE FROM bibdoc WHERE id=%s", (record[0],)) write_message("""%s obsoleted deleted documents cleaned""" % len(records)) write_message("""CLEANING OF OBSOLETED DELETED DOCUMENTS FINISHED""") return len(records)
def get_bibdoc(recid): """ Retrieves using BibDoc all the files related with a given record @param recid @return BibDoc of the given record """ if not recid or recid < 0: return None from invenio.legacy.bibdocfile.api import BibDoc, InvenioBibDocFileError try: return BibDoc(int(recid)) except InvenioBibDocFileError: return None
def dumpfiles(): """Dump files.""" from invenio.base.factory import create_app app = create_app() with app.app_context(): from invenio.modules.editor.models import Bibdoc from invenio.legacy.bibdocfile.api import BibDoc q = Bibdoc.query with open('files.json', 'w') as fp: fp.write("[") with click.progressbar(q, length=q.count()) as query: for d in query: bd = BibDoc(d.id) try: for f in bd.docfiles: fp.write( json.dumps( dict( comment=f.comment, creation_date=f.cd.isoformat(), modification_date=f.md.isoformat(), description=f.description, docid=f.docid, encoding=f.encoding, flags=f.flags, format=f.get_format(), hidden=f.hidden, is_icon=f.is_icon(), magic=f.get_magic(), md5=f.get_checksum(), mime=f.mime, name=f.get_full_name(), path=f.fullpath, recid=f.get_recid(), size=f.size, status=f.status, subformat=f.get_subformat(), version=f.version, ))) fp.write(",") except Exception: print("Failed: {0}".format(bd.id)) fp.seek(fp.tell() - 1) fp.write("]")
def _getfile_py(req, recid=0, docid=0, version="", name="", docformat="", ln=CFG_SITE_LANG): if not recid: ## Let's obtain the recid from the docid if docid: try: bibdoc = BibDoc(docid=docid) recid = bibdoc.bibrec_links[0]["recid"] except InvenioBibDocFileError: return warning_page( _("An error has happened in trying to retrieve the requested file." ), req, ln) else: return warning_page( _('Not enough information to retrieve the document'), req, ln) else: brd = BibRecDocs(recid) if not name and docid: ## Let's obtain the name from the docid try: name = brd.get_docname(docid) except InvenioBibDocFileError: return warning_page( _("An error has happened in trying to retrieving the requested file." ), req, ln) docformat = normalize_format(docformat) redirect_to_url( req, '%s/%s/%s/files/%s%s?ln=%s%s' % (CFG_SITE_URL, CFG_SITE_RECORD, recid, name, docformat, ln, version and 'version=%s' % version or ''), apache.HTTP_MOVED_PERMANENTLY)
def openaire_create_icon(docid=None, recid=None, reformat=True): """ Celery task to create an icon for all documents in a given record or for just a specific document. """ if recid: docs = BibRecDocs(recid).list_bibdocs() else: docs = [BibDoc(docid)] # Celery task will fail if BibDoc does not exists (on purpose ;-) for d in docs: logger.debug("Checking document %s" % d) if not d.get_icon(subformat_re=re.compile(ICON_SUBFORMAT)): logger.debug("Document has no icon") for f in d.list_latest_files(): logger.debug("Checking file %s" % f) if not f.is_icon(): logger.debug("File not an icon") file_path = f.get_full_path() icon_path = None try: filename = os.path.splitext( os.path.basename(file_path) )[0] logger.info("Creating icon from file %s" % file_path) (icon_dir, icon_name) = create_icon( {'input-file': file_path, 'icon-name': "icon-%s" % filename, 'multipage-icon': False, 'multipage-icon-delay': 0, 'icon-scale': ICON_SIZE, 'icon-file-format': ICON_FILEFORMAT, 'verbosity': 0}) icon_path = os.path.join(icon_dir, icon_name) except InvenioWebSubmitIconCreatorError, e: logger.warning( 'Icon for file %s could not be created: %s' % ( file_path, str(e)) ) register_exception( prefix='Icon for file %s could not be created: %s' % (file_path, str(e)), alert_admin=False ) try: if icon_path and os.path.exists(icon_path): logger.debug( "Adding icon %s to document" % icon_path) d.add_icon(icon_path, subformat=ICON_SUBFORMAT) recid_list = ",".join( [str(x['recid']) for x in d.bibrec_links]) if reformat: task_low_level_submission( 'bibreformat', 'openaire', '-i', recid_list ) except InvenioBibDocFileError, e: logger.warning( 'Icon %s for file %s could not be added to ' 'document: %s' % (icon_path, f, str(e)) ) register_exception( prefix='Icon %s for file %s could not be added' ' to document: %s' % (icon_path, f, str(e)), alert_admin=False )